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.cli;
007:
008: import com.sun.portal.search.admin.CSConfig;
009: import com.sun.portal.search.util.SearchConfig;
010: import com.sun.portal.log.common.PortalLogger;
011: import com.sun.portal.util.Platform;
012:
013: import java.lang.*;
014: import java.io.*;
015: import java.util.*;
016: import java.util.logging.Level;
017: import java.util.logging.Logger;
018: import javax.servlet.jsp.JspWriter;
019: import java.net.*;
020:
021: public class RunImportAgent {
022:
023: static public void main(String[] args) {
024: if (args.length < 2) {
025: System.out
026: .println("Usage:RunImportAgent server_root action");
027: return;
028: }
029: CSConfig.init(args[0]);
030: RunImportAgent impAgent = new RunImportAgent(CSConfig
031: .getServerRoot(), CSConfig.getBinPath(), CSConfig
032: .getLibDir(), CSConfig.getLibPath(), null, null);
033: impAgent.command(args[1]);
034: }
035:
036: public static final String IMPORTMGR_CMD = "importmgr";
037:
038: private String[] r_env = null;
039: private boolean isValid = false;
040: private boolean isRunning = false;
041: private boolean isQuery = false;
042: static final String rCmd = "cmd";
043: static final String rQuery = "query";
044: private String binDir = "";
045: private String libDir = "";
046: private String libPath = "";
047: private String serverRoot = "";
048: private String configDir = "";
049: private String logDir = "";
050: private boolean wait = true;
051:
052: // Create a Logger for this class
053: private static Logger debugLogger = PortalLogger
054: .getLogger(RunImportAgent.class);
055:
056: public RunImportAgent(String in_serverRootPath, String in_binPath,
057: String in_libDir, String in_libPath, String in_configPath,
058: String in_logPath) {
059: SearchConfig csidConf = SearchConfig.getSearchConfig();
060: if (in_serverRootPath != null) {
061: this .serverRoot = in_serverRootPath;
062: if (in_binPath != null) {
063: this .binDir = in_binPath;
064: }
065: if (in_libPath != null) {
066: this .libPath = in_libPath;
067: }
068: if (in_libDir != null) {
069: this .libDir = in_libDir;
070: }
071: } else {
072: this .serverRoot = CSConfig.getServerRoot();
073: this .binDir = CSConfig.getBinPath();
074: this .libDir = CSConfig.getLibDir();
075: this .libPath = CSConfig.getLibPath();
076: }
077: if (in_configPath != null) {
078: this .configDir = in_configPath;
079: } else {
080: this .configDir = this .serverRoot + File.separator
081: + "config";
082: }
083: if (in_logPath != null) {
084: this .logDir = in_logPath;
085: } else {
086: this .logDir = this .serverRoot + File.separator + "logs";
087: }
088:
089: if (isWindows()) {
090: r_env = new String[7];
091: } else {
092: r_env = new String[6];
093: }
094: r_env[0] = "LD_LIBRARY_PATH=" + this .libPath;
095: r_env[1] = "CS_CONFIG_PATH=" + this .configDir;
096: r_env[2] = "CS_LOGDIR=" + this .logDir;
097: r_env[3] = "PATH=" + this .libPath + ":" + this .binDir;
098: r_env[4] = "CAT_SERVER_ROOT=" + serverRoot;
099: r_env[5] = "SHLIB_PATH=" + this .libPath;
100: if (isWindows()) {
101: String winDir = "C:\\WINNT";
102: if (System.getProperty("os.name").equals("Windows XP"))
103: winDir = "C:\\WINDOWS";
104:
105: //Note: importmgr needs compsec env var to be set
106: String comSpecKey = "COMSPEC";
107: String comSpecVal = System.getProperty(comSpecKey);
108: if (comSpecVal == null || "".equals(comSpecVal.trim()))
109: comSpecVal = winDir + "\\system32\\cmd.exe";
110: r_env[6] = comSpecKey + "=" + comSpecVal;
111: }
112: }
113:
114: public void command(String action) {
115: if (action.compareToIgnoreCase("Run") == 0) {
116: run(null);
117: }
118: /* else if (action.compareToIgnoreCase("Stop") == 0) {
119: stop();
120: }
121: else if (action.compareToIgnoreCase("pause") == 0) {
122: pause();
123: }
124: else if (action.compareToIgnoreCase("resume") == 0) {
125: resume();
126: }
127: else {
128: byte[] result = this.robotURLGet(rCmd , action);
129: if (result != null) {
130: String rs = new String(result);
131: System.out.println(rs);
132: }
133: }
134: */
135: }
136:
137: public synchronized void run(JspWriter out) {
138: debugLogger.finer("PSSH_CSPSAC0012");
139: String csid = SearchConfig.getSearchConfig().getValue(
140: SearchConfig.CSID);
141:
142: String cmd = libDir + File.separator + this .IMPORTMGR_CMD
143: + " -c " + this .configDir + File.separator
144: + SearchConfig.SEARCH_CONF + " " + csid + " run all";
145: String cmds[] = {
146: libDir + File.separator + this .IMPORTMGR_CMD,
147: "-c",
148: this .configDir + File.separator
149: + SearchConfig.SEARCH_CONF, csid, "run", "all" };
150: debugLogger.log(Level.FINER, "PSSH_CSPSAC0001", cmd);
151: Runtime rt = Runtime.getRuntime();
152: try {
153: if (out != null) {
154: out.flush();
155: }
156: Process ps = rt.exec(cmds, r_env);
157: BufferedReader buf = new BufferedReader(
158: new InputStreamReader(ps.getInputStream()));
159: String outLine = null;
160: while ((outLine = buf.readLine()) != null) {
161: if (out != null) {
162: out.println(outLine);
163: out.flush();
164: }
165: }
166: // return ps.exitValue() == 0 ? true : false;
167: } catch (IOException e) {
168: debugLogger.log(Level.INFO, "PSSH_CSPSA0003", e
169: .getMessage());
170: // return false;
171: }
172: }
173:
174: static public boolean isWindows() {
175: if (Platform.name.startsWith("windows")) {
176: return true;
177: } else {
178: return false;
179: }
180: }
181: }
|