001: package com.sun.portal.search.rdmgr;
002:
003: import java.io.*;
004:
005: //import com.sun.portal.search.rdmgr.*;
006: //import org.w3c.util.*;
007:
008: public class SearchSetup {
009:
010: static final int TYPE_PERL = 0;
011: static final int TYPE_SHELL = 1;
012: static final int TYPE_JAVA = 2;
013: static final int HTTP_IAS = 0;
014: static final int HTTP_IWS = 1;
015: static String psConfigDirSol = "/etc/opt/SUNWportal";
016: static String psProductDirSol = "SUNWps";
017: static String psConfigDirLnx = "/etc/opt/sun/portal";
018: static String psProductDirLnx = "portal";
019: static String psProductDirHpux = "portal";
020: static String psConfigDirHpux = "/etc/opt/sun/portal";
021: static String psConfigPath = "/etc/opt/SUNWportal/PSConfig.properties";
022: String gJRE = null;
023:
024: boolean isEnable(String buf) {
025: if (buf.equalsIgnoreCase("true") || buf.equalsIgnoreCase("on")
026: || buf.equalsIgnoreCase("1")
027: || buf.equalsIgnoreCase("yes"))
028: return true;
029: else
030: return false;
031: }
032:
033: void copy(String from, String to) throws IOException {
034: InputStream in = null;
035: OutputStream out = null;
036: try {
037: in = new FileInputStream(from);
038: out = new FileOutputStream(to);
039: int length = in.available(); // danger! for large file
040: byte[] bytes = new byte[length];
041: in.read(bytes);
042: out.write(bytes);
043: } finally {
044: if (in != null) {
045: in.close();
046: }
047: if (out != null) {
048: out.close();
049: }
050: }
051: }
052:
053: void mkdir(String server_root, String child) throws Exception {
054: File childdir = new File(server_root, child);
055: if (childdir.isDirectory()) {
056: System.err.println("Directory already exists: " + childdir);
057: return;
058: }
059: if (!childdir.mkdir())
060: throw new Exception("Can not create directory: "
061: + server_root + File.separator + child);
062: }
063:
064: void copy_conf_file(String src_dir, String dest_dir,
065: String file_name) throws Exception {
066: copy(src_dir + File.separator + file_name, dest_dir
067: + File.separator + file_name);
068: }
069:
070: String replace(String line, String src, String dest) {
071: String buf = "";
072: int i = 0;
073: int p = 0;
074: while ((i = line.indexOf(src, p)) >= 0) {
075: buf += line.substring(p, i);
076: buf += dest;
077: p = i + src.length();
078: }
079: buf += line.substring(p, line.length());
080: return buf;
081: }
082:
083: void create_conf_file(String base_dir, String server_root,
084: String file_name, String port, String host, String nick,
085: String ps_product_dir) throws Exception {
086:
087: String src_dir = base_dir + File.separator + ps_product_dir
088: + File.separator + "samples" + File.separator
089: + "config";
090: String dest_dir = server_root + File.separator + "config";
091:
092: String cs_bin = base_dir + File.separator + ps_product_dir
093: + File.separator + "bin";
094:
095: String cs_lib = base_dir + File.separator + ps_product_dir
096: + File.separator + "lib";
097:
098: if (isWindows()) {
099: String separator = "/";
100: src_dir = base_dir + separator + "samples" + separator
101: + "config";
102:
103: cs_bin = base_dir + separator + "bin";
104:
105: cs_lib = base_dir + separator + "lib";
106: }
107:
108: BufferedReader in = null;
109: PrintWriter out = null;
110: try {
111: in = new BufferedReader(new FileReader(src_dir
112: + File.separator + file_name));
113: out = new PrintWriter(new BufferedWriter(new FileWriter(
114: dest_dir + File.separator + file_name)));
115: String line;
116: while ((line = in.readLine()) != null) {
117: line = replace(line, "$NICK", nick);
118: line = replace(line, "$HOST", host);
119: line = replace(line, "$PORT", port);
120: line = replace(line, "$CSROOT", server_root);
121: line = replace(line, "$CSBIN", cs_bin);
122: line = replace(line, "$CSLIB", cs_lib);
123: out.println(line);
124: }
125: } finally {
126: if (in != null) {
127: in.close();
128: }
129: if (out != null) {
130: out.close();
131: }
132: }
133: }
134:
135: void gen_server_rdm(String base_dir, String server_root,
136: String ps_product_dir) throws Exception {
137: String buildsd = base_dir + File.separator + ps_product_dir
138: + File.separator + "lib" + File.separator + "buildsd";
139: if (isWindows()) {
140: buildsd = base_dir + File.separator + "lib"
141: + File.separator + "buildsd";
142: }
143: String cmds[] = { buildsd };
144:
145: String dest = server_root + File.separator + "config"
146: + File.separator + "server.rdm";
147:
148: Process proc = Runtime.getRuntime().exec(cmds);
149: BufferedReader br = new BufferedReader(new InputStreamReader(
150: proc.getInputStream()));
151: PrintWriter out = new PrintWriter(new BufferedWriter(
152: new FileWriter(dest)));
153: String s;
154: while ((s = br.readLine()) != null) {
155: out.println(s);
156: }
157: out.close();
158: br.close();
159: }
160:
161: void gen_script(String base_dir, String server_root,
162: int script_type, int http_type, String file_name,
163: String command_name, String ps_product_dir)
164: throws Exception {
165: String perl_cli = "perl";
166: String win_base_dir = base_dir.replace('/', '\\');
167: base_dir = "\"" + base_dir + "\"";
168:
169: /* this is not correct anymore in 6.2, but perl_cli is never used.
170: if (http_type == HTTP_IWS) {
171: perl_cli = base_dir + File.separator + "SUNWam" +
172: File.separator + "servers" + File.separator + "bin" +
173: File.separator + "https" + File.separator + "https" +
174: File.separator + "perl";
175: }
176: else {
177: perl_cli = base_dir + File.separator + "install" +
178: File.separator + "perl";
179: }
180: */
181: String kt_search_lib = "/usr/share/lib/";
182: String jss_nss_nspr_lib = "/usr/lib/mps/secv1";
183: String jss_nss_nspr_cert7_lib = "/usr/lib/mps";
184:
185: if (isHP_UX()) {
186: kt_search_lib = "/opt/sun/private/share/lib/";
187: jss_nss_nspr_lib = "/opt/sun/private/lib";
188: jss_nss_nspr_cert7_lib = "/opt/sun/private/lib";
189: }
190: if (isLinux()) {
191: kt_search_lib = "/opt/sun/lib/";
192: jss_nss_nspr_lib = "/opt/sun/private/lib";
193: jss_nss_nspr_cert7_lib = "/opt/sun/private/lib";
194: }
195:
196: String cs_bin = base_dir + File.separator + ps_product_dir
197: + File.separator + "bin" + File.separator;
198:
199: String cs_java = base_dir + File.separator + ps_product_dir
200: + File.separator + "lib" + File.separator;
201: String cs_jars = " -cp " + cs_java + "searchadmin.jar:"
202: + cs_java + "searchserver.jar:" + cs_java + "db.jar:"
203: + kt_search_lib + "ktsearch.jar ";
204:
205: String cs_lib = base_dir + File.separator + ps_product_dir
206: + File.separator + "lib" + File.separator;
207: if (isWindows()) {
208: cs_bin = win_base_dir + File.separator + "bin"
209: + File.separator;
210:
211: cs_java = win_base_dir + File.separator + "lib"
212: + File.separator;
213: cs_jars = " -cp \"" + cs_java + "searchadmin.jar;"
214: + cs_java + "searchserver.jar;" + cs_java
215: + "db.jar;" + cs_java + "ktsearch.jar\" ";
216: cs_lib = win_base_dir + File.separator + "lib"
217: + File.separator;
218: }
219:
220: String script_name;
221:
222: script_name = server_root + File.separator + file_name;
223: PrintWriter out = null;
224: try {
225: out = new PrintWriter(new BufferedWriter(new FileWriter(
226: script_name)));
227: if (isWindows()) {
228: out.println("@echo off");
229: out.println("");
230: out.println("");
231: } else {
232: out.write("#!/bin/sh\n\n");
233: }
234: if (script_type == TYPE_PERL) {
235: out.println("cd \"" + server_root + "\"; " + perl_cli
236: + " " + cs_lib + command_name);
237: } else if (script_type == TYPE_JAVA) {
238: if (isHP_UX()) {
239: out.println("SHLIB_PATH=" + cs_lib + ":"
240: + jss_nss_nspr_lib + ":"
241: + jss_nss_nspr_cert7_lib + ":/usr/lib/lwp"
242: + ":${SHLIB_PATH}");
243: out.println("export SHLIB_PATH");
244: } else if (isWindows()) {
245:
246: out.println("set PATH=" + cs_lib + ";%PATH%");
247: } else {
248: out.println("LD_LIBRARY_PATH=" + cs_lib + ":"
249: + jss_nss_nspr_lib + ":"
250: + jss_nss_nspr_cert7_lib + ":/usr/lib/lwp"
251: + ":${LD_LIBRARY_PATH}");
252: out.println("export LD_LIBRARY_PATH");
253: }
254: String envVars = "";
255: if (isWindows()) {
256: //StartRobot,StopRobot need some additional environment variables set.
257: String winDir = "C:\\WINNT";
258: if (System.getProperty("os.name").equals(
259: "Windows XP"))
260: winDir = "C:\\WINDOWS";
261:
262: //SYSTEMROOT var is used by robot nspr socket libraries
263: String SYSTEMROOT = "SYSTEMROOT";
264: String sysRootVal = System.getProperty(SYSTEMROOT);
265: if (sysRootVal == null) {
266: System.out
267: .println("SearchSetup: got SYSTEMROOT is null");
268: sysRootVal = winDir;
269: }
270: out.println("set SYSTEMROOT=" + sysRootVal);
271:
272: //ComSpec var is used by rdregister to run rdmgr.bat
273: String COMSPEC = "ComSpec";
274: String comSpecVal = System.getProperty(COMSPEC);
275: if (comSpecVal == null)
276: comSpecVal = winDir + "\\system32\\cmd.exe";
277: out.println("set COMSPEC=" + comSpecVal);
278:
279: envVars = " -DSYSTEMROOT=%SYSTEMROOT% -DCOMSPEC=%COMSPEC% ";
280: }
281: String java = (gJRE == null ? "java" : gJRE
282: + File.separator + "bin" + File.separator
283: + "java");
284: out.println(java + cs_jars + envVars + command_name);
285: } else {
286: if (isWindows()) {
287: out.println("set PATH=" + cs_bin + ";" + cs_lib
288: + ";" + cs_lib + "filter;%PATH%");
289: //Note : on windows os, for "cd" file seperators need to be back-slashes
290: String server_root_windows_style = server_root
291: .replace('/', '\\');
292: out.println("cd \"" + server_root_windows_style
293: + "\"");
294: String win_drive = null;
295: int win_drive_idx = server_root_windows_style
296: .indexOf(':');
297: if (win_drive_idx != -1) {
298: win_drive = server_root_windows_style
299: .substring(0, win_drive_idx + 1);
300: out.println(win_drive);
301: }
302: out.println(command_name);
303: }
304:
305: else if (isHP_UX()) {
306: out.println("SHLIB_PATH=" + cs_lib + ":" + cs_lib
307: + "filter" + ":" + jss_nss_nspr_lib
308: + ":/usr/lib/mps:/usr/lib/lwp"
309: + ":${SHLIB_PATH}");
310: out.println("export SHLIB_PATH");
311: out.println("PATH=" + cs_bin + ":" + cs_lib
312: + ":${PATH}");
313: out.println("export PATH");
314: out.println("cd \"" + server_root + "\"; "
315: + command_name);
316: }
317:
318: else {
319: out.println("LD_LIBRARY_PATH=" + cs_lib + ":"
320: + cs_lib + "filter" + ":"
321: + jss_nss_nspr_lib
322: + ":/usr/lib/mps:/usr/lib/lwp"
323: + ":${LD_LIBRARY_PATH}");
324: out.println("export LD_LIBRARY_PATH");
325: out.println("PATH=" + cs_bin + ":" + cs_lib
326: + ":${PATH}");
327: out.println("export PATH");
328: out.println("cd \"" + server_root + "\"; "
329: + command_name);
330: }
331: }
332: } finally {
333: if (out != null) {
334: out.close();
335: }
336: }
337: if (!isWindows()) {
338: String cmd[] = { "chmod", "700", script_name };
339: Runtime.getRuntime().exec(cmd);
340: }
341: }
342:
343: public void setup_instance(String base_dir, String server_root,
344: int http_type, String server_host, String server_port,
345: String server_nick) throws Exception {
346:
347: String ps_product_dir = psProductDirSol;
348: String psdir = base_dir + File.separator + ps_product_dir;
349:
350: if (isHP_UX()) {
351: psdir = base_dir + File.separator + psProductDirHpux;
352: psConfigPath = psConfigDirHpux + File.separator
353: + "PSConfig.properties";
354: ps_product_dir = psProductDirHpux;
355: }
356: if (isLinux()) {
357: psdir = base_dir + File.separator + psProductDirLnx;
358: psConfigPath = psConfigDirLnx + File.separator
359: + "PSConfig.properties";
360: ps_product_dir = psProductDirLnx;
361: }
362: if (isWindows()) {
363: psdir = base_dir;
364: psConfigPath = base_dir + File.separator + "config"
365: + File.separator + "PSConfig.properties";
366: }
367: String confsrcdir = psdir + File.separator + "samples"
368: + File.separator + "config";
369: String confdestdir = server_root + File.separator + "config";
370:
371: String jre = null;
372: BufferedReader in = null;
373: try {
374: // rdmgr is setup during install time
375: in = new BufferedReader(new FileReader(psConfigPath));
376: String line;
377: while ((line = in.readLine()) != null) {
378: if (line.startsWith("JDK_DIR="))
379: jre = line.substring(8).trim();
380: }
381: in.close();
382: } catch (Exception e) {
383: if (in != null)
384: in.close();
385: }
386: gJRE = jre;
387:
388: // server should be stopped
389: try {
390: // it should be run by the server user -- no chown
391: mkdir(server_root, "config");
392: mkdir(server_root, "logs");
393: mkdir(server_root, "robot");
394: mkdir(server_root, "tmp");
395: mkdir(server_root, "db");
396:
397: gen_server_rdm(base_dir, server_root, ps_product_dir);
398: /* XXX obsolated
399: gen_script(base_dir, server_root, TYPE_PERL, http_type,
400: "start-robot", "start_robot");
401: gen_script(base_dir, server_root, TYPE_PERL, http_type,
402: "stop-robot", "stop_robot");
403: */
404: if (isWindows()) {
405: gen_script(base_dir, server_root, TYPE_SHELL,
406: http_type, "run-cs-cli.bat", "%*",
407: ps_product_dir);
408: gen_script(base_dir, server_root, TYPE_JAVA, http_type,
409: "StartRobot.bat",
410: "com.sun.portal.search.admin.cli.RobotController \""
411: + server_root + "\" start",
412: ps_product_dir);
413: gen_script(base_dir, server_root, TYPE_JAVA, http_type,
414: "StopRobot.bat",
415: "com.sun.portal.search.admin.cli.RobotController \""
416: + server_root + "\" stop",
417: ps_product_dir);
418: } else {
419: gen_script(base_dir, server_root, TYPE_SHELL,
420: http_type, "run-cs-cli", "\"$@\"",
421: ps_product_dir);
422: gen_script(base_dir, server_root, TYPE_JAVA, http_type,
423: "StartRobot",
424: "com.sun.portal.search.admin.cli.RobotController \""
425: + server_root + "\" start",
426: ps_product_dir);
427: gen_script(base_dir, server_root, TYPE_JAVA, http_type,
428: "StopRobot",
429: "com.sun.portal.search.admin.cli.RobotController \""
430: + server_root + "\" stop",
431: ps_product_dir);
432: }
433: // XXX dbswitch.conf for pks
434:
435: create_conf_file(base_dir, server_root, "search.conf",
436: server_port, server_host, server_nick,
437: ps_product_dir);
438: create_conf_file(base_dir, server_root, "robot.conf",
439: server_port, server_host, server_nick,
440: ps_product_dir);
441: create_conf_file(base_dir, server_root, "filter.conf",
442: server_port, server_host, server_nick,
443: ps_product_dir);
444:
445: // in case substitutions are required
446: create_conf_file(base_dir, server_root, "import.conf",
447: server_port, server_host, server_nick,
448: ps_product_dir);
449: create_conf_file(base_dir, server_root, "filterrules.conf",
450: server_port, server_host, server_nick,
451: ps_product_dir);
452: create_conf_file(base_dir, server_root,
453: "classification.conf", server_port, server_host,
454: server_nick, ps_product_dir);
455: create_conf_file(base_dir, server_root, "convert.conf",
456: server_port, server_host, server_nick,
457: ps_product_dir);
458: create_conf_file(base_dir, server_root, "mime.types",
459: server_port, server_host, server_nick,
460: ps_product_dir);
461: create_conf_file(base_dir, server_root, "schema.rdm",
462: server_port, server_host, server_nick,
463: ps_product_dir);
464: create_conf_file(base_dir, server_root, "taxonomy.rdm",
465: server_port, server_host, server_nick,
466: ps_product_dir);
467: create_conf_file(base_dir, server_root, "converter.cfg",
468: server_port, server_host, server_nick,
469: ps_product_dir);
470: create_conf_file(base_dir, server_root,
471: "converter.template", server_port, server_host,
472: server_nick, ps_product_dir);
473:
474: String efile = server_root + File.separator + "tmp"
475: + File.separator + "empty";
476:
477: FileOutputStream eout = new FileOutputStream(efile);
478: eout.close();
479:
480: int status = 0;
481: String cmd[] = {
482: server_root + File.separator + "run-cs-cli",
483: "rdmgr", "-q", efile };
484: if (isWindows()) {
485: cmd[0] = server_root + File.separator
486: + "run-cs-cli.bat";
487: cmd[1] = "rdmgr.bat";
488: }
489: Runtime.getRuntime().exec(cmd);
490: //Runtime.getRuntime().exec(server_root + File.separator +
491: // "run-cs-cli\" rdmgr -q \"" + efile + "\"");
492:
493: // new File(efile).delete();
494: //Runtime.getRuntime().exec(server_root + File.separator +
495: // "run-cs-cli\" rdmgr -T \"" +
496: // confdestdir + File.separator + "taxonomy.rdm\"");
497: cmd[2] = "-T";
498: cmd[3] = confdestdir + File.separator + "taxonomy.rdm";
499: Runtime.getRuntime().exec(cmd);
500: } catch (Exception e) {
501: throw new Exception("Error during Compass instance setup: "
502: + e);
503: }
504: }
505:
506: static void usage() {
507: System.out
508: .println("Usage: java SearchSetup ps_install_dir server_instance_dir is_ias server_instance_host server_instance_port server_instance_nick");
509: }
510:
511: public static void main(String args[]) {
512: final int expectedargs = 6;
513: /*
514: if (args.length > expectedargs)
515: Unix.setUID(args[expectedargs]);
516: */
517: if (args.length != expectedargs) {
518: usage();
519: } else {
520: int http_type = HTTP_IWS;
521: if (args[2].substring(0, 1).equalsIgnoreCase("y"))
522: http_type = HTTP_IAS;
523: try {
524: new SearchSetup().setup_instance(args[0], args[1],
525: http_type, args[3], args[4], args[5]);
526: } catch (Exception e) {
527: System.out.println(e);
528: }
529: }
530: }
531:
532: protected static String osName = System.getProperty("os.name");
533:
534: /**
535: * check if operating system is Windows
536: * <br><br>
537: *
538: * @return true if operating system is Windows
539: */
540: static public boolean isWindows() {
541: if (osName.startsWith("Windows")) {
542: return true;
543: } else {
544: return false;
545: }
546: }
547:
548: public static boolean isHP_UX() {
549: if (osName.startsWith("HP-UX")) {
550: return true;
551: } else {
552: return false;
553: }
554: }
555:
556: /**
557: * check if operating system is Linux
558: * <br><br>
559: *
560: * @return true if operating system is Linux
561: */
562: public static boolean isLinux() {
563: if (osName.startsWith("Linux")) {
564: return true;
565: } else {
566: return false;
567: }
568: }
569:
570: }
|