KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > menu > MenuFactory


1 /*
2  * $Id: MenuFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 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 package org.ofbiz.widget.menu;
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.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 JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46
47
48 /**
49  * Widget Library - Menu factory class
50  *
51  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
52  * @version $Rev: 5462 $
53  * @since 2.2
54  */

55 public class MenuFactory {
56     
57     public static final String JavaDoc 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 JavaDoc resourceName, String JavaDoc menuName, GenericDelegator delegator, LocalDispatcher dispatcher)
64             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
65         Map JavaDoc modelMenuMap = (Map JavaDoc) menuClassCache.get(resourceName);
66         if (modelMenuMap == null) {
67             synchronized (MenuFactory.class) {
68                 modelMenuMap = (Map JavaDoc) menuClassCache.get(resourceName);
69                 if (modelMenuMap == null) {
70                     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
71                     if (loader == null) {
72                         loader = MenuFactory.class.getClassLoader();
73                     }
74                     
75                     URL JavaDoc menuFileUrl = loader.getResource(resourceName);
76                     Document JavaDoc 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 JavaDoc("Could not find menu with name [" + menuName + "] in class resource [" + resourceName + "]");
86         }
87         return modelMenu;
88     }
89     
90     public static ModelMenu getMenuFromWebappContext(String JavaDoc resourceName, String JavaDoc menuName, HttpServletRequest JavaDoc request)
91             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
92         String JavaDoc webappName = UtilHttp.getApplicationName(request);
93         String JavaDoc cacheKey = webappName + "::" + resourceName;
94         
95         
96         Map JavaDoc modelMenuMap = (Map JavaDoc) menuWebappCache.get(cacheKey);
97         if (modelMenuMap == null) {
98             synchronized (MenuFactory.class) {
99                 modelMenuMap = (Map JavaDoc) menuWebappCache.get(cacheKey);
100                 if (modelMenuMap == null) {
101                     ServletContext JavaDoc servletContext = (ServletContext JavaDoc) request.getAttribute("servletContext");
102                     GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
103                     LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
104                     
105                     URL JavaDoc menuFileUrl = servletContext.getResource(resourceName);
106                     Document JavaDoc 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 JavaDoc("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 JavaDoc readMenuDocument(Document JavaDoc menuFileDoc, GenericDelegator delegator, LocalDispatcher dispatcher) {
121         Map JavaDoc modelMenuMap = new HashMap JavaDoc();
122         if (menuFileDoc != null) {
123             // read document and construct ModelMenu for each menu element
124
Element JavaDoc rootElement = menuFileDoc.getDocumentElement();
125             List JavaDoc menuElements = UtilXml.childElementList(rootElement, "menu");
126             Iterator JavaDoc menuElementIter = menuElements.iterator();
127             while (menuElementIter.hasNext()) {
128                 Element JavaDoc menuElement = (Element JavaDoc) 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 JavaDoc resourceName, String JavaDoc menuName, GenericDelegator delegator, LocalDispatcher dispatcher)
137             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
138         Map JavaDoc modelMenuMap = (Map JavaDoc) menuLocationCache.get(resourceName);
139         if (modelMenuMap == null) {
140             synchronized (MenuFactory.class) {
141                 modelMenuMap = (Map JavaDoc) menuLocationCache.get(resourceName);
142                 if (modelMenuMap == null) {
143                     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
144                     if (loader == null) {
145                         loader = MenuFactory.class.getClassLoader();
146                     }
147                     
148                     URL JavaDoc menuFileUrl = null;
149                     menuFileUrl = FlexibleLocation.resolveLocation(resourceName); //, loader);
150
Document JavaDoc 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 JavaDoc("Could not find menu with name [" + menuName + "] in class resource [" + resourceName + "]");
160         }
161         return modelMenu;
162     }
163     
164 }
165
Popular Tags