001: package com.sun.portal.search.admin.util;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.text.*;
006: import com.sun.portal.search.soif.*;
007: import com.sun.portal.search.rdm.*;
008: import java.text.SimpleDateFormat;
009: import com.sun.portal.search.util.SearchConfig;
010: import com.sun.portal.util.Platform;
011:
012: public class DBUtil {
013:
014: static String getListCmd = "run-cs-cli rdmgr -y root -U";
015: static String getRDCountCmd = "/run-cs-cli rdmgr -n -y ";
016: static HashMap dbList = null;
017: static HashMap lastUpdated = null;
018: public static SimpleDateFormat df = new SimpleDateFormat(
019: "EEE, d MMM yyyy HH:mm:ss z");
020: public static final String STAT_LAST_MODIFIED = "DbStat-LastModified";
021: public static final String STAT_NB_SERVER = "DbStat-NbServer";
022: public static final String STAT_HREF = "DbStat-Href";
023: public static final String STAT_HREF_COUNT = "DbStat-Href-count";
024: public static final String STAT_NB_PROTOCOL = "DbStat-NbProtocol";
025: public static final String STAT_PROTOCOL = "DbStat-Protocol";
026: public static final String STAT_PROTOCOL_COUNT = "DbStat-Protocol-count";
027:
028: public DBUtil() {
029: }
030:
031: static public List getDBList(String server_root) throws Exception {
032: if (dbList == null) {
033: dbList = new HashMap();
034: }
035: if (lastUpdated == null) {
036: lastUpdated = new HashMap();
037: }
038:
039: Date d = (Date) lastUpdated.get(server_root);
040: if (d != null) {
041: File f = new File(server_root + File.separator + "db"
042: + File.separator + "root" + File.separator
043: + "rd.db");
044: if (f.exists()) {
045: if (d.after(new Date(f.lastModified()))) {
046: ArrayList al = (ArrayList) dbList.get(server_root);
047: if (al != null) {
048: return al;
049: }
050: }
051: }
052: }
053:
054: ArrayList al = new ArrayList();
055: String command = server_root + File.separator + getListCmd;
056: Process p = exec(command);
057: SOIFInputStream is = new SOIFInputStream(p.getInputStream(),
058: "UTF-8");
059: while (!is.isEOS()) {
060: SOIF s1 = is.readSOIF();
061: if (s1 != null) {
062: if (!s1.getURL().equals("root")) {
063: int i = 0;
064: while (i < al.size()) {
065: SOIF s2 = (SOIF) al.get(i++);
066: if (s2.getURL().compareTo(s1.getURL()) > 0) {
067: break;
068: }
069: }
070: String dbClass = s1.getValue("class");
071: // only add native database
072: if (dbClass == null
073: || dbClass.equals("")
074: || dbClass
075: .equals("com.sun.portal.search.db.IndexedSOIFDb")) {
076: al.add(i, s1);
077: }
078: }
079: }
080: }
081:
082: dbList.put(server_root, al);
083: lastUpdated.put(server_root, new Date());
084:
085: return al;
086: }
087:
088: static public String[] getDBStringArray(String server_root)
089: throws Exception {
090: List dbs = getDBList(server_root);
091: String[] names = new String[dbs.size()];
092: for (int i = 0; i < dbs.size(); i++) {
093: SOIF s = (SOIF) dbs.get(i);
094: names[i] = s.getURL();
095: }
096: return names;
097: }
098:
099: static public boolean updateSoif(String server_root, SOIF s) {
100: try {
101: String tmpFile = server_root + File.separator + "tmp"
102: + File.separator + "_tmpDBUtil."
103: + Long.toString(System.currentTimeMillis());
104: SOIFOutputStream so;
105: so = new SOIFOutputStream(tmpFile);
106: so.write(s);
107: so.close();
108: String cmd = server_root
109: + "/run-cs-cli rdmgr -D -y root -m -P -q "
110: + tmpFile;
111: Runtime rt = Runtime.getRuntime();
112: Process ps = exec(cmd);
113: ps.waitFor();
114: if (ps.exitValue() == 0) {
115: return true;
116: }
117: } catch (Exception e) {
118:
119: }
120: return false;
121: }
122:
123: static public boolean replaceSoif(String server_root, SOIF s) {
124: try {
125: String tmpFile = server_root + File.separator + "tmp"
126: + File.separator + "_tmpDBUtil."
127: + Long.toString(System.currentTimeMillis());
128: SOIFOutputStream so;
129: so = new SOIFOutputStream(tmpFile);
130: so.write(s);
131: so.close();
132: String cmd = server_root
133: + "/run-cs-cli rdmgr -D -y root -u -P -q "
134: + tmpFile;
135: Runtime rt = Runtime.getRuntime();
136: Process ps = exec(cmd);
137: ps.waitFor();
138: if (ps.exitValue() == 0) {
139: return true;
140: }
141: } catch (Exception e) {
142:
143: }
144: return false;
145: }
146:
147: static synchronized public boolean isDBExist(String server_root,
148: String dbName) throws Exception {
149: List dbs = DBUtil.getDBList(server_root);
150: for (int i = 0; i < dbs.size(); i++) {
151: SOIF s = (SOIF) dbs.get(i);
152: if (s.getURL().compareToIgnoreCase(dbName) == 0) {
153: return true;
154: }
155: }
156: return false;
157: }
158:
159: static synchronized public boolean addNewDB(String server_root,
160: String dbName, String dbTitle, String dbDesc)
161: throws Exception {
162: List dbs = DBUtil.getDBList(server_root);
163: for (int i = 0; i < dbs.size(); i++) {
164: SOIF s = (SOIF) dbs.get(i);
165: if (s.getURL().compareToIgnoreCase(dbName) == 0) {
166: return false;
167: }
168: }
169: String cmd = server_root + "/run-cs-cli rdmgr -y " + dbName
170: + " -n";
171: Runtime rt = Runtime.getRuntime();
172: Process ps = exec(cmd);
173: ps.waitFor();
174: if (ps.exitValue() != 0) {
175: return false;
176: }
177: SOIF newDB = new SOIF("Database", dbName);
178: newDB.insert(RDM.A_RDM_DESC, dbDesc);
179: newDB.insert("Title", dbTitle);
180: return updateSoif(server_root, newDB);
181: }
182:
183: static public SOIF getDBSoif(String server_root, String dbName) {
184: try {
185: List soifs = getDBList(server_root);
186: for (int i = 0; i < soifs.size(); i++) {
187: SOIF s = (SOIF) soifs.get(i);
188: if (s.getURL().equals(dbName)) {
189: return s;
190: }
191: }
192: } catch (Exception e) {
193: }
194: return null;
195: }
196:
197: static synchronized public boolean updateDB(String server_root,
198: String dbName, String dbTitle, String dbDesc)
199: throws Exception {
200: SOIF s = getDBSoif(server_root, dbName);
201: if (s != null) {
202: s.replace(RDM.A_RDM_DESC, dbDesc);
203: s.replace("Title", dbTitle);
204: return updateSoif(server_root, s);
205: }
206: return false;
207: }
208:
209: static synchronized public boolean updateDB(String server_root,
210: String dbName, Date timeStamp, Hashtable listServer,
211: Hashtable listProtocol) throws Exception {
212: SOIF s = getDBSoif(server_root, dbName);
213: if (s != null) {
214: int i;
215: try {
216: //remove existing statistic attributes
217: String count = s.getValue(STAT_NB_SERVER);
218: if (count != null) {
219: for (i = 0; i < Integer.parseInt(count); i++) {
220: s.remove(STAT_HREF, i);
221: s.remove(STAT_HREF_COUNT, i);
222: }
223: }
224: count = s.getValue(STAT_NB_PROTOCOL);
225: if (count != null) {
226: for (i = 0; i < Integer.parseInt(count); i++) {
227: s.remove(STAT_PROTOCOL, i);
228: s.remove(STAT_PROTOCOL_COUNT, i);
229: }
230: }
231: //add new statistics
232: s.replace(STAT_LAST_MODIFIED, (timeStamp != null) ? df
233: .format(timeStamp) : df.format(new Date()));
234: s.replace(STAT_NB_SERVER,
235: (listServer != null) ? Integer
236: .toString(listServer.size()) : Integer
237: .toString(0));
238: s.replace(STAT_NB_PROTOCOL,
239: (listProtocol != null) ? Integer
240: .toString(listProtocol.size())
241: : Integer.toString(0));
242: Enumeration en = null;
243: String attrib = null;
244: if (listServer != null) {
245: en = listServer.keys();
246: i = 0;
247: while (en.hasMoreElements()) {
248: s.insert(STAT_HREF, (String) en.nextElement(),
249: i);
250: i++;
251: }
252: en = listServer.elements();
253: i = 0;
254: while (en.hasMoreElements()) {
255: s.insert(STAT_HREF_COUNT, (String) en
256: .nextElement(), i);
257: i++;
258: }
259: }
260: if (listProtocol != null) {
261: en = listProtocol.keys();
262: i = 0;
263: while (en.hasMoreElements()) {
264: s.insert(STAT_PROTOCOL, (String) en
265: .nextElement(), i);
266: i++;
267: }
268: en = listProtocol.elements();
269: i = 0;
270: while (en.hasMoreElements()) {
271: s.insert(STAT_PROTOCOL_COUNT, (String) en
272: .nextElement(), i);
273: i++;
274: }
275: }
276: //actually updating the Soif DB
277: return replaceSoif(server_root, s);
278: } catch (Exception e) {
279: return false;
280: }
281: } else {
282: return false;
283: }
284: }
285:
286: static public int getRDCount(String server_root, String dbname)
287: throws Exception {
288: int count = 0;
289: String cmd = server_root + File.separator + getRDCountCmd
290: + dbname;
291: Runtime rt = Runtime.getRuntime();
292: Process ps = exec(cmd);
293: BufferedReader buf = new BufferedReader(new InputStreamReader(
294: ps.getInputStream()));
295: String line = buf.readLine();
296: NumberFormat nf = NumberFormat.getInstance();
297: count = nf.parse(line).intValue();
298: return count;
299:
300: }
301:
302: static synchronized boolean executeRDMgr(String cmd, Writer out) {
303: boolean res = false;
304: Runtime rt = Runtime.getRuntime();
305: try {
306: Process process = exec(cmd);
307: BufferedReader buf = new BufferedReader(
308: new InputStreamReader(process.getInputStream()));
309: String outLine = null;
310: while ((outLine = buf.readLine()) != null) {
311: //CSDebug.logln("outLine=" + outLine);
312: if (out != null) {
313: out.write(outLine + "\n");
314: out.flush();
315: }
316: }
317: res = process.waitFor() == 0;
318: } catch (Exception e) {
319: // XXX Not logging exceptions? XXX
320: //CSDebug.logln("rt.exec Exception:" + e.getMessage());
321: }
322: return res;
323:
324: }
325:
326: static synchronized BufferedReader executeRDMgr(String cmd) {
327: BufferedReader buf = null;
328: Runtime rt = Runtime.getRuntime();
329: //CSDebug.logln("rt.exec " + cmd);
330: try {
331: Process process = exec(cmd);
332: buf = new BufferedReader(new InputStreamReader(process
333: .getInputStream()));
334: } catch (Exception e) {
335: // XXX Not logging exceptions? XXX
336: //CSDebug.logln("rt.exec Exception:" + e.getMessage());
337: }
338: return buf;
339: }
340:
341: static public boolean purgeDatabase(String server_root,
342: String dbname, Writer out) {
343: String cmd = server_root + File.separator
344: + "run-cs-cli rdmgr -X -p stdout -y " + dbname;
345: return executeRDMgr(cmd, out);
346: }
347:
348: static public boolean reindexDatabase(String server_root,
349: String dbname, Writer out) {
350: String cmd = server_root + File.separator
351: + "run-cs-cli rdmgr -I -p stdout -y " + dbname;
352: return executeRDMgr(cmd, out);
353: }
354:
355: static public boolean expireDatabase(String server_root,
356: String dbname, Writer out) {
357: String cmd = server_root + File.separator
358: + "run-cs-cli rdmgr -E -p stdout -y " + dbname;
359: return executeRDMgr(cmd, out);
360: }
361:
362: static public boolean deleteDatabase(String server_root,
363: String dbname, Writer out) {
364: String cmd = server_root + File.separator
365: + "run-cs-cli rdmgr -B -p stdout -y " + dbname;
366: return executeRDMgr(cmd, out);
367: }
368:
369: static public BufferedReader urlStatDatabase(String server_root,
370: String dbname) {
371: String cmd = server_root + File.separator
372: + "run-cs-cli rdmgr -L -a url -y " + dbname;
373: return executeRDMgr(cmd);
374: }
375:
376: static public boolean dbStatOnFile(String server_root, String dbname) {
377: boolean ret = false;
378: try {
379: ret = (DBUtil.getDBSoif(server_root, dbname).getValue(
380: STAT_LAST_MODIFIED) != null);
381: } catch (Exception e) {
382: //CSDebug.logln("DBUtil-dbStatOnFile("+server_root+","+dbname+")");
383: }
384: return ret;
385:
386: }
387:
388: static public boolean indexCategories(String server_root) {
389: boolean ret = true;
390: String cmd[] = {
391: server_root + File.separator
392: + "run-cs-cli rdmgr -p stdout -T -X",
393: server_root + File.separator
394: + "run-cs-cli rdmgr -TV -p stdout "
395: + SearchConfig.getValue(SearchConfig.TAX) };
396: for (int i = 0; i < 2; i++) {
397: ret = executeRDMgr(cmd[i], null);
398: if (ret != true) {
399: break;
400: }
401: }
402: return ret;
403: }
404:
405: static public boolean isWindows() {
406: if (Platform.name.startsWith("windows")) {
407: return true;
408: } else {
409: return false;
410: }
411: }
412:
413: static String RUN_CS_CLI = "run-cs-cli";
414: static String RUN_CS_CLI_BAT = "run-cs-cli.bat";
415:
416: /*
417: TODO: Note: rt.exec() should be used instead of this after the code is tweaked
418: to be OS independent. This function merely to minimizes impact of changes.
419: */
420: static public Process exec(String command) throws IOException {
421: Runtime rt = Runtime.getRuntime();
422: Process process = null;
423: if (isWindows()) {
424: //Takes care of problematic back-slash '\' charectors
425: command = command.replace('\\', '/');
426: //Take care of executing bat files by replaceing 'run-cs-cli' with 'run-cs-cli.bat'
427: int idx = command.indexOf(RUN_CS_CLI);
428: StringBuffer buf = new StringBuffer(command.substring(0,
429: idx));
430: buf.append(RUN_CS_CLI_BAT);
431: buf.append(command.substring(idx + RUN_CS_CLI.length()));
432: command = buf.toString();
433: //CSDebug.logln("command:"+ command);
434: process = rt.exec(command);
435: } else {
436: process = rt.exec(command);
437: }
438: return process;
439: }
440: }
|