Child pages
  • How to Access Resources via JNDI
Skip to end of metadata
Go to start of metadata
{scrollbar}

Introduction

Java Naming and Directory Interface (JNDI) is an interface to resources in Apache Geronimo and other application servers. Through this interface, developers have access to all sorts of resources such as Java objects, Enterprise Java Beans (EJBs), databases, mails sessions, and other connections.

The following article provides concept-rich documentation on how to use JNDI to access connection pools for data sources, Java Messaging Services (JMS), mail sessions, and URL connections: Apache Geronimo JNDI naming and Java resource connection pools, Part 1: Data source connections. URL: http://www-128.ibm.com/developerworks/opensource/library/os-ag-jndi1. It is a useful article, but it is written for Geronimo 1.x.

How to Access Resources using JNDI in Geronimo

There are some additional configurations that are necesary in order to get the sample J2EE enterprise application up and running. Here is an outline of how to deploy the J2EE enterprise application in a running Geronimo 2.x instance:
1) Add j2ee.jar file to your classpath.
2) Use the Geronimo/Tomcat distribution.
3) Create the database pool (CustomerServicePool).
a. Manually create and deploy the deployment test plan file.
b. Update deployment version for the CustomerServicePool.
c. Update dependency version number.
d. Update deployment descriptor.
4) Deploy and test the application.

Note: The working environment for this information is Windows XP Professional, Version 5.1.2600 Service Pack 2 Build 2600.

The following steps will guide you to a successful deployment of the Customer Service J2EE enterprise application, which can be downloaded here: http://www.ibm.com/developerworks/views/download.jsp?contentid=175237&filename=CustomerService-part1.zip&method=http&locale=worldwide

1) Add j2ee.jar file to your classpath.

A j2ee.jar file, which can be found in the lib directory of an application server such as WebSphere or WebLogic, must be added to the classpath, or you will get the following error when attempting to run the ant build script:

#000000solid compile: [javac] Compiling 9 source files to E:\DOWNLOADS\GERONIMO\CustomerService\build\class [javac] E:\DOWNLOADS\GERONIMO\CustomerService\src\com\service\customer\ejb\Customer.java:6: package javax.ejb does not exist [javac] import javax.ejb.EJBObject; [javac] ^

2) Use the Geronimo/Tomcat distribution.

Use the Tomcat distribution because the Jetty (geronimo-jetty6-jee5-2.0-M1) distribution currently does not display any items in the "Database Types" dropdown list in the Geronimo Server Console | Services | Database Pools | Database Wizard.

3) Create the database pool (CustomerServicePool). Even the Tomcat distribution has problems creating a new database pool. For exampe, when you attempt to create a new database pool, select "Derby embedded" from the database type and then test the connection. That works, but no new database pool is created. The <GERONIMO_HOME>\var\log\geronimo.log file will have the following entry:

