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.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-db subcommand. The
038: * create-search-db subcommand calls the DatabaseMBean and creates a
039: * new search database.
040: */
041: public class CreateDatabaseCommand extends GenericCommand {
042:
043: /**
044: * An abstract method that executes the command
045: * @throws CommandException
046: * @throws CommandValidationException
047: */
048: public void runCommand() throws CommandException,
049: CommandValidationException {
050: validateOptions();
051: validateSearchServerID();
052: String method = "";
053:
054: try {
055: LinkedList path = new LinkedList();
056: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
057: path.addFirst(getSearchServerId());
058: path.addFirst("database");
059: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
060: AdminClientUtil.SEARCH_DATABASE_MBEAN_TYPE, path);
061:
062: String name = getOption("database");
063: Properties p = getPropertiesOption("attributes");
064: Object[] params = { name, p };
065: String[] signatures = { "java.lang.String",
066: "java.util.Properties" };
067: method = "create";
068:
069: MBeanServerConnection msc = getMBeanServerConnection(
070: getUserId(), getPassword(), getHost());
071: msc.invoke(on, "create", params, signatures);
072: } catch (InstanceNotFoundException ie) {
073: throw new CommandException(getLocalizedString(
074: ERROR_MBEAN_INSTANCE_NOT_FOUND,
075: new Object[] { method }), ie);
076: } catch (MBeanException me) {
077: throw new CommandException(getLocalizedString(
078: ERROR_JMX_INVOKE, new Object[] { method }), me);
079: } catch (ReflectionException re) {
080: throw new CommandException(getLocalizedString(
081: ERROR_MBEAN_REFLECTION_ERROR,
082: new Object[] { method }), re);
083: } catch (CommandException ce) {
084: throw ce;
085: } catch (Exception e) {
086: throw new CommandException(
087: getLocalizedString(COMMAND_FAILED), e);
088: } finally {
089: closeMBeanServerConnection();
090: }
091: }
092:
093: private Properties getPropertiesOption(String optionName)
094: throws CommandException {
095: Properties p = new Properties();
096:
097: List values = getMultiValueOption(optionName);
098: if (values != null) {
099: for (int index = 0; index < values.size(); index++) {
100: StringTokenizer st = new StringTokenizer(
101: (String) values.get(index), "=");
102: String key = (st.hasMoreTokens()) ? st.nextToken() : "";
103: String value = (st.hasMoreTokens()) ? st.nextToken()
104: : "";
105: p.setProperty(key, value);
106: }
107: }
108:
109: return p;
110: }
111:
112: }
|