KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > handler > Handlers


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
37 import java.io.Serializable JavaDoc;
38
39 /**
40  * ServletContext-scoped container for various Page Flow {@link Handler} instances.
41  */

42 public class Handlers
43         implements Serializable JavaDoc
44 {
45     private static final Logger _log = Logger.getInstance( Handlers.class );
46     
47     private static final String JavaDoc 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 JavaDoc _servletContext;
55     
56     public static Handlers get( ServletContext JavaDoc 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             //
70
// We can initialize it now, but it's not good because many requests could conceivably be in this
71
// code at the same time.
72
//
73
return init( servletContext );
74         }
75         
76         handlers.reinit( servletContext );
77         return handlers;
78     }
79     
80     public static Handlers init( ServletContext JavaDoc 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 JavaDoc servletContext )
89     {
90         _servletContext = servletContext;
91         
92         //
93
// Load/create Handlers.
94
//
95
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 JavaDoc 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 JavaDoc baseClassOrInterface,
165                                          ServletContext JavaDoc servletContext )
166     {
167         Handler retVal = defaultHandler;
168         
169         if ( handlerBeanConfigs != null )
170         {
171             for ( int i = 0; i < handlerBeanConfigs.length; i++ )
172             {
173                 String JavaDoc 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     /**
198      * Instantiates a handler, based on the class name in the given HandlerConfig.
199      *
200      * @param className the class name of the desired Handler.
201      * @param baseClassOrInterface the required base class or interface. May be <code>null</code>.
202      * @return an initialized Handler.
203      */

204     private static Handler createHandler( String JavaDoc className, Class JavaDoc baseClassOrInterface, Handler previousHandler,
205                                          ServletContext JavaDoc servletContext )
206     {
207         assert Handler.class.isAssignableFrom( baseClassOrInterface )
208                 : baseClassOrInterface.getName() + " cannot be assigned to " + Handler.class.getName();
209         
210         ClassLoader JavaDoc cl = DiscoveryUtils.getClassLoader();
211         
212         try
213         {
214             Class JavaDoc 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 JavaDoc e )
227         {
228             _log.error( "Could not find Handler class " + className, e );
229         }
230         catch ( InstantiationException JavaDoc e )
231         {
232             _log.error( "Could not instantiate Handler class " + className, e );
233         }
234         catch ( IllegalAccessException JavaDoc e )
235         {
236             _log.error( "Could not instantiate Handler class " + className, e );
237         }
238         
239         return null;
240     }
241 }
242
Popular Tags