001: /*
002: * Copyright 2004 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.util.*;
009: import java.io.*;
010: import com.sun.portal.search.util.PBlock;
011: import com.sun.portal.search.util.OrderedMap;
012:
013: public class FilterConfigurator {
014: private String server_root;
015: private ArrayList outFilterConf = new ArrayList();
016:
017: public FilterConfigurator(String serverRoot) {
018: server_root = serverRoot;
019: }
020:
021: public void readFilterToArray() throws Exception {
022: outFilterConf = new ArrayList();
023: try {
024: BufferedReader br = new BufferedReader(new FileReader(
025: server_root + File.separator + "config"
026: + File.separator + "filter.conf"));
027: String line = null;
028: while ((line = br.readLine()) != null) {
029: outFilterConf.add(line);
030: }
031: } catch (Exception e) {
032: throw new Exception(e);
033: /*
034: CSDebug.logln("[ReadFilterConf]Exception:" + e.getMessage());
035: CSDebug.logln("While trying to load:" + server_root + File.separator + "config" + File.separator + "filter.conf");
036: */
037: }
038: }
039:
040: public String getFilterConfVars(String type, String fnName,
041: String attrName, String defaultValue) {
042: String line = null;
043: for (int i = 0; i < outFilterConf.size(); i++) {
044: line = (String) outFilterConf.get(i);
045: if (line.startsWith(type)) {
046: Map map = new HashMap();
047: try {
048: PBlock.str2pblock(line.substring(type.length()),
049: map);
050: String fn = (String) map.get("fn");
051: if (fn != null && fn.equalsIgnoreCase(fnName)) {
052: return (String) map.get(attrName);
053: }
054: } catch (Exception e) { //ignore
055: }
056: }
057: }
058: return defaultValue;
059: }
060:
061: public void setFilterConfVars(String type, String fnName,
062: String attrName, String setVal) {
063: String line = null;
064: for (int i = 0; i < outFilterConf.size(); i++) {
065: line = (String) outFilterConf.get(i);
066: if (line.startsWith(type)) {
067: OrderedMap map = null;
068: try {
069: map = PBlock.str2pblock(line);
070: } catch (Exception e) {
071: continue;
072: }
073:
074: String fn = (String) map.get("fn");
075: if (fn != null && fn.equalsIgnoreCase(fnName)) {
076: map.put(attrName, setVal);
077: String newLine = null;
078: try {
079: newLine = PBlock.pblock2str(map);
080: } catch (Exception e) {
081: }
082:
083: /* Iterator it = map.keySet().iterator();
084: StringBuffer sb = new StringBuffer(type);
085: while (it.hasNext()) {
086: String key = (String) it.next();
087: String value = (String) map.get(key);
088: sb.append(" " + key + "=" + value);
089: }
090: String newLine = sb.toString();
091:
092: */
093: if (newLine != null) {
094: outFilterConf.remove(i);
095: outFilterConf.add(i, newLine);
096: }
097: return;
098: }
099: }
100: }
101: }
102:
103: public void updateFilterConf() {
104: try {
105: FileOutputStream fout = new FileOutputStream(server_root
106: + File.separator + "config" + File.separator
107: + "filter.conf");
108: PrintWriter out = new PrintWriter(fout, true);
109: for (int i = 0; i < outFilterConf.size(); i++) {
110: out.println((String) outFilterConf.get(i));
111: }
112: out.close();
113: } catch (Exception E) {
114: //CSDebug.logln("cannot write to filter.conf file");
115: }
116:
117: }
118:
119: }
|