1 25 package org.ofbiz.webapp.event; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import javax.servlet.ServletContext ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 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 48 public class EventFactory { 49 50 public static final String module = EventFactory.class.getName(); 51 52 protected RequestHandler requestHandler = null; 53 protected RequestManager requestManager = null; 54 protected ServletContext context = null; 55 protected Map handlers = null; 56 57 public EventFactory(RequestHandler requestHandler) { 58 handlers = new HashMap (); 59 this.requestHandler = requestHandler; 60 this.requestManager = requestHandler.getRequestManager(); 61 this.context = requestHandler.getServletContext(); 62 63 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 handlers = requestManager.getHandlerKeys(RequestManager.EVENT_HANDLER_KEY); 74 if (handlers != null) { 75 Iterator i = handlers.iterator(); 76 while (i.hasNext()) { 77 String type = (String ) i.next(); 78 this.handlers.put(type, this.loadEventHandler(type)); 79 } 80 } 81 } 82 83 public EventHandler getEventHandler(String type) throws EventHandlerException { 84 if (handlers.size() == 0) { 86 this.preLoadAll(); 87 } 88 89 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 type) throws EventHandlerException { 111 EventHandler handler = null; 112 String 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 e) { 121 throw new EventHandlerException("No class def found for handler [" + handlerClass + "]", e); 122 } catch (ClassNotFoundException cnf) { 123 throw new EventHandlerException("Cannot load handler class [" + handlerClass + "]", cnf); 124 } catch (InstantiationException ie) { 125 throw new EventHandlerException("Cannot get instance of the handler [" + handlerClass + "]", ie); 126 } catch (IllegalAccessException iae) { 127 throw new EventHandlerException(iae.getMessage(), iae); 128 } 129 return handler; 130 } 131 132 public static String runRequestEvent(HttpServletRequest request, HttpServletResponse response, String requestUri) 133 throws EventHandlerException { 134 ServletContext application = ((ServletContext ) request.getAttribute("servletContext")); 135 RequestHandler handler = (RequestHandler) application.getAttribute("_REQUEST_HANDLER_"); 136 RequestManager rm = handler.getRequestManager(); 137 String eventType = rm.getEventType(requestUri); 138 String eventPath = rm.getEventPath(requestUri); 139 String eventMethod = rm.getEventMethod(requestUri); 140 return handler.runEvent(request, response, eventType, eventPath, eventMethod); 141 } 142 } 143 | Popular Tags |