001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter;
006:
007: import com.sun.portal.rewriter.engines.AbstractRewriter;
008: import com.sun.portal.rewriter.engines.LanguageConstants;
009: import com.sun.portal.rewriter.engines.RewriterBroker;
010: import com.sun.portal.rewriter.rom.InvalidXMLException;
011: import com.sun.portal.rewriter.rom.RuleSet;
012: import com.sun.portal.rewriter.rom.RuleSetManager;
013: import com.sun.portal.rewriter.services.DataService;
014: import com.sun.portal.rewriter.services.DataServiceException;
015: import com.sun.portal.rewriter.util.Constants;
016: import com.sun.portal.rewriter.util.Debug;
017: import com.sun.portal.rewriter.util.StringHelper;
018: import com.sun.portal.rewriter.util.collections.TypedHashCache;
019: import com.sun.portal.rewriter.util.uri.PageSpec;
020:
021: import java.util.Properties;
022:
023: /**
024: * Factory class for getting the instance of Rewriter Objects base on the MIME
025: * type. All the clients should use this class to get the instance of Rewriter.
026: * Directly creating Rewriter Object's may break the code as the component
027: * evolves, Cacheing of these objects is not necessory as there is no XML
028: * Parsing is done.
029: *
030: * @version 1.0 12/15/2001
031: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
032: */
033: public final class RewriterPool {
034: private static RewriterPool defaultPool;
035:
036: private final TypedHashCache rewriterBrokerCache = new TypedHashCache(
037: RewriterBroker.class);
038:
039: public final RuleSetManager rulesetManager;
040:
041: public RewriterPool(final RuleSetManager aRuleSetManager) {
042: rulesetManager = aRuleSetManager;
043: rulesetManager.getDataService().getChangeNotifier()
044: .addObserver(rewriterBrokerCache);
045: if (defaultPool == null) {
046: defaultPool = this ;
047: }
048: }//init()
049:
050: /**
051: * Use cache to get the instance, else create one and store it in cache
052: */
053: public Rewriter getRewriter(final String aRuleSetID,
054: final PageSpec aPageSpec) throws InvalidXMLException,
055: DataServiceException {
056: return getRewriter(aRuleSetID, aPageSpec.getMIME());
057: }//getRewriter()
058:
059: public Rewriter getRewriter(final String aRuleSetID,
060: final String aMIME) throws InvalidXMLException,
061: DataServiceException {
062: //see if this is present in Cache, always return the UNIVERASAL_MIME
063: //rewriter, if this is text content and clients call getInstance() method
064: //then see if it is allready in cache. For all other method calls
065: //no cache check is made.
066: //cache assumes that rulesetID is case insensitive
067: RewriterBroker lRewriterBroker = (RewriterBroker) rewriterBrokerCache
068: .get(aRuleSetID.toLowerCase());
069: if (lRewriterBroker == null) {
070: //not present in cache, create one and store it in cache.
071: RuleSet lRuleSet = rulesetManager.fetchRuleSet(aRuleSetID);
072: lRewriterBroker = new RewriterBroker(lRuleSet);
073: rewriterBrokerCache.put(lRuleSet.getID(), lRewriterBroker);
074: } else if (Debug.isMessageEnabled()) {
075: Object[] param = { aRuleSetID };
076: //Debug.message( "Using the Cached Rewriter id : " + aRuleSetID );
077: Debug.message("PSRW_CSPR_0013", param);
078: }
079:
080: return lRewriterBroker.getInstance(aMIME);
081: }//getInstance()
082:
083: public static Rewriter create(final String aXMLRuleSet,
084: final String aMIME) throws InvalidXMLException {
085: final RuleSet lRuleSet = RuleSetManager.create(aXMLRuleSet);
086: return create(lRuleSet, aMIME);
087: }//create()
088:
089: public static Rewriter create(final RuleSet aRuleSet,
090: final String aMIME) {
091: final RewriterBroker aChain = new RewriterBroker(aRuleSet);
092: return aChain.getInstance(StringHelper.normalize(aMIME));
093: }//create()
094:
095: public static final RewriterPool getDefault() {
096: return defaultPool;
097: }//getDefault()
098:
099: public static void main(String[] args) throws Exception {
100: String lBaseDir = args[0];
101: String lFileName = args[1];
102: Properties props = new Properties();
103: props.setProperty(Constants.PROPERTY_DATA_SOURCE_TYPE,
104: Constants.FILE);
105: props.setProperty(DataService.PROPERTY_DATA_SERVICE_BASE,
106: lBaseDir);
107: RewriterModule.init(props, null, null);
108: Rewriter r = RewriterPool.create(RuleSetManager.getDefault()
109: .retrieve(lFileName), LanguageConstants.CSS_MIME);
110: Debug.println(((AbstractRewriter) r).getRuleSet().toXML());
111: }//main()
112:
113: }//class RewriterPool
|