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;
013:
014: import java.io.*;
015: import java.lang.*;
016: import java.util.*;
017: import java.text.*;
018:
019: // JMX
020: import javax.management.MBeanServerConnection;
021: import javax.management.ObjectName;
022: import javax.management.InstanceNotFoundException;
023: import javax.management.MBeanException;
024: import javax.management.ReflectionException;
025: import javax.management.MalformedObjectNameException;
026:
027: // PS Admin Common
028: import com.sun.portal.admin.common.util.AdminClientUtil;
029:
030: // CLI framework
031: import com.sun.enterprise.cli.framework.*;
032:
033: // Base Class
034: import com.sun.portal.admin.cli.commands.GenericCommand;
035:
036: /**
037: * This class implements the psadmin create-search-server subcommand. The
038: * create-search-server subcommand calls the SearchServerMBean and creates a
039: * new search server.
040: */
041: public class CreateSearchServerCommand extends GenericCommand {
042:
043: public static final String BAD_SEARCH_SERVER = "error.psadmin.createsearchserver.bad";
044:
045: /*
046: * An abstract method that executes the command
047: * @throws CommandException
048: * @throws CommandValidationException
049: */
050: public void runCommand() throws CommandException,
051: CommandValidationException {
052: validateOptions();
053:
054: Properties p = getWebConfigProp();
055:
056: String host = (String) p.get("Host");
057: String port = (String) p.get("Port");
058: String instance = (String) p.get("WebContainerInstanceName");
059:
060: boolean failed = false;
061: try {
062: MBeanServerConnection msc = getMBeanServerConnection(
063: getUserId(), getPassword(), getHost());
064:
065: ObjectName on = AdminClientUtil
066: .getSearchServerPattern(AdminClientUtil.DEFAULT_DOMAIN);
067: Set objects = msc.queryNames(on, new ObjectName(""));
068: Iterator i = objects.iterator();
069: while (i.hasNext()) {
070: on = (ObjectName) i.next();
071: String testHost = (String) msc.getAttribute(on, "Host");
072: String testPort = (String) msc.getAttribute(on, "Port");
073: String testInstance = (String) msc.getAttribute(on,
074: "Instance");
075: if (testHost.equals(host) && testPort.equals(port)
076: && testInstance.equals(instance)) {
077: failed = true;
078: }
079: }
080: } catch (Exception e) {
081: throw new CommandException(
082: getLocalizedString(COMMAND_FAILED), e);
083: }
084: if (failed) {
085: throw new CommandException(
086: getLocalizedString(BAD_SEARCH_SERVER), null);
087: }
088:
089: String operation = "";
090:
091: try {
092: LinkedList path = new LinkedList();
093: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
094: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
095: AdminClientUtil.PORTAL_DOMAIN_MBEAN_TYPE, path);
096:
097: Object[] params = { getOption("searchserver"), p };
098: String[] signatures = { "java.lang.String",
099: "java.util.Properties" };
100: operation = "createSearchServer";
101:
102: MBeanServerConnection msc = getMBeanServerConnection(
103: getUserId(), getPassword(), getHost());
104:
105: msc.invoke(on, operation, params, signatures);
106: } catch (InstanceNotFoundException ie) {
107: throw new CommandException(getLocalizedString(
108: ERROR_MBEAN_INSTANCE_NOT_FOUND,
109: new Object[] { operation }), ie);
110: } catch (MBeanException me) {
111: throw new CommandException(getLocalizedString(
112: ERROR_JMX_INVOKE, new Object[] { operation }), me);
113: } catch (ReflectionException re) {
114: throw new CommandException(getLocalizedString(
115: ERROR_MBEAN_REFLECTION_ERROR,
116: new Object[] { operation }), re);
117: } catch (CommandException ce) {
118: throw ce;
119: } catch (Exception e) {
120: throw new CommandException(
121: getLocalizedString(COMMAND_FAILED), e);
122: } finally {
123: closeMBeanServerConnection();
124: }
125: }
126:
127: }
|