001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.admin;
007:
008: import java.lang.*;
009: import java.util.*;
010: import java.util.logging.Logger;
011: import java.util.logging.Level;
012: import java.util.logging.LogRecord;
013: import java.io.*;
014:
015: import com.sun.portal.search.util.*;
016: import com.sun.portal.log.common.PortalLogger;
017:
018: public class CnConfig {
019:
020: public static String CLASSIFICATION_CONF = "classification.conf";
021:
022: // List of src's
023: public static final String SRC_URL = "URL";
024: public static final String SRC_HOST = "host";
025: public static final String SRC_PROTOCOL = "protocol";
026: public static final String SRC_URI = "uri";
027: public static final String SRC_IP = "ip";
028: public static final String SRC_TYPE = "type";
029: public static final String SRC_ATTRIBUTE = "attribute";
030:
031: // List of methods
032: public static final String METHOD_EXACT = "by-exact";
033: public static final String METHOD_PREFIX = "by-prefix";
034: public static final String METHOD_SUFFIX = "by-suffix";
035: public static final String METHOD_SUBSTR = "by-substr";
036: public static final String METHOD_REGEX = "by-regex";
037:
038: public static final String METHOD_LIST[] = { METHOD_EXACT,
039: METHOD_PREFIX, METHOD_SUFFIX, METHOD_SUBSTR, METHOD_REGEX };
040:
041: // file header and footer initialized upon reading the file
042: private StringBuffer filename;
043: private StringBuffer rulesHeader = new StringBuffer();
044: private StringBuffer rulesFooter = new StringBuffer();
045:
046: // initialized upon reading the file and updated when
047: // add, update, delete are done
048: ArrayList rulesList;
049:
050: // Create a logger for this class
051: private static Logger debugLogger = PortalLogger
052: .getLogger(CnConfig.class);
053:
054: public CnConfig(String server_root) {
055: // Read classification.conf from server_root/config directory
056: // parse the file and store the rules in an array
057: filename = new StringBuffer();
058: filename.append(server_root + File.separator);
059: filename.append(SearchConfig.CONFDIR + File.separator);
060: filename.append(CnConfig.CLASSIFICATION_CONF);
061:
062: parseClassificationFile(filename.toString());
063: }
064:
065: public ArrayList getRulesList() {
066: return rulesList;
067: }
068:
069: public boolean isExist(String src, String method, String name,
070: String action) {
071: return isExist(src, method, name, action, -1);
072: }
073:
074: public boolean isExist(String src, String method, String name,
075: String action, int exceptIndex) {
076: for (int i = 0; i < rulesList.size(); i++) {
077: if (i == exceptIndex) {
078: continue;
079: }
080: Rule r = (Rule) rulesList.get(i);
081: if (r.getSrc().equalsIgnoreCase(src)
082: && r.getMethod().equalsIgnoreCase(method)
083: && r.getName().equals(name)
084: && r.getAction().equals(action)) {
085: return true;
086: }
087: }
088: return false;
089: }
090:
091: public boolean add(String src, String method, String name,
092: String action, boolean isCase) {
093: // Create a new rule object with src, method, name, action paramter
094: // Create new rule with the params
095: // add the rule to the rulesList
096:
097: if (isExist(src, method, name, action)) {
098: return false;
099: }
100: Rule r = new Rule(src, method, name, action, isCase);
101: addSortedBySrc(r);
102: return true;
103: }
104:
105: void addSortedBySrc(Rule r) {
106: for (int i = 0; i < rulesList.size(); i++) {
107: Rule element = (Rule) rulesList.get(i);
108: if (r.getSrc().compareTo(element.getSrc()) < 0) {
109: rulesList.add(i, r);
110: return;
111: }
112: }
113: rulesList.add(r);
114: }
115:
116: public void delete(int index) {
117: // Delete will have an index parameter which is the rule number
118: // Delete the rule from the rule list
119: rulesList.remove(index);
120: }
121:
122: public void removeAll() {
123: rulesList.clear();
124: }
125:
126: public void update(int index, String src, String method,
127: String name, String action) {
128: // Is a little complicated
129: // extract the rule from rulesList
130: // save the new values in the rule, delete the old rule
131:
132: Rule r = (Rule) rulesList.get(index);
133: r.setSrc(src);
134: r.setMethod(method);
135: r.setName(name);
136: r.setAction(action);
137: }
138:
139: public void save() {
140: // open the file. copy the rulesHeader
141: // Copy rulesList
142: // copy rulesFooter
143: // close the file
144:
145: try {
146: FileOutputStream fos = new FileOutputStream(filename
147: .toString());
148: PrintWriter pw = new PrintWriter(new BufferedWriter(
149: new OutputStreamWriter(fos, "UTF8")), true);
150: pw.println(rulesHeader);
151: StringBuffer buf = new StringBuffer();
152: for (int i = 0; i < rulesList.size(); i++) {
153: Rule r = (Rule) rulesList.get(i);
154: buf.append("Classification");
155: buf.append(" ");
156: buf.append("src=");
157: buf.append("\"" + r.getSrc() + "\"");
158: buf.append(" ");
159: buf.append(r.getMethod());
160: buf.append("=");
161: buf.append(PBlock.quotedString(r.getName()));
162: buf.append(" ");
163: if (!r.isCaseSensitive()) {
164: buf.append("case="
165: + (r.isCaseSensitive() ? "true" : "false"));
166: }
167: buf.append(" ");
168: buf.append("action=");
169: buf.append(PBlock.quotedString(r.getAction()));
170: buf.append("\n");
171: }
172: pw.println(buf.toString());
173: pw.print(rulesFooter);
174: pw.close();
175: } catch (Exception e) {
176: if (debugLogger.isLoggable(Level.INFO)) {
177: LogRecord logRecord = new LogRecord(Level.INFO,
178: "PSSH_CSPSA0006");
179: logRecord.setParameters(new Object[] { filename });
180: logRecord.setThrown(e);
181: logRecord.setLoggerName(debugLogger.getName());
182: debugLogger.log(logRecord);
183: }
184: }
185: }
186:
187: public void parseClassificationFile(String fname) {
188: // read the file, parse it
189: // set rulesHeader, rulesFooter
190: // Create each rule object with appropriate values
191: // create rulesList
192:
193: rulesHeader = new StringBuffer("");
194: rulesFooter = new StringBuffer("");
195: rulesList = new ArrayList();
196:
197: boolean inHeader = true;
198: boolean inFooter = false;
199:
200: try {
201: BufferedReader br = new BufferedReader(
202: new InputStreamReader(new FileInputStream(fname),
203: "UTF-8"));
204: String line = null;
205: while ((line = br.readLine()) != null) {
206: if (line.startsWith("Classification")) {
207: addRule(line);
208: } else if (line
209: .startsWith("<Preprocess directive=\"Generate\">")) {
210: rulesHeader.append(line + "\n");
211: inHeader = false;
212: } else if (line.startsWith("</Preprocess>")) {
213: rulesFooter.append(line + "\n");
214: inFooter = true;
215: } else if (inHeader) {
216: rulesHeader.append(line + "\n");
217: } else if (inFooter) {
218: rulesFooter.append(line + "\n");
219: }
220: }
221: br.close();
222: } catch (IOException e) {
223: debugLogger.info("PSSH_CSPSA0007");
224: }
225: }
226:
227: public void addRule(String rule) {
228: HashMap map = new HashMap();
229: try {
230: PBlock.str2pblock(rule, map);
231: Rule r = new Rule();
232:
233: String value;
234:
235: value = (String) map.get("src");
236: if (value != null) {
237: int methodIndex;
238: r.setSrc(value);
239: for (methodIndex = 0; methodIndex < METHOD_LIST.length; methodIndex++) {
240: value = (String) map.get(METHOD_LIST[methodIndex]);
241: if (value != null)
242: break;
243: }
244: if (value != null) {
245: r.setMethod(METHOD_LIST[methodIndex]);
246: r.setName(value);
247: value = (String) map.get("action");
248: if (value != null) {
249: r.setAction(value);
250: value = (String) map.get("case");
251: if (value != null && value.equals("false")) {
252: r.setCaseSensitive(false);
253: }
254: if (!isExist(r.getSrc(), r.getMethod(), r
255: .getName(), r.getAction())) {
256: addSortedBySrc(r);
257: return;
258: }
259: }
260: }
261: }
262:
263: } catch (Exception e) {
264: }
265: debugLogger.log(Level.INFO, "PSSH_CSPSA0008", rule);
266: return;
267: }
268:
269: // Method to return the number of rules
270: public int getNumberOfRules() {
271: return rulesList.size();
272: }
273: }
|