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.text.MessageFormat;
008: import java.util.*;
009: import java.util.logging.Logger;
010: import java.util.logging.Level;
011:
012: // JMX
013: import javax.management.MBeanServerConnection;
014: import javax.management.ObjectName;
015: import javax.management.InstanceNotFoundException;
016: import javax.management.MBeanException;
017: import javax.management.ReflectionException;
018: import javax.management.MalformedObjectNameException;
019:
020: // CLI framework
021: import com.sun.enterprise.cli.framework.*;
022:
023: // Base Class
024: import com.sun.portal.admin.cli.commands.AdminBaseCommand;
025: import com.sun.portal.admin.cli.commands.GenericCommand;
026:
027: // PS Admin Common
028: import com.sun.portal.admin.common.util.AdminUtil;
029:
030: /**
031: * This class implements the psadmin create-robot-site subcommand. The
032: * create-robot-site subcommand calls the PortalDomainMBean and performs
033: * the following tasks:
034: * <UL>
035: * <LI>edit robot crawling site
036: * <UL>
037: */
038:
039: public class CreateRobotSiteCommand extends GenericCommand {
040:
041: //public Logger logger = super.getLogger();
042:
043: //command error messages
044: public static final String ERROR_SITENAME_NOT_FOUND = "error.psadmin.robot.site.name.not.found";
045:
046: //command options
047: private static final String SITE_NAME = "sitename";
048: private static final String IS_DOMAIN = "isdomain";
049: private static final String DEPTH = "depth";
050:
051: String siteName = "";
052:
053: public boolean validateOptions() throws CommandValidationException {
054: siteName = getOption(SITE_NAME);
055: if (siteName == null || siteName.equals("")) {
056: throw new CommandValidationException(
057: getLocalizedString(ERROR_SITENAME_NOT_FOUND));
058: }
059:
060: return super .validateOptions();
061: }
062:
063: public void runCommand() throws CommandException,
064: CommandValidationException {
065:
066: if (!validateOptions()) {
067: throw new CommandValidationException(
068: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
069: }
070: validateSearchServerID();
071:
072: Boolean isDomain;
073: if (getBooleanOption(IS_DOMAIN)) {
074: isDomain = Boolean.TRUE;
075: } else {
076: isDomain = Boolean.FALSE;
077: }
078:
079: String depth = "";
080: depth = getOption(DEPTH);
081: if (depth == null || depth.equals("")) {
082: depth = "10";
083: }
084:
085: String database = getOption("database");
086: if (database == null) {
087: database = "";
088: }
089:
090: String siteID = null;
091: try {
092: LinkedList path = new LinkedList();
093: path.addFirst(getDomainId());
094: path.addFirst(getSearchServerId());
095: path.addFirst("robot");
096: ObjectName objName = AdminUtil.getResourceMBeanObjectName(
097: AdminUtil.SEARCH_ROBOT_MBEAN_TYPE, path);
098:
099: MBeanServerConnection msc = getMBeanServerConnection(
100: getUserId(), getPassword(), getHost());
101:
102: Object[] params0 = { siteName };
103: String[] signature0 = { "java.lang.String" };
104: String msgKey = (String) msc.invoke(objName,
105: "validateSiteURL", params0, signature0);
106:
107: if (!msgKey.equals("")) {
108:
109: handleReturnValue(msgKey);
110:
111: } else {
112:
113: Object[] params = { isDomain, siteName, depth, database };
114: String[] signature = { "java.lang.Boolean",
115: "java.lang.String", "java.lang.String",
116: "java.lang.String" };
117:
118: siteID = (String) msc.invoke(objName, "createSite",
119: params, signature);
120:
121: handleReturnValue(siteID);
122: }
123:
124: } catch (InstanceNotFoundException ie) {
125: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ie);
126: throw new CommandException(getLocalizedString(
127: ERROR_MBEAN_INSTANCE_NOT_FOUND,
128: new Object[] { "createSite" }), ie);
129: } catch (MBeanException me) {
130: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", me);
131: throw new CommandException(getLocalizedString(
132: ERROR_JMX_INVOKE, new Object[] { "createSite" }),
133: me);
134: } catch (ReflectionException re) {
135: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", re);
136: throw new CommandException(getLocalizedString(
137: ERROR_MBEAN_REFLECTION_ERROR,
138: new Object[] { "createSite" }), re);
139: } catch (CommandException ce) {
140: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ce);
141: throw ce;
142: } catch (Exception ex) {
143: ex.printStackTrace();
144: logger.log(Level.SEVERE, "PSALI_CSPACCSH0003", ex);
145: throw new CommandException(
146: getLocalizedString(COMMAND_FAILED), ex);
147: } finally {
148: closeMBeanServerConnection();
149: }
150:
151: }
152:
153: /**
154: * handles return value from mbean
155: * @param siteID
156: */
157: protected void handleReturnValue(String siteID) {
158:
159: if (siteID != null) {
160: if (siteID.startsWith("search.error")) {
161: CLILogger.getInstance().printMessage(
162: getLocalizedString(siteID));
163: } else {
164: CLILogger.getInstance().printMessage(
165: getLocalizedString("robot.site.id.create",
166: new Object[] { siteID }));
167: }
168: } else {
169: CLILogger.getInstance().printMessage(
170: getLocalizedString(COMMAND_FAILED));
171: }
172:
173: }
174:
175: }
|