Chapter 5 Naming Services
Java Naming and Directory Interface (JNDI) is a standard Java interface for accessing distributed objects and services by name. It provides a portable, unified interface for naming and directory services. The JNDI specification is independent of any specific directory or naming service such as LDAP, NDS, DCE/CDS, or NIS.
EAServer's JNDI implementation includes the JNDI service provider interface (SPI), which enables you to use a variety of custom directory and naming services. EAServer uses the SPI in conjunction with the CosNaming interface to provide component lookup capability. Given a bound name, the SPI locates the referenced package and component. Once it locates the component, the SPI works with the client stub interface to instantiate the component and return the requested object.
For complete information about instantiating and resolving objects with JNDI, see Chapter 12, "Creating CORBA Java Clients," in the EAServer Programmer's Guide.
When you start the server, the JNDI classes required
for the server's JDK version are configured automatically.
In J2EE, you can use the application component's naming environment to customize an application's business logic without accessing the source code. The application component's container implements the environment as a JNDI naming context and provides the JNDI interfaces to access the environment properties that you define in the deployment descriptor.
When you deploy a J2EE application, use the deployment descriptor to define all the environment properties that the application component needs to access. This sample code defines the environment property (env-entry) maxExemptions as an Integer and sets its value to 10:
<env-entry> <description> The maximum number of tax exemptions </description> <env-entry-name>maxExemptions</env-entry-name> <env-entry-type>java.lang.Integer</env-entry-type> <env-entry-value>10</env-entry-value> </env-entry>
The information between the opening and closing env-entry tags defines an environment entry element, which consists of:
Within the same container, all instances of an application component share the same environment properties. The component instances cannot modify the environment at runtime.
An application component instance uses the JNDI interfaces to locate the environment naming context and access the environment properties. To locate the naming context, an application creates a javax.naming.InitialContext object and gets the InitialContext for java:comp/env. In this example, the application retrieves the value of the environment property maxExemptions and uses that value to determine an outcome:
Context initContext = new InitialContext(); Context myEnv = (Context)initContext.lookup("java:comp/env"); // Get the maximum number of tax exemptions Integer max=(Integer)myEnv.lookup("maxExemptions"); // Get the minimum number of tax exemptions Integer min = (Integer)myEnv.lookup("minExemptions"); // Use these properties to customize the business logic if (numberOfExemptions > max.intValue() || (numberOfExemptions < min.intValue()) throw new InvalidNumberOfExemptionsException();
Default name service When you call the empty constructor to create a new InitialContext, EAServer sets the Context.INITIAL_CONTEXT_FACTORY system property and sets EAServer's EJB name service as the default.
For information about using Jaguar Manager to add and configure environment properties for a Web application, application client, or EJB component, see "Environment properties" in Chapter 19, "Creating Web Applications," in the EAServer Programmer's Guide.
An EJB reference identifies the home of an enterprise bean. You can use the deployment descriptor to create a link between an EJB reference and an enterprise bean, contained within an EJB-JAR file. Deployment descriptor interfaces allow an application component to access an enterprise bean's home interface using EJB references.
To locate an enterprise bean's home interface, declare an EJB reference in the deployment descriptor and use JNDI to look up the interface. The referenced enterprise bean must be in the ejb subcontext of the application component's environment.
You can declare an EJB reference in the deployment descriptor using the ejb-ref element. The data between the opening and closing ejb-ref tags defines an ejb-ref element. This code sample defines an EJB reference to the Employee entity bean:
<ejb-ref> <description> Reference to the Employee entity bean </description> <ejb-ref-name>ejb/Employee</ejb-ref-name> <ejb-ref-type>Entity</ejb-ref-type> <home>com.wooster.empl.EmployeeHome</home> <remote>com.wooster.empl.Employee</remote> </ejb-ref>
An ejb-ref element contains:
This code sample illustrates how to use JNDI to look up the home interface of the Employee enterprise bean:
// Get the default initial JNDI context Context initContext = new InitialContext(); // Look up the home interface of the Employee enterprise // bean Object result = initContext.lookup("java:comp/env/ejb/Employee"); // Convert the result to the correct type EmployeeHome empHome = (EmployeeHome) javax.rmi.PortableRemoteObject.narrow(result, EmployeeHome.class);
To access an EJB's local interface, instead of the remote interface, define an EJB local reference (ejb-local-ref). Local interfaces are available only to EJB components, Java servlets, and JSPs hosted on the same server as the target component. This sample declares a local reference to the Employee bean, which provides access to its local interface:
<ejb-local-ref> <ejb-ref-name>ejb/EmployeeLocal</ejb-ref-name> <ejb-ref-type>Entity</ejb-ref-type> <local-home> com.wooster.empl.EmployeeLocalHome </local-home> <local>com.wooster.empl.EmployeeLocal</local> <ejb-link>Employee</ejb-link> </ejb-local-ref>
You can define a link from an EJB reference to an enterprise bean by declaring an ejb-link element in the deployment descriptor. The application component and the target enterprise bean must be in the same J2EE application. This example creates a link to the Employee enterprise bean, by adding an ejb-link element to the bean's EJB reference definition:
<ejb-ref> <ejb-ref-name>ejb/Employee</ejb-ref-name> <ejb-ref-type>Entity</ejb-ref-type> <home>com.wooster.empl.EmployeeHome</home> <remote>com.wooster.empl.Employee</remote> <ejb-link>Employee</ejb-link> </ejb-ref>
For information about using Jaguar Manager to add and configure EJB references in Web applications, EJB components, and application clients, see "EJB references" in Chapter 19, "Creating Web Applications," in the EAServer Programmer's Guide.
A resource factory is an object that you use to create resources. You can assign a logical name to a resource factory in the deployment descriptor.
A resource-ref element defines a single resource factory reference. This code sample defines a reference to the resource factory that implements the DataSource interface:
<resource-ref> <description> Data source for the database in which the Employee enterprise bean records transactions </description> <res-ref-name>jdbc/EmployeeAppDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
A resource-ref element contains:
This code sample obtains a reference to the resource factory that implements the DataSource interface, and uses that reference to get a database connection (resource):
// Obtain the initial JNDI context Context initContext = new InitialContext(); // Look up the resource factory using JNDI javax.sql.DataSource ds = (javax.sql.DataSource) initContext.lookup ("java:comp/env/jdbc/EmployeeAppDB"); // Get a database connection java.sql.Connection connection = ds.getConnection();
For information about using Jaguar Manager to add and configure resource references in Web applications, EJB components, or application clients, see "Resource references" in Chapter 19, "Creating Web Applications," in the EAServer Programmer's Guide.
J2EE application components can use the Java Transaction API (JTA) UserTransaction interface to manage transactions. A component instance can look up an object that implements the interface using the JNDI name java:comp/UserTransaction.
In this code sample, an application component uses the interface to manage a transaction:
// Get the initial JNDI context Context initContext = new InitialContext(); // Look up the UserTransaction object UserTransaction tran = (UserTransaction) initContext.lookup("java:comp/UserTransaction"); // Start a transaction tran.begin(); // data updates // Commit the transaction tran.commit();
Copyright © 2002 Sybase, Inc. All rights reserved. |
![]() |