4/17/2010

Env-entry enhancement in JavaEE 6

JavaEE 6 adds 2 additional types to the list of env-entry types: java.lang.Class and any enum type. So there are now 11 portable env-entry types that must be supported by all compliant application servers:

       java.lang.Class
any enum type
java.lang.Boolean
java.lang.Byte
java.lang.Character
java.lang.String
java.lang.Short
java.lang.Integer
java.lang.Long
java.lang.Float
java.lang.Double
For example, to use java.lang.Class and enum env-entry-type in a web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<env-entry>
<description>example of java.lang.Class env-entry-type</description>
<env-entry-name>java:app/env/myImplClass</env-entry-name>
<env-entry-type>java.lang.Class</env-entry-type>
<env-entry-value>test.MyServiceProvider</env-entry-value>
</env-entry>

<env-entry>
<description>example of enum env-entry-type</description>
<env-entry-name>java:app/env/day</env-entry-name>
<env-entry-type>com.examplexxx.util.Day</env-entry-type>
<env-entry-value>SUNDAY</env-entry-value>
</env-entry>
</web-app>
To inject the above 2 env-entry into fields in a servlet, or EJB bean class, or any other components in the EAR:
@Resource(lookup="java:app/env/myImplClass")
@SuppressWarnings("unchecked")
private Class myImplClass;

@Resource(lookup="java:app/env/day")
private com.examplexxx.util.Day day;
For java.lang.Class type, only the raw type, not the generic type, can be declared as the field type, hence @SuppressWarnings("unchecked").