Specifying ProjectStage in JNDI with WebLogic

original post

##Overview Starting in JSF 2.0 (JSR-314) a feature was added called ProjectStage. This is an attribute that signifies the stage where the project is running: Development, UnitTest, SystemTest, or Production. The idea is that if the code knows where it is running, it can plan better. As an example, in a production environment certain optimizations could occur during startup that would cause better overal performance. However, these optimizations during start up is a problem during development because during development there is a lot more shutting down and starting up of the server.

There are two ways to specify the ProjectStage: web.xml and JNDI. The problem with using web.xml is that you need to update or override it as you go move from one environment to the next. JNDI is configured on the application server, so it is the ideal place to configure ProjectStage.

In WebLogic (11g), you cannot add custom resources to JNDI. So, out of the box, you can not specify ProjectStage in JNDI. In a previous post, I described Weblogic JNDI Custom Resource Configuration, which is a project where I configure JNDI in WebLogic using Startup Classes.

##Adding ProjectStage to JNDI Follow the documentation at Weblogic JNDI Custom Resource Configuration to install it. Use the following configuration:

ProjectStage Configuration

Property Value
Name jsf-stage
Class Name: com.idmworks.weblogic.jndiconfiguration.StringInitializer
Failure is Fatal: unticked
Run Before Application Deployments: unticked
Run Before Application Activations: ticked

This will configure jsf/master/ProjectStage to store the value of “Development”

###Add Resource to web.xml In the previous step, “Development” was added to the global name space as jsf/master/ProjectStage. The JSF JSR Documentation states that it looks for the ProjectStage at java:comp/env/jsf/ProjectStage which is the local namespace. In order to define it in the local name space, we need to specify it in web.xml.

<web-app version="2.5" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <!-- ... other configuration removed ... -->
  <resource-ref>
    <res-ref-name>jsf/ProjectStage</res-ref-name>
    <res-type>java.lang.String</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <!-- ... other configuration removed ... -->
</web-app>

###Add Resource to weblogic.xml In the previous step, we allocated the place holder for java:comp/env/jsf/ProjectStage, we now need to map the global location to the local place holder:

####weblogic.xml

<weblogic-web-app xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.bea.com/ns/weblogic/90" xsi:schemalocation="http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">
  <!-- ... other configuration removed ... -->
  <resource-description>
    <res-ref-name>jsf/ProjectStage</res-ref-name>
    <jndi-name>jsf/master/ProjectStage</jndi-name>
  </resource-description>
</weblogic-web-app>

###Verify ProjectStage Below is a small snippet of code that you can add to any facelet to check what the current projectStage is set to

Stage: #{facesContext.application.projectStage}

##References

Written on April 3, 2012