001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.admin.cli.commands.search;
006:
007: import java.net.URL;
008: import java.util.*;
009: import java.util.logging.Logger;
010: import java.util.logging.Level;
011:
012: import javax.management.MBeanServerConnection;
013: import javax.management.ObjectName;
014: import javax.management.InstanceNotFoundException;
015: import javax.management.MBeanException;
016: import javax.management.ReflectionException;
017: import javax.management.MalformedObjectNameException;
018: import javax.management.Attribute;
019:
020: import com.sun.enterprise.cli.framework.*;
021:
022: import com.sun.portal.admin.cli.commands.GenericCommand;
023: import com.sun.portal.admin.common.util.AdminUtil;
024:
025: /**
026: * This class implements the psadmin run-robot-simulator subcommand. The
027: * run-robot-simulator subcommand calls the PortalDomainMBean and performs
028: * the following tasks:
029: * <UL>
030: * <LI>Run robot simulator
031: * <UL>
032: */
033:
034: public class RunRobotSimulatorCommand extends GenericCommand {
035:
036: //public Logger logger = super.getLogger();
037:
038: //command error messages
039:
040: //command options
041: private static final String SITENAME = "sitename";
042: private static final String NODNS = "nodns";
043: private static final String NOREDIRECT = "noredirect";
044:
045: String siteName = "";
046: String siteURL = "";
047: Boolean checkDNS = Boolean.TRUE;
048: Boolean checkRedirect = Boolean.TRUE;
049:
050: public void runCommand() throws CommandException,
051: CommandValidationException {
052: validateOptions();
053: validateSearchServerID();
054:
055: siteName = getOption(SITENAME);
056: if (siteName == null || siteName.equals("")) {
057: siteName = "";
058: } else {
059: if (siteName.indexOf("://") > 0)
060: siteURL = siteName;
061: else
062: siteURL = "http://" + siteName + ":80/";
063: }
064:
065: if (getBooleanOption(NODNS))
066: checkDNS = Boolean.FALSE;
067:
068: if (getBooleanOption(NOREDIRECT))
069: checkRedirect = Boolean.FALSE;
070:
071: HashMap resultmap;
072: try {
073: LinkedList path = new LinkedList();
074: path.addFirst(getDomainId());
075: path.addFirst(getSearchServerId());
076: path.addFirst("robot");
077: ObjectName objName = AdminUtil.getResourceMBeanObjectName(
078: AdminUtil.SEARCH_ROBOT_MBEAN_TYPE, path);
079:
080: Object[] params = { siteURL, checkDNS, checkRedirect };
081: String[] signature = { "java.lang.String",
082: "java.lang.Boolean", "java.lang.Boolean" };
083:
084: MBeanServerConnection msc = getMBeanServerConnection(
085: getUserId(), getPassword(), getHost());
086:
087: resultmap = (HashMap) msc.invoke(objName, "runSimulator",
088: params, signature);
089:
090: handleReturnValue(resultmap);
091:
092: } catch (InstanceNotFoundException ie) {
093: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
094: // "runCommand()", ie.getMessage(), ie);
095: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ie);
096: throw new CommandException(getLocalizedString(
097: ERROR_MBEAN_INSTANCE_NOT_FOUND,
098: new Object[] { "runSimulator" }), ie);
099: } catch (MBeanException me) {
100: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
101: // "runCommand()", me.getMessage(), me);
102: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", me);
103: throw new CommandException(getLocalizedString(
104: ERROR_JMX_INVOKE, new Object[] { "runSimulator" }),
105: me);
106: } catch (ReflectionException re) {
107: // logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
108: // "runCommand()", re.getMessage(), re);
109: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", re);
110: throw new CommandException(getLocalizedString(
111: ERROR_MBEAN_REFLECTION_ERROR,
112: new Object[] { "runSimulator" }), re);
113: } catch (CommandException ce) {
114: // logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
115: // "runCommand()", ce.getMessage(), ce);
116: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ce);
117: throw ce;
118: } catch (Exception ex) {
119: ex.printStackTrace();
120: // logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
121: // "runCommand()", ex.getMessage(), ex);
122: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ex);
123: throw new CommandException(
124: getLocalizedString(COMMAND_FAILED), ex);
125: } finally {
126: closeMBeanServerConnection();
127: }
128:
129: }
130:
131: /**
132: * handles return value from mbean
133: * @param map
134: */
135: protected void handleReturnValue(HashMap map) {
136:
137: if (map == null) {
138: CLILogger.getInstance().printMessage(
139: getLocalizedString(COMMAND_FAILED));
140: } else {
141: StringBuffer sb = new StringBuffer();
142:
143: boolean isSmartHostHeuristics = true;
144:
145: if (map.containsKey("smart-host-heuristics")) {
146: String shh = (String) map.get("smart-host-heuristics");
147: if (shh.equalsIgnoreCase("false"))
148: isSmartHostHeuristics = false;
149: map.remove("smart-host-heuristics");
150: }
151:
152: sb.append(getLocalizedString("robot.simulator.result")
153: + "\n\n");
154:
155: Iterator it = map.keySet().iterator();
156: while (it.hasNext()) {
157: siteURL = (String) it.next();
158: siteName = siteURL;
159: try {
160: siteName = getHostName(siteURL);
161: } catch (Exception e) {
162: e.printStackTrace();
163: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
164: // "handleReturnValue()", e.getMessage(), e);
165: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", e);
166: }
167:
168: HashMap url_map = (HashMap) map.get(siteURL);
169:
170: if (url_map.containsKey("frsimResult")) {
171: String s = ((String) url_map.get("frsimResult"))
172: .replaceAll(
173: "ACCEPTED:",
174: getLocalizedString("robot.simulator.accept"));
175: sb.append(s + "\n");
176: //sb.append((String)url_map.get("frsimResult"));
177: }
178:
179: if (url_map.containsKey("URLMalformed")) {
180: sb
181: .append(getLocalizedString("robot.simulator.malformed")
182: + "\n");
183: CLILogger.getInstance().printMessage(sb.toString());
184: CLILogger.getInstance().printMessage(
185: getLocalizedString(COMMAND_FAILED));
186: return;
187: }
188:
189: if (url_map.containsKey("HostResult")
190: && checkDNS.booleanValue()) {
191:
192: LinkedList hr = (LinkedList) url_map
193: .get("HostResult");
194: if (hr != null) {
195: String h_cname = (String) hr.get(0);
196: int h_type = ((Integer) hr.get(1)).intValue();
197: int h_errno = ((Integer) hr.get(2)).intValue();
198: int h_length = ((Integer) hr.get(3)).intValue();
199: String[] h_ips = (String[]) hr.get(4);
200: String[] h_aliases = (String[]) hr.get(5);
201: String org_url = (String) hr.get(6);
202:
203: if (h_errno != 0) {
204: if (h_errno == 1) {
205: sb.append(getLocalizedString(
206: "robot.simulator.invalidhost",
207: new Object[] { siteName })
208: + "\n");
209: } else if (h_errno == 2 || h_errno == 3) {
210: sb
211: .append(getLocalizedString("robot.simulator.dnsreterror")
212: + "\n");
213: } else if (h_errno == 4) {
214: sb
215: .append(getLocalizedString(
216: "robot.simulator.domainnothost",
217: new Object[] { siteName })
218: + "\n");
219: } else {
220: sb
221: .append(getLocalizedString("robot.simulator.unknownerror")
222: + "\n");
223: }
224: }
225:
226: if (h_aliases != null) {
227: boolean found = false;
228: boolean ok = false;
229: for (int j = 0; j < h_aliases.length; j++) {
230: if (h_aliases[j].indexOf(siteName) > -1) {
231: found = true;
232: break;
233: }
234: }
235: if (found && isSmartHostHeuristics
236: && h_cname.length() > 4) {
237:
238: char c = h_cname.charAt(4);
239:
240: if (h_cname.startsWith("www")
241: && (c == '.' || Character
242: .isDigit(c)))
243: ok = true;
244: }
245: if (ok) {
246: sb.append(getLocalizedString(
247: "robot.simulator.aliasallow",
248: new Object[] { h_cname })
249: + "\n");
250: } else {
251: sb.append(getLocalizedString(
252: "robot.simulator.aliasreject",
253: new Object[] { h_cname })
254: + "\n");
255: }
256: }
257:
258: } // if (hr != null)
259:
260: sb.append(getLocalizedString(
261: "robot.simulator.validhostname",
262: new Object[] { siteName })
263: + "\n");
264: sb.append("\n");
265:
266: } // end of HostResult
267:
268: if (url_map.containsKey("RedirectResult")
269: && checkRedirect.booleanValue()) {
270:
271: LinkedList rr = (LinkedList) url_map
272: .get("RedirectResult");
273: if (rr != null) {
274: String redirect_url = (String) rr.get(0);
275: String server_type = (String) rr.get(1);
276: int content_length = ((Integer) rr.get(2))
277: .intValue();
278: int return_code = ((Integer) rr.get(3))
279: .intValue();
280:
281: if (return_code < 0) {
282: sb.append(getLocalizedString(
283: "robot.simulator.failconnect",
284: new Object[] { siteURL })
285: + "\n");
286: }
287:
288: if (return_code == 0) {
289: sb
290: .append(getLocalizedString("robot.simulator.noredirect")
291: + "\n");
292: } else if (sameHost(redirect_url, siteName)) {
293: sb.append(getLocalizedString(
294: "robot.simulator.rejectbyrule",
295: new Object[] { siteName })
296: + "\n");
297: } else {
298: sb.append(getLocalizedString(
299: "robot.simulator.redirect2",
300: new Object[] { redirect_url })
301: + "\n");
302: }
303: sb.append("\n");
304: }
305:
306: } //end of RedirectResult
307:
308: } // end of while
309:
310: CLILogger.getInstance().printMessage(sb.toString());
311: } //end of map != null
312:
313: }
314:
315: private String getHostName(String siteURL) throws Exception {
316: String host = siteURL;
317: try {
318: URL url = new URL(siteURL);
319: host = url.getHost();
320: } catch (Exception e) {
321: // logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
322: // "getHostName()", e.getMessage(), e);
323: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", e);
324: }
325: return host;
326: }
327:
328: private boolean sameHost(String url1, String host) {
329: if (url1.charAt(0) == '/') // no protocal, uri only
330: return true;
331: if (url1.indexOf("://" + host) > 0) // same host
332: return true;
333: return false;
334: }
335:
336: }
|