views:

227

answers:

2

I'm having some problems with the global JNDI names of my EJB resources which is (or at least will) cause my JNDI look ups to fail. The project is being developed on Netbeans and is a standard Maven Web Application. When my application is deployed to GF3.0 the application name is set to something like:

com.example_myapp_war_1.0-SNAPSHOT

which is all well and good from Netbeans point of view because it ensures the name is unique but it also means all the EJBs get global names such as this:

java:global/com.example_myapp_war_1.0-SNAPSHOT/CustomerService

This, of course, is going to cause problems because every time the version changes all the global names change (I've tested this by changing the version and the names indeed changed). The name is being generated from the POM file and it's a concatenation of:

<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

Up until now I've got away with just injecting all the resources using @EJB but now I need to access the CustomerService EJB from a JSF Converter so I'm doing a JNDI look up like this:

try {
    Context ctx = new InitialContext();
    CustomerService customerService = (CustomerService)ctx.lookup( "java:global/com.example_myapp_war_1.0-SNAPSHOT/CustomerService" );
    return customerService.get(submittedValue);
} catch( Exception e ) {
    logger.error( "Failed to convert customer.", e );
    return null;
}

which will clearly break when the application is properly released and the module name changes. So, the million dollar question: how can I set the modle name in maven or how do I recover the module name so that I can programatically build the JNDI name at runtile. I've tried setting it in the web.xml file as suggested by that link but it was ignored. I think I'd rather build the name at runtime as that means there is less scope for screw ups when the application is deployed.

Many thanks for any help, I've been tearing my hair out all day on this.

A: 

how can I set the modle name in maven or how do I recover the module name so that I can programatically build the JNDI name at runtime.

Well, as written in the provided link, about the module-name of Portable Global JNDI name in EJB 3.1:

<module-name> defaults to bundle name (.war or .jar) without the bundle extension.

So I would configure Maven and set the finalName to not include the version in the name of the WAR:

<project>
  ...
  <build>
    <finalName>${artifactId}</finalName>
    ...
  </build>
</project>

Not sure why NetBeans includes the package name and underscore (NetBeans is doing that, right?) when deploying on GlassFish and if you can avoid NB doing that.

Pascal Thivent
Sorry, I've already tried that <finalName>myapp</finalName> and it didn't help. It did, however, stop the version being included in the name of the war.To be honest I'm not sure if it's maven or netbeans that is deploying the application with the name "com.example_myapp_war_1.0-SNAPSHOT" but that is the name shown in the GF admin console. I know the parts of the name come from the POM so I assume it's Maven that's generating the name. I think GF is running the application from the target war directory so not picking up the name of the war file.
wobblycogs
A: 

If the EJB is included in the WAR, just use the java:module alias:

java:module/CustomerService
bkail
That got it, many thanks. To anyone else having this problem some additional information can be found here: http://blogs.sun.com/kensaks/entry/application_specified_portable_jndi_names
wobblycogs