1 25 package org.ofbiz.webapp.event; 26 27 import java.lang.reflect.Method ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.ServletContext ; 33 34 import org.ofbiz.base.util.Debug; 35 36 44 public class JavaEventHandler implements EventHandler { 45 46 public static final String module = JavaEventHandler.class.getName(); 47 48 private Map eventClassMap = new HashMap (); 49 50 53 public void init(ServletContext context) throws EventHandlerException { 54 } 55 56 59 public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { 60 Class eventClass = (Class ) this.eventClassMap.get(eventPath); 61 62 if (eventClass == null) { 63 synchronized (this) { 64 eventClass = (Class ) this.eventClassMap.get(eventPath); 65 if (eventClass == null) { 66 try { 67 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 68 eventClass = loader.loadClass(eventPath); 69 } catch (ClassNotFoundException 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 [] paramTypes = new Class [] {HttpServletRequest .class, HttpServletResponse .class}; 81 82 Debug.logVerbose("*[[Event invocation]]*", module); 83 Object [] params = new Object [] {request, response}; 84 85 return invoke(eventPath, eventMethod, eventClass, paramTypes, params); 86 } 87 88 private String invoke(String eventPath, String eventMethod, Class eventClass, Class [] paramTypes, Object [] 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 m = eventClass.getMethod(eventMethod, paramTypes); 99 String eventReturn = (String ) m.invoke(null, params); 100 101 if (Debug.verboseOn()) Debug.logVerbose("[Event Return]: " + eventReturn, module); 102 return eventReturn; 103 } catch (java.lang.reflect.InvocationTargetException e) { 104 Throwable 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 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 |