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.Attribute;
018:
019: import com.sun.enterprise.cli.framework.*;
020:
021: import com.sun.portal.admin.cli.commands.GenericCommand;
022: import com.sun.portal.admin.common.util.AdminUtil;
023:
024: /**
025: * This class implements the psadmin run-robot-siteprobe subcommand. The
026: * run-robot-siteprobe subcommand calls the PortalDomainMBean and performs
027: * the following tasks:
028: * <UL>
029: * <LI>Run robot siteprobe
030: * <UL>
031: */
032:
033: public class RunRobotSiteProbeCommand extends GenericCommand {
034:
035: //public Logger logger = super.getLogger();
036:
037: //command error messages
038: public static final String ERROR_SITENAME_NOT_FOUND = "error.psadmin.robot.site.name.not.found";
039:
040: //command options
041: private static final String SITENAME = "sitename";
042: private static final String NODNS = "nodns";
043:
044: String siteName = "";
045: String siteURL = "";
046: Boolean checkDNS = Boolean.TRUE;
047:
048: public boolean validateOptions() throws CommandValidationException {
049: siteName = getOption(SITENAME);
050: if (siteName == null || siteName.equals("")) {
051: throw new CommandValidationException(
052: getLocalizedString(ERROR_SITENAME_NOT_FOUND));
053: }
054: return super .validateOptions();
055: }
056:
057: public void runCommand() throws CommandException,
058: CommandValidationException {
059: if (!validateOptions()) {
060: throw new CommandValidationException(
061: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
062: }
063: validateSearchServerID();
064:
065: if (siteName.indexOf("://") > 0)
066: siteURL = siteName;
067: else
068: siteURL = "http://" + siteName + ":80/";
069:
070: if (getBooleanOption(NODNS))
071: checkDNS = Boolean.FALSE;
072:
073: HashMap resultmap;
074: try {
075: LinkedList path = new LinkedList();
076: path.addFirst(getDomainId());
077: path.addFirst(getSearchServerId());
078: path.addFirst("siteprobe");
079: ObjectName objName = AdminUtil.getResourceMBeanObjectName(
080: AdminUtil.SEARCH_SITEPROBE_MBEAN_TYPE, path);
081:
082: Object[] params = { siteURL, checkDNS };
083: String[] signature = { "java.lang.String",
084: "java.lang.Boolean" };
085:
086: MBeanServerConnection msc = getMBeanServerConnection(
087: getUserId(), getPassword(), getHost());
088:
089: resultmap = (HashMap) msc.invoke(objName, "runSiteProbe",
090: params, signature);
091:
092: handleReturnValue(resultmap);
093:
094: } catch (InstanceNotFoundException ie) {
095: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
096: // "runCommand()", ie.getMessage(), ie);
097: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ie);
098: throw new CommandException(getLocalizedString(
099: ERROR_MBEAN_INSTANCE_NOT_FOUND,
100: new Object[] { "runSiteProbe" }), ie);
101: } catch (MBeanException me) {
102: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
103: // "runCommand()", me.getMessage(), me);
104: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", me);
105: throw new CommandException(getLocalizedString(
106: ERROR_JMX_INVOKE, new Object[] { "runSiteProbe" }),
107: me);
108: } catch (ReflectionException re) {
109: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
110: // "runCommand()", re.getMessage(), re);
111: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", re);
112: throw new CommandException(getLocalizedString(
113: ERROR_MBEAN_REFLECTION_ERROR,
114: new Object[] { "runSiteProbe" }), re);
115: } catch (CommandException ce) {
116: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
117: // "runCommand()", ce.getMessage(), ce);
118: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ce);
119: throw ce;
120: } catch (Exception ex) {
121: ex.printStackTrace();
122: //logger.logp(Level.SEVERE, "RunRobotSimulatorCommand",
123: // "runCommand()", ex.getMessage(), ex);
124: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ex);
125: throw new CommandException(
126: getLocalizedString(COMMAND_FAILED), ex);
127: } finally {
128: closeMBeanServerConnection();
129: }
130:
131: }
132:
133: /**
134: * handles return value from mbean
135: * @param map
136: */
137: protected void handleReturnValue(HashMap map) {
138:
139: if (map == null) {
140: CLILogger.getInstance().printMessage(
141: getLocalizedString(COMMAND_FAILED));
142: } else {
143: StringBuffer sb = new StringBuffer();
144:
145: if (map.containsKey("URLMalformed")) {
146: sb.append(getLocalizedString("siteprobe.malformed")
147: + "\n");
148: CLILogger.getInstance().printMessage(sb.toString());
149: CLILogger.getInstance().printMessage(
150: getLocalizedString(COMMAND_FAILED));
151: return;
152: }
153:
154: boolean isSmartHostHeuristics = true;
155:
156: if (map.containsKey("smart-host-heuristics")) {
157: String shh = (String) map.get("smart-host-heuristics");
158: if (shh.equalsIgnoreCase("false"))
159: isSmartHostHeuristics = false;
160: //map.remove("smart-host-heuristics");
161: }
162:
163: sb.append(getLocalizedString("siteprobe.checkingurl")
164: + "\n\n");
165:
166: if (map.containsKey("HostResult")) {
167:
168: LinkedList hr = (LinkedList) map.get("HostResult");
169: if (hr != null) {
170: String h_cname = (String) hr.get(0);
171: int h_type = ((Integer) hr.get(1)).intValue();
172: int h_errno = ((Integer) hr.get(2)).intValue();
173: int h_length = ((Integer) hr.get(3)).intValue();
174: String[] h_ips = (String[]) hr.get(4);
175: String[] h_aliases = (String[]) hr.get(5);
176: String org_url = (String) hr.get(6);
177:
178: if (h_errno != 0) {
179: //sb.append(getLocalizedString("siteprobe.successdns") + "\n");
180: if (h_errno == 1) {
181: sb
182: .append(getLocalizedString("siteprobe.namenotfound")
183: + "\n");
184: } else if (h_errno == 2) {
185: sb
186: .append(getLocalizedString("siteprobe.dnsserverfail")
187: + "\n");
188: } else if (h_errno == 3) {
189: sb
190: .append(getLocalizedString("siteprobe.norecovery")
191: + "\n");
192: } else if (h_errno == 4) {
193: sb
194: .append(getLocalizedString("siteprobe.domainname")
195: + "\n");
196: } else {
197: sb
198: .append(getLocalizedString("siteprobe.unknownerror")
199: + "\n");
200: }
201: }
202:
203: sb.append("\n");
204:
205: if (checkDNS.booleanValue()) {
206: sb
207: .append(getLocalizedString("siteprobe.resultfor")
208: + " '" + siteName + "'\n");
209: sb.append(getLocalizedString("siteprobe.name")
210: + " ");
211: sb.append(h_cname != null ? h_cname + "\n" : ""
212: + "\n");
213:
214: if (h_aliases != null) {
215: for (int i = 0; i < h_aliases.length
216: && h_aliases[i] != null; i++) {
217: sb
218: .append(getLocalizedString("siteprobe.alias")
219: + " ");
220: sb.append(h_aliases[i] + "\n");
221: }
222: }
223:
224: sb
225: .append(getLocalizedString("siteprobe.addresstype")
226: + " ");
227: sb.append(h_type + "\n");
228:
229: sb
230: .append(getLocalizedString("siteprobe.length")
231: + " ");
232: sb.append(h_length + "\n");
233:
234: if (h_ips != null) {
235: for (int i = 0; i < h_ips.length
236: && h_ips[i] != null; i++) {
237: sb
238: .append(getLocalizedString("siteprobe.ip")
239: + " ");
240: sb.append(h_ips[i] + "\n");
241: }
242: }
243:
244: } // end of checkDNS
245:
246: sb.append("\n");
247:
248: sb.append(getLocalizedString("siteprobe.result")
249: + " ");
250:
251: sb.append(getLocalizedString("siteprobe.validname",
252: new Object[] { siteName })
253: + "\n");
254:
255: boolean is_alias = false;
256: if (h_aliases != null) {
257: for (int j = 0; j < h_aliases.length; j++) {
258: if (h_aliases[j].indexOf(siteName) > -1) {
259: is_alias = true;
260: break;
261: }
262: }
263: }
264: if (is_alias) {
265: sb.append(getLocalizedString("siteprobe.note")
266: + " ");
267: sb.append(getLocalizedString(
268: "siteprobe.aliasnamefor", new Object[] {
269: siteName, h_cname })
270: + "\n");
271: } else {
272: sb.append(getLocalizedString("siteprobe.note")
273: + " ");
274: sb.append(getLocalizedString(
275: "siteprobe.cnamefor",
276: new Object[] { siteName })
277: + "\n");
278: sb.append(getLocalizedString("siteprobe.note")
279: + " ");
280: sb
281: .append(getLocalizedString("siteprobe.nodnstrans")
282: + "\n");
283:
284: if (!siteName.equalsIgnoreCase(h_cname)) {
285: sb.append(getLocalizedString(
286: "siteprobe.refername",
287: new Object[] { h_cname })
288: + "\n");
289: }
290: }
291:
292: boolean is_cname_shh = false;
293: if (is_alias && siteName.startsWith("www.")
294: && h_cname.startsWith("www")) {
295: int k;
296: for (k = 3; k < h_cname.length()
297: && Character.isDigit(h_cname.charAt(k)); k++)
298: ;
299: if (k < h_cname.length()
300: && h_cname.charAt(k) == '.')
301: is_cname_shh = true;
302: }
303:
304: boolean is_host_shh = false;
305: String host_main = "";
306: if (is_alias && siteName.startsWith("www")) {
307: int k;
308: for (k = 3; k < siteName.length()
309: && Character
310: .isDigit(siteName.charAt(k)); k++)
311: ;
312: if (k < siteName.length()
313: && siteName.charAt(k) == '.') {
314: is_host_shh = true;
315: host_main = "www" + siteName.substring(k);
316: }
317: }
318:
319: if (is_cname_shh) {
320: if (isSmartHostHeuristics) {
321: sb
322: .append(getLocalizedString("siteprobe.note")
323: + " ");
324: sb.append(getLocalizedString(
325: "siteprobe.smarthoston",
326: new Object[] { siteName })
327: + "\n");
328: } else {
329: sb
330: .append(getLocalizedString("siteprobe.note")
331: + " ");
332: sb.append(getLocalizedString(
333: "siteprobe.issmarthoston",
334: new Object[] { h_cname, siteName })
335: + "\n");
336: }
337: sb.append(getLocalizedString("siteprobe.note")
338: + " ");
339: sb.append(getLocalizedString(
340: "siteprobe.hasunique",
341: new Object[] { h_cname })
342: + "\n");
343: } else if (is_host_shh) {
344: if (isSmartHostHeuristics) {
345: sb.append(getLocalizedString(
346: "siteprobe.actionsmarthostison",
347: new Object[] { host_main })
348: + "\n");
349: } else {
350: sb
351: .append(getLocalizedString("siteprobe.note")
352: + " ");
353: sb.append(getLocalizedString(
354: "siteprobe.dnsmirror",
355: new Object[] { siteName })
356: + "\n");
357: sb.append(getLocalizedString(
358: "siteprobe.heavilyload",
359: new Object[] { host_main })
360: + "\n");
361: sb.append(getLocalizedString(
362: "siteprobe.enablesmarthost",
363: new Object[] { siteName })
364: + "\n");
365: }
366: sb.append(getLocalizedString("siteprobe.note")
367: + " ");
368: sb.append(getLocalizedString(
369: "siteprobe.hasunique",
370: new Object[] { h_cname })
371: + "\n");
372: }
373:
374: } // if (hr != null)
375:
376: } // end of HostResult
377:
378: sb.append("\n");
379:
380: if (map.containsKey("RedirectResult")) {
381:
382: LinkedList rr = (LinkedList) map.get("RedirectResult");
383: if (rr != null) {
384: String redirect_url = (String) rr.get(0);
385: String server_type = (String) rr.get(1);
386: int content_length = ((Integer) rr.get(2))
387: .intValue();
388: int return_code = ((Integer) rr.get(3)).intValue();
389:
390: if (return_code < 0) {
391: sb.append(getLocalizedString(
392: "siteprobe.failconnect",
393: new Object[] { siteURL })
394: + "\n");
395: } else if (return_code == 0) {
396: sb.append(getLocalizedString(
397: "siteprobe.noredirectat",
398: new Object[] { siteURL })
399: + "\n");
400: } else {
401: if (redirect_url.indexOf("://") > 0)
402: sb.append(getLocalizedString(
403: "siteprobe.isredirect",
404: new Object[] { siteURL,
405: redirect_url })
406: + "\n");
407: else
408: sb
409: .append(getLocalizedString(
410: "siteprobe.isredirect",
411: new Object[] {
412: siteURL,
413: siteURL
414: + (redirect_url
415: .charAt(0) == '/' ? "/"
416: : "")
417: + redirect_url })
418: + "\n");
419: if (sameHost(redirect_url, siteName)) {
420: sb
421: .append(getLocalizedString("siteprobe.redirect2same")
422: + "\n");
423: } else {
424: sb.append(getLocalizedString(
425: "siteprobe.needsite4redirect",
426: new Object[] { redirect_url })
427: + "\n");
428: }
429: }
430:
431: sb.append("\n");
432:
433: sb
434: .append(getLocalizedString("siteprobe.servertype")
435: + " ");
436: sb
437: .append(server_type == null ? getLocalizedString("siteprobe.unknowntype")
438: + "\n"
439: : server_type + "\n");
440:
441: sb.append("\n");
442:
443: if (return_code >= 0) {
444: sb
445: .append(getLocalizedString("siteprobe.checkvirtual")
446: + "\n");
447: }
448:
449: } // end of rr!=null
450: } // end of RedirectResult
451:
452: String url2 = (String) map.get("RedirectResult2URL");
453: int r1_length = 0;
454:
455: if (map.containsKey("RedirectResult1")) {
456:
457: LinkedList rr1 = (LinkedList) map
458: .get("RedirectResult1");
459: if (rr1 != null) {
460:
461: String r1_redirect_url = (String) rr1.get(0);
462: String r1_server_type = (String) rr1.get(1);
463: int r1_content_length = ((Integer) rr1.get(2))
464: .intValue();
465: int r1_return_code = ((Integer) rr1.get(3))
466: .intValue();
467:
468: r1_length = r1_content_length;
469:
470: if (r1_return_code < 0) {
471: sb.append(getLocalizedString(
472: "siteprobe.failconnect",
473: new Object[] { siteURL })
474: + "\n");
475: }
476:
477: if (r1_content_length < 0) {
478: sb.append(getLocalizedString(
479: "siteprobe.failconnect",
480: new Object[] { url2 })
481: + "\n");
482: }
483:
484: if (r1_return_code < 0) {
485: CLILogger.getInstance().printMessage(
486: sb.toString());
487: return;
488: }
489:
490: if (r1_content_length == 0) {
491: sb.append(getLocalizedString(
492: "siteprobe.failcontentlength",
493: new Object[] { siteURL })
494: + "\n");
495: CLILogger.getInstance().printMessage(
496: sb.toString());
497: return;
498: }
499:
500: } // end of rr1 != null
501: } // end of RedirectResult1
502:
503: if (map.containsKey("RedirectResult2")) {
504:
505: LinkedList rr2 = (LinkedList) map
506: .get("RedirectResult2");
507: if (rr2 != null) {
508:
509: String r2_redirect_url = (String) rr2.get(0);
510: String r2_server_type = (String) rr2.get(1);
511: int r2_content_length = ((Integer) rr2.get(2))
512: .intValue();
513: int r2_return_code = ((Integer) rr2.get(3))
514: .intValue();
515:
516: if (r2_return_code < 0) {
517: sb.append(getLocalizedString(
518: "siteprobe.failconnect",
519: new Object[] { url2 })
520: + "\n");
521: CLILogger.getInstance().printMessage(
522: sb.toString());
523: return;
524: }
525:
526: if (r2_content_length == 0) {
527: sb.append(getLocalizedString(
528: "siteprobe.failcontentlength",
529: new Object[] { url2 })
530: + "\n");
531: CLILogger.getInstance().printMessage(
532: sb.toString());
533: return;
534: }
535:
536: if (r1_length == r2_content_length) {
537: sb.append(getLocalizedString(
538: "siteprobe.novirtual",
539: new Object[] { siteURL })
540: + "\n");
541: } else {
542: sb.append(getLocalizedString(
543: "siteprobe.isvirtual", new Object[] {
544: url2, url2 })
545: + "\n");
546: }
547:
548: } // rr2 != null
549: } // end of RedirectResult2
550:
551: CLILogger.getInstance().printMessage(sb.toString());
552: } //end of map != null
553:
554: }
555:
556: private boolean sameHost(String url1, String host) {
557: if (url1.charAt(0) == '/') // no protocal, uri only
558: return true;
559: if (url1.indexOf("://" + host) > 0) // same host
560: return true;
561: return false;
562: }
563:
564: }
|