THIS METHOD IS AVAILABLE IN 2.0, BUT IT IS RECOMMENDED TO USE OmniFaces 2.2 SNAPSHOT (FINAL) or higher!
Some environments unintelligibly wrap NameNotFoundException in
a plain NamingException - this was fixed starting with version 2.2 - SNAPSHOT (FINAL).
Note that the JSF spec defines one parameter that can optionally be defined via an env entry instead of the typical context/init parameter;
Note that the JSF spec defines one parameter that can optionally be defined via an env entry instead of the typical context/init parameter;
ProjectStage#PROJECT_STAGE_JNDI_NAME
. Mojarra defines an additional proprietary one: java:comp/env/ClientStateSavingPassword
.[OmniFaces utilities] The
getEnvEntry()
method returns the named environment entry for the deployment component from where this is called. From within the web module this returns the <env-entry-value>
of a <env-entry>
in web.xml
associated with the given <env-entry-name>
.Note that the name used here is relative to "java:comp/env", which is exactly as it appears in
web.xml
.Example:
web.xml
<env-entry>
<env-entry-name>org.omnifaces.TEST_INTEGER</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>10</env-entry-value>
</env-entry>
Lookup in Java using relative name
Integer test = JNDI.getEnvEntry("org.omnifaces.TEST_INTEGER");
Lookup in Java using full JNDI name
Integer test = JNDI.lookup("java:comp/env/org.omnifaces.TEST_INTEGER");
Note that even the "full JNDI name" is relative to the "deployment component" from which the lookup is done. To use a true global JNDI name an additional
<lookup-name>
should be specified in web.xml
.[OmniFaces utilities] The
lookup()
method returns the named object from the default JNDI instance.Methods (
getEnvEntry()
and lookup()
):
When
annotations and resource injection are not supported, you can still get the
desired resource using "manual" JNDI lookup. For example, the OmniFaces,
JNDI#getEnvEntry() performs a lookup using an
environment entry name relative to "java:comp/env".
It will return the environment entry value associated with the given name, or null if there is none.
DemoEJB ejb;
DataSource ds;
JMSContext jmsContext;
import org.omnifaces.util.JNDI;
...
ds =
JNDI.getEnvEntry("jdbc/sample");
jmsContext =
JNDI.getEnvEntry("jms/testQueueFactory");
The JNDI#getEnvEntry() method actually calls "under the
hood" the JNDI#lookup() method, which
is used to lookup using a full JNDI name:
import org.omnifaces.util.JNDI;
...
ejb =
JNDI.lookup("java:module/DemoEJB");
ds =
JNDI.lookup("java:comp/env/jdbc/sample");
jmsContext =
JNDI.lookup("java:comp/env/jms/testQueueFactory");
Note that
the “full JNDI name” is relative to the “deployment component” from which the
lookup is done.
At the end,
you don’t need to worry about closing the InitialContext,
as it is done automatically by OmniFaces using a private JNDI#close() method.
Niciun comentariu :
Trimiteți un comentariu