7.2.5. A Sample Skeleton JCA Resource Adaptor
src/main/org/jboss/book/jca/ex1 directory of the book examples. A class diagram that shows the mapping from the required javax.resource.spi interfaces to the resource adaptor implementation is given in Figure 7.3, “The file system RAR class diagram”.

Figure 7.3. The file system RAR class diagram
deploy/lib directory, execute the following Ant command in the book examples directory.
[examples]$ ant -Dchap=jca build-chap
jca-ex1.sar and a notxfs-service.xml service descriptor. The example resource adaptor deployment descriptor is shown in Example 7.1, “The nontransactional file system resource adaptor deployment descriptor.”.
Example 7.1. The nontransactional file system resource adaptor deployment descriptor.
<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://java.sun.com/xml/ns/"Whats_new_in_JBoss_4-J2EE_Certification_and_Standards_Compliance"
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">
<display-name>File System Adapter</display-name>
<vendor-name>JBoss</vendor-name>
<eis-type>FileSystem</eis-type>
<resourceadapter-version>1.0</resourceadapter-version>
<license>
<description>LGPL</description>
<license-required>false</license-required>
</license>
<resourceadapter>
<resourceadapter-class>
org.jboss.resource.deployment.DummyResourceAdapter
</resourceadapter-class>
<outbound-resourceadapter>
<connection-definition>
<managedconnectionfactory-class> org.jboss.book.jca.ex1.ra.FSManagedConnectionFactory </managedconnectionfactory-class>
<config-property>
<config-property-name>FileSystemRootDir</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>/tmp/db/fs_store</config-property-value>
</config-property>
<config-property>
<config-property-name>UserName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value/>
</config-property>
<config-property>
<config-property-name>Password</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value/>
</config-property>
<connectionfactory-interface> org.jboss.book.jca.ex1.ra.DirContextFactory </connectionfactory-interface> <connectionfactory-impl-class> org.jboss.book.jca.ex1.ra.DirContextFactoryImpl </connectionfactory-impl-class> <connection-interface> javax.naming.directory.DirContext </connection-interface> <connection-impl-class> org.jboss.book.jca.ex1.ra.FSDirContext </connection-impl-class>
</connection-definition>
<transaction-support>NoTransaction</transaction-support>
<authentication-mechanism>
<authentication-mechanism-type>BasicPassword</authentication-mechanism-type>
<credential-interface>
javax.resource.spi.security.PasswordCredential
</credential-interface>
</authentication-mechanism>
<reauthentication-support>true</reauthentication-support>
</outbound-resourceadapter>
<security-permission>
<description> Read/Write access is required to the contents of the
FileSystemRootDir </description>
<security-permission-spec> permission java.io.FilePermission
"/tmp/db/fs_store/*", "read,write";
</security-permission-spec>
</security-permission>
</resourceadapter>
</connector>
- managedconnectionfactory-class: The implementation of the
ManagedConnectionFactoryinterface,org.jboss.book.jca.ex1.ra.FSManagedConnectionFactory - connectionfactory-interface: This is the interface that clients will obtain when they lookup the connection factory instance from JNDI, here a proprietary resource adaptor value,
org.jboss.book.jca.ex1.ra.DirContextFactory. This value will be needed when we create the JBossds.xmlto use the resource. - connectionfactory-impl-class: This is the class that provides the implementation of the
connectionfactory-interface,org.jboss.book.jca.ex1.ra.DirContextFactoryImpl. - connection-interface: This is the interface for the connections returned by the resource adaptor connection factory, here the JNDI
javax.naming.directory.DirContextinterface. - connection-impl-class: This is he class that provides the
connection-interfaceimplementation,org.jboss.book.jca.ex1.ra.FSDirContext. - transaction-support: The level of transaction support, here defined as
NoTransaction, meaning the file system resource adaptor does not do transactional work.
ds.xml descriptor file. An example of this for the file system adaptor is shown in Example 7.2, “The notxfs-ds.xml resource adaptor MBeans service descriptor.”.
Example 7.2. The notxfs-ds.xml resource adaptor MBeans service descriptor.
<!DOCTYPE connection-factories PUBLIC
"-//JBoss//DTD JBOSS JCA Config 1.5//EN"
"http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<!--
The non-transaction FileSystem resource adaptor service configuration
-->
<connection-factories>
<no-tx-connection-factory>
<jndi-name>NoTransFS</jndi-name>
<rar-name>jca-ex1.rar</rar-name>
<connection-definition>
org.jboss.book.jca.ex1.ra.DirContextFactory
</connection-definition>
<config-property name="FileSystemRootDir"
type="java.lang.String">/tmp/db/fs_store</config-property>
</no-tx-connection-factory>
</connection-factories>
- jndi-name: This specifies where the connection factory will be bound into JNDI. For this deployment that binding will be
java:/NoTransFS. - rar-name: This is the name of the RAR file that contains the definition for the resource we want to provide. For nested RAR files, the name would look like
myapplication.ear#my.rar. In this example, it is simplyjca-ex1.rar. - connection-definition: This is the connection factory interface class. It should match the
connectionfactory-interfacein thera.xmlfile. Here our connection factory interface isorg.jboss.book.jca.ex1.ra.DirContextFactory. - config-property: This can be used to provide non-default settings to the resource adaptor connection factory. Here the
FileSystemRootDiris being set to/tmp/db/fs_store. This overrides the default value in thera.xmlfile.
[examples]$ ant -Dchap=jca config
echo. Inside of the echo method the EJB accesses the resource adaptor connection factory, creates a connection, and then immediately closes the connection. The echo method code is shown below.
Example 7.3. The stateless session bean echo method code that shows the access of the resource adaptor connection factory.
public String echo(String arg)
{
log.info("echo, arg="+arg);
try {
InitialContext ctx = new InitialContext();
Object ref = ctx.lookup("java:comp/env/ra/DirContextFactory");
log.info("echo, ra/DirContextFactory=" + ref);
DirContextFactory dcf = (DirContextFactory) ref;
log.info("echo, found dcf=" + dcf);
DirContext dc = dcf.getConnection();
log.info("echo, lookup dc=" + dc);
dc.close();
} catch(NamingException e) {
log.error("Failed during JNDI access", e);
}
return arg;
}
DirContextFactory interface that returns a JNDI DirContext object as the connection object. The example EJB is simply exercising the system contract layer by looking up the resource adaptor connection factory, creating a connection to the resource and closing the connection. The EJB does not actually do anything with the connection, as this would only exercise the resource adaptor implementation since this is a non-transactional resource.
EchoBean.echo method by running Ant as follows from the examples directory:
[examples]$ ant -Dchap=jca -Dex=1 run-example
server/production/log/server.log file. Don't worry if you see exceptions. They are just stack traces to highlight the call path into parts of the adaptor. To help understand the interaction between the adaptor and the JBoss JCA layer, we'll summarize the events seen in the log using a sequence diagram. Figure 7.4, “A sequence diagram illustrating the key interactions between the JBossCX framework and the example resource adaptor that result when the EchoBean accesses the resource adaptor connection factory.” is a sequence diagram that summarizes the events that occur when the EchoBean accesses the resource adaptor connection factory from JNDI and creates a connection.

