001: /*
002: * $Id: JavaEventHandler.java,v 1.3 2003/09/14 05:36:47 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.event;
026:
027: import java.lang.reflect.Method;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.ofbiz.base.util.Debug;
035:
036: /**
037: * JavaEventHandler - Static Method Java Event Handler
038: *
039: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
040: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
041: * @version $Revision: 1.3 $
042: * @since 2.0
043: */
044: public class JavaEventHandler implements EventHandler {
045:
046: public static final String module = JavaEventHandler.class
047: .getName();
048:
049: private Map eventClassMap = new HashMap();
050:
051: /**
052: * @see org.ofbiz.content.webapp.event.EventHandler#invoke(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
053: */
054: public String invoke(String eventPath, String eventMethod,
055: HttpServletRequest request, HttpServletResponse response)
056: throws EventHandlerException {
057: Class eventClass = (Class) this .eventClassMap.get(eventPath);
058:
059: if (eventClass == null) {
060: synchronized (this ) {
061: eventClass = (Class) this .eventClassMap.get(eventPath);
062: if (eventClass == null) {
063: try {
064: ClassLoader loader = Thread.currentThread()
065: .getContextClassLoader();
066: eventClass = loader.loadClass(eventPath);
067: } catch (ClassNotFoundException e) {
068: Debug
069: .logError(
070: e,
071: "Error loading class with name: "
072: + eventPath
073: + ", will not be able to run event...",
074: module);
075: }
076: if (eventClass != null) {
077: eventClassMap.put(eventPath, eventClass);
078: }
079: }
080: }
081: }
082: if (Debug.verboseOn())
083: Debug.logVerbose("[Set path/method]: " + eventPath + " / "
084: + eventMethod, module);
085:
086: Class[] paramTypes = new Class[] { HttpServletRequest.class,
087: HttpServletResponse.class };
088:
089: Debug.logVerbose("*[[Event invocation]]*", module);
090: Object[] params = new Object[] { request, response };
091:
092: return invoke(eventPath, eventMethod, eventClass, paramTypes,
093: params);
094: }
095:
096: private String invoke(String eventPath, String eventMethod,
097: Class eventClass, Class[] paramTypes, Object[] params)
098: throws EventHandlerException {
099: if (eventClass == null) {
100: throw new EventHandlerException(
101: "Error invoking event, the class " + eventPath
102: + " was not found");
103: }
104: if (eventPath == null || eventMethod == null) {
105: throw new EventHandlerException(
106: "Invalid event method or path; call initialize()");
107: }
108:
109: Debug.logVerbose("[Processing]: JAVA Event", module);
110: try {
111: Method m = eventClass.getMethod(eventMethod, paramTypes);
112: String eventReturn = (String) m.invoke(null, params);
113:
114: if (Debug.verboseOn())
115: Debug.logVerbose("[Event Return]: " + eventReturn,
116: module);
117: return eventReturn;
118: } catch (java.lang.reflect.InvocationTargetException e) {
119: Throwable t = e.getTargetException();
120:
121: if (t != null) {
122: Debug.logError(t, "Problems Processing Event", module);
123: throw new EventHandlerException(
124: "Problems processing event: " + t.toString(), t);
125: } else {
126: Debug.logError(e, "Problems Processing Event", module);
127: throw new EventHandlerException(
128: "Problems processing event: " + e.toString(), e);
129: }
130: } catch (Exception e) {
131: Debug.logError(e, "Problems Processing Event", module);
132: throw new EventHandlerException(
133: "Problems processing event: " + e.toString(), e);
134: }
135: }
136: }
|