1 18 package org.apache.beehive.netui.pageflow.handler; 19 20 import org.apache.beehive.netui.util.internal.DiscoveryUtils; 21 import org.apache.beehive.netui.util.config.bean.CustomProperty; 22 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument; 23 import org.apache.beehive.netui.util.config.bean.PageflowHandlers; 24 import org.apache.beehive.netui.util.config.ConfigUtil; 25 import org.apache.beehive.netui.util.logging.Logger; 26 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 27 import org.apache.beehive.netui.pageflow.internal.DefaultLoginHandler; 28 import org.apache.beehive.netui.pageflow.internal.DefaultForwardRedirectHandler; 29 import org.apache.beehive.netui.pageflow.internal.DefaultReloadableClassHandler; 30 import org.apache.beehive.netui.pageflow.internal.DefaultExceptionsHandler; 31 import org.apache.beehive.netui.pageflow.internal.DefaultActionForwardHandler; 32 import org.apache.beehive.netui.pageflow.internal.DefaultHandler; 33 import org.apache.beehive.netui.pageflow.PageFlowActionServlet; 34 import org.apache.beehive.netui.pageflow.PageFlowContextListener; 35 36 import javax.servlet.ServletContext ; 37 import java.io.Serializable ; 38 39 42 public class Handlers 43 implements Serializable 44 { 45 private static final Logger _log = Logger.getInstance( Handlers.class ); 46 47 private static final String CONTEXT_ATTR = InternalConstants.ATTR_PREFIX + "_handlers"; 48 49 private ActionForwardHandler _actionForwardHandler = null; 50 private ExceptionsHandler _exceptionsHandler = null; 51 private ForwardRedirectHandler _forwardRedirectHandler = null; 52 private LoginHandler _loginHandler = null; 53 private ReloadableClassHandler _reloadableClassHandler = null; 54 private transient ServletContext _servletContext; 55 56 public static Handlers get( ServletContext servletContext ) 57 { 58 Handlers handlers = ( Handlers ) servletContext.getAttribute( CONTEXT_ATTR ); 59 60 if ( handlers == null ) 61 { 62 if ( _log.isErrorEnabled() ) 63 { 64 _log.error( "Page Flow Handlers not initialized; either " 65 + PageFlowActionServlet.class.getName() + " must be the Struts action servlet, or " 66 + PageFlowContextListener.class.getName() + " must be registered as a listener in web.xml." ); 67 } 68 69 return init( servletContext ); 74 } 75 76 handlers.reinit( servletContext ); 77 return handlers; 78 } 79 80 public static Handlers init( ServletContext servletContext ) 81 { 82 assert servletContext.getAttribute( CONTEXT_ATTR ) == null : Handlers.class.getName() + " already initialized."; 83 Handlers handlers = new Handlers( servletContext ); 84 servletContext.setAttribute( CONTEXT_ATTR, handlers ); 85 return handlers; 86 } 87 88 private Handlers( ServletContext servletContext ) 89 { 90 _servletContext = servletContext; 91 92 NetuiConfigDocument.NetuiConfig netuiConfig = ConfigUtil.getConfig(); 96 PageflowHandlers handlers = netuiConfig.getPageflowHandlers(); 97 98 DefaultHandler defaultActionForwardHandler = new DefaultActionForwardHandler( servletContext ); 99 DefaultHandler defaultExceptionsHandler = new DefaultExceptionsHandler( servletContext ); 100 DefaultHandler defaultForwardRedirectHandler = new DefaultForwardRedirectHandler( servletContext ); 101 DefaultHandler defaultLoginHandler = new DefaultLoginHandler( servletContext ); 102 DefaultHandler defaultReloadableClassHandler = new DefaultReloadableClassHandler( servletContext ); 103 104 _actionForwardHandler = ( ActionForwardHandler ) 105 adaptHandler( handlers != null ? handlers.getActionForwardHandlerArray() : null, 106 defaultActionForwardHandler, ActionForwardHandler.class, servletContext ); 107 108 _exceptionsHandler = ( ExceptionsHandler ) 109 adaptHandler( handlers != null ? handlers.getExceptionsHandlerArray() : null, defaultExceptionsHandler, 110 ExceptionsHandler.class, servletContext ); 111 112 _forwardRedirectHandler = ( ForwardRedirectHandler ) 113 adaptHandler( handlers != null ? handlers.getForwardRedirectHandlerArray() : null, 114 defaultForwardRedirectHandler, ForwardRedirectHandler.class, servletContext ); 115 116 _loginHandler = ( LoginHandler ) 117 adaptHandler( handlers != null ? handlers.getLoginHandlerArray() : null, defaultLoginHandler, 118 LoginHandler.class, servletContext ); 119 120 _reloadableClassHandler = ( ReloadableClassHandler ) 121 adaptHandler( handlers != null ? handlers.getReloadableClassHandlerArray() : null, 122 defaultReloadableClassHandler, ReloadableClassHandler.class, servletContext ); 123 } 124 125 public void reinit( ServletContext servletContext ) 126 { 127 if ( _servletContext == null ) 128 { 129 _servletContext = servletContext; 130 _actionForwardHandler.reinit( servletContext ); 131 _exceptionsHandler.reinit( servletContext ); 132 _forwardRedirectHandler.reinit( servletContext ); 133 _loginHandler.reinit( servletContext ); 134 _reloadableClassHandler.reinit( servletContext ); 135 } 136 } 137 138 public ActionForwardHandler getActionForwardHandler() 139 { 140 return _actionForwardHandler; 141 } 142 143 public ExceptionsHandler getExceptionsHandler() 144 { 145 return _exceptionsHandler; 146 } 147 148 public ForwardRedirectHandler getForwardRedirectHandler() 149 { 150 return _forwardRedirectHandler; 151 } 152 153 public LoginHandler getLoginHandler() 154 { 155 return _loginHandler; 156 } 157 158 public ReloadableClassHandler getReloadableClassHandler() 159 { 160 return _reloadableClassHandler; 161 } 162 163 private static Handler adaptHandler( org.apache.beehive.netui.util.config.bean.Handler[] handlerBeanConfigs, 164 DefaultHandler defaultHandler, Class baseClassOrInterface, 165 ServletContext servletContext ) 166 { 167 Handler retVal = defaultHandler; 168 169 if ( handlerBeanConfigs != null ) 170 { 171 for ( int i = 0; i < handlerBeanConfigs.length; i++ ) 172 { 173 String handlerClass = handlerBeanConfigs[i].getHandlerClass(); 174 CustomProperty[] props = handlerBeanConfigs[i].getCustomPropertyArray(); 175 Handler handler = createHandler( handlerClass, baseClassOrInterface, retVal, servletContext ); 176 177 if ( handler != null ) 178 { 179 HandlerConfig config = new HandlerConfig( handlerClass ); 180 181 for ( int j = 0; j < props.length; j++ ) 182 { 183 CustomProperty prop = props[j]; 184 config.addCustomProperty( prop.getName(), prop.getValue() ); 185 } 186 187 handler.init( config, retVal, servletContext ); 188 retVal = handler; 189 } 190 } 191 } 192 193 defaultHandler.setRegisteredHandler( retVal ); 194 return retVal; 195 } 196 197 204 private static Handler createHandler( String className, Class baseClassOrInterface, Handler previousHandler, 205 ServletContext servletContext ) 206 { 207 assert Handler.class.isAssignableFrom( baseClassOrInterface ) 208 : baseClassOrInterface.getName() + " cannot be assigned to " + Handler.class.getName(); 209 210 ClassLoader cl = DiscoveryUtils.getClassLoader(); 211 212 try 213 { 214 Class handlerClass = cl.loadClass( className ); 215 216 if ( ! baseClassOrInterface.isAssignableFrom( handlerClass ) ) 217 { 218 _log.error( "Handler " + handlerClass.getName() + " does not implement or extend " 219 + baseClassOrInterface.getName() ); 220 return null; 221 } 222 223 Handler handler = ( Handler ) handlerClass.newInstance(); 224 return handler; 225 } 226 catch ( ClassNotFoundException e ) 227 { 228 _log.error( "Could not find Handler class " + className, e ); 229 } 230 catch ( InstantiationException e ) 231 { 232 _log.error( "Could not instantiate Handler class " + className, e ); 233 } 234 catch ( IllegalAccessException e ) 235 { 236 _log.error( "Could not instantiate Handler class " + className, e ); 237 } 238 239 return null; 240 } 241 } 242 | Popular Tags |