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.rom;
006:
007: import com.sun.portal.rewriter.services.DataService;
008: import com.sun.portal.rewriter.services.DataServiceException;
009: import com.sun.portal.rewriter.util.Debug;
010: import com.sun.portal.rewriter.util.Resource;
011: import com.sun.portal.rewriter.util.collections.TypedHashCache;
012: import com.sun.portal.rewriter.util.xml.Document;
013:
014: import org.xml.sax.SAXException;
015:
016: import java.io.FileReader;
017: import java.io.Reader;
018: import java.util.Set;
019:
020: /**
021: * Wrapper class to be used by admin module of Rewriter to edit/delete/create
022: * new RuleSet's
023: *
024: * @version 1.0 12/15/2001
025: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
026: */
027: public final class RuleSetManager {
028: private static RuleSetManager defaultManager;
029:
030: private final TypedHashCache ruleSetCache = new TypedHashCache(
031: RuleSet.class);
032: private DataService dataService;
033:
034: public RuleSetManager(final DataService aDataService) {
035: dataService = aDataService;
036: dataService.getChangeNotifier().addObserver(ruleSetCache);
037: if (defaultManager == null) {
038: defaultManager = this ;
039: }
040: }//init()
041:
042: public DataService getDataService() {
043: return dataService;
044: }//getDataService()
045:
046: /**
047: * first it will look into the cache else it creates one and stores in the
048: * cache
049: */
050: public RuleSet fetchRuleSet(final String aRuleSetID)
051: throws InvalidXMLException, DataServiceException {
052: if (aRuleSetID.equals(Rule.NONE)) {
053: return null;
054: }
055:
056: final RuleSet result = (RuleSet) ruleSetCache.get(aRuleSetID);
057: if (result != null) {
058: return result;
059: }
060:
061: //if not present in cache create one and store the same
062: String lXMLString = retrieve(aRuleSetID);
063:
064: if (Debug.isMessageEnabled()) {
065: Debug
066: .recordRuleSetMessage("\n\n\nRetrieved XML For RuleSet ID: "
067: + aRuleSetID + " is\n\n" + lXMLString);
068: }
069:
070: RuleSet lRuleSet = create(lXMLString);
071: ruleSetCache.put(lRuleSet.getID(), lRuleSet);
072: return lRuleSet;
073: }//fetchRuleSet()
074:
075: public Set getRuleSetNames() throws DataServiceException {
076: return dataService.retrieveKeys();
077: }//getRuleSetNames()
078:
079: public String matchesWithID(final String aXMLString)
080: throws DataServiceException, InvalidXMLException {
081: final RuleSet r = create(aXMLString);
082: return dataService.matchesWithID(r.getID());
083: }//matchesWithID()
084:
085: private boolean isRuleSetExists(final String aXMLString)
086: throws DataServiceException, InvalidXMLException {
087: if (matchesWithID(aXMLString) == null) {
088: return false;
089: } else {
090: return true;
091: }
092: }//isRuleSetExists()
093:
094: public String retrieve(final String aRuleSetID)
095: throws DataServiceException {
096: return dataService.retrieveXML(aRuleSetID);
097: }//retrive()
098:
099: public String store(final String aXMLString)
100: throws InvalidXMLException, DataServiceException {
101: final RuleSet r = create(aXMLString);
102: final String keyIDFromXML = r.getID();
103:
104: if (isRuleSetExists(aXMLString)) {
105: delete(keyIDFromXML);
106: }
107:
108: return dataService.storeXML(keyIDFromXML, aXMLString);
109: }//store()
110:
111: public String delete(final String aRuleSetID)
112: throws DataServiceException {
113: return dataService.deleteKey(aRuleSetID);
114: }//delete()
115:
116: public static RuleSet create(final String aXMLString)
117: throws InvalidXMLException {
118: try {
119: return new RuleSet(Document.create(aXMLString)
120: .getRootNode());
121: } catch (InvalidXMLException iXMLe) {
122: throw iXMLe;
123: } catch (SAXException se) {
124: throw new InvalidXMLException(se.getMessage(), aXMLString,
125: se, InvalidXMLException.SAX_EXCEPTION);
126: } catch (Exception e) {
127: e.printStackTrace();
128: throw new InvalidXMLException(e.getMessage(), aXMLString,
129: e, InvalidXMLException.UNKNOWN_EXCEPTION);
130: }//try/catch
131: }//create()
132:
133: public static RuleSet create(final Reader aXMLStringReader)
134: throws InvalidXMLException {
135: return create(Resource.read(aXMLStringReader));
136: }//create()
137:
138: public static boolean validate(final String aXMLString) {
139: try {
140: create(aXMLString);
141: return true;
142: } catch (Exception e) {
143: return false;
144: }//try/catch
145: }//validate
146:
147: public static final RuleSetManager getDefault() {
148: return defaultManager;
149: }//getDefault()
150:
151: public static void main(String[] args) throws Exception {
152: RuleSet lRuleSet = RuleSetManager
153: .create(new FileReader(args[0]));
154: Debug.println(lRuleSet.toXML());
155: }//main()
156:
157: }//class RuleSetManager
|