이 콘텐츠는 선택한 언어로 제공되지 않습니다.
9.6. J2EE and JNDI - The Application Component Environment
- Application component business logic should be coded to access information from its ENC. The component provider uses the standard deployment descriptor for the component to specify the required ENC entries. The entries are declarations of the information and resources the component requires at runtime.
- The container provides tools that allow a deployer of a component to map the ENC references made by the component developer to the deployment environment entity that satisfies the reference.
- The component deployer utilizes the container tools to ready a component for final deployment.
- The component container uses the deployment package information to build the complete component ENC at runtime
javax.naming.InitialContext object by using the no argument constructor and then looks up the naming environment under the name java:comp/env. The application component's environment entries are stored directly in the ENC, or in its subcontexts. Example 9.4, “ENC access sample code” illustrates the prototypical lines of code a component uses to access its ENC.
Example 9.4. ENC access sample code
// Obtain the application component's ENC
Context iniCtx = new InitialContext();
Context compEnv = (Context) iniCtx.lookup("java:comp/env");
// Obtain the application component's ENC
Context iniCtx = new InitialContext();
Context compEnv = (Context) iniCtx.lookup("java:comp/env");
Bean1 cannot access the ENC elements of EJB Bean2, and vice versa. Similarly, Web application Web1 cannot access the ENC elements of Web application Web2 or Bean1 or Bean2 for that matter. Also, arbitrary client code, whether it is executing inside of the application server VM or externally cannot access a component's java:comp JNDI context. The purpose of the ENC is to provide an isolated, read-only namespace that the application component can rely on regardless of the type of environment in which the component is deployed. The ENC must be isolated from other components because each component defines its own ENC content. Components A and B, for example, may define the same name to refer to different objects. For example, EJB Bean1 may define an environment entry java:comp/env/red to refer to the hexadecimal value for the RGB color for red, while Web application Web1 may bind the same name to the deployment environment language locale representation of red.
java:comp, names under java:, and any other name. As discussed, the java:comp context and its subcontexts are only available to the application component associated with that particular context. Subcontexts and object bindings directly under java: are only visible within the server virtual machine and not to remote clients. Any other context or object binding is available to remote clients, provided the context or object supports serialization. You'll see how the isolation of these naming scopes is achieved in the Section 9.2, “The JBoss Naming Service Architecture”.
java: context is useful would be a javax.sql.DataSource connection factory that can only be used inside of the server where the associated database pool resides. On the other hand, an EJB home interface would be bound to a globally visible name that should accessible by remote client.
9.6.1. ENC Usage Conventions 링크 복사링크가 클립보드에 복사되었습니다!
ejb-jar.xml deployment descriptor for EJB components, and the standard web.xml deployment descriptor for Web components. Several different types of information may be stored in and retrieved from JNDI including:
- Environment entries as declared by the
env-entryelements - EJB references as declared by
ejb-refandejb-local-refelements. - Resource manager connection factory references as declared by the
resource-refelements - Resource environment references as declared by the
resource-env-refelements
9.6.1.1. Environment Entries 링크 복사링크가 클립보드에 복사되었습니다!
env-entry element in the standard deployment descriptors. The env-entry element contains the following child elements:
- An optional description element that provides a description of the entry
- An env-entry-name element giving the name of the entry relative to
java:comp/env - An env-entry-type element giving the Java type of the entry value that must be one of:
java.lang.Bytejava.lang.Booleanjava.lang.Characterjava.lang.Doublejava.lang.Floatjava.lang.Integerjava.lang.Longjava.lang.Shortjava.lang.String
- An env-entry-value element giving the value of entry as a string
env-entry fragment from an ejb-jar.xml deployment descriptor is given in Example 9.5, “An example ejb-jar.xml env-entry fragment”. There is no JBoss specific deployment descriptor element because an env-entry is a complete name and value specification. Example 9.6, “ENC env-entry access code fragment” shows a sample code fragment for accessing the maxExemptions and taxRate and env-entry values declared in the deployment descriptor.
Example 9.5. An example ejb-jar.xml env-entry fragment
Example 9.6. ENC env-entry access code fragment
InitialContext iniCtx = new InitialContext();
Context envCtx = (Context) iniCtx.lookup("java:comp/env");
Integer maxExemptions = (Integer) envCtx.lookup("maxExemptions");
Float taxRate = (Float) envCtx.lookup("taxRate");
InitialContext iniCtx = new InitialContext();
Context envCtx = (Context) iniCtx.lookup("java:comp/env");
Integer maxExemptions = (Integer) envCtx.lookup("maxExemptions");
Float taxRate = (Float) envCtx.lookup("taxRate");
9.6.1.2. EJB References 링크 복사링크가 클립보드에 복사되었습니다!
java:comp/env/ejb context of the application component's environment.
ejb-ref element in the deployment descriptor. Each ejb-ref element describes the interface requirements that the referencing application component has for the referenced enterprise bean. The ejb-ref element contains the following child elements:
- An optional description element that provides the purpose of the reference.
- An ejb-ref-name element that specifies the name of the reference relative to the
java:comp/envcontext. To place the reference under the recommendedjava:comp/env/ejbcontext, use anejb/link-nameform for theejb-ref-namevalue. - An ejb-ref-type element that specifies the type of the EJB. This must be either
EntityorSession. - A home element that gives the fully qualified class name of the EJB home interface.
- A remote element that gives the fully qualified class name of the EJB remote interface.
- An optional ejb-link element that links the reference to another enterprise bean in the same EJB JAR or in the same J2EE application unit. The
ejb-linkvalue is theejb-nameof the referenced bean. If there are multiple enterprise beans with the sameejb-name, the value uses the path name specifying the location of theejb-jarfile that contains the referenced component. The path name is relative to the referencingejb-jarfile. The Application Assembler appends theejb-nameof the referenced bean to the path name separated by#. This allows multiple beans with the same name to be uniquely identified.
ejb-ref element. This means that the EJB reference is not accessible from other application components at runtime, and that other application components may define ejb-ref elements with the same ejb-ref-name without causing a name conflict. Example 9.7, “An example ejb-jar.xml ejb-ref descriptor fragment” provides an ejb-jar.xml fragment that illustrates the use of the ejb-ref element. A code sample that illustrates accessing the ShoppingCartHome reference declared in Example 9.7, “An example ejb-jar.xml ejb-ref descriptor fragment” is given in Example 9.8, “ENC ejb-ref access code fragment”.
Example 9.7. An example ejb-jar.xml ejb-ref descriptor fragment
Example 9.8. ENC ejb-ref access code fragment
InitialContext iniCtx = new InitialContext();
Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup("ShoppingCartHome");
InitialContext iniCtx = new InitialContext();
Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup("ShoppingCartHome");
9.6.1.3. EJB References with jboss.xml and jboss-web.xml 링크 복사링크가 클립보드에 복사되었습니다!
jboss.xml EJB deployment descriptor affects EJB references in two ways. First, the jndi-name child element of the session and entity elements allows the user to specify the deployment JNDI name for the EJB home interface. In the absence of a jboss.xml specification of the jndi-name for an EJB, the home interface is bound under the ejb-jar.xmlejb-name value. For example, the session EJB with the ejb-name of ShoppingCartBean in Example 9.7, “An example ejb-jar.xml ejb-ref descriptor fragment” would have its home interface bound under the JNDI name ShoppingCartBean in the absence of a jboss.xmljndi-name specification.
jboss.xml descriptor with respect to ejb-refs is the setting of the destination to which a component's ENC ejb-ref refers. The ejb-link element cannot be used to refer to EJBs in another enterprise application. If your ejb-ref needs to access an external EJB, you can specify the JNDI name of the deployed EJB home using the jboss.xmlejb-ref/jndi-name element.
jboss-web.xml descriptor is used only to set the destination to which a Web application ENC ejb-ref refers. The content model for the JBoss ejb-ref is as follows:
- An ejb-ref-name element that corresponds to the ejb-ref-name element in the ejb-jar.xml or web.xml standard descriptor
- A
jndi-nameelement that specifies the JNDI name of the EJB home interface in the deployment environment
jboss.xml descriptor fragment that illustrates the following usage points:
- The
ProductBeanUserejb-reflink destination is set to the deployment name ofjboss/store/ProductHome - The deployment JNDI name of the
ProductBeanis set tojboss/store/ProductHome
Example 9.9. An example jboss.xml ejb-ref fragment
9.6.1.4. EJB Local References 링크 복사링크가 클립보드에 복사되었습니다!
java:comp/env/ejb context of the application component's environment.
ejb-local-ref element in the deployment descriptor. Each ejb-local-ref element describes the interface requirements that the referencing application component has for the referenced enterprise bean. The ejb-local-ref element contains the following child elements:
- An optional description element that provides the purpose of the reference.
- An ejb-ref-name element that specifies the name of the reference relative to the
java:comp/envcontext. To place the reference under the recommendedjava:comp/env/ejbcontext, use anejb/link-nameform for theejb-ref-namevalue. - An ejb-ref-type element that specifies the type of the EJB. This must be either
EntityorSession. - A local-home element that gives the fully qualified class name of the EJB local home interface.
- A local element that gives the fully qualified class name of the EJB local interface.
- An ejb-link element that links the reference to another enterprise bean in the
ejb-jarfile or in the same J2EE application unit. Theejb-linkvalue is theejb-nameof the referenced bean. If there are multiple enterprise beans with the sameejb-name, the value uses the path name specifying the location of theejb-jarfile that contains the referenced component. The path name is relative to the referencingejb-jarfile. The Application Assembler appends theejb-nameof the referenced bean to the path name separated by#. This allows multiple beans with the same name to be uniquely identified. Anejb-linkelement must be specified in JBoss to match the local reference to the corresponding EJB.
ejb-local-ref element. This means that the EJB local reference is not accessible from other application components at runtime, and that other application components may define ejb-local-ref elements with the same ejb-ref-name without causing a name conflict. Example 9.10, “An example ejb-jar.xml ejb-local-ref descriptor fragment” provides an ejb-jar.xml fragment that illustrates the use of the ejb-local-ref element. A code sample that illustrates accessing the ProbeLocalHome reference declared in Example 9.10, “An example ejb-jar.xml ejb-local-ref descriptor fragment” is given in Example 9.11, “ENC ejb-local-ref access code fragment”.
Example 9.10. An example ejb-jar.xml ejb-local-ref descriptor fragment
Example 9.11. ENC ejb-local-ref access code fragment
InitialContext iniCtx = new InitialContext();
Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
ProbeLocalHome home = (ProbeLocalHome) ejbCtx.lookup("ProbeLocalHome");
InitialContext iniCtx = new InitialContext();
Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
ProbeLocalHome home = (ProbeLocalHome) ejbCtx.lookup("ProbeLocalHome");
9.6.1.5. Resource Manager Connection Factory References 링크 복사링크가 클립보드에 복사되었습니다!
resource-ref elements in the standard deployment descriptors. The Deployer binds the resource manager connection factory references to the actual resource manager connection factories that exist in the target operational environment using the jboss.xml and jboss-web.xml descriptors.
resource-ref element describes a single resource manager connection factory reference. The resource-ref element consists of the following child elements:
- An optional description element that provides the purpose of the reference.
- A res-ref-name element that specifies the name of the reference relative to the
java:comp/envcontext. The resource type based naming convention for which subcontext to place theres-ref-nameinto is discussed in the next paragraph. - A res-type element that specifies the fully qualified class name of the resource manager connection factory.
- A res-auth element that indicates whether the application component code performs resource sign on programmatically, or whether the container signs on to the resource based on the principal mapping information supplied by the Deployer. It must be one of
ApplicationorContainer. - An optional res-sharing-scope element. This currently is not supported by JBoss.
- JDBC
DataSourcereferences should be declared in thejava:comp/env/jdbcsubcontext. - JMS connection factories should be declared in the
java:comp/env/jmssubcontext. - JavaMail connection factories should be declared in the
java:comp/env/mailsubcontext. - URL connection factories should be declared in the
java:comp/env/urlsubcontext.
web.xml descriptor fragment that illustrates the resource-ref element usage. Example 9.13, “ENC resource-ref access sample code fragment” provides a code fragment that an application component would use to access the DefaultMail resource declared by the resource-ref.
Example 9.12. A web.xml resource-ref descriptor fragment
Example 9.13. ENC resource-ref access sample code fragment
Context initCtx = new InitialContext();
javax.mail.Session s = (javax.mail.Session)
initCtx.lookup("java:comp/env/mail/DefaultMail");
Context initCtx = new InitialContext();
javax.mail.Session s = (javax.mail.Session)
initCtx.lookup("java:comp/env/mail/DefaultMail");
9.6.1.6. Resource Manager Connection Factory References with jboss.xml and jboss-web.xml 링크 복사링크가 클립보드에 복사되었습니다!
jboss.xml EJB deployment descriptor and jboss-web.xml Web application deployment descriptor is to provide the link from the logical name defined by the res-ref-name element to the JNDI name of the resource factory as deployed in JBoss. This is accomplished by providing a resource-ref element in the jboss.xml or jboss-web.xml descriptor. The JBoss resource-ref element consists of the following child elements:
- A res-ref-name element that must match the
res-ref-nameof a correspondingresource-refelement from theejb-jar.xmlorweb.xmlstandard descriptors - An optional res-type element that specifies the fully qualified class name of the resource manager connection factory
- A jndi-name element that specifies the JNDI name of the resource factory as deployed in JBoss
- A res-url element that specifies the URL string in the case of a
resource-refof typejava.net.URL
jboss-web.xml descriptor fragment that shows sample mappings of the resource-ref elements given in Example 9.12, “A web.xml resource-ref descriptor fragment”.
Example 9.14. A sample jboss-web.xml resource-ref descriptor fragment
9.6.1.7. Resource Environment References 링크 복사링크가 클립보드에 복사되었습니다!
resource-env-ref elements in the standard deployment descriptors. The Deployer binds the resource environment references to the actual administered objects location in the target operational environment using the jboss.xml and jboss-web.xml descriptors.
resource-env-ref element describes the requirements that the referencing application component has for the referenced administered object. The resource-env-ref element consists of the following child elements:
- An optional description element that provides the purpose of the reference.
- A resource-env-ref-name element that specifies the name of the reference relative to the
java:comp/envcontext. Convention places the name in a subcontext that corresponds to the associated resource factory type. For example, a JMS queue reference namedMyQueueshould have aresource-env-ref-nameofjms/MyQueue. - A resource-env-ref-type element that specifies the fully qualified class name of the referenced object. For example, in the case of a JMS queue, the value would be
javax.jms.Queue.
resource-ref-env element declaration by a session bean. Example 9.16, “ENC resource-env-ref access code fragment” gives a code fragment that illustrates how to look up the StockInfo queue declared by the resource-env-ref.
Example 9.15. An example ejb-jar.xml resource-env-ref fragment
Example 9.16. ENC resource-env-ref access code fragment
InitialContext iniCtx = new InitialContext();
javax.jms.Queue q = (javax.jms.Queue)
envCtx.lookup("java:comp/env/jms/StockInfo");
InitialContext iniCtx = new InitialContext();
javax.jms.Queue q = (javax.jms.Queue)
envCtx.lookup("java:comp/env/jms/StockInfo");
9.6.1.8. Resource Environment References and jboss.xml, jboss-web.xml 링크 복사링크가 클립보드에 복사되었습니다!
jboss.xml EJB deployment descriptor and jboss-web.xml Web application deployment descriptor is to provide the link from the logical name defined by the resource-env-ref-name element to the JNDI name of the administered object deployed in JBoss. This is accomplished by providing a resource-env-ref element in the jboss.xml or jboss-web.xml descriptor. The JBoss resource-env-ref element consists of the following child elements:
- A
resource-env-ref-nameelement that must match theresource-env-ref-nameof a correspondingresource-env-refelement from theejb-jar.xmlorweb.xmlstandard descriptors - A
jndi-nameelement that specifies the JNDI name of the resource as deployed in JBoss
jboss.xml descriptor fragment that shows a sample mapping for the StockInforesource-env-ref.
Example 9.17. A sample jboss.xml resource-env-ref descriptor fragment