001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.handler;
020:
021: import org.apache.beehive.netui.util.internal.DiscoveryUtils;
022: import org.apache.beehive.netui.util.config.ConfigUtil;
023: import org.apache.beehive.netui.util.config.bean.NetUIConfig;
024: import org.apache.beehive.netui.util.config.bean.PageFlowHandlersConfig;
025: import org.apache.beehive.netui.util.config.bean.CustomPropertyConfig;
026: import org.apache.beehive.netui.util.logging.Logger;
027: import org.apache.beehive.netui.pageflow.internal.InternalConstants;
028: import org.apache.beehive.netui.pageflow.internal.DefaultLoginHandler;
029: import org.apache.beehive.netui.pageflow.internal.DefaultForwardRedirectHandler;
030: import org.apache.beehive.netui.pageflow.internal.DefaultReloadableClassHandler;
031: import org.apache.beehive.netui.pageflow.internal.DefaultExceptionsHandler;
032: import org.apache.beehive.netui.pageflow.internal.DefaultActionForwardHandler;
033: import org.apache.beehive.netui.pageflow.internal.DefaultHandler;
034: import org.apache.beehive.netui.pageflow.internal.DeferredSessionStorageHandler;
035: import org.apache.beehive.netui.pageflow.PageFlowActionServlet;
036: import org.apache.beehive.netui.pageflow.PageFlowContextListener;
037:
038: import javax.servlet.ServletContext;
039: import java.io.Serializable;
040:
041: /**
042: * ServletContext-scoped container for various Page Flow {@link Handler} instances.
043: */
044: public class Handlers implements Serializable {
045: private static final Logger _log = Logger
046: .getInstance(Handlers.class);
047:
048: private static final String CONTEXT_ATTR = InternalConstants.ATTR_PREFIX
049: + "_handlers";
050:
051: private ActionForwardHandler _actionForwardHandler = null;
052: private ExceptionsHandler _exceptionsHandler = null;
053: private ForwardRedirectHandler _forwardRedirectHandler = null;
054: private LoginHandler _loginHandler = null;
055: private StorageHandler _storageHandler = null;
056: private ReloadableClassHandler _reloadableClassHandler = null;
057: private transient ServletContext _servletContext;
058:
059: public static Handlers get(ServletContext servletContext) {
060: Handlers handlers = (Handlers) servletContext
061: .getAttribute(CONTEXT_ATTR);
062:
063: if (handlers == null) {
064: if (_log.isErrorEnabled()) {
065: _log
066: .error("Page Flow Handlers not initialized; either "
067: + PageFlowActionServlet.class.getName()
068: + " must be the Struts action servlet, or "
069: + PageFlowContextListener.class
070: .getName()
071: + " must be registered as a listener in web.xml.");
072: }
073:
074: //
075: // We can initialize it now, but it's not good because many requests could conceivably be in this
076: // code at the same time.
077: //
078: return init(servletContext);
079: }
080:
081: handlers.reinit(servletContext);
082: return handlers;
083: }
084:
085: public static Handlers init(ServletContext servletContext) {
086: Handlers handlers = new Handlers(servletContext);
087: servletContext.setAttribute(CONTEXT_ATTR, handlers);
088: return handlers;
089: }
090:
091: private Handlers(ServletContext servletContext) {
092: _servletContext = servletContext;
093:
094: //
095: // Load/create Handlers.
096: //
097: NetUIConfig netuiConfig = ConfigUtil.getConfig();
098: PageFlowHandlersConfig handlers = netuiConfig
099: .getPageFlowHandlers();
100:
101: DefaultHandler defaultActionForwardHandler = new DefaultActionForwardHandler(
102: servletContext);
103: DefaultHandler defaultExceptionsHandler = new DefaultExceptionsHandler(
104: servletContext);
105: DefaultHandler defaultForwardRedirectHandler = new DefaultForwardRedirectHandler(
106: servletContext);
107: DefaultHandler defaultLoginHandler = new DefaultLoginHandler(
108: servletContext);
109: DefaultHandler defaultStorageHandler = new DeferredSessionStorageHandler(
110: servletContext);
111: DefaultHandler defaultReloadableClassHandler = new DefaultReloadableClassHandler(
112: servletContext);
113:
114: _actionForwardHandler = (ActionForwardHandler) adaptHandler(
115: handlers != null ? handlers.getActionForwardHandlers()
116: : null, defaultActionForwardHandler,
117: ActionForwardHandler.class, servletContext);
118:
119: _exceptionsHandler = (ExceptionsHandler) adaptHandler(
120: handlers != null ? handlers.getExceptionsHandlers()
121: : null, defaultExceptionsHandler,
122: ExceptionsHandler.class, servletContext);
123:
124: _forwardRedirectHandler = (ForwardRedirectHandler) adaptHandler(
125: handlers != null ? handlers
126: .getForwardRedirectHandlers() : null,
127: defaultForwardRedirectHandler,
128: ForwardRedirectHandler.class, servletContext);
129:
130: _loginHandler = (LoginHandler) adaptHandler(
131: handlers != null ? handlers.getLoginHandlers() : null,
132: defaultLoginHandler, LoginHandler.class, servletContext);
133:
134: _storageHandler = (StorageHandler) adaptHandler(
135: handlers != null ? handlers.getStorageHandlers() : null,
136: defaultStorageHandler, StorageHandler.class,
137: servletContext);
138:
139: _reloadableClassHandler = (ReloadableClassHandler) adaptHandler(
140: handlers != null ? handlers
141: .getReloadableClassHandlers() : null,
142: defaultReloadableClassHandler,
143: ReloadableClassHandler.class, servletContext);
144: }
145:
146: public void reinit(ServletContext servletContext) {
147: if (_servletContext == null) {
148: _servletContext = servletContext;
149: _actionForwardHandler.reinit(servletContext);
150: _exceptionsHandler.reinit(servletContext);
151: _forwardRedirectHandler.reinit(servletContext);
152: _loginHandler.reinit(servletContext);
153: _storageHandler.reinit(servletContext);
154: _reloadableClassHandler.reinit(servletContext);
155: }
156: }
157:
158: public ActionForwardHandler getActionForwardHandler() {
159: return _actionForwardHandler;
160: }
161:
162: public ExceptionsHandler getExceptionsHandler() {
163: return _exceptionsHandler;
164: }
165:
166: public ForwardRedirectHandler getForwardRedirectHandler() {
167: return _forwardRedirectHandler;
168: }
169:
170: public LoginHandler getLoginHandler() {
171: return _loginHandler;
172: }
173:
174: public StorageHandler getStorageHandler() {
175: return _storageHandler;
176: }
177:
178: public ReloadableClassHandler getReloadableClassHandler() {
179: return _reloadableClassHandler;
180: }
181:
182: private static Handler adaptHandler(
183: org.apache.beehive.netui.util.config.bean.HandlerConfig[] handlerBeanConfigs,
184: DefaultHandler defaultHandler, Class baseClassOrInterface,
185: ServletContext servletContext) {
186: Handler retVal = defaultHandler;
187:
188: if (handlerBeanConfigs != null) {
189: for (int i = 0; i < handlerBeanConfigs.length; i++) {
190: String handlerClass = handlerBeanConfigs[i]
191: .getHandlerClass();
192: CustomPropertyConfig[] props = handlerBeanConfigs[i]
193: .getCustomProperties();
194: Handler handler = createHandler(handlerClass,
195: baseClassOrInterface, retVal, servletContext);
196:
197: if (handler != null) {
198: HandlerConfig config = new HandlerConfig(
199: handlerClass);
200:
201: if (props != null) {
202: for (int j = 0; j < props.length; j++) {
203: CustomPropertyConfig prop = props[j];
204: config.addCustomProperty(prop.getName(),
205: prop.getValue());
206: }
207: }
208: handler.init(config, retVal, servletContext);
209: retVal = handler;
210: }
211: }
212: }
213:
214: defaultHandler.setRegisteredHandler(retVal);
215: return retVal;
216: }
217:
218: /**
219: * Instantiates a handler, based on the class name in the given HandlerConfig.
220: *
221: * @param className the class name of the desired Handler.
222: * @param baseClassOrInterface the required base class or interface. May be <code>null</code>.
223: * @return an initialized Handler.
224: */
225: private static Handler createHandler(String className,
226: Class baseClassOrInterface, Handler previousHandler,
227: ServletContext servletContext) {
228: assert Handler.class.isAssignableFrom(baseClassOrInterface) : baseClassOrInterface
229: .getName()
230: + " cannot be assigned to " + Handler.class.getName();
231:
232: ClassLoader cl = DiscoveryUtils.getClassLoader();
233:
234: try {
235: Class handlerClass = cl.loadClass(className);
236:
237: if (!baseClassOrInterface.isAssignableFrom(handlerClass)) {
238: _log.error("Handler " + handlerClass.getName()
239: + " does not implement or extend "
240: + baseClassOrInterface.getName());
241: return null;
242: }
243:
244: Handler handler = (Handler) handlerClass.newInstance();
245: return handler;
246: } catch (ClassNotFoundException e) {
247: _log.error("Could not find Handler class " + className, e);
248: } catch (InstantiationException e) {
249: _log.error("Could not instantiate Handler class "
250: + className, e);
251: } catch (IllegalAccessException e) {
252: _log.error("Could not instantiate Handler class "
253: + className, e);
254: }
255:
256: return null;
257: }
258: }
|