001: //@todo - add code for
002: // <xsd:element name="MainLayout" type="xsd:string" minOccurs="0" maxOccurs="1"/>
003: // <xsd:element name="FinderLayout" type="xsd:string" minOccurs="0" maxOccurs="1"/>
004: // <xsd:element name="FinderExcelLayout" type="xsd:string" minOccurs="0" maxOccurs="1"/>
005: // <xsd:element name="FinderXmlLayout" type="xsd:string" minOccurs="0" maxOccurs="1"/>
006:
007: /*
008: * ====================================================================
009: * JAFFA - Java Application Framework For All
010: *
011: * Copyright (C) 2002 JAFFA Development Group
012: *
013: * This library is free software; you can redistribute it and/or
014: * modify it under the terms of the GNU Lesser General Public
015: * License as published by the Free Software Foundation; either
016: * version 2.1 of the License, or (at your option) any later version.
017: *
018: * This library is distributed in the hope that it will be useful,
019: * but WITHOUT ANY WARRANTY; without even the implied warranty of
020: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
021: * Lesser General Public License for more details.
022: *
023: * You should have received a copy of the GNU Lesser General Public
024: * License along with this library; if not, write to the Free Software
025: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026: *
027: * Redistribution and use of this software and associated documentation ("Software"),
028: * with or without modification, are permitted provided that the following conditions are met:
029: * 1. Redistributions of source code must retain copyright statements and notices.
030: * Redistributions must also contain a copy of this document.
031: * 2. Redistributions in binary form must reproduce the above copyright notice,
032: * this list of conditions and the following disclaimer in the documentation
033: * and/or other materials provided with the distribution.
034: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
035: * this Software without prior written permission. For written permission,
036: * please contact mail to: jaffagroup@yahoo.com.
037: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
038: * appear in their names without prior written permission.
039: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
040: *
041: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: */
055: package org.jaffa.components.navigation;
056:
057: import java.net.MalformedURLException;
058: import java.net.URL;
059: import javax.xml.bind.JAXBContext;
060: import javax.xml.bind.Unmarshaller;
061: import org.apache.log4j.Logger;
062: import org.jaffa.components.navigation.domain.GlobalMenu;
063: import org.jaffa.config.Config;
064: import org.jaffa.util.URLHelper;
065: import org.jaffa.util.XmlHelper;
066:
067: /** This singleton class loads in the global navigation xml and caches it
068: * to in can be used for constructing instances of NavAccessor's
069: *
070: * @author PaulE
071: * @version 1.0
072: */
073: public class NavCache {
074:
075: private static final Logger log = Logger.getLogger(NavCache.class);
076: private static final String DEFAULT_NAVIGATION_LOCATION = "classpath:///resources/navigation.xml";
077: private static NavCache m_singleton = null;
078: private GlobalMenu m_menu = null;
079:
080: /** Creates an instance of NavCache, if not already instantiated.
081: * @return An instance of the NavCache.
082: */
083: public static NavCache getInstance() {
084: if (m_singleton == null)
085: createNavCacheInstance();
086: return m_singleton;
087: }
088:
089: private static synchronized void createNavCacheInstance() {
090: if (m_singleton == null) {
091: String initfile = (String) Config.getProperty(
092: Config.PROP_MENULIST_URL,
093: DEFAULT_NAVIGATION_LOCATION);
094:
095: if (log.isDebugEnabled())
096: log
097: .debug("Creating an instance of NavCache using file: "
098: + initfile);
099: m_singleton = new NavCache(initfile);
100: }
101: }
102:
103: private NavCache(String initFile) {
104: URL navUrl = null;
105: try {
106: // Create a URL for the resource file...
107: navUrl = URLHelper.newExtendedURL(initFile);
108: } catch (MalformedURLException e) {
109: log.fatal("Can't Find Navigation File, Bad URL - "
110: + initFile, e);
111: throw new SecurityException();
112: }
113:
114: try {
115: // create a JAXBContext capable of handling classes generated into the package
116: JAXBContext jc = JAXBContext
117: .newInstance("org.jaffa.components.navigation.domain");
118:
119: // create an Unmarshaller
120: Unmarshaller u = jc.createUnmarshaller();
121:
122: // enable validation
123: u.setValidating(true);
124:
125: // unmarshal a document into a tree of Java content objects composed of classes from the package.
126: m_menu = (GlobalMenu) u.unmarshal(XmlHelper
127: .stripDoctypeDeclaration(navUrl));
128:
129: } catch (Exception e) {
130: String str = "Error while parsing the Navigation file "
131: + initFile;
132: log.fatal(str, e);
133: throw new SecurityException(str);
134: }
135: }
136:
137: public GlobalMenu getGlobalMenu() {
138: return m_menu;
139: }
140:
141: public void clearCache() {
142: m_singleton = null;
143: }
144: }
|