#000000solid 14:20:19,687 ERROR [DatabasePoolPortlet] Unable to save connection pool javax.enterprise.deploy.spi.exceptions.InvalidModuleException: Not supported at org.apache.geronimo.deployment.plugin.jmx.JMXDeploymentManager.createConfiguration(JMXDeploymentManager.java:297) at org.apache.geronimo.console.databasemanager.wizard.DatabasePoolPortlet.save(DatabasePoolPortlet.java:880) at org.apache.geronimo.console.databasemanager.wizard.DatabasePoolPortlet.processAction(DatabasePoolPortlet.java:341) at org.apache.pluto.core.PortletServlet.dispatch(PortletServlet.java:229) at org.apache.pluto.core.PortletServlet.doPost(PortletServlet.java:163) at javax.servlet.http.HttpServlet.service(HttpServlet.java:713) at javax.servlet.http.HttpServlet.service(HttpServlet.java:806) at org.apache.pluto.core.PortletServlet.service(PortletServlet.java:153) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:683) at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:585) at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:505) at org.apache.pluto.invoker.impl.PortletInvokerImpl.invoke(PortletInvokerImpl.java:120) at org.apache.pluto.invoker.impl.PortletInvokerImpl.action(PortletInvokerImpl.java:68) at org.apache.pluto.PortletContainerImpl.processPortletAction(PortletContainerImpl.java:164) at org.apache.pluto.portalImpl.core.PortletContainerWrapperImpl.processPortletAction(PortletContainerWrapperImpl.java:82) at org.apache.pluto.portalImpl.Servlet.doGet(Servlet.java:227) at org.apache.pluto.portalImpl.Servlet.doPost(Servlet.java:267) at javax.servlet.http.HttpServlet.service(HttpServlet.java:713) at javax.servlet.http.HttpServlet.service(HttpServlet.java:806) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.geronimo.tomcat.valve.DefaultSubjectValve.invoke(DefaultSubjectValve.java:56) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525) at org.apache.geronimo.tomcat.GeronimoStandardContext$SystemMethodValve.invoke(GeronimoStandardContext.java:325) at org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke(GeronimoBeforeAfterValve.java:47) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:542) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:818) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:624) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445) at java.lang.Thread.run(Thread.java:595)

3.a) Manually create and deploy the deployment test plan file.

Geronimo is not creating the deployment plan for the "Derby embedded" connection pool. So, you must manually create a deployment plan and deploy it as follows:

Create a the following deployment plan file (CustomerServicePool_test_plan.xml)

xmlsolid <?xml version="1.0" encoding="UTF-8"?> <connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"> <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2"> <dep:moduleId> <dep:groupId>console.dbpool</dep:groupId> <dep:artifactId>CustomerServicePool</dep:artifactId> <dep:version>2.0</dep:version> <dep:type>rar</dep:type> </dep:moduleId> <dep:dependencies> <dep:dependency> <dep:groupId>org.apache.derby</dep:groupId> <dep:artifactId>derby</dep:artifactId> <dep:version>10.1.3.1</dep:version> <dep:type>jar</dep:type> </dep:dependency> </dep:dependencies> </dep:environment> <resourceadapter> <outbound-resourceadapter> <connection-definition> <connectionfactory-interface>javax.sql.DataSource</connectionfactory-interface> <connectiondefinition-instance> <name>CustomerServicePool</name> <config-property-setting name="Driver">org.apache.derby.jdbc.EmbeddedDriver</config-property-setting> <config-property-setting name="ConnectionURL">jdbc:derby:CustomerServiceDatabase</config-property-setting> <connectionmanager> <local-transaction/> <single-pool> <max-size>10</max-size> <min-size>0</min-size> <match-one/> </single-pool> </connectionmanager> </connectiondefinition-instance> </connection-definition> </outbound-resourceadapter> </resourceadapter> </connector>

Run the following at the command prompt in the <GERONIMO_HOME>\bin directory:

deploy --verbose --user system --password manager deploy D:\appsData\GERONIMO\JNDIApp\CustomerService.ear

3.b) Update deployment version for the CustomerServicePool.

