1 25 package org.ofbiz.webapp.view; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.List ; 30 import java.util.Iterator ; 31 import javax.servlet.ServletContext ; 32 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.base.util.ObjectType; 35 import org.ofbiz.base.util.GeneralRuntimeException; 36 import org.ofbiz.webapp.control.RequestHandler; 37 import org.ofbiz.webapp.control.RequestManager; 38 39 46 public class ViewFactory { 47 48 public static final String module = ViewFactory.class.getName(); 49 50 protected RequestHandler requestHandler = null; 51 protected RequestManager requestManager = null; 52 protected ServletContext context = null; 53 protected Map handlers = null; 54 55 public ViewFactory(RequestHandler requestHandler) { 56 this.handlers = new HashMap (); 57 this.requestHandler = requestHandler; 58 this.requestManager = requestHandler.getRequestManager(); 59 this.context = requestHandler.getServletContext(); 60 61 try { 63 this.preLoadAll(); 64 } catch (ViewHandlerException e) { 65 Debug.logError(e, module); 66 throw new GeneralRuntimeException(e); 67 } 68 } 69 70 private void preLoadAll() throws ViewHandlerException { 71 List handlers = requestManager.getHandlerKeys(RequestManager.VIEW_HANDLER_KEY); 72 if (handlers != null) { 73 Iterator i = handlers.iterator(); 74 while (i.hasNext()) { 75 String type = (String ) i.next(); 76 this.handlers.put(type, this.loadViewHandler(type)); 77 } 78 } 79 80 if (!this.handlers.containsKey("default")) { 82 try { 83 ViewHandler h = (ViewHandler) ObjectType.getInstance("org.ofbiz.webapp.view.JspViewHandler"); 84 h.init(context); 85 this. handlers.put("default", h); 86 } catch (Exception e) { 87 throw new ViewHandlerException(e); 88 } 89 } 90 } 91 92 public ViewHandler getViewHandler(String type) throws ViewHandlerException { 93 if (type == null || type.length() == 0) { 94 type = "default"; 95 } 96 97 if (handlers.size() == 0) { 99 this.preLoadAll(); 100 } 101 102 ViewHandler handler = (ViewHandler) handlers.get(type); 104 105 if (handler == null) { 107 synchronized (ViewFactory.class) { 108 handler = (ViewHandler) handlers.get(type); 109 if (handler == null) { 110 handler = this.loadViewHandler(type); 111 handlers.put(type, handler); 112 } 113 } 114 if (handler == null) { 115 throw new ViewHandlerException("No handler found for type: " + type); 116 } 117 } 118 return handler; 119 } 120 121 public void clear() { 122 handlers.clear(); 123 } 124 125 private ViewHandler loadViewHandler(String type) throws ViewHandlerException { 126 ViewHandler handler = null; 127 String handlerClass = requestManager.getHandlerClass(type, RequestManager.VIEW_HANDLER_KEY); 128 if (handlerClass == null) 129 throw new ViewHandlerException("Unknown handler type: " + type); 130 131 try { 132 handler = (ViewHandler) ObjectType.getInstance(handlerClass); 133 handler.init(context); 134 } catch (ClassNotFoundException cnf) { 135 Debug.logWarning("Warning: could not load view handler class because it was not found; note that some views may not work: " + cnf.toString(), module); 137 } catch (InstantiationException ie) { 138 throw new ViewHandlerException("Cannot get instance of the handler", ie); 139 } catch (IllegalAccessException iae) { 140 throw new ViewHandlerException(iae.getMessage(), iae); 141 } 142 143 return handler; 144 } 145 } 146 | Popular Tags |