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.util.*;
009: import java.util.logging.Level;
010: import java.util.logging.Logger;
011: import java.io.*;
012:
013: import com.sun.portal.search.util.PBlock;
014: import com.sun.portal.log.common.PortalLogger;
015:
016: /**
017: * General purpose configuration parameters
018: */
019: public class ImportConfig {
020: private static Logger debugLogger = PortalLogger
021: .getLogger(ImportConfig.class);
022:
023: static public void main(String[] args) {
024: try {
025: init(args[0]);
026: } catch (Exception e) {
027: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
028: .getMessage());
029: }
030: updateFile(args[0] + ".new");
031: }
032:
033: // internal used value
034: public static final String QL_GATHERER = "gatherer";
035: public static final String QL_COMPASS = "compass";
036: public static final String QL_SEARCH = "search";
037: public static final String TYPE_LOCALFILE = "localfile";
038: public static final String TYPE_COMPASS = "cs";
039:
040: // File defaults
041: public static final String CONFDIR = "config";
042: public static final String IMPORT_CONF = "import.conf";
043:
044: // header for import.conf
045: static final String header = "# Written automatically by importmgr -- DO NOT EDIT";
046:
047: // Attributes in <import .......>
048: public static final String ID = "id";
049: public static final String NAME = "name";
050:
051: // Attributes between tags <import > ......</import>
052: public static final String CSID = "csid";
053: public static final String SEARCH_URI = "search-uri";
054: public static final String SEARCH_DB = "search-db";
055: public static final String LOCAL_DB = "local-db";
056: public static final String USE_RDM_INCOMING = "use-rdm-incoming";
057: public static final String SRCFILE = "srcfile";
058: public static final String RDM_QUERY_LANGUAGE = "rdm-query-language";
059: public static final String DESTINATION_CSID = "destination-csid";
060: public static final String ENABLE = "enable";
061: public static final String TIME_STAMP = "last-collection-time";
062: public static final String EMAIL = "email";
063: public static final String USER = "user";
064: public static final String PASSWORD = "password";
065: public static final String VIEW_ATTRIBUTES = "view-attributes";
066: public static final String VIEW_HITS = "view-hits";
067: public static final String VIEW_ORDER = "view-order";
068: public static final String SCOPE = "scope";
069: public static final String NICKNAME = "nickname";
070: public static final String TIMEOUT = "timeout";
071:
072: private static final String[] use4equalFields = { CSID, LOCAL_DB,
073: SRCFILE, RDM_QUERY_LANGUAGE, VIEW_ATTRIBUTES, SCOPE };
074:
075: //Makeup Attributes
076: //public static final String USE_GATHER = "use_gatherer";
077: public static final String US_AUTH = "use-auth";
078: public static final String AGENT_TYPE = "agent-type";
079: public static final String SERVER_PORT = "server";
080: public static final String INSTANCE_NAME = "instance-name";
081: public static final String IS_SSL = "is-ssl";
082: public static final String IS_COMPASS = "is-compass";
083: public static final String CHARSET = "charset";
084:
085: HashMap valuesMap = new HashMap();
086: int id = 0;
087:
088: static int lastID = 0;
089: static long fileLastModified = 0;
090:
091: static String default_csid;
092: static String fileName = null;
093: static ArrayList importAgents = new ArrayList(); // singleton
094: static HashMap importAgentsMap = new HashMap(); // singleton
095:
096: /* hidden */
097: private ImportConfig(int id, HashMap map) {
098: this .id = id;
099: valuesMap = map;
100: }
101:
102: /**
103: * Load import.conf configuration file
104: * This version only supports one csid
105: */
106: static public void init(String filename) throws Exception {
107: fileName = filename;
108: try {
109: File file = new File(filename);
110: fileLastModified = file.lastModified();
111: BufferedReader in = new BufferedReader(
112: new InputStreamReader(new FileInputStream(file),
113: "UTF-8"));
114: String line = in.readLine();
115: boolean inTag = false;
116: HashMap currAgent = null;
117: while (line != null) {
118: String nline = line.trim();
119: if (inTag) {
120: if (nline.startsWith("</Import>")) {
121: String id = (String) currAgent.get("id");
122: if (id != null) {
123: int currID = Integer.parseInt(id);
124: if (currID > lastID) {
125: lastID = currID;
126: }
127: ImportConfig impCFG = new ImportConfig(
128: Integer.parseInt(id), currAgent);
129: importAgents.add(impCFG);
130: importAgentsMap.put(id, impCFG);
131: inTag = false;
132: }
133: } else {
134: if (nline.startsWith("#")) {
135: line = in.readLine();
136: continue;
137: }
138: PBlock.str2pblock(nline, currAgent);
139: }
140: } else if (nline.startsWith("<Import ")
141: && nline.endsWith(">")) {
142: inTag = true;
143: currAgent = new HashMap();
144: String values = nline.substring(8,
145: nline.length() - 1);
146: PBlock.str2pblock(values, currAgent);
147: }
148:
149: line = in.readLine();
150: }
151: } catch (Exception e) {
152: //ignore
153: }
154: }
155:
156: static void reInit() {
157: try {
158: File file = new File(fileName);
159: long lastmodified = file.lastModified();
160: if (lastmodified <= fileLastModified) {
161: return;
162: }
163: lastID = 0;
164: importAgents.clear();
165: importAgentsMap.clear();
166: init(fileName);
167: } catch (Exception e) {
168: //ignore
169: }
170: }
171:
172: static public void updateFile() {
173: updateFile(fileName);
174: }
175:
176: static public void updateFile(String filename) {
177: PrintWriter out = null;
178: try {
179: FileOutputStream fout = new FileOutputStream(filename);
180: out = new PrintWriter(new BufferedWriter(
181: new OutputStreamWriter(fout, "UTF-8")), true);
182: out.println(header);
183: for (int i = 0; i < importAgents.size(); i++) {
184: ImportConfig impCfg = (ImportConfig) importAgents
185: .get(i);
186: impCfg.print(out);
187: out.println();
188: }
189: out.close();
190: File file = new File(filename);
191: fileLastModified = file.lastModified();
192: } catch (Exception e) {
193: return;
194: }
195:
196: }
197:
198: synchronized static public ImportConfig newImportAgent() {
199: HashMap map = new HashMap();
200: int id = lastID + 1;
201: map.put(ImportConfig.ID, Integer.toString(id));
202: ImportConfig imp = new ImportConfig(id, map);
203: //importAgents.add(imp);
204: //importAgentsMap.put(Integer.toString(id), imp);
205: lastID++;
206: return imp;
207: }
208:
209: static public boolean addImportAgent(ImportConfig imp) {
210: for (int i = 0; i < importAgents.size(); i++) {
211: ImportConfig impc = (ImportConfig) importAgents.get(i);
212: if (impc.isEqualTo(imp)) {
213: return false;
214: }
215: }
216: importAgents.add(imp);
217: importAgentsMap.put(Integer.toString(imp.id), imp);
218: return true;
219:
220: }
221:
222: /** @return all the ImportConfig in an ArrayList object */
223: static public ArrayList getImportAgents() {
224: reInit();
225: return importAgents;
226: }
227:
228: /** @return the ImportConfig by index */
229: static public ImportConfig getImportConfigByIndex(int i) {
230: reInit();
231: if (i >= 0 && i < importAgents.size()) {
232: return (ImportConfig) importAgents.get(i);
233: }
234: return null;
235: }
236:
237: /** Delete ImportConfig by index */
238: static public boolean DeleteImportConfigByIndex(int i) {
239: if (i >= 0 && i < importAgents.size()) {
240: ImportConfig imp = (ImportConfig) importAgents.remove(i);
241: if (imp != null) {
242: if (importAgentsMap.remove(imp
243: .getValue(ImportConfig.ID)) != null) {
244: return true;
245: }
246: }
247: }
248: return false;
249: }
250:
251: /** Delete ImportConfig by ID */
252: static public boolean DeleteImportConfigByID(String id) {
253: ImportConfig imp = (ImportConfig) importAgentsMap.remove(id);
254: if (imp != null) {
255: importAgents.remove(imp);
256: return true;
257: }
258: return false;
259: }
260:
261: /** @return the ImportConfig by id */
262: static public ImportConfig getImportConfigByID(String id) {
263: reInit();
264: return (ImportConfig) importAgentsMap.get(id);
265: }
266:
267: /** @return the number of ImportConfigs */
268: static public int numOfImportConfig() {
269: reInit();
270: return importAgents.size();
271: }
272:
273: /** Set the default CSID - XXX not checking valid CSID */
274: public void setDefault(String csid) {
275: default_csid = csid;
276: }
277:
278: /** Get the default CSID */
279: public String getDefault() {
280: return default_csid;
281: }
282:
283: /** Get the default */
284: public String getID() {
285: return (String) getValue(ID);
286: }
287:
288: /**
289: * Find a configuration parameter based on the default CSID.
290: */
291: public String getValue(String param) {
292: if (param.compareToIgnoreCase(RDM_QUERY_LANGUAGE) == 0) {
293: String value = (String) valuesMap.get(RDM_QUERY_LANGUAGE);
294: if (value != null && value.compareTo(QL_GATHERER) != 0) {
295: return QL_SEARCH;
296: }
297: return QL_GATHERER;
298: }
299: if (param.compareToIgnoreCase(US_AUTH) == 0) {
300: String value = (String) valuesMap.get(USER);
301: if (value != null) {
302: return "true";
303: }
304: return "false";
305: }
306: if (param.compareToIgnoreCase(AGENT_TYPE) == 0) {
307: String value = (String) valuesMap.get(CSID);
308: if (value != null && value.length() > 0) {
309: return TYPE_COMPASS;
310: }
311: return TYPE_LOCALFILE;
312: }
313: /* x-catalogs://pearl16.red.iplanet.com:3001/naren-robot */
314: if (param.compareToIgnoreCase(SERVER_PORT) == 0) {
315: String value = (String) valuesMap.get(CSID);
316: if (value != null && value.length() > 0) {
317: int start = value.indexOf("//") + 2;
318: int end = value.lastIndexOf('/');
319: return value.substring(start, end);
320: }
321: return "";
322: }
323: if (param.compareToIgnoreCase(INSTANCE_NAME) == 0) {
324: String value = (String) valuesMap.get(CSID);
325: if (value != null && value.length() > 0) {
326: int start = value.lastIndexOf('/') + 1;
327: return value.substring(start);
328: }
329: return "";
330: }
331: if (param.compareToIgnoreCase(IS_COMPASS) == 0) {
332: String value = (String) valuesMap.get(USE_RDM_INCOMING);
333: if (value != null) {
334: return "true";
335: }
336: return "false";
337: }
338: if (param.compareToIgnoreCase(IS_SSL) == 0) {
339: String value = (String) valuesMap.get(CSID);
340: if (value != null && value.length() > 0) {
341: if (value.startsWith("x-catalogs"))
342: return "true";
343: }
344: return "false";
345: }
346: return (String) valuesMap.get(param);
347: }
348:
349: /**
350: * sets a value in the CSID
351: */
352: public void setValue(Object key, Object value) {
353: if (valuesMap != null) {
354: if (valuesMap.containsKey(key)) {
355: valuesMap.remove(key);
356: }
357: valuesMap.put(key, value);
358: }
359: }
360:
361: /**
362: * remove AV
363: */
364: public void remove(Object key) {
365: if (valuesMap != null) {
366: if (valuesMap.containsKey(key)) {
367: valuesMap.remove(key);
368: }
369: }
370: }
371:
372: /**
373: * returning the map of config
374: */
375: public HashMap getMap() {
376: return (HashMap) valuesMap;
377: }
378:
379: public void print(PrintWriter out) {
380: String id = (String) valuesMap.get("id");
381: out.println("<Import id=" + id + ">");
382: Iterator it = valuesMap.keySet().iterator();
383: while (it.hasNext()) {
384: String key = (String) it.next();
385: if (key.compareToIgnoreCase("id") != 0) {
386: out.println(key + "=\"" + (String) valuesMap.get(key)
387: + "\"");
388: }
389: }
390: out.println("</Import>");
391: }
392:
393: public boolean isEqualTo(ImportConfig imp) {
394: for (int i = 0; i < use4equalFields.length; i++) {
395: String v1 = getValue(use4equalFields[i]);
396: String v2 = imp.getValue(use4equalFields[i]);
397: if ((v1 == null && v1 != null)
398: || (v1 != null && v2 == null)
399: || (v1 != null && v2 != null && !v1.equals(v2))) {
400: return false;
401: }
402: }
403: return true;
404:
405: }
406:
407: }
|