Portable Naming and Binding of Resources
thomas.kriechbaum Mar 7, 2014 6:35 PMHello,
how do I have to define resources (DataSource, URL) in a portable way, so that an EAR can be deployed without any changes to different server configurations (e.g. Test/Production; Customer A/Customer B)?
Is portability of JNDI names an open issue in general?
E.g. arungupta noted in Migrating a Java EE App from GlassFish to WildFly · WildFly)
"Because of differences in the JNDI naming rules, it’s necessary to change all occurrences of the previous JNDI name to the new one. "
To be more specific:
We typically package service applications as EARs containing one WAR- and one or more EJB-Modules (EJB-JARs are packaged within WEB-INF/lib as part of the WAR). All these modules should have access to the same configuration URL or DataSource using the app-namespace (e.g. java:app/url/Configuration, java:app/jdbc/SandboxDS).The configuration URL and/or the DataSource can vary between different operating zones and/or customer deployments.
As recommended in the Wildfly documentation (JNDI Reference - WildFly 8 - Project Documentation Editor) I tried to define a binding in the "Naming Subsystem Configuration" (standalone.xml). Since the resource should be accessible within the app-namespace I have specified the app-name as part of the global JNDI-name.
<subsystem xmlns="urn:jboss:domain:naming:2.0">
<bindings>
<simple name="java:global/wildfly-services-ear-1.0.0/url/Configuration" value="file:///data/conf/test/wildfly-services-1.0.0" type="java.net.URL"/>
</bindings>
<remote-naming/>
</subsystem>
In my EJB, the correct app-name is provided by
Context ctx = new InitialContext();
String name = (String) ctx.lookup("java:app/AppName");
==> wildfly-services-ear-1.0.0
ctx.lookup("java:global/wildfly-services-ear-1.0.0/url/Configuration")
returns the expected URL, but
ctx.lookup("java:app/url/Configuration")
causes an NameNotFoundException.
Because of this exception I tried to define the resource within the web.xml and bound it via jboss-web.xml. But binding URL-references does not work with Wildfly, because
org.jboss.as.ee.utils.InjectionUtils.getInjectionSource(InjectionUtils.java:128)
does not resolve JNDI-Names for java.net.URL types. In this case, the target URL has to be defined within jboss-web.xml directly. But as mentioned above, the EAR itself must not be adopted for specific operating zones or customers.
Is this way of URL resolving a known issue or the expected behavior?
Whats about referencing DataSources within the persistence.xml in a portable way (define and bind them external to the EAR)?
e.g <jta-data-source>java:app/jdbc/SandboxDS</jta-data-source>
Thanks,
Thomas