Annotating a Resource Adapter
Java EE Connector Architecture 1.6 Specification introduces new features that include meta-data annotations.
These annotations are used to define various resource-adapter artificats and hence it is possible to have a resource-adapter
without deployment descriptor (ra.xml).
Annotations
@Connector :
Specify that the JavaBean is a resource adapter JavaBean. Used to provide metadata about the capabilities provided by the resource adapter. It is optional to provide a JavaBean implementing the ResourceAdapter interface.@ConnectionDefinition(s) :
Annotations to represent a connection definition with managed-connection-factory, connection-factory artifact details.@AdministeredObject :
Designates a JavaBean as an administered object.@Activation :
Designates a JavaBean as an ActivationSpec JavaBean. Also provides Message Listener details.@ConfigProperty :
Specifies to the application server, that the decorated property is a configuration property for that JavaBean. Configuration Properties are now auto-discoverable by the application server and hence need not be specified using the deployment descriptor.Connector
annotations support in GlassFish :
GlassFish
V3 will be the reference implementation for Java EE 6 Specification
(and hence Java EE Connector Architecture 6 Specification)
GlassFish V3 Preview release has support for connector annotations, so one can deploy an annotated resource-adapter.
Java EE 6 Preview SDK will have samples bundle demonstrating various new features introduced.
Mail Connector RA sample will demonstrate a resource-adapter without deployment descriptor.
This sample was bundled in J2EE SDK 1.4 and is now bundled in Java EE 6 Preview SDK with annotations instead of deployment descriptor.
Java EE 6 Preview SDK is available here, soon.
Connectors sample will be available in "SAMPLES_DIR/javaee6/connectors/apps/mailconnector"
Read the documentation of connectors sample ("SAMPLES_DIR/javaee6/connectors/apps/mailconnector/docs") for more details.
Deployment descriptor elements and equivalent annotations:
| <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_5.xsd" version="1.5"> <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> |
@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 { ... ... } |
| <connection-definition> <managedconnectionfactory-class>samples.connectors.mailconnector.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> </config-property> ... ... <connectionfactory-interface>samples.connectors.mailconnector.api.JavaMailConnectionFactory</connectionfactory-interface> <connectionfactory-impl-class>samples.connectors.mailconnector.ra.outbound.JavaMailConnectionFactoryImpl</connectionfactory-impl-class> <connection-interface>samples.connectors.mailconnector.api.JavaMailConnection</connection-interface> <connection-impl-class>samples.connectors.mailconnector.ra.outbound.JavaMailConnectionImpl</connection-impl-class> </connection-definition> |
@ConnectionDefinition( connectionFactory = samples.connectors.mailconnector.api.JavaMailConnectionFactory.class, connectionFactoryImpl = samples.connectors.mailconnector.ra.outbound.JavaMailConnectionFactoryImpl.class, connection = samples.connectors.mailconnector.api.JavaMailConnection.class, connectionImpl = samples.connectors.mailconnector.ra.outbound.JavaMailConnectionImpl.class ) public class ManagedConnectionFactoryImpl implements ManagedConnectionFactory, Serializable { ... ... @ConfigProperty( type = String.class, defaultValue = "UnknownHostName" ) public void setServerName(String serverName) { String oldName = this.serverName; this.serverName = serverName; changes.firePropertyChange("serverName", oldName, serverName); } } |
| <messageadapter> <messagelistener> <messagelistener-type>samples.connectors.mailconnector.api.JavaMailMessageListener</messagelistener-type> <activationspec> <activationspec-class>samples.connectors.mailconnector.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-name> </required-config-property> <required-config-property> <config-property-name>password</config-property-name> </required-config-property> <required-config-property> <config-property-name>folderName</config-property-name> </required-config-property> <required-config-property> <description>Normally imap or pop3</description> <config-property-name>protocol</config-property-name> </required-config-property> </activationspec> </messagelistener> </messageadapter> |
@Activation( messageListeners = {samples.connectors.mailconnector.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"); ... ... } |