Running the previous deploy command will expose another configuration opportunity:
#000000solid Using GERONIMO_BASE: C:\GERONIMO Using GERONIMO_HOME: C:\GERONIMO Using GERONIMO_TMPDIR: C:\GERONIMO\var\temp Using JRE_HOME: C:\Java\Java150_10\jre Error: Unable to distribute CustomerService.ear: Unable to create configuration for deployment org.apache.geronimo.common.DeploymentException: Unable to create configuration for deployment at org.apache.geronimo.deployment.DeploymentContext.createTempConfiguration(DeploymentContext.java:121) at org.apache.geronimo.deployment.DeploymentContext.<init>(DeploymentContext.java:101) at org.apache.geronimo.deployment.DeploymentContext.<init>(DeploymentContext.java:81) at org.apache.geronimo.j2ee.deployment.EARContext.<init>(EARContext.java:65) at org.apache.geronimo.j2ee.deployment.EARConfigBuilder.buildConfiguration(EARConfigBuilder.java:475) at org.apache.geronimo.j2ee.deployment.EARConfigBuilder$$FastClassByCGLIB$$38e56ec6.invoke(<generated>) at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53) at org.apache.geronimo.gbean.runtime.FastMethodInvoker.invoke(FastMethodInvoker.java:38) at org.apache.geronimo.gbean.runtime.GBeanOperation.invoke(GBeanOperation.java:124) at org.apache.geronimo.gbean.runtime.GBeanInstance.invoke(GBeanInstance.java:820) at org.apache.geronimo.gbean.runtime.RawInvoker.invoke(RawInvoker.java:57) at org.apache.geronimo.kernel.basic.RawOperationInvoker.invoke(RawOperationInvoker.java:35) at org.apache.geronimo.kernel.basic.ProxyMethodInterceptor.intercept(ProxyMethodInterceptor.java:96) at org.apache.geronimo.j2ee.deployment.CorbaGBeanNameSource$$EnhancerByCGLIB$$44342279.buildConfiguration(<generated>) at org.apache.geronimo.deployment.Deployer.deploy(Deployer.java:302) at org.apache.geronimo.deployment.Deployer.deploy(Deployer.java:124) at org.apache.geronimo.deployment.Deployer$$FastClassByCGLIB$$734a235d.invoke(<generated>) at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53) at org.apache.geronimo.gbean.runtime.FastMethodInvoker.invoke(FastMethodInvoker.java:38) at org.apache.geronimo.gbean.runtime.GBeanOperation.invoke(GBeanOperation.java:124) at org.apache.geronimo.gbean.runtime.GBeanInstance.invoke(GBeanInstance.java:855) at org.apache.geronimo.kernel.basic.BasicKernel.invoke(BasicKernel.java:239) at org.apache.geronimo.kernel.KernelGBean.invoke(KernelGBean.java:338) at org.apache.geronimo.kernel.KernelGBean$$FastClassByCGLIB$$1cccefc9.invoke(<generated>) at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53) at org.apache.geronimo.gbean.runtime.FastMethodInvoker.invoke(FastMethodInvoker.java:38) at org.apache.geronimo.gbean.runtime.GBeanOperation.invoke(GBeanOperation.java:124) at org.apache.geronimo.gbean.runtime.GBeanInstance.invoke(GBeanInstance.java:855) at org.apache.geronimo.kernel.basic.BasicKernel.invoke(BasicKernel.java:239) at org.apache.geronimo.system.jmx.MBeanGBeanBridge.invoke(MBeanGBeanBridge.java:168) at com.sun.jmx.mbeanserver.DynamicMetaDataImpl.invoke(DynamicMetaDataImpl.java:213) at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:220) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:815) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:784) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1408) at javax.management.remote.rmi.RMIConnectionImpl.access$100(RMIConnectionImpl.java:81) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1245) at java.security.AccessController.doPrivileged(Native Method) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1348) at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:782) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294) at sun.rmi.transport.Transport$1.run(Transport.java:153) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:149) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707) at java.lang.Thread.run(Thread.java:595) Caused by: org.apache.geronimo.kernel.config.LifecycleException: load of default/CustomerService/1.0/ear failed at org.apache.geronimo.kernel.config.SimpleConfigurationManager.loadConfiguration(SimpleConfigurationManager.java:322) at org.apache.geronimo.deployment.DeploymentConfigurationManager.loadConfiguration(DeploymentConfigurationManager.java:115) at org.apache.geronimo.kernel.config.SimpleConfigurationManager.loadConfiguration(SimpleConfigurationManager.java:281) at org.apache.geronimo.deployment.DeploymentConfigurationManager.loadConfiguration(DeploymentConfigurationManager.java:111) at org.apache.geronimo.deployment.DeploymentContext.createTempConfiguration(DeploymentContext.java:118) ... 50 more Caused by: org.apache.geronimo.kernel.config.InvalidConfigException: Error starting configuration gbean default/CustomerService/1.0/ear at org.apache.geronimo.kernel.config.SimpleConfigurationManager.load(SimpleConfigurationManager.java:347) at org.apache.geronimo.deployment.DeploymentConfigurationManager.load(DeploymentConfigurationManager.java:119) at org.apache.geronimo.kernel.config.SimpleConfigurationManager.loadConfiguration(SimpleConfigurationManager.java:307) ... 54 more Caused by: org.apache.geronimo.kernel.repository.MissingDependencyException: Unable to resolve dependency console.dbpool/CustomerServicePool/1.0/rar at org.apache.geronimo.kernel.config.ConfigurationResolver.resolve(ConfigurationResolver.java:112) at org.apache.geronimo.kernel.config.Configuration.buildClassPath(Configuration.java:402) at org.apache.geronimo.kernel.config.Configuration.createConfigurationClasssLoader(Configuration.java:324) at org.apache.geronimo.kernel.config.Configuration.<init>(Configuration.java:268) at org.apache.geronimo.kernel.config.SimpleConfigurationManager.load(SimpleConfigurationManager.java:343) ... 56 more

