KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > event > EventFactory


1 /*
2  * $Id: EventFactory.java 6236 2005-12-02 10:48:53Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.event;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.GeneralRuntimeException;
37 import org.ofbiz.base.util.ObjectType;
38 import org.ofbiz.webapp.control.RequestHandler;
39 import org.ofbiz.webapp.control.RequestManager;
40
41 /**
42  * EventFactory - Event Handler Factory
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 6236 $
46  * @since 2.0
47  */

48 public class EventFactory {
49
50     public static final String JavaDoc module = EventFactory.class.getName();
51
52     protected RequestHandler requestHandler = null;
53     protected RequestManager requestManager = null;
54     protected ServletContext JavaDoc context = null;
55     protected Map JavaDoc handlers = null;
56
57     public EventFactory(RequestHandler requestHandler) {
58         handlers = new HashMap JavaDoc();
59         this.requestHandler = requestHandler;
60         this.requestManager = requestHandler.getRequestManager();
61         this.context = requestHandler.getServletContext();
62
63         // pre-load all event handlers
64
try {
65             this.preLoadAll();
66         } catch (EventHandlerException e) {
67             Debug.logError(e, module);
68             throw new GeneralRuntimeException(e);
69         }
70     }
71
72     private void preLoadAll() throws EventHandlerException {
73         List JavaDoc handlers = requestManager.getHandlerKeys(RequestManager.EVENT_HANDLER_KEY);
74         if (handlers != null) {
75             Iterator JavaDoc i = handlers.iterator();
76             while (i.hasNext()) {
77                 String JavaDoc type = (String JavaDoc) i.next();
78                 this.handlers.put(type, this.loadEventHandler(type));
79             }
80         }
81     }
82
83     public EventHandler getEventHandler(String JavaDoc type) throws EventHandlerException {
84         // check if we are new / empty and add the default handler in
85
if (handlers.size() == 0) {
86             this.preLoadAll();
87         }
88
89         // attempt to get a pre-loaded handler
90
EventHandler handler = (EventHandler) handlers.get(type);
91
92         if (handler == null) {
93             synchronized (EventHandler.class) {
94                 handler = (EventHandler) handlers.get(type);
95                 if (handler == null) {
96                     handler = this.loadEventHandler(type);
97                     handlers.put(type, handler);
98                 }
99             }
100             if (handler == null)
101                 throw new EventHandlerException("No handler found for type: " + type);
102         }
103         return handler;
104     }
105
106     public void clear() {
107         handlers.clear();
108     }
109
110     private EventHandler loadEventHandler(String JavaDoc type) throws EventHandlerException {
111         EventHandler handler = null;
112         String JavaDoc handlerClass = requestManager.getHandlerClass(type, RequestManager.EVENT_HANDLER_KEY);
113         if (handlerClass == null) {
114             throw new EventHandlerException("Unknown handler type: " + type);
115         }
116
117         try {
118             handler = (EventHandler) ObjectType.getInstance(handlerClass);
119             handler.init(context);
120         } catch (NoClassDefFoundError JavaDoc e) {
121             throw new EventHandlerException("No class def found for handler [" + handlerClass + "]", e);
122         } catch (ClassNotFoundException JavaDoc cnf) {
123             throw new EventHandlerException("Cannot load handler class [" + handlerClass + "]", cnf);
124         } catch (InstantiationException JavaDoc ie) {
125             throw new EventHandlerException("Cannot get instance of the handler [" + handlerClass + "]", ie);
126         } catch (IllegalAccessException JavaDoc iae) {
127             throw new EventHandlerException(iae.getMessage(), iae);
128         }
129         return handler;
130     }
131
132     public static String JavaDoc runRequestEvent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc requestUri)
133             throws EventHandlerException {
134         ServletContext JavaDoc application = ((ServletContext JavaDoc) request.getAttribute("servletContext"));
135         RequestHandler handler = (RequestHandler) application.getAttribute("_REQUEST_HANDLER_");
136         RequestManager rm = handler.getRequestManager();
137         String JavaDoc eventType = rm.getEventType(requestUri);
138         String JavaDoc eventPath = rm.getEventPath(requestUri);
139         String JavaDoc eventMethod = rm.getEventMethod(requestUri);
140         return handler.runEvent(request, response, eventType, eventPath, eventMethod);
141     }
142 }
143
Popular Tags