KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JavaEventHandler.java 5462 2005-08-05 18:35:48Z 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.lang.reflect.Method JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35
36 /**
37  * JavaEventHandler - Static Method Java Event Handler
38  *
39  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
40  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  * @version $Rev: 5462 $
42  * @since 2.0
43  */

44 public class JavaEventHandler implements EventHandler {
45
46     public static final String JavaDoc module = JavaEventHandler.class.getName();
47
48     private Map JavaDoc eventClassMap = new HashMap JavaDoc();
49
50     /**
51      * @see org.ofbiz.webapp.event.EventHandler#init(javax.servlet.ServletContext)
52      */

53     public void init(ServletContext JavaDoc context) throws EventHandlerException {
54     }
55     
56     /**
57      * @see org.ofbiz.webapp.event.EventHandler#invoke(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
58      */

59     public String JavaDoc invoke(String JavaDoc eventPath, String JavaDoc eventMethod, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws EventHandlerException {
60         Class JavaDoc eventClass = (Class JavaDoc) this.eventClassMap.get(eventPath);
61
62         if (eventClass == null) {
63             synchronized (this) {
64                 eventClass = (Class JavaDoc) this.eventClassMap.get(eventPath);
65                 if (eventClass == null) {
66                     try {
67                         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
68                         eventClass = loader.loadClass(eventPath);
69                     } catch (ClassNotFoundException JavaDoc e) {
70                         Debug.logError(e, "Error loading class with name: " + eventPath + ", will not be able to run event...", module);
71                     }
72                     if (eventClass != null) {
73                         eventClassMap.put(eventPath, eventClass);
74                     }
75                 }
76             }
77         }
78         if (Debug.verboseOn()) Debug.logVerbose("[Set path/method]: " + eventPath + " / " + eventMethod, module);
79
80         Class JavaDoc[] paramTypes = new Class JavaDoc[] {HttpServletRequest JavaDoc.class, HttpServletResponse JavaDoc.class};
81
82         Debug.logVerbose("*[[Event invocation]]*", module);
83         Object JavaDoc[] params = new Object JavaDoc[] {request, response};
84
85         return invoke(eventPath, eventMethod, eventClass, paramTypes, params);
86     }
87
88     private String JavaDoc invoke(String JavaDoc eventPath, String JavaDoc eventMethod, Class JavaDoc eventClass, Class JavaDoc[] paramTypes, Object JavaDoc[] params) throws EventHandlerException {
89         if (eventClass == null) {
90             throw new EventHandlerException("Error invoking event, the class " + eventPath + " was not found");
91         }
92         if (eventPath == null || eventMethod == null) {
93             throw new EventHandlerException("Invalid event method or path; call initialize()");
94         }
95
96         Debug.logVerbose("[Processing]: JAVA Event", module);
97         try {
98             Method JavaDoc m = eventClass.getMethod(eventMethod, paramTypes);
99             String JavaDoc eventReturn = (String JavaDoc) m.invoke(null, params);
100
101             if (Debug.verboseOn()) Debug.logVerbose("[Event Return]: " + eventReturn, module);
102             return eventReturn;
103         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
104             Throwable JavaDoc t = e.getTargetException();
105
106             if (t != null) {
107                 Debug.logError(t, "Problems Processing Event", module);
108                 throw new EventHandlerException("Problems processing event: " + t.toString(), t);
109             } else {
110                 Debug.logError(e, "Problems Processing Event", module);
111                 throw new EventHandlerException("Problems processing event: " + e.toString(), e);
112             }
113         } catch (Exception JavaDoc e) {
114             Debug.logError(e, "Problems Processing Event", module);
115             throw new EventHandlerException("Problems processing event: " + e.toString(), e);
116         }
117     }
118 }
119
Popular Tags