001: package com.icesoft.faces.facelets;
002:
003: import java.io.IOException;
004: import java.net.URL;
005: import java.lang.reflect.Method;
006:
007: import javax.faces.context.FacesContext;
008:
009: import com.sun.facelets.impl.ResourceResolver;
010: import com.sun.facelets.impl.DefaultResourceResolver;
011: import com.icesoft.util.SeamUtilities;
012:
013: /**
014: * Intercepts any request for a path like /debug.xxx and renders
015: * the Seam debug page using facelets.
016: *
017: * @author Mark Collette
018: */
019: public class SeamDebugResourceResolver implements ResourceResolver {
020: private static final String Init_className = "org.jboss.seam.core.Init";
021: private static final String SeamDebugPhaseListener_className = "org.jboss.seam.debug.jsf.SeamDebugPhaseListener";
022: private static Class Init_class;
023: private static Method Init_instance_method;
024: private static Method Init_isDebug_method;
025: private static Class SeamDebugPhaseListener_class;
026: private static boolean loaded = false;
027:
028: public static ResourceResolver build(ResourceResolver delegate) {
029: if (delegate == null) {
030: throw new IllegalArgumentException(
031: "SeamDebugResourceResolver must have valid delegate ResourceResolver");
032: }
033: if (!loadSeamDebugClasses())
034: return null;
035: return new SeamDebugResourceResolver(delegate);
036: }
037:
038: private static boolean loadSeamDebugClasses() {
039: if (!loaded) {
040: try {
041: ClassLoader dbgClassLoader = SeamUtilities
042: .getSeamDebugPhaseListenerClassLoader();
043: if (dbgClassLoader == null) {
044: dbgClassLoader = Thread.currentThread()
045: .getContextClassLoader();
046: }
047: Init_class = Class.forName(Init_className, true,
048: dbgClassLoader);
049: Init_instance_method = Init_class.getMethod("instance",
050: new Class[0]);
051: Init_isDebug_method = Init_class.getMethod("isDebug",
052: new Class[0]);
053: SeamDebugPhaseListener_class = Class.forName(
054: SeamDebugPhaseListener_className, true,
055: dbgClassLoader);
056: loaded = true;
057: } catch (Exception e) {
058: //e.printStackTrace();
059: // Silently fail, since it's valid to not be in Seam,
060: // or not have the Seam debug JAR available
061: Init_class = null;
062: Init_instance_method = null;
063: Init_isDebug_method = null;
064: SeamDebugPhaseListener_class = null;
065: loaded = false;
066: }
067: }
068: return loaded;
069: }
070:
071: private ResourceResolver delegate;
072:
073: private SeamDebugResourceResolver(ResourceResolver delegate) {
074: this .delegate = delegate;
075: }
076:
077: public URL resolveUrl(String path) {
078: //System.out.println("SeamDebugResourceResolver.resolveUrl() path: " + path);
079: if (path != null && path.startsWith("/debug.")
080: && Init_instance_isDebug()) {
081: URL url = SeamDebugPhaseListener_class.getClassLoader()
082: .getResource("META-INF/debug.xhtml");
083: //System.out.println("SeamDebugResourceResolver.resolveUrl() url: " + url);
084: return url;
085: }
086: return delegate.resolveUrl(path);
087: }
088:
089: private static boolean Init_instance_isDebug() {
090: try {
091: Object instance = Init_instance_method.invoke(null,
092: new Object[0]);
093: Object isDebug = Init_isDebug_method.invoke(instance,
094: new Object[0]);
095: return ((Boolean) isDebug).booleanValue();
096: } catch (Exception e) {
097: //e.printStackTrace();
098: return false;
099: }
100: }
101: }
|