1 24 package org.ofbiz.widget.screen; 25 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import javax.servlet.ServletContext ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.xml.parsers.ParserConfigurationException ; 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 ; 43 import org.w3c.dom.Element ; 44 import org.xml.sax.SAXException ; 45 46 47 54 public class ScreenFactory { 55 56 public static final String 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 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 getResourceNameFromCombined(String combinedName) { 73 int numSignIndex = combinedName.lastIndexOf("#"); 75 if (numSignIndex == -1) { 76 throw new IllegalArgumentException ("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 ("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 resourceName = combinedName.substring(0, numSignIndex); 82 return resourceName; 83 } 84 85 public static String getScreenNameFromCombined(String combinedName) { 86 int numSignIndex = combinedName.lastIndexOf("#"); 88 if (numSignIndex == -1) { 89 throw new IllegalArgumentException ("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 ("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 screenName = combinedName.substring(numSignIndex + 1); 95 return screenName; 96 } 97 98 public static ModelScreen getScreenFromLocation(String combinedName) 99 throws IOException , SAXException , ParserConfigurationException { 100 String resourceName = getResourceNameFromCombined(combinedName); 101 String screenName = getScreenNameFromCombined(combinedName); 102 return getScreenFromLocation(resourceName, screenName); 103 } 104 105 public static ModelScreen getScreenFromLocation(String resourceName, String screenName) 106 throws IOException , SAXException , ParserConfigurationException { 107 Map modelScreenMap = (Map ) screenLocationCache.get(resourceName); 108 if (modelScreenMap == null) { 109 synchronized (ScreenFactory.class) { 110 modelScreenMap = (Map ) screenLocationCache.get(resourceName); 111 if (modelScreenMap == null) { 112 long startTime = System.currentTimeMillis(); 113 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 114 if (loader == null) { 115 loader = ScreenFactory.class.getClassLoader(); 116 } 117 118 URL screenFileUrl = null; 119 screenFileUrl = FlexibleLocation.resolveLocation(resourceName, loader); 120 if (screenFileUrl == null) { 121 throw new IllegalArgumentException ("Could not resolve location to URL: " + resourceName); 122 } 123 Document 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 ("Could not find screen with name [" + screenName + "] in class resource [" + resourceName + "]"); 135 } 136 return modelScreen; 137 } 138 139 public static ModelScreen getScreenFromWebappContext(String resourceName, String screenName, HttpServletRequest request) 140 throws IOException , SAXException , ParserConfigurationException { 141 String webappName = UtilHttp.getApplicationName(request); 142 String cacheKey = webappName + "::" + resourceName; 143 144 145 Map modelScreenMap = (Map ) screenWebappCache.get(cacheKey); 146 if (modelScreenMap == null) { 147 synchronized (ScreenFactory.class) { 148 modelScreenMap = (Map ) screenWebappCache.get(cacheKey); 149 if (modelScreenMap == null) { 150 ServletContext servletContext = (ServletContext ) request.getAttribute("servletContext"); 151 152 URL screenFileUrl = servletContext.getResource(resourceName); 153 Document 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 ("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 readScreenDocument(Document screenFileDoc, String sourceLocation) { 168 Map modelScreenMap = new HashMap (); 169 if (screenFileDoc != null) { 170 Element rootElement = screenFileDoc.getDocumentElement(); 172 List screenElements = UtilXml.childElementList(rootElement, "screen"); 173 Iterator screenElementIter = screenElements.iterator(); 174 while (screenElementIter.hasNext()) { 175 Element screenElement = (Element ) screenElementIter.next(); 176 ModelScreen modelScreen = new ModelScreen(screenElement, modelScreenMap, sourceLocation); 177 modelScreenMap.put(modelScreen.getName(), modelScreen); 179 } 180 } 181 return modelScreenMap; 182 } 183 } 184 | Popular Tags |