001: /**
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.admin.cli.commands.search;
013:
014: import java.util.*;
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017:
018: // JMX
019: import javax.management.MBeanServerConnection;
020: import javax.management.ObjectName;
021: import javax.management.InstanceNotFoundException;
022: import javax.management.MBeanException;
023: import javax.management.ReflectionException;
024:
025: // CLI framework
026: import com.sun.enterprise.cli.framework.*;
027:
028: // Base Class
029: import com.sun.portal.admin.cli.commands.GenericCommand;
030:
031: // PS Admin Common
032: import com.sun.portal.admin.common.util.AdminUtil;
033:
034: /**
035: * This class implements the psadmin get-robot-status subcommand.
036: * The get-robot-status subcommand calls the PortalDomainMBean
037: * and performs the following tasks:
038: * <UL>
039: * <LI>Display the search robot status
040: * <UL>
041: */
042:
043: public class GetRobotStatusCommand extends GenericCommand {
044:
045: //public Logger logger = super.getLogger();
046:
047: //command error messages
048:
049: //command options
050:
051: public void runCommand() throws CommandException,
052: CommandValidationException {
053:
054: validateOptions();
055: validateSearchServerID();
056:
057: //String status = null;
058: String status[] = null;
059: try {
060: LinkedList path = new LinkedList();
061: path.addFirst(getDomainId());
062: path.addFirst(getSearchServerId());
063: path.addFirst("robot");
064: ObjectName objName = AdminUtil.getResourceMBeanObjectName(
065: AdminUtil.SEARCH_ROBOT_MBEAN_TYPE, path);
066:
067: MBeanServerConnection msc = getMBeanServerConnection(
068: getUserId(), getPassword(), getHost());
069: Object[] params = {};
070: String[] signatures = {};
071: status = (String[]) msc.invoke(objName,
072: "retrieveRobotStatus", params, signatures);
073:
074: handleReturnValue(status);
075:
076: } catch (InstanceNotFoundException ie) {
077: //logger.logp(Level.SEVERE, "GetRobotStatusCommand",
078: //"runCommand()", ie.getMessage(), ie);
079: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ie);
080: throw new CommandException(getLocalizedString(
081: ERROR_MBEAN_INSTANCE_NOT_FOUND,
082: new Object[] { "getAttribute:RobotStatus" }), ie);
083: } catch (MBeanException me) {
084: //logger.logp(Level.SEVERE, "GetRobotStatusCommand",
085: // "runCommand()", me.getMessage(), me);
086: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", me);
087: throw new CommandException(getLocalizedString(
088: ERROR_JMX_INVOKE,
089: new Object[] { "getAttribute:RobotStatus" }), me);
090: } catch (ReflectionException re) {
091: //logger.logp(Level.SEVERE, "GetRobotStatusCommand",
092: // "runCommand()", re.getMessage(), re);
093: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", re);
094: throw new CommandException(getLocalizedString(
095: ERROR_MBEAN_REFLECTION_ERROR,
096: new Object[] { "getAttribute:RobotStatus" }), re);
097: } catch (CommandException ce) {
098: //logger.logp(Level.SEVERE, "GetRobotStatusCommand",
099: // "runCommand()", ce.getMessage(), ce);
100: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ce);
101: throw ce;
102: } catch (Exception ex) {
103: ex.printStackTrace();
104: //logger.logp(Level.SEVERE, "GetRobotStatusCommand",
105: // "runCommand()", ex.getMessage(), ex);
106: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ex);
107: throw new CommandException(
108: getLocalizedString(COMMAND_FAILED), ex);
109: } finally {
110: closeMBeanServerConnection();
111: }
112:
113: }
114:
115: /**
116: * handles return value from mbean
117: * @param states
118: */
119: protected void handleReturnValue(String[] states) {
120:
121: if (states != null) {
122: StringBuffer sb = new StringBuffer();
123:
124: sb.append(getLocalizedString("robot.is") + " ");
125: if (states[0].equalsIgnoreCase("Idle")) {
126: sb.append(getLocalizedString("robot.status.idle")
127: + "\n");
128: } else if (states[0].equalsIgnoreCase("Running")) {
129: sb.append(getLocalizedString("robot.status.running")
130: + "\n");
131: } else if (states[0].equalsIgnoreCase("Paused")) {
132: sb.append(getLocalizedString("robot.status.paused")
133: + "\n");
134: } else {
135: sb
136: .append(getLocalizedString("robot.status.off")
137: + "\n");
138: }
139:
140: sb.append(getLocalizedString("robot.startingpoints",
141: new Object[] { (String) states[1] })
142: + "\n");
143:
144: sb.append(getLocalizedString("robot.urlpool",
145: new Object[] { (String) states[2] })
146: + "\n");
147:
148: sb.append(getLocalizedString("robot.extracting",
149: new Object[] { (String) states[3] })
150: + "\n");
151:
152: sb.append(getLocalizedString("robot.filtering",
153: new Object[] { (String) states[4] })
154: + "\n");
155:
156: sb.append(getLocalizedString("robot.indexing",
157: new Object[] { (String) states[5] })
158: + "\n");
159:
160: sb.append(getLocalizedString("robot.excludedurls",
161: new Object[] { (String) states[6],
162: (String) states[7] })
163: + "\n");
164:
165: sb.append(getLocalizedString("robot.rds", new Object[] {
166: (String) states[8], (String) states[9] })
167: + "\n");
168:
169: sb
170: .append(getLocalizedString("robot.generalstatus")
171: + " "
172: + getLocalizedString(
173: "robot.urlretrieved",
174: new Object[] { (String) states[10] })
175: + ", "
176: + getLocalizedString(
177: "robot.avgsize",
178: new Object[] { (String) states[11] })
179: + ", "
180: + getLocalizedString(
181: "robot.runningtime",
182: new Object[] { (String) getRunTime(states[12]) })
183: + "\n");
184:
185: CLILogger.getInstance().printMessage(sb.toString());
186: } else {
187: CLILogger.getInstance().printMessage(
188: getLocalizedString(COMMAND_FAILED));
189: }
190: }
191:
192: private String getRunTime(String s) {
193:
194: String runTime = "";
195: long run_time = Long.parseLong(s);
196: long day = 0, hour = 0, minute = 0, second = 0;
197: if (run_time >= 86400) {
198: day = run_time / 86400;
199: run_time = run_time % 86400;
200: if (day == 1) {
201: runTime = "1 " + getLocalizedString("time.day") + " ";
202: } else {
203: runTime = Long.toString(day) + " "
204: + getLocalizedString("time.days") + " ";
205: }
206: }
207: if (run_time > 3600) {
208: hour = run_time / 3600;
209: run_time = run_time % 3600;
210: if (hour == 1) {
211: runTime = runTime + "1 "
212: + getLocalizedString("time.hour") + " ";
213: } else {
214: runTime = runTime + Long.toString(hour) + " "
215: + getLocalizedString("time.hours") + " ";
216: }
217: }
218: if (run_time > 60) {
219: minute = run_time / 60;
220: run_time = run_time % 60;
221: if (minute == 1) {
222: runTime = runTime + "1 "
223: + getLocalizedString("time.minute") + " ";
224: } else {
225: runTime = runTime + Long.toString(minute) + " "
226: + getLocalizedString("time.minutes") + " ";
227: }
228: }
229: if (run_time == 1) {
230: runTime = runTime + "1 "
231: + getLocalizedString("time.second") + " ";
232: } else {
233: runTime = runTime + Long.toString(run_time) + " "
234: + getLocalizedString("time.seconds") + " ";
235: }
236:
237: return runTime;
238: }
239:
240: }
|