KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > javaee > blueprints > components > ui > example > CompBPhaseListener


1 /*
2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
3 * http://developer.sun.com/berkeley_license.html
4 * $Id: CompBPhaseListener.java,v 1.4 2006/06/26 16:29:05 basler Exp $
5 */

6 /*
7  * CompBPhaseListener.java
8  *
9  * Created on June 16, 2006, 1:41 PM
10  *
11  * To change this template, choose Tools | Template Manager
12  * and open the template in the editor.
13  */

14 package com.sun.javaee.blueprints.components.ui.example;
15
16 import java.io.DataInputStream JavaDoc;
17 import java.io.DataOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLConnection JavaDoc;
23 import javax.faces.event.PhaseEvent;
24 import javax.faces.event.PhaseId;
25 import javax.faces.event.PhaseListener;
26 import javax.faces.context.FacesContext;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.el.MethodExpression;
29
30 /**
31  * <p>
32  * Phase listener that responds to requests for the JavaScript files,
33  * images and text templates
34  *
35  * @author Mark Basler
36  */

37 public class CompBPhaseListener implements PhaseListener {
38     
39     public static final String JavaDoc PATH_PREFIX = "/META-INF";
40     public static final String JavaDoc SCRIPT_SUFFIX = ".js";
41     public static final String JavaDoc CSS_SUFFIX = ".css";
42     public static final String JavaDoc GIF_SUFFIX = ".gif";
43     public static final String JavaDoc JPG_SUFFIX = ".jpg";
44     public static final String JavaDoc PNG_SUFFIX = ".png";
45     public static final String JavaDoc APP_KEY = "/jsf-example/";
46     public static final String JavaDoc ACTION_KEY = "/bpui_compb_action/";
47     public static final boolean bDebug=false;
48     
49     public CompBPhaseListener() {
50     }
51
52     /*
53      * After phase listener that serves resources
54      */

55     public void afterPhase(PhaseEvent event) {
56         FacesContext context=event.getFacesContext();
57         String JavaDoc rootId=context.getViewRoot().getViewId();
58
59         if(bDebug) System.out.println("PhaseListener - Root ID " + rootId);
60         int iPos=rootId.indexOf(APP_KEY);
61         int iPosx=rootId.indexOf(ACTION_KEY);
62         
63         // see what suffix is used for mapping to content type
64
if (rootId.endsWith(SCRIPT_SUFFIX) && iPos > -1) {
65             handleResourceRequest(event, PATH_PREFIX + rootId, "text/javascript");
66         } else if (rootId.endsWith(CSS_SUFFIX) && iPos > -1) {
67             handleResourceRequest(event, PATH_PREFIX + rootId, "text/css");
68         } else if (rootId.endsWith(GIF_SUFFIX) && iPos > -1) {
69             handleResourceRequest(event, PATH_PREFIX + rootId, "image/gif");
70         } else if (rootId.endsWith(JPG_SUFFIX) && iPos > -1) {
71             handleResourceRequest(event, PATH_PREFIX + rootId, "image/jpeg");
72         } else if (rootId.endsWith(PNG_SUFFIX) && iPos > -1) {
73             handleResourceRequest(event, PATH_PREFIX + rootId, "image/x-png");
74         } else if (iPosx > -1) {
75             // action to invoke through a deferred method expression
76
String JavaDoc methodx="#{" + rootId.substring(iPos + ACTION_KEY.length() + 1) + "}";
77             methodx=methodx.replace('/','.');
78             if(bDebug) System.out.println("Method expression to call = " + methodx);
79             
80             try {
81                 Class JavaDoc[] argTypes = { PhaseEvent.class};
82                 MethodExpression mex=context.getApplication().getExpressionFactory().createMethodExpression(context.getELContext(),
83                         methodx, null, argTypes);
84                 Object JavaDoc[] args = { event };
85                 mex.invoke(context.getELContext(), args);
86             } catch (Exception JavaDoc e) {
87                 // Just log exception
88
e.printStackTrace();
89             }
90             
91             
92         }
93     }
94
95     
96     /**
97      * The URL looks like a request for a resource, such as a JavaScript or CSS file. Write
98      * the given resource to the response writer.
99      */

100     private void handleResourceRequest(PhaseEvent event, String JavaDoc resource, String JavaDoc contentType) {
101         
102         // get full qualified path of class
103
URL JavaDoc sxURL = CompBPhaseListener.class.getResource(resource);
104         if(bDebug) System.out.println("Resource url = " + sxURL);
105         // check for resource
106
if(sxURL == null) return;
107
108         HttpServletResponse JavaDoc response=(HttpServletResponse JavaDoc)event.getFacesContext().getExternalContext().getResponse();
109         OutputStream JavaDoc outStream=null;
110         try {
111             response.setContentType(contentType);
112             response.setStatus(200);
113             outStream=response.getOutputStream();
114             // use util to write binary to output stream
115
readWriteBinaryUtil(sxURL, outStream);
116         } catch (Exception JavaDoc e) {
117             String JavaDoc message = "Can't find resource \"" + resource + "\" in the same directory as the class - " + getClass().getResource("PopupPhaseListener.class");
118             System.out.println(message);
119             e.printStackTrace();
120         } finally {
121             try {
122                 outStream.flush();
123                 //outStream.close();
124
} catch (Exception JavaDoc ee) {}
125             event.getFacesContext().responseComplete();
126         }
127     }
128
129     
130     
131     public void beforePhase(PhaseEvent event) {
132     }
133
134     public PhaseId getPhaseId() {
135         return PhaseId.RESTORE_VIEW;
136     }
137
138     /**
139      * The URL looks like a request for a resource, such as a JavaScript or CSS file. Write
140      * the given resource to the response output in binary format (needed for images).
141      */

142     public static void readWriteBinaryUtil(URL JavaDoc sxURL, OutputStream JavaDoc outStream) throws IOException JavaDoc {
143         
144         DataOutputStream JavaDoc outData=null;
145         DataInputStream JavaDoc inData=null;
146         int byteCnt=0;
147         byte[] buffer=new byte[4096];
148         
149         // get full qualified path of class
150
if(bDebug) System.out.println("RW Loading - " + sxURL);
151         try {
152             outData=new DataOutputStream JavaDoc(outStream);
153             inData=new DataInputStream JavaDoc(openStream(sxURL));
154             
155             while ((byteCnt=inData.read(buffer)) != -1) {
156                 if (outData != null && byteCnt > 0) {
157                     outData.write(buffer, 0, byteCnt);
158                 }
159             }
160         } catch (IOException JavaDoc e) {
161             throw e;
162         } finally {
163             try {
164                 if(inData != null) {
165                     inData.close();
166                 }
167             } catch (IOException JavaDoc ioe) {}
168         }
169     }
170     
171     
172     private static InputStream JavaDoc openStream(URL JavaDoc sxURL) throws IOException JavaDoc {
173         // set cache to false, so the ./lib/bp-popup-balloon.jar file doesn't get locked on redeploy or undeploy on windows.
174
InputStream JavaDoc isx=null;
175         if(sxURL!= null) {
176             URLConnection JavaDoc urlConn = sxURL.openConnection();
177             urlConn.setUseCaches(false);
178             isx=urlConn.getInputStream();
179         }
180         return isx;
181     }
182     
183 }
184
Popular Tags