Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i config module and application name for JNDI Lookups

In EJB 3.1 JNDI Lookups can be made with different Lookup-Names:

java:global[/<app-name>]/<module-name>/<bean-name>!<fully-qualifiedbean interface-name>           
java:global[/<app-name>]/<module-name>/<bean-name> 
java:app/<module-name>/<bean-name>!<fully-qualified-bean-interface-name> 
java:app/<module-name>/<bean-name> 
java:module/<bean-name>!<fully-qualified-bean-interface-name> 
java:module/<bean-name>

In my JavaEE 6 Project (with Maven 2, Netbeans 6 and Glassfish v3) the Application name is X-Snapshot.ear and the EJB-Module is Y-Snapshot.jar. How can i config this maven project to use another application and module name? I don't wnat to change all JNDI Lookups when this names change!! So is it possible to config application and module names for JNDI LookUps?

like image 727
Michael W. Avatar asked Aug 07 '10 17:08

Michael W.


People also ask

What does JNDI lookup () method do?

The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and resources (in the form of Java objects) via a name.

What is JNDI name example?

A name that is bound within a context is the JNDI name of the object. In Specifying a Resource Reference, for example, the JNDI name for the JDBC resource (or data source) is jdbc/ejbTutorialDB .

Which exception do you use to detect if a lookup on a JNDI resource has failed?

If a resource reference does not exist in the application module, the JNDI lookup will fail with the javax. naming. NamingException mentioned above.


2 Answers

Naive approach

The Maven EAR Plugin allows to Customize A Module Filename and you can set the final name or the EAR using project.build.finalName.

Much better approach

Override the <application-name> and the <module-name> in the application.xml and the ejb-jar.xml respectively. Quoting Portable Global JNDI name in EJB 3.1:

In addition to the above name, if the EJB exposes just a single client view (that is it implements just one interface or the no interface view), the container is also mandated to map the bean to

java:global/[<application-name>]/<module-name>/<bean-name>

Where

  1. <aplication-name> defaults to the bundle name (.ear file name) without the bundle extension. This can be overridden in application.xml. Also, <application-name> is applicable only if the bean is packaged inside a .ear file.
  2. <module-name> defaults to bundle name (.war or .jar) without the bundle extension. Again, this can be overridden in ejb-jar.xml.
  3. <bean-name> defaults to the unqualified class name of the bean. However, if @Stateful or @Stateless or @Singleton uses the name attribute, then the value specified there will be used as the bean name.
like image 58
Pascal Thivent Avatar answered Oct 05 '22 06:10

Pascal Thivent


The application name and module names can be looked up at runtime via JNDI:

@Resource(lookup = "java:app/AppName")
private String appName;

@Resource(lookup = "java:module/ModuleName")
private String moduleName;

Although you can configure the application-name and module-name in your application deployment descriptor as described, these names can still be overridden at deployment-time (per the Java EE specification, as indicated below), so it's best not to hard-code these values in your application code.

EE.8.5.2 Deploying a Java EE Application and EE.8.5.1 Deploying a Stand-Alone Java EE Module

The deployment tool must ensure that the application name is unique in the application server instance. If the name is not unique, the deployment tool may automatically choose a unique name or allow the Deployer to choose a unique name

EE.8.1.1 Component Creation

If and only if the name is not unique (e.g., because two names are identical after removing different filename extensions) the deployment tool may choose new unique names for any of the conflicting modules; module names that do not conflict must not be changed. The algorithm for choosing unique names in such a case is product specific.

like image 33
shelley Avatar answered Oct 05 '22 04:10

shelley