1 24 package org.ofbiz.widget.menu; 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.UtilHttp; 38 import org.ofbiz.base.util.UtilXml; 39 import org.ofbiz.base.util.cache.UtilCache; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.service.LocalDispatcher; 42 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.xml.sax.SAXException ; 46 47 48 55 public class MenuFactory { 56 57 public static final String module = MenuFactory.class.getName(); 58 59 public static final UtilCache menuClassCache = new UtilCache("widget.menu.classResource", 0, 0, false); 60 public static final UtilCache menuWebappCache = new UtilCache("widget.menu.webappResource", 0, 0, false); 61 public static final UtilCache menuLocationCache = new UtilCache("widget.menu.locationResource", 0, 0, false); 62 63 public static ModelMenu getMenuFromClass(String resourceName, String menuName, GenericDelegator delegator, LocalDispatcher dispatcher) 64 throws IOException , SAXException , ParserConfigurationException { 65 Map modelMenuMap = (Map ) menuClassCache.get(resourceName); 66 if (modelMenuMap == null) { 67 synchronized (MenuFactory.class) { 68 modelMenuMap = (Map ) menuClassCache.get(resourceName); 69 if (modelMenuMap == null) { 70 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 71 if (loader == null) { 72 loader = MenuFactory.class.getClassLoader(); 73 } 74 75 URL menuFileUrl = loader.getResource(resourceName); 76 Document menuFileDoc = UtilXml.readXmlDocument(menuFileUrl, true); 77 modelMenuMap = readMenuDocument(menuFileDoc, delegator, dispatcher); 78 menuClassCache.put(resourceName, modelMenuMap); 79 } 80 } 81 } 82 83 ModelMenu modelMenu = (ModelMenu) modelMenuMap.get(menuName); 84 if (modelMenu == null) { 85 throw new IllegalArgumentException ("Could not find menu with name [" + menuName + "] in class resource [" + resourceName + "]"); 86 } 87 return modelMenu; 88 } 89 90 public static ModelMenu getMenuFromWebappContext(String resourceName, String menuName, HttpServletRequest request) 91 throws IOException , SAXException , ParserConfigurationException { 92 String webappName = UtilHttp.getApplicationName(request); 93 String cacheKey = webappName + "::" + resourceName; 94 95 96 Map modelMenuMap = (Map ) menuWebappCache.get(cacheKey); 97 if (modelMenuMap == null) { 98 synchronized (MenuFactory.class) { 99 modelMenuMap = (Map ) menuWebappCache.get(cacheKey); 100 if (modelMenuMap == null) { 101 ServletContext servletContext = (ServletContext ) request.getAttribute("servletContext"); 102 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 103 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 104 105 URL menuFileUrl = servletContext.getResource(resourceName); 106 Document menuFileDoc = UtilXml.readXmlDocument(menuFileUrl, true); 107 modelMenuMap = readMenuDocument(menuFileDoc, delegator, dispatcher); 108 menuWebappCache.put(cacheKey, modelMenuMap); 109 } 110 } 111 } 112 113 ModelMenu modelMenu = (ModelMenu) modelMenuMap.get(menuName); 114 if (modelMenu == null) { 115 throw new IllegalArgumentException ("Could not find menu with name [" + menuName + "] in webapp resource [" + resourceName + "] in the webapp [" + webappName + "]"); 116 } 117 return modelMenu; 118 } 119 120 public static Map readMenuDocument(Document menuFileDoc, GenericDelegator delegator, LocalDispatcher dispatcher) { 121 Map modelMenuMap = new HashMap (); 122 if (menuFileDoc != null) { 123 Element rootElement = menuFileDoc.getDocumentElement(); 125 List menuElements = UtilXml.childElementList(rootElement, "menu"); 126 Iterator menuElementIter = menuElements.iterator(); 127 while (menuElementIter.hasNext()) { 128 Element menuElement = (Element ) menuElementIter.next(); 129 ModelMenu modelMenu = new ModelMenu(menuElement, delegator, dispatcher); 130 modelMenuMap.put(modelMenu.getName(), modelMenu); 131 } 132 } 133 return modelMenuMap; 134 } 135 136 public static ModelMenu getMenuFromLocation(String resourceName, String menuName, GenericDelegator delegator, LocalDispatcher dispatcher) 137 throws IOException , SAXException , ParserConfigurationException { 138 Map modelMenuMap = (Map ) menuLocationCache.get(resourceName); 139 if (modelMenuMap == null) { 140 synchronized (MenuFactory.class) { 141 modelMenuMap = (Map ) menuLocationCache.get(resourceName); 142 if (modelMenuMap == null) { 143 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 144 if (loader == null) { 145 loader = MenuFactory.class.getClassLoader(); 146 } 147 148 URL menuFileUrl = null; 149 menuFileUrl = FlexibleLocation.resolveLocation(resourceName); Document menuFileDoc = UtilXml.readXmlDocument(menuFileUrl, true); 151 modelMenuMap = readMenuDocument(menuFileDoc, delegator, dispatcher); 152 menuLocationCache.put(resourceName, modelMenuMap); 153 } 154 } 155 } 156 157 ModelMenu modelMenu = (ModelMenu) modelMenuMap.get(menuName); 158 if (modelMenu == null) { 159 throw new IllegalArgumentException ("Could not find menu with name [" + menuName + "] in class resource [" + resourceName + "]"); 160 } 161 return modelMenu; 162 } 163 164 } 165 | Popular Tags |