001: /*
002: * $Id: ViewFactory.java,v 1.3 2003/09/14 05:36:48 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.view;
026:
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import javax.servlet.ServletContext;
031:
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.base.util.ObjectType;
034: import org.ofbiz.content.webapp.control.RequestHandler;
035: import org.ofbiz.content.webapp.control.RequestManager;
036:
037: /**
038: * ViewFactory - View Handler Factory
039: *
040: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
041: * @version $Revision: 1.3 $
042: * @since 2.0
043: */
044: public class ViewFactory {
045:
046: public static final String module = ViewFactory.class.getName();
047:
048: protected RequestHandler requestHandler = null;
049: protected RequestManager requestManager = null;
050: protected ServletContext context = null;
051: protected Map handlers = null;
052:
053: public ViewFactory(RequestHandler requestHandler) {
054: handlers = new HashMap();
055: this .requestHandler = requestHandler;
056: this .requestManager = requestHandler.getRequestManager();
057: this .context = requestHandler.getServletContext();
058: }
059:
060: public ViewHandler getViewHandler(String type)
061: throws ViewHandlerException {
062: if (type == null || type.length() == 0) {
063: type = "default";
064: }
065:
066: // check if we are new / empty and add the default handler in
067: if (handlers.size() == 0) {
068: try {
069: ViewHandler h = (ViewHandler) ObjectType
070: .getInstance("org.ofbiz.content.webapp.view.JspViewHandler");
071: h.init(context);
072: handlers.put("default", h);
073: } catch (Exception e) {
074: Debug
075: .logError(
076: e,
077: "[viewFactory.getDefault]: Cannot load default handler.",
078: module);
079: }
080: }
081:
082: // get the view handler by type from the contextHandlers
083: ViewHandler handler = (ViewHandler) handlers.get(type);
084:
085: // if none found lets create it and add it in
086: if (handler == null) {
087: synchronized (ViewFactory.class) {
088: handler = (ViewHandler) handlers.get(type);
089: if (handler == null) {
090: String handlerClass = requestManager
091: .getHandlerClass(type,
092: RequestManager.VIEW_HANDLER_KEY);
093: if (handlerClass == null)
094: throw new ViewHandlerException(
095: "Unknown handler type: " + type);
096:
097: try {
098: handler = (ViewHandler) ObjectType
099: .getInstance(handlerClass);
100: handler.init(context);
101: handlers.put(type, handler);
102: } catch (ClassNotFoundException cnf) {
103: throw new ViewHandlerException(
104: "Cannot load handler class", cnf);
105: } catch (InstantiationException ie) {
106: throw new ViewHandlerException(
107: "Cannot get instance of the handler",
108: ie);
109: } catch (IllegalAccessException iae) {
110: throw new ViewHandlerException(
111: iae.getMessage(), iae);
112: }
113: }
114: }
115: if (handler == null) {
116: throw new ViewHandlerException(
117: "No handler found for type: " + type);
118: }
119: }
120: return handler;
121: }
122: }
|