001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.components.menu;
051:
052: import java.util.*;
053: import java.net.*;
054: import java.io.*;
055: import java.lang.reflect.*;
056: import javax.servlet.http.HttpServletRequest;
057: import javax.servlet.jsp.PageContext;
058: import org.jdom.*;
059: import org.jdom.input.SAXBuilder;
060: import org.apache.struts.action.Action;
061: import org.apache.log4j.Logger;
062: import org.jaffa.security.SecurityTag;
063: import org.jaffa.config.Config;
064: import org.jaffa.util.URLHelper;
065: import org.jaffa.util.MessageHelper;
066: import org.jaffa.util.DefaultEntityResolver;
067: import org.jaffa.util.DefaultErrorHandler;
068:
069: /** MainMenu Class - Reads the XML file containing the Menu Groups and Options
070: * and builds a LinkedHashMap of Groups / Options that the User logged in has
071: * security access to.
072: *
073: * @author MegS
074: * @version 1.0
075: */
076: public class MainMenu {
077: /** Set up Logging for Log4J */
078: private static Logger log = Logger.getLogger(MainMenu.class);
079:
080: // If no property is specified in the framework.properties file, this is where the system
081: // will look for the menulist.xml policy file.
082: private static final String DEFAULT_MENULIST_LOCATION = "classpath:///resources/menu-list.xml";
083:
084: private Option m_option = null;
085: private Group m_group = null;
086: private Option[] m_options = null;
087: private Collection coll = new ArrayList();
088: // XML Elements referred
089: private static final String XML_GROUP = "group";
090: private static final String XML_OPTION = "option";
091: private static final String XML_GROUPNAME = "group-name";
092: private static final String XML_DESC = "description";
093: private static final String XML_GROUPICON = "group-icon";
094: private static final String XML_OPTIONNAME = "option-name";
095: private static final String XML_OPTIONICON = "option-icon";
096: private static final String XML_COMPONENT = "component";
097: private static final String XML_TITLE = "title";
098: private static final String XML_URL = "url";
099: private Map hm;
100:
101: /** Default constructor.
102: */
103: public MainMenu() {
104: reset();
105: }
106:
107: /** Builds the HashMap of the Menu Groups / Options the User logged in has
108: * security access to, if the HashMap hasnt already been built.
109: * @param request The web server request to use for context */
110: public void determineComp(HttpServletRequest request,
111: PageContext pageContext) {
112: if (hm == null) {
113: try {
114: this .process(request, pageContext);
115: } catch (JDOMException j) {
116: log.error("Error Loading Menu XML", j);
117: } catch (FileNotFoundException f) {
118: log.error("Error Loading Menu XML - File Not Found");
119: }
120: }
121: }
122:
123: private void process(HttpServletRequest request,
124: PageContext pageContext) throws JDOMException,
125: FileNotFoundException {
126: String prop = (String) Config.getProperty(
127: Config.PROP_MENULIST_URL, DEFAULT_MENULIST_LOCATION);
128: URL roleUrl = null;
129: try {
130: // Create a URL for the resource file...
131: roleUrl = URLHelper.newExtendedURL(prop);
132: } catch (MalformedURLException e) {
133: log
134: .fatal("Can't Find Menu List File, Bad URL - "
135: + prop, e);
136: throw new SecurityException();
137: }
138: // parse the tempFileName.. Force XML validation
139: SAXBuilder builder = new SAXBuilder(true);
140:
141: // Specifies the EntityResolver to resolve DTD used in XML documents
142: builder.setEntityResolver(new DefaultEntityResolver());
143:
144: // Specifies the ErrorHandler to handle warning/error/fatalError conditions
145: builder.setErrorHandler(new DefaultErrorHandler());
146:
147: Document document = builder.build(roleUrl);
148: Element root = document.getRootElement();
149: hm = new LinkedHashMap();
150: List components = root.getChildren();
151:
152: List groups = new ArrayList();
153:
154: for (Iterator itr = components.iterator(); itr.hasNext();) {
155:
156: if (m_group == null)
157: m_group = new Group();
158: else {
159: if (!(coll.isEmpty())) {
160: hm.put(m_group, coll);
161: coll = new ArrayList();
162: m_group = new Group();
163: }
164: }
165:
166: Element group = (Element) itr.next();
167: String groupName = group.getChildTextTrim(XML_GROUPNAME);
168: String description = group.getChildTextTrim(XML_DESC);
169: String groupIcon = group.getChildTextTrim(XML_GROUPICON);
170: String title = group.getChildTextTrim(XML_TITLE);
171: String gTitle = null;
172: if (MessageHelper.hasTokens(title)) {
173: gTitle = MessageHelper
174: .replaceTokens(pageContext, title);
175: title = gTitle;
176: }
177: m_group.setGroupName(groupName);
178: m_group.setDescription(description);
179: m_group.setGroupIcon(groupIcon);
180: m_group.setGroupTitle(title);
181:
182: List groupElements = group.getChildren(XML_OPTION);
183: int i = 0;
184: for (Iterator itr1 = groupElements.iterator(); itr1
185: .hasNext();) {
186: Element build = (Element) itr1.next();
187: String optionName = build
188: .getChildTextTrim(XML_OPTIONNAME);
189: String optionIcon = build
190: .getChildTextTrim(XML_OPTIONICON);
191: String component = build
192: .getChildTextTrim(XML_COMPONENT);
193: String comp_URL;
194: boolean compExist = false;
195: if (build.getChildTextTrim(XML_COMPONENT) == null
196: || build.getChildTextTrim(XML_COMPONENT)
197: .length() == 0)
198: comp_URL = build.getChildTextTrim(XML_URL);
199: else {
200: comp_URL = build.getChildTextTrim(XML_COMPONENT);
201: compExist = true;
202: }
203:
204: if (log.isDebugEnabled())
205: log.debug(XML_COMPONENT + ":" + comp_URL);
206: m_option = new Option();
207: String name = null;
208: if (MessageHelper.hasTokens(optionName)) {
209: name = MessageHelper.replaceTokens(pageContext,
210: optionName);
211: optionName = name;
212: }
213: if (log.isDebugEnabled())
214: log.debug("The Option Name is : " + optionName);
215: m_option.setName(optionName);
216: m_option.setIcon(optionIcon);
217: m_option.setCompURL(comp_URL);
218: if (compExist) {
219: m_option.setURLTrue(false);
220: // check if user has access to the component
221: if (SecurityTag.hasComponentAccess(request,
222: comp_URL))
223: coll.add(m_option);
224: } else {
225: m_option.setURLTrue(true);
226: coll.add(m_option);
227: }
228: }
229: }
230: if (!(coll.isEmpty()))
231: hm.put(m_group, coll);
232: }
233:
234: /** Reset the HashMap to null
235: */
236: public void reset() {
237: hm = null;
238: }
239:
240: /** Return Map containing list of Groups / Options User logged in has access.
241: * @return Map - Groups and Options User has access to.
242: */
243: public Map getComponents() {
244: return (Map) hm;
245: }
246:
247: /** Test that the menu can be read ok */
248: public static void main(String[] args) {
249: MainMenu menu = new MainMenu();
250: menu.determineComp(null, null);
251: Map mp = menu.getComponents();
252: if (mp.isEmpty()) {
253: System.out.println("HashMap is empty");
254: } else {
255: Collection coll = new ArrayList();
256:
257: Set set = mp.entrySet();
258: Iterator i = set.iterator();
259: while (i.hasNext()) {
260: Map.Entry me = (Map.Entry) i.next();
261: Group group = (Group) me.getKey();
262: ArrayList al = new ArrayList();
263: al = (ArrayList) me.getValue();
264: System.out.println("Group = " + group);
265: String icon = group.getGroupIcon();
266: String desc = group.getDescription();
267: System.out.println("Desc = " + desc);
268: System.out.println("Option = " + al);
269: for (Iterator it = al.iterator(); it.hasNext();) {
270: Option opt = (Option) it.next();
271: String optIcon = opt.getIcon();
272: String optLink = opt.getCompURL();
273: boolean URL = opt.getURLTrue();
274: System.out.println("Each option is: " + opt);
275: System.out.println("Icon is: " + optIcon);
276: }
277: }
278: }
279: }
280: }
|