package org.jboss.seam.debug.jsf; import java.io.IOException; import java.net.URL; import javax.faces.context.FacesContext; import org.jboss.seam.core.Init; import com.sun.facelets.impl.ResourceResolver; import com.sun.facelets.impl.DefaultResourceResolver; /** * Intercepts any request for a path like /debug.xxx and renders * the Seam debug page using facelets. * * @author Mark Collette */ public class SeamDebugResourceResolver implements ResourceResolver { private final static String PARAM_RESOURCE_RESOLVER = "seam.RESOURCE_RESOLVER"; private ResourceResolver delegate; public SeamDebugResourceResolver() { delegate = buildDelegateResourceResolver(); } public URL resolveUrl(String path) { System.out.println("SeamDebugResourceResolver.resolveUrl() path: " + path); if ( path!=null && path.startsWith("/debug.") && Init.instance().isDebug() ) { URL url = SeamDebugResourceResolver.class.getClassLoader().getResource("META-INF/debug.xhtml"); System.out.println("SeamDebugResourceResolver.resolveUrl() url: " + url); return url; } return delegate.resolveUrl(path); } private ResourceResolver buildDelegateResourceResolver() { FacesContext ctx = FacesContext.getCurrentInstance(); ResourceResolver resourceResolver = null; String paramResourceResolver = ctx.getExternalContext().getInitParameter(PARAM_RESOURCE_RESOLVER ); if( paramResourceResolver != null && paramResourceResolver.length() > 0 ) { try { Class resourceResolverClass = Class.forName( paramResourceResolver, true, Thread.currentThread().getContextClassLoader()); resourceResolver = (ResourceResolver) resourceResolverClass.newInstance(); } catch(Exception e) { throw new RuntimeException("Problem initializing Seam delegate ResourceResolver: " + paramResourceResolver, e); } } if( resourceResolver == null ) resourceResolver = new DefaultResourceResolver(); return resourceResolver; } }