001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.dataxslt;
034:
035: import java.util.HashMap;
036:
037: /**
038: * A SoftReferences cache to Microsite objects.
039: * When working with XSL tranformations, typically only a small number of Microsite
040: * definitions are used. the definitions are stored in XML files that have to be parsed
041: * for adding data to PageSets.<br>
042: * MicrositeFactory loads once and the reuses Microsite objects reducing disk access
043: * and CPU intensive XML parsing routines.
044: * @author Sergio Montoro Ten
045: * @version 1.0
046: */
047: import java.lang.ref.SoftReference;
048:
049: public class MicrositeFactory {
050: public static boolean bCache = true;
051: public static HashMap oMicrosites = new HashMap();
052:
053: public MicrositeFactory() {
054: }
055:
056: /**
057: * @return Caching status on/off
058: */
059: public static boolean cache() {
060: return bCache;
061: }
062:
063: /**
064: * Turns Microsite caching on/off
065: * @param bCacheOnOf <b>true</b> if Microsite caching is to be activated,
066: * <b>false</b> if Microsite caching is to be deactivated.
067: */
068: public static void cache(boolean bCacheOnOf) {
069: bCache = bCacheOnOf;
070: if (false == bCacheOnOf)
071: oMicrosites.clear();
072: }
073:
074: /**
075: * Get a Microsite from an XML file
076: * If Microsite is cached then cached instance is returned.
077: * @param sURI XML file URI starting with file://
078: * (for example file:///opt/knowgate/storage/xslt/templates/Comtemporary.xml)
079: * @param bValidateXML <b>true</b> if XML validation with W3C schemas is to be done,
080: * <b>false</b> is no validation is to be done.
081: * @return Microsite instance
082: * @throws ClassNotFoundException
083: * @throws IllegalAccessException
084: */
085: public static synchronized Microsite getInstance(String sURI,
086: boolean bValidateXML) throws ClassNotFoundException,
087: Exception, IllegalAccessException {
088: Microsite oRetObj;
089: Object oRefObj;
090:
091: if (bCache) {
092: oRefObj = oMicrosites.get(sURI);
093:
094: if (null == oRefObj) {
095: oRetObj = new Microsite(sURI, bValidateXML);
096: oMicrosites.put(sURI, new SoftReference(oRetObj));
097: } else {
098: oRetObj = (Microsite) ((SoftReference) oRefObj).get();
099: if (null == oRetObj)
100: oRetObj = new Microsite(sURI, bValidateXML);
101: }
102: return oRetObj;
103: } else
104: oRetObj = new Microsite(sURI, bValidateXML);
105:
106: return oRetObj;
107: } // getInstance
108:
109: // ---------------------------------------------------------------------------
110:
111: /**
112: * Get a Microsite from an XML file
113: * If Microsite is cached then cached instance is returned.
114: * XML validation is disabled.
115: * @param sURI XML file path
116: * @return Microsite instance
117: * @throws ClassNotFoundException
118: * @throws IllegalAccessException
119: */
120:
121: public static Microsite getInstance(String sURI)
122: throws ClassNotFoundException, Exception,
123: IllegalAccessException {
124: return getInstance(sURI, false);
125: } // getInstance
126: }
|