My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

joi, 23 aprilie 2015

[OmniFaces utilities (2.2)] Get the named env entry for the deployment component from where this is called


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; 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()):
Usage:

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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors