KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > ScreenFactory


1 /*
2  * $Id: ScreenFactory.java 7061 2006-03-24 04:37:21Z jonesde $
3  *
4  * Copyright (c) 2003-2005 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 package org.ofbiz.widget.screen;
25
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35
36 import org.ofbiz.base.location.FlexibleLocation;
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilHttp;
39 import org.ofbiz.base.util.UtilXml;
40 import org.ofbiz.base.util.cache.UtilCache;
41
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46
47 /**
48  * Widget Library - Screen factory class
49  *
50  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
51  * @version $Rev: 7061 $
52  * @since 3.1
53  */

54 public class ScreenFactory {
55     
56     public static final String JavaDoc module = ScreenFactory.class.getName();
57
58     public static final UtilCache screenLocationCache = new UtilCache("widget.screen.locationResource", 0, 0, false);
59     public static final UtilCache screenWebappCache = new UtilCache("widget.screen.webappResource", 0, 0, false);
60
61     public static boolean isCombinedName(String JavaDoc combinedName) {
62         int numSignIndex = combinedName.lastIndexOf("#");
63         if (numSignIndex == -1) {
64             return false;
65         }
66         if (numSignIndex + 1 >= combinedName.length()) {
67             return false;
68         }
69         return true;
70     }
71     
72     public static String JavaDoc getResourceNameFromCombined(String JavaDoc combinedName) {
73         // split out the name on the last "#"
74
int numSignIndex = combinedName.lastIndexOf("#");
75         if (numSignIndex == -1) {
76             throw new IllegalArgumentException JavaDoc("Error in screen location/name: no \"#\" found to separate the location from the name; correct example: component://product/screen/product/ProductScreens.xml#EditProduct");
77         }
78         if (numSignIndex + 1 >= combinedName.length()) {
79             throw new IllegalArgumentException JavaDoc("Error in screen location/name: the \"#\" was at the end with no screen name after it; correct example: component://product/screen/product/ProductScreens.xml#EditProduct");
80         }
81         String JavaDoc resourceName = combinedName.substring(0, numSignIndex);
82         return resourceName;
83     }
84     
85     public static String JavaDoc getScreenNameFromCombined(String JavaDoc combinedName) {
86         // split out the name on the last "#"
87
int numSignIndex = combinedName.lastIndexOf("#");
88         if (numSignIndex == -1) {
89             throw new IllegalArgumentException JavaDoc("Error in screen location/name: no \"#\" found to separate the location from the name; correct example: component://product/screen/product/ProductScreens.xml#EditProduct");
90         }
91         if (numSignIndex + 1 >= combinedName.length()) {
92             throw new IllegalArgumentException JavaDoc("Error in screen location/name: the \"#\" was at the end with no screen name after it; correct example: component://product/screen/product/ProductScreens.xml#EditProduct");
93         }
94         String JavaDoc screenName = combinedName.substring(numSignIndex + 1);
95         return screenName;
96     }
97     
98     public static ModelScreen getScreenFromLocation(String JavaDoc combinedName)
99             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
100         String JavaDoc resourceName = getResourceNameFromCombined(combinedName);
101         String JavaDoc screenName = getScreenNameFromCombined(combinedName);
102         return getScreenFromLocation(resourceName, screenName);
103     }
104     
105     public static ModelScreen getScreenFromLocation(String JavaDoc resourceName, String JavaDoc screenName)
106             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
107         Map JavaDoc modelScreenMap = (Map JavaDoc) screenLocationCache.get(resourceName);
108         if (modelScreenMap == null) {
109             synchronized (ScreenFactory.class) {
110                 modelScreenMap = (Map JavaDoc) screenLocationCache.get(resourceName);
111                 if (modelScreenMap == null) {
112                     long startTime = System.currentTimeMillis();
113                     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
114                     if (loader == null) {
115                         loader = ScreenFactory.class.getClassLoader();
116                     }
117                     
118                     URL JavaDoc screenFileUrl = null;
119                     screenFileUrl = FlexibleLocation.resolveLocation(resourceName, loader);
120                     if (screenFileUrl == null) {
121                         throw new IllegalArgumentException JavaDoc("Could not resolve location to URL: " + resourceName);
122                     }
123                     Document JavaDoc screenFileDoc = UtilXml.readXmlDocument(screenFileUrl, true);
124                     modelScreenMap = readScreenDocument(screenFileDoc, resourceName);
125                     screenLocationCache.put(resourceName, modelScreenMap);
126                     double totalSeconds = (System.currentTimeMillis() - startTime)/1000.0;
127                     Debug.logInfo("Got " + modelScreenMap.size() + " screens in " + totalSeconds + "s from: " + screenFileUrl.toExternalForm(), module);
128                 }
129             }
130         }
131         
132         ModelScreen modelScreen = (ModelScreen) modelScreenMap.get(screenName);
133         if (modelScreen == null) {
134             throw new IllegalArgumentException JavaDoc("Could not find screen with name [" + screenName + "] in class resource [" + resourceName + "]");
135         }
136         return modelScreen;
137     }
138     
139     public static ModelScreen getScreenFromWebappContext(String JavaDoc resourceName, String JavaDoc screenName, HttpServletRequest JavaDoc request)
140             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
141         String JavaDoc webappName = UtilHttp.getApplicationName(request);
142         String JavaDoc cacheKey = webappName + "::" + resourceName;
143         
144         
145         Map JavaDoc modelScreenMap = (Map JavaDoc) screenWebappCache.get(cacheKey);
146         if (modelScreenMap == null) {
147             synchronized (ScreenFactory.class) {
148                 modelScreenMap = (Map JavaDoc) screenWebappCache.get(cacheKey);
149                 if (modelScreenMap == null) {
150                     ServletContext JavaDoc servletContext = (ServletContext JavaDoc) request.getAttribute("servletContext");
151                     
152                     URL JavaDoc screenFileUrl = servletContext.getResource(resourceName);
153                     Document JavaDoc screenFileDoc = UtilXml.readXmlDocument(screenFileUrl, true);
154                     modelScreenMap = readScreenDocument(screenFileDoc, resourceName);
155                     screenWebappCache.put(cacheKey, modelScreenMap);
156                 }
157             }
158         }
159         
160         ModelScreen modelScreen = (ModelScreen) modelScreenMap.get(screenName);
161         if (modelScreen == null) {
162             throw new IllegalArgumentException JavaDoc("Could not find screen with name [" + screenName + "] in webapp resource [" + resourceName + "] in the webapp [" + webappName + "]");
163         }
164         return modelScreen;
165     }
166     
167     public static Map JavaDoc readScreenDocument(Document JavaDoc screenFileDoc, String JavaDoc sourceLocation) {
168         Map JavaDoc modelScreenMap = new HashMap JavaDoc();
169         if (screenFileDoc != null) {
170             // read document and construct ModelScreen for each screen element
171
Element JavaDoc rootElement = screenFileDoc.getDocumentElement();
172             List JavaDoc screenElements = UtilXml.childElementList(rootElement, "screen");
173             Iterator JavaDoc screenElementIter = screenElements.iterator();
174             while (screenElementIter.hasNext()) {
175                 Element JavaDoc screenElement = (Element JavaDoc) screenElementIter.next();
176                 ModelScreen modelScreen = new ModelScreen(screenElement, modelScreenMap, sourceLocation);
177                 //Debug.logInfo("Read Screen with name: " + modelScreen.getName(), module);
178
modelScreenMap.put(modelScreen.getName(), modelScreen);
179             }
180         }
181         return modelScreenMap;
182     }
183 }
184
Popular Tags