KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > ViewFactory


1 /*
2  * $Id: ViewFactory.java 6610 2006-01-29 10:00:13Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.view;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
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 /**
40  * ViewFactory - View Handler Factory
41  *
42  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  * @version $Rev: 6610 $
44  * @since 2.0
45  */

46 public class ViewFactory {
47     
48     public static final String JavaDoc module = ViewFactory.class.getName();
49
50     protected RequestHandler requestHandler = null;
51     protected RequestManager requestManager = null;
52     protected ServletContext JavaDoc context = null;
53     protected Map JavaDoc handlers = null;
54     
55     public ViewFactory(RequestHandler requestHandler) {
56         this.handlers = new HashMap JavaDoc();
57         this.requestHandler = requestHandler;
58         this.requestManager = requestHandler.getRequestManager();
59         this.context = requestHandler.getServletContext();
60
61         // pre-load all the view handlers
62
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 JavaDoc handlers = requestManager.getHandlerKeys(RequestManager.VIEW_HANDLER_KEY);
72         if (handlers != null) {
73             Iterator JavaDoc i = handlers.iterator();
74             while (i.hasNext()) {
75                 String JavaDoc type = (String JavaDoc) i.next();
76                 this.handlers.put(type, this.loadViewHandler(type));
77             }
78         }
79
80         // load the "default" type
81
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 JavaDoc e) {
87                 throw new ViewHandlerException(e);
88             }
89         }
90     }
91
92     public ViewHandler getViewHandler(String JavaDoc type) throws ViewHandlerException {
93         if (type == null || type.length() == 0) {
94             type = "default";
95         }
96                             
97         // check if we are new / empty and add the default handler in
98
if (handlers.size() == 0) {
99             this.preLoadAll();
100         }
101         
102         // get the view handler by type from the contextHandlers
103
ViewHandler handler = (ViewHandler) handlers.get(type);
104
105         // if none found lets create it and add it in
106
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 JavaDoc type) throws ViewHandlerException {
126         ViewHandler handler = null;
127         String JavaDoc 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 JavaDoc cnf) {
135             //throw new ViewHandlerException("Cannot load handler class", cnf);
136
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 JavaDoc ie) {
138             throw new ViewHandlerException("Cannot get instance of the handler", ie);
139         } catch (IllegalAccessException JavaDoc iae) {
140             throw new ViewHandlerException(iae.getMessage(), iae);
141         }
142
143         return handler;
144     }
145 }
146
Popular Tags