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.log.common.PortalLogger;
010:
011: import java.lang.*;
012: import java.io.*;
013: import java.util.*;
014: import java.util.logging.Logger;
015: import java.util.logging.Level;
016: import javax.servlet.jsp.JspWriter;
017: import java.net.*;
018:
019: public class CronController {
020:
021: static public void main(String[] args) {
022: if (args.length < 2) {
023: System.out
024: .println("Usage:CronController server_root action");
025: return;
026: }
027: CSConfig.init(args[0]);
028: CronController cronControl = new CronController(CSConfig
029: .getServerRoot(), CSConfig.getBinPath(), CSConfig
030: .getLibPath(), null, null);
031: cronControl.action(null, args[1]);
032: }
033:
034: private String[] r_env = null;
035: private boolean isValid = false;
036: private boolean isRunning = false;
037: private boolean isQuery = false;
038: static final String rCmd = "cmd";
039: static final String rQuery = "query";
040: private String binDir = "";
041: private String libDir = "";
042: private String serverRoot = "";
043: private String configDir = "";
044: private String logDir = "";
045: private boolean wait = true;
046: static String pscronctl = "pscronctl";
047:
048: // Create a Logger for this class
049: private static Logger debugLogger = PortalLogger
050: .getLogger(CronController.class);
051:
052: public CronController(String serverRootPath, String binPath,
053: String libPath, String configPath, String logPath) {
054: if (serverRootPath != null) {
055: this .serverRoot = serverRootPath;
056: if (binPath != null) {
057: this .binDir = binPath;
058: }
059: if (libPath != null) {
060: this .libDir = libPath;
061: }
062: } else {
063: serverRoot = CSConfig.getServerRoot();
064: binDir = CSConfig.getBinPath();
065: libDir = CSConfig.getLibPath();
066: }
067: if (configPath != null) {
068: this .configDir = configPath;
069: } else {
070: this .configDir = this .serverRoot + File.separator
071: + "config";
072: }
073: if (logPath != null) {
074: this .logDir = logPath;
075: } else {
076: this .logDir = this .serverRoot + File.separator + "logs";
077: }
078: r_env = new String[4];
079: r_env[0] = "LD_LIBRARY_PATH=" + libDir;
080: r_env[1] = "CS_CONFIG_PATH=" + configDir;
081: r_env[2] = "SERVER_ROOT=" + this .serverRoot;
082: r_env[3] = "BINDIR=" + libDir;
083: r_env[4] = "SHLIB_PATH=" + libDir;
084:
085: }
086:
087: public void action(JspWriter out, String action) {
088: if (action.compareToIgnoreCase("start") == 0) {
089: execute(out, "start");
090: } else if (action.compareToIgnoreCase("stop") == 0) {
091: execute(out, "stop");
092: } else if (action.compareToIgnoreCase("restart") == 0) {
093: execute(out, "restart");
094: } else if (action.compareToIgnoreCase("status") == 0) {
095: if (out != null) {
096: execute(out, "status");
097: } else {
098: System.out.println(isRunning() ? "running" : "off");
099: }
100: }
101: }
102:
103: public boolean isRunning() {
104: Runtime rt = Runtime.getRuntime();
105: String cmd = libDir + File.separator + pscronctl + " status";
106: //String r_env[] = {"SERVER_ROOT=" + serverRoot,"BINDIR=" + libDir};
107: debugLogger.log(Level.FINER, "PSSH_CSPSAC0001", cmd);
108: try {
109: Process process = rt.exec(cmd, r_env);
110: BufferedReader buf = new BufferedReader(
111: new InputStreamReader(process.getInputStream()));
112: String outLine = null;
113: while ((outLine = buf.readLine()) != null) {
114: debugLogger.log(Level.FINEST, "PSSH_CSPSAC0002",
115: outLine);
116: if (outLine.compareToIgnoreCase("running") == 0) {
117: return true;
118: }
119: }
120: } catch (IOException e) {
121: debugLogger.log(Level.INFO, "PSSH_CSPSA0003", e
122: .getMessage());
123: }
124: return false;
125: }
126:
127: public synchronized void execute(JspWriter out, String action) {
128: Runtime rt = Runtime.getRuntime();
129: String cmd = libDir + File.separator + pscronctl + " " + action;
130: //String r_env[] = {"SERVER_ROOT=" + serverRoot,"BINDIR=" + libDir};
131: debugLogger.log(Level.FINER, "PSSH_CSPSAC0001", cmd);
132: try {
133: if (out != null) {
134: out.flush();
135: }
136: Process process = rt.exec(cmd, r_env);
137: BufferedReader buf = new BufferedReader(
138: new InputStreamReader(process.getInputStream()));
139: String outLine = null;
140: while ((outLine = buf.readLine()) != null) {
141: if (out != null) {
142: debugLogger.log(Level.FINEST, "PSSH_CSPSAC0002",
143: outLine);
144: out.println(outLine);
145: out.flush();
146: }
147: }
148: } catch (IOException e) {
149: debugLogger.log(Level.INFO, "PSSH_CSPSA0003", e
150: .getMessage());
151: }
152: }
153:
154: public synchronized String execute(String action) {
155: String outText = "";
156: Runtime rt = Runtime.getRuntime();
157: String cmd = libDir + File.separator + pscronctl + " " + action;
158: //String r_env[] = {"SERVER_ROOT=" + serverRoot,"BINDIR=" + libDir};
159: debugLogger.log(Level.FINER, "PSSH_CSPSAC0001", cmd);
160: try {
161: Process process = rt.exec(cmd, r_env);
162: BufferedReader buf = new BufferedReader(
163: new InputStreamReader(process.getInputStream()));
164: String outLine = null;
165: while ((outLine = buf.readLine()) != null) {
166: outText = outText + outLine;
167: debugLogger.log(Level.FINEST, "PSSH_CSPSAC0002",
168: outLine);
169: }
170: } catch (IOException e) {
171: debugLogger.log(Level.INFO, "PSSH_CSPSA0003", e
172: .getMessage());
173: }
174: return outText;
175: }
176:
177: }
|