The Java EE 6 Tutorial, Volume I

Replacing Deployment Descriptors With Metadata Annotations

The use of annotations reduces or completely eliminates the need to deal with a deployment descriptor in many cases. The use of annotations also reduces the need to keep the deployment descriptor synchronized with changes to source code.

The following examples provide sample deployment descriptors and equivalent annotations.

Example 1: @Connector Annotation

The following deployment descriptor defines a connector:

<connector xmlns "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/connector_1_6.xsd" 
version="1.6">
<description>Sample adapter using the JavaMail API</description>
<display-name>InboundResourceAdapter</display-name>
<icon></icon>
<vendor-name>Sun Microsystems, Inc</vendor-name>

<eis-type>MAIL</eis-type>
<resourceadapter-version>1.0</resourceadapter-version>
...
...
...
<authentication-mechanism>
	<authentication-mechanism-type>BasicPassword</authentication-mechanism-type>
<credential-interface>javax.resource.spi.security.PasswordCredential</credential-interface>
</authentication-mechanism>
<reauthentication-support>false</reauthentication-support>
...
...
</connector>

The equivalent metadata annotation is as follows:

@Connector(
description = "Sample adapter using the JavaMail API",
   displayName = "InboundResourceAdapter",
   vendorName = "Sun Microsystems, Inc.",
    eisType = "MAIL",
    version = "1.0",
   authMechanisms = {
                @AuthenticationMechanism(
          authMechanism = "BasicPassword",
credentialInterface = AuthenticationMechanism.CredentialInterface.PasswordCredential
                )
        }

/*
 // Since the following attribute values denote the default values of the annotation,
 // they need not be specified explicitly

 transactionSupport = TransactionSupport.TransactionSupportLevel.NoTransaction,
    reauthenticationSupport = false
*/
)

public class ResourceAdapterImpl 
implements ResourceAdapter, java.io.Serializable
{
...
...
}

Example 2: @ConnectionDefinition Annotation

The following deployment descriptor snippet describes a connection definition:

<connection-definition>
<managedconnectionfactory-class>
samples.mailra.ra.outbound.ManagedConnectionFactoryImpl
</managedconnectionfactory-class>
<config-property><config-property-name>serverName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>UnknownHostName</config-property-value>

...
...
<connectionfactory-interface>samples.mailra.api.JavaMailConnectionFactory</connectionfactory-interface>
<connectionfactory-impl-class>samples.mailra.ra.outbound.JavaMailConnectionFactoryImpl</connectionfactory-impl-class>
<connection-interface>samples.mailra.api.JavaMailConnection</connection-interface>
<credential-interface>javax.resource.spi.security.PasswordCredential</credential-interface>
<connection-impl-class>samples.mailra.ra.outbound.JavaMailConnectionImpl</connection-impl-class>
</connectiondefinition>

The equivalent metadata annotation is as follows:

@ConnectionDefinition(
        connectionFactory = 
samples.mailra..api.JavaMailConnectionFactory.class,
        connectionFactoryImpl = 
samples.mailra.ra.outbound.JavaMailConnectionFactoryImpl.class,
        connection = samples.connectors.mailconnector.api.JavaMailConnection.class,
        connectionImpl = 
samples.mailra..ra.outbound.JavaMailConnectionImpl.class
)
public class ManagedConnectionFactoryImpl implements
    ManagedConnectionFactory, Serializable
{
...
...
 @ConfigProperty(
          defaultValue = "UnknownHostName"
    )
    public void setServerName(String serverName)
    {
       ...
    }

}

Example 3: @Activation Annotation

The following deployment descriptor snippet describes the messaging capabilities of a resource adapter:

<messageadapter>
<messagelistener>
<messagelistener-type>samples.mailra.api.JavaMailMessageListener</messagelistener-type>
<activationspec>
<activationspec-class>samples.mailra.ra.inbound.ActivationSpecImpl</activationspec-class>
required-config-property>
	<config-property-name>serverName</config-property-name>
</required-config-property>
<required-config-property>
	<config-property-name>userName</config-property-value>
</required-config-property>
<required-config-property>
	<config-property-name>password</config-property-value>
</required-config-property>
<required-config-property>
<config-property-name>folderName</config-property-value>
</required-config-property>
<required-config-property>
<description>Normally imap or pop3</description>
<config-property-name>protocol</config-property-name>
<config-property-value>IMAP</config-property-value>
</required-config-property>
</activationspec>
</messagelistener>
</messageadapter>

The equivalent metadata annotation is as follows:

@Activation(
        messageListeners = {samples.mailra.api.JavaMailMessageListener.class}

)
public class ActivationSpecImpl implements javax.resource.spi.ActivationSpec,
                                           java.io.Serializable
{
...
 @ConfigProperty()
    // serverName property value
    private String serverName = new String("");

    @ConfigProperty()
    // userName property value
    private String userName = new String("");

    @ConfigProperty()
    // password property value
    private String password = new String("");

    @ConfigProperty()
    // folderName property value
    private String folderName = new String("Inbox");

    // protocol property value
    // Normally imap or pop3
    @ConfigProperty(
            description = "Normally imap or pop3"
    )
    private String protocol = new String("imap");


...
...
}