1 33 34 package com.icesoft.faces.context; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 import javax.faces.FacesException; 40 import javax.faces.context.FacesContext; 41 import javax.faces.context.FacesContextFactory; 42 import javax.faces.lifecycle.Lifecycle; 43 import javax.servlet.ServletContext ; 44 import javax.servlet.ServletRequest ; 45 import java.lang.reflect.Method ; 46 47 58 public class FacesContextFactoryImpl extends FacesContextFactory { 59 60 protected static Log log = LogFactory.getLog(FacesContextFactoryImpl.class); 61 62 public static final String SERVLET_KEY = "servletkey"; 63 public static final String PERSISTENT = "persistent"; 64 65 private FacesContextFactory delegate; 66 67 public FacesContextFactoryImpl() { 68 69 } 70 71 public FacesContextFactoryImpl(FacesContextFactory delegate) { 72 this.delegate = delegate; 73 } 74 75 public FacesContext getFacesContext(Object context, Object request, 76 Object response, 77 Lifecycle lifecycle) 78 throws FacesException { 79 80 if (context == null || request == null || 82 response == null || lifecycle == null) { 83 throw new NullPointerException (); 84 } 85 86 if (shouldDelegate(request)) { 89 return delegate 90 .getFacesContext(context, request, response, lifecycle); 91 } 92 93 if (context instanceof ServletContext ) { 98 return null; 99 } else { 100 throw new IllegalStateException ("Unknown environment"); 101 } 102 } 103 104 private boolean shouldDelegate(Object request) { 105 if (delegate == null) { 106 return false; 107 } 108 Method getAttributeMethod = null; 112 Class portletRequestClass = null; 113 try { 114 portletRequestClass = 115 Class.forName("javax.portlet.PortletRequest"); 116 getAttributeMethod = portletRequestClass 117 .getMethod("getAttribute", new Class []{String .class}); 118 } catch (Throwable t) { 119 if (log.isDebugEnabled()) { 120 log.debug("Portlet classes not available"); 121 } 122 } 123 124 if ((null != portletRequestClass) && 125 (portletRequestClass.isInstance(request))) { 126 String portletType = null; 128 try { 129 portletType = (String ) getAttributeMethod 130 .invoke(request, new Object []{SERVLET_KEY}); 131 } catch (Exception e) { 132 if (log.isWarnEnabled()) { 133 log.warn("Reflection failure: request.getAttribute", e); 134 } 135 } 136 137 if (portletType != null && 138 portletType.equalsIgnoreCase(PERSISTENT)) { 139 return false; 140 } 141 } else if (request instanceof ServletRequest ) { 142 String servletType = (String ) ((ServletRequest ) request) 143 .getAttribute(SERVLET_KEY); 144 if (servletType != null && 145 servletType.equalsIgnoreCase(PERSISTENT)) { 146 return false; 147 } 148 } 149 150 return true; 151 } 152 153 } 154 | Popular Tags |