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.services.file;
006:
007: import com.sun.portal.rewriter.RewriterModule;
008: import com.sun.portal.rewriter.services.DataService;
009: import com.sun.portal.rewriter.services.DataServiceException;
010: import com.sun.portal.rewriter.services.DataServiceHelper;
011: import com.sun.portal.rewriter.util.Constants;
012: import com.sun.portal.rewriter.util.Debug;
013: import com.sun.portal.rewriter.util.Resource;
014: import com.sun.portal.rewriter.util.ConfigManager;
015:
016: import java.io.File;
017: import java.io.FileOutputStream;
018: import java.io.FileReader;
019: import java.util.Arrays;
020: import java.util.HashSet;
021: import java.util.Observable;
022: import java.util.Properties;
023: import java.util.Set;
024: import java.util.logging.FileHandler;
025:
026: /**
027: * DataService implentation to store/retrive data to and from local filesystem
028: * Used mainly for testing rewriter component, where IDSAME server does
029: * not exist
030: *
031: * @version 1.0 12/15/2001
032: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
033: */
034: public final class FileDataService implements DataService {
035: public static final String[][] default_rulesets = {
036: /*{
037: RewriterModule.DEFAULT_RULESET_ID,
038: RewriterModule.RESOURCE_DEFAULT_RULESET_LOCATION,
039: },*/
040:
041: { RewriterModule.GENRIC_RULESET_ID,
042: RewriterModule.RESOURCE_GENERIC_RULESET_LOCATION, },
043:
044: {
045: "default_gateway_ruleset",
046: RewriterModule.RESOURCE_HOME
047: + "/DefaultGatewayRuleSet.xml", }, };
048:
049: private static final FileEventListenerImpl registar = new FileEventListenerImpl();
050: private static final String FILE_BASE_DIR = DataService.PROPERTY_DATA_SERVICE_BASE;
051:
052: static {
053: DataServiceHelper.initLogSystem(FileHandler.class,
054: ConfigManager.getLogLocation(), ConfigManager
055: .getLogLevel());
056: }
057:
058: private String baseDir;
059:
060: public FileDataService(final Properties aProps) {
061: final String lBaseDir = aProps.getProperty(FILE_BASE_DIR, ".");
062: if (!lBaseDir.endsWith(File.separator)) {
063: baseDir = lBaseDir + File.separator;
064: }
065:
066: //if the base directory does not exists, create one
067: final File f = new File(baseDir);
068: if (!f.exists()) {
069: f.mkdirs();
070: }
071: }//constructor
072:
073: public String storeXML(final String aRuleSetID,
074: final String aXMLRuleSet) throws DataServiceException,
075: UnsupportedOperationException {
076: String result = "";
077: try {
078: result = retrieveXML(aRuleSetID);
079: File f = new File(baseDir + aRuleSetID);
080: f.createNewFile();
081: FileOutputStream fOut = new FileOutputStream(f);
082: fOut.write(aXMLRuleSet.getBytes());
083: fOut.flush();
084: fOut.close();
085: fOut = null;
086: fireEvent(aRuleSetID);
087: } catch (Exception e) {
088: throw new DataServiceException(e.getMessage(), e);
089: }//try/catch
090: return result;
091: }//storeXML()
092:
093: public String deleteKey(final String aRuleSetID)
094: throws DataServiceException, UnsupportedOperationException {
095: try {
096: String oldValue = readAndDelete(aRuleSetID);
097: fireEvent(aRuleSetID);
098: return oldValue;
099: } catch (Exception e) {
100: throw new DataServiceException(e.getMessage(), e);
101: }//try/catch
102: }//deleteKey()
103:
104: public String retrieveXML(final String aRuleSetID)
105: throws DataServiceException {
106: Debug.message("Retrieve the RuleSet Located at: " + baseDir
107: + aRuleSetID);
108: try {
109: for (int i = 0; i < default_rulesets.length; i++) {
110: if (aRuleSetID.equals(default_rulesets[i][0])) {
111: return Resource.readXML(default_rulesets[i][1]);
112: }
113: }
114:
115: return read(new File(baseDir + aRuleSetID));
116: } catch (Exception e) {
117: throw new DataServiceException(e.getMessage(), e);
118: }//try/catch
119: }//retrieveXML()
120:
121: public Set retrieveKeys() throws DataServiceException {
122: try {
123: return new HashSet(Arrays
124: .asList((new File(baseDir)).list()));
125: } catch (Exception e) {
126: throw new DataServiceException(e.getMessage(), e);
127: }//try/catch
128: }//retrieveKeys()
129:
130: private String readAndDelete(final String aRuleSetIDKey)
131: throws Exception {
132: final File f = new File(baseDir + aRuleSetIDKey);
133: final String result = read(f);
134: f.delete();
135: return result;
136: }//readAndDelete()
137:
138: private static String read(final File f) throws Exception {
139: final String result = "File Does Not Exisit: " + f.toURL();
140: if (f.exists()) {
141: return Resource.read(new FileReader(f));
142: }
143: return result;
144: }//read()
145:
146: //dummy not usefull in the case of file system
147: public Observable getChangeNotifier() {
148: return registar;
149: }//getChangeNotifier()
150:
151: private void fireEvent(final String aEventInfo) {
152: ((FileEventListenerImpl) getChangeNotifier())
153: .dispatch(aEventInfo);
154: }//fireEvent()
155:
156: public String matchesWithID(final String aKey)
157: throws DataServiceException {
158: if (retrieveKeys().contains(aKey)) {
159: return aKey;
160: } else {
161: return null;
162: }
163: }//matchesWithID()
164:
165: public static void main(String[] args) throws DataServiceException {
166: //args[0] - base direcotry name
167: //args[1] - file name
168:
169: String lBaseDir = args[0];
170: String lFileName = args[1];
171: Properties props = new Properties();
172: props.setProperty(Constants.PROPERTY_DATA_SOURCE_TYPE,
173: Constants.FILE);
174: props.setProperty(DataService.PROPERTY_DATA_SERVICE_BASE,
175: lBaseDir);
176:
177: DataService dataService = new FileDataService(props);
178: Debug.println(dataService.storeXML(lFileName,
179: "one raja devaki devi"));
180: Debug.println(dataService.retrieveKeys());
181: Debug.println(dataService.storeXML(lFileName,
182: "two raja nagendra kumar"));
183: Debug.println(dataService.storeXML(lFileName + lFileName,
184: "two raja nagendra kumar"));
185: Debug.println(dataService.deleteKey(lFileName));
186: }//main()
187:
188: }//class FileDataService
|