com.sun.j2ee.blueprints.ui.util
Class ResourcePhaseListener

java.lang.Object
  extended by com.sun.j2ee.blueprints.ui.util.ResourcePhaseListener
All Implemented Interfaces:
java.io.Serializable, java.util.EventListener, javax.faces.event.PhaseListener

public class ResourcePhaseListener
extends java.lang.Object
implements javax.faces.event.PhaseListener

Leverage the Faces Lifecycle to load non-Java class resources out of jars in the web-app classloader. This class enables packing non-Java class resources associated with a component into the same jar as the classes of the component itself. For example, a component may require extra JavaScript and style sheet files. It is a burden to distribute these artifacts separately from the classes themselves.

Security Caution: The default operation of this class allows anyone to access the contents of the web app classloader using a simple HTTP get. Users are advised to restrict access to the "/resource" viewId such that such accesses are subject to standard Servlet security constraints.

Build-Time Usage

This listener is self-configured by placing the jar file in which it is distributed into WEB-INF/lib.

Referencing Resources Using this PhaseListener

Let's assume we have a JavaScript file in our component.jar under the jar entry named /META-INF/autocomplete/script.js. To access this jar from a web page in a prefix mapped FacesServlet, the following <script> tag must be used.


<script type="text/javascript" 
           src="faces/resource?r=/META-INF/autocomplete/script.js" />

To do so in an extension mapped FacesServlet, do this:


<script type="text/javascript" 
           src="resource.faces?r=/META-INF/autocomplete/script.js" />

How it works

This class is very simple. Inspection of the above HTML code reveals that we simply use a well known JSF view id of "resource" combined with the query parameter named r whose value is the path to the resource in the web app classloader. An additional query parameter of ct is supported to pass the content-type. If no ct parameter is specified, this class uses the extension to identify the content-type.

The following extension to content-type mappings are understood.

extension content-type
js text/javascript
gif image/gif
jpeg image/jpeg
jpg image/jpeg
png image/png
ico image/vnd.microsoft.icon

See Also:
Serialized Form

Field Summary
static java.lang.String DEFAULT_CONTENT_TYPE
           
static java.lang.String RESOURCE_PREFIX
           
 
Constructor Summary
ResourcePhaseListener()
          Creates a new instance of ResourcePhaseListener
 
Method Summary
 void afterPhase(javax.faces.event.PhaseEvent event)
           
 void beforePhase(javax.faces.event.PhaseEvent phaseEvent)
           
 javax.faces.event.PhaseId getPhaseId()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESOURCE_PREFIX

public static final java.lang.String RESOURCE_PREFIX
See Also:
Constant Field Values

DEFAULT_CONTENT_TYPE

public static final java.lang.String DEFAULT_CONTENT_TYPE
See Also:
Constant Field Values
Constructor Detail

ResourcePhaseListener

public ResourcePhaseListener()
Creates a new instance of ResourcePhaseListener

Method Detail

getPhaseId

public javax.faces.event.PhaseId getPhaseId()
Specified by:
getPhaseId in interface javax.faces.event.PhaseListener

beforePhase

public void beforePhase(javax.faces.event.PhaseEvent phaseEvent)
Specified by:
beforePhase in interface javax.faces.event.PhaseListener

afterPhase

public void afterPhase(javax.faces.event.PhaseEvent event)
Specified by:
afterPhase in interface javax.faces.event.PhaseListener