KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > ui > UIDebug


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag.ui;
16
17 import java.io.IOException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.LinkedHashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Map.Entry;
23
24 import javax.faces.component.UIComponentBase;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseWriter;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import com.sun.facelets.util.DevTools;
30 import com.sun.facelets.util.FastWriter;
31
32 /**
33  * @author Jacob Hookom
34  * @version $Id: UIDebug.java,v 1.5 2006/05/09 02:49:40 adamwiner Exp $
35  */

36 public final class UIDebug extends UIComponentBase {
37
38     public final static String JavaDoc COMPONENT_TYPE = "facelets.ui.Debug";
39     public final static String JavaDoc COMPONENT_FAMILY = "facelets";
40     private static long nextId = System.currentTimeMillis();
41     private final static String JavaDoc KEY = "facelets.ui.DebugOutput";
42     public final static String JavaDoc DEFAULT_HOTKEY = "D";
43     private String JavaDoc hotkey = DEFAULT_HOTKEY;
44     
45     public UIDebug() {
46         super();
47         this.setTransient(true);
48         this.setRendered(true);
49         this.setRendererType(null);
50     }
51
52     public String JavaDoc getFamily() {
53         return COMPONENT_FAMILY;
54     }
55
56     public List JavaDoc getChildren() {
57         return new ArrayList JavaDoc() {
58             public boolean add(Object JavaDoc o) {
59                 throw new IllegalStateException JavaDoc("<ui:debug> does not support children");
60             }
61
62             public void add(int index, Object JavaDoc o) {
63                 throw new IllegalStateException JavaDoc("<ui:debug> does not support children");
64             }
65         };
66     }
67
68     public void encodeBegin(FacesContext faces) throws IOException JavaDoc {
69
70         String JavaDoc actionId = faces.getApplication().getViewHandler().getActionURL(faces, faces.getViewRoot().getViewId());
71         
72         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(512);
73         sb.append("<script language=\"javascript\" type=\"text/javascript\">\n");
74         sb.append("//<![CDATA[\n");
75         sb.append("function faceletsDebug(URL) { day = new Date(); id = day.getTime(); eval(\"page\" + id + \" = window.open(URL, '\" + id + \"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 240,top = 212');\"); };");
76         sb.append("var faceletsOrigKeyup = document.onkeyup; document.onkeyup = function(e) { if (window.event) e = window.event; if (String.fromCharCode(e.keyCode) == '" + this.getHotkey() + "' & e.shiftKey & e.ctrlKey) faceletsDebug('");
77         sb.append(actionId);
78         sb.append('?');
79         sb.append(KEY);
80         sb.append('=');
81         sb.append(writeDebugOutput(faces));
82         sb.append("'); else if (faceletsOrigKeyup) faceletsOrigKeyup(e); };\n");
83         sb.append("//]]>\n");
84         sb.append("</script>\n");
85         
86         ResponseWriter writer = faces.getResponseWriter();
87         writer.write(sb.toString());
88     }
89     
90     private static String JavaDoc writeDebugOutput(FacesContext faces) throws IOException JavaDoc {
91         FastWriter fw = new FastWriter();
92         DevTools.debugHtml(fw, faces);
93         
94         Map JavaDoc session = faces.getExternalContext().getSessionMap();
95         Map JavaDoc debugs = (Map JavaDoc) session.get(KEY);
96         if (debugs == null) {
97             debugs = new LinkedHashMap JavaDoc() {
98                 protected boolean removeEldestEntry(Entry eldest) {
99                     return (this.size() > 5);
100                 }
101             };
102             session.put(KEY, debugs);
103         }
104         String JavaDoc id = "" + nextId++;
105         debugs.put(id, fw.toString());
106         return id;
107     }
108     
109     private static String JavaDoc fetchDebugOutput(FacesContext faces, String JavaDoc id) {
110         Map JavaDoc session = faces.getExternalContext().getSessionMap();
111         Map JavaDoc debugs = (Map JavaDoc) session.get(KEY);
112         if (debugs != null) {
113             return (String JavaDoc) debugs.get(id);
114         }
115         return null;
116     }
117     
118     public static boolean debugRequest(FacesContext faces) {
119         String JavaDoc id = (String JavaDoc) faces.getExternalContext().getRequestParameterMap().get(KEY);
120         if (id != null) {
121             Object JavaDoc resp = faces.getExternalContext().getResponse();
122             if (!faces.getResponseComplete()
123                 && resp instanceof HttpServletResponse JavaDoc) {
124                 try {
125                     HttpServletResponse JavaDoc httpResp = (HttpServletResponse JavaDoc) resp;
126                     String JavaDoc page = fetchDebugOutput(faces, id);
127                     if (page != null) {
128                         httpResp.setContentType("text/html");
129                         httpResp.getWriter().write(page);
130                     } else {
131                         httpResp.setContentType("text/plain");
132                         httpResp.getWriter().write("No Debug Output Available");
133                     }
134                     httpResp.flushBuffer();
135                     faces.responseComplete();
136                 } catch (IOException JavaDoc e) {
137                     return false;
138                 }
139                 return true;
140             }
141         }
142         return false;
143     }
144     
145     public String JavaDoc getHotkey() {
146         return this.hotkey;
147     }
148     
149     public void setHotkey(String JavaDoc hotkey) {
150         this.hotkey = (hotkey != null) ? hotkey.toUpperCase() : "";
151     }
152
153 }
154
Popular Tags