Notice the following:
#000000solid Caused by: org.apache.geronimo.kernel.repository.MissingDependencyException: Unable to resolve dependency console.dbpool/CustomerServicePool/1.0/rar

This highlights the fact that the files in the article are targeted for an earlier (1.0) deployment of the CustomerServicePool (a Derby embedded database pool).

3.c) Update dependency version number.

You can fix this by changing the "dep:version" tag in the dependencies section of the CustomerEJB-openejb.xml file from "1.0" to "2.0" as follows:

XMLsolid <?xml version="1.0" encoding="UTF-8"?> <openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1" xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.1"> <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1"> <dep:moduleId> <dep:groupId>default</dep:groupId> <dep:artifactId>CustomerEJB</dep:artifactId> <dep:version>1.0</dep:version> <dep:type>jar</dep:type> </dep:moduleId> <dep:dependencies> <dep:dependency> <dep:groupId>console.dbpool</dep:groupId> <dep:artifactId>CustomerServicePool</dep:artifactId> <dep:version>2.0</dep:version> <dep:type>rar</dep:type> </dep:dependency> </dep:dependencies> . . .

Re-run the following at the command prompt in the <GERONIMO_HOME>\bin directory:

deploy --verbose --user system --password manager deploy D:\appsData\GERONIMO\JNDIApp\CustomerService.ear

3.d) Update deployment descriptor.

Modify application.xml deployment descriptor file to be compatible with Geronimo 2.x requirements.

After running the previous deploy command, yet another error can then be found in the geronimo.log file: 

