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