Figure 7.4. A sequence diagram illustrating the key interactions between the JBossCX framework and the example resource adaptor that result when the EchoBean accesses the resource adaptor connection factory.
EchoBean.echo method. For the sake of conciseness of the diagram, the client is shown directly invoking the EchoBean.echo method when in reality the JBoss EJB container handles the invocation. There are three distinct interactions between the EchoBean and the resource adaptor; the lookup of the connection factory, the creation of a connection, and the close of the connection.
- 1, the echo method invokes the
getConnectionmethod on the resource adaptor connection factory obtained from the JNDI lookup on thejava:comp/env/ra/DirContextFactoryname which is a link to thejava:/NoTransFSlocation. - 1.1, the
DirContextFactoryImplclass asks its associatedConnectionManagerto allocate a connection. It passes in theManagedConnectionFactoryandFSRequestInfothat were associated with theDirContextFactoryImplduring its construction. - 1.1.1, the
ConnectionManagerinvokes itsgetManagedConnectionmethod with the currentSubjectandFSRequestInfo. - 1.1.1.1, the
ConnectionManagerasks its object pool for a connection object. TheJBossManagedConnectionPool$BasePoolis get the key for the connection and then asks the matchingInternalPoolfor a connection. - 1.1.1.1.1, Since no connections have been created the pool must create a new connection. This is done by requesting a new managed connection from the
ManagedConnectionFactory. TheSubjectassociated with the pool as well as theFSRequestInfodata are passed as arguments to thecreateManagedConnectionmethod invocation. - 1.1.1.1.1.1, the
ConnectionFactorycreates a newFSManagedConnectioninstance and passes in theSubjectandFSRequestInfodata. - 1.1.1.2, a
javax.resource.spi.ConnectionListenerinstance is created. The type of listener created is based on the type ofConnectionManager. In this case it is anorg.jboss.resource.connectionmgr.BaseConnectionManager2$NoTransactionListenerinstance. - 1.1.1.2.1, the listener registers as a
javax.resource.spi.ConnectionEventListenerwith theManagedConnectioninstance created in 1.2.1.1. - 1.1.2, the
ManagedConnectionis asked for the underlying resource manager connection. TheSubjectandFSRequestInfodata are passed as arguments to thegetConnectionmethod invocation. - The resulting connection object is cast to a
javax.naming.directory.DirContextinstance since this is the public interface defined by the resource adaptor. - After the
EchoBeanhas obtained theDirContextfor the resource adaptor, it simply closes the connection to indicate its interaction with the resource manager is complete.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.