#000000solid Deployer operation failed: Could not parse application.xml org.apache.geronimo.common.DeploymentException: Could not parse application.xml at org.apache.geronimo.j2ee.deployment.EARConfigBuilder.getEarPlan(EARConfigBuilder.java:314) at org.apache.geronimo.j2ee.deployment.EARConfigBuilder.getDeploymentPlan(EARConfigBuilder.java:261) at org.apache.geronimo.j2ee.deployment.EARConfigBuilder$$FastClassByCGLIB$$38e56ec6.invoke(<generated>) at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53) at org.apache.geronimo.gbean.runtime.FastMethodInvoker.invoke(FastMethodInvoker.java:38) at org.apache.geronimo.gbean.runtime.GBeanOperation.invoke(GBeanOperation.java:124) at org.apache.geronimo.gbean.runtime.GBeanInstance.invoke(GBeanInstance.java:820) at org.apache.geronimo.gbean.runtime.RawInvoker.invoke(RawInvoker.java:57) at org.apache.geronimo.kernel.basic.RawOperationInvoker.invoke(RawOperationInvoker.java:35) at org.apache.geronimo.kernel.basic.ProxyMethodInterceptor.intercept(ProxyMethodInterceptor.java:96) at org.apache.geronimo.j2ee.deployment.CorbaGBeanNameSource$$EnhancerByCGLIB$$44342279.getDeploymentPlan(<generated>) at org.apache.geronimo.deployment.Deployer.deploy(Deployer.java:232) at org.apache.geronimo.deployment.Deployer.deploy(Deployer.java:124) at org.apache.geronimo.deployment.Deployer$$FastClassByCGLIB$$734a235d.invoke(<generated>) at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53) at org.apache.geronimo.gbean.runtime.FastMethodInvoker.invoke(FastMethodInvoker.java:38) at org.apache.geronimo.gbean.runtime.GBeanOperation.invoke(GBeanOperation.java:124) at org.apache.geronimo.gbean.runtime.GBeanInstance.invoke(GBeanInstance.java:855) at org.apache.geronimo.kernel.basic.BasicKernel.invoke(BasicKernel.java:239) at org.apache.geronimo.deployment.plugin.local.AbstractDeployCommand.doDeploy(AbstractDeployCommand.java:114) at org.apache.geronimo.deployment.plugin.local.DistributeCommand.run(DistributeCommand.java:60) at java.lang.Thread.run(Thread.java:595) Caused by: org.apache.xmlbeans.XmlException: Invalid deployment descriptor: [error: cvc-complex-type.2.4a: Expected element 'module@http://java.sun.co m/xml/ns/j2ee' instead of 'description@http://java.sun.com/xml/ns/j2ee' here in element application@http://java.sun.com/xml/ns/j2ee] Descriptor: <application xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" version="1.4" xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <display-name>Customer Service utility</display-name> <description>Customer Service utility</description> <module> <web> <web-uri>CustomerService-web.war</web-uri> <context-root>/service</context-root> </web> </module> <module> <ejb>CustomerEJB.jar</ejb> </module> <module> <ejb>ProcessCustomerEJB.jar</ejb> </module> </application> at org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil.validateDD(XmlBeansUtil.java:213) at org.apache.geronimo.j2ee.deployment.EARConfigBuilder.convertToApplicationSchema(EARConfigBuilder.java:420) at org.apache.geronimo.j2ee.deployment.EARConfigBuilder.getEarPlan(EARConfigBuilder.java:312) ... 21 more

Remove the "description: and "display-name" tags from the application.xml file and save it back into the CustomerService.ear file.application.xml file: 

XMLsolid <?xml version="1.0" ?> <application 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/application_1_4.xsd" version="1.4"> <display-name>Customer Service utility</display-name> <module> <web> <web-uri>CustomerService-web.war</web-uri> <context-root>/service</context-root> </web> </module> <module> <ejb>CustomerEJB.jar</ejb> </module> <module> <ejb>ProcessCustomerEJB.jar</ejb> </module> </application>

This time, the CustomerService.ear file will successfully deploy.

deploy --verbose --user system --password manager deploy D:\appsData\GERONIMO\JNDIApp\CustomerService.ear

4) Deploy and test the application.

  • Go to the Geronimo Server Console | Applications | Deploy New window.
  • Click the <Browse> button and choose the CustomerService.ear file.
  • Click <Install>
  • Test deployed application at http://localhost:8080/service/customers/

Summary

Using the following steps, we can successfully deploy the J2EE enterprise application in a running Geronimo 2.x instance:
1) Add j2ee.jar file to your classpath.
2) Use the Geronimo/Tomcat distribution.
3) Create the database pool (CustomerServicePool).
a. Manually create and deploy the deployment test plan file.
b. Update deployment version for the CustomerServicePool.
c. Update dependency version number.
d. Update deployment descriptor.
4) Deploy and test the application.

  • No labels