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-importagent
038: * subcommand. The create-search-importagent subcommand calls
039: * the SearchServerMBean and creates a new search import agent.
040: */
041: public class CreateImportAgentCommand 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: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
059: AdminClientUtil.SEARCHSERVER_MBEAN_TYPE, path);
060:
061: MBeanServerConnection msc = getMBeanServerConnection(
062: getUserId(), getPassword(), getHost());
063:
064: Properties srcP = new Properties();
065: try {
066: srcP.load(new FileInputStream(getOption("file")));
067: } catch (Exception e) {
068: throw new CommandException(
069: getLocalizedString(COMMAND_FAILED), e);
070: }
071: if (srcP.containsKey("input-file")) {
072: Object[] params = { srcP.getProperty("input-file"),
073: getOption("database") };
074: String[] signatures = { "java.lang.String",
075: "java.lang.String" };
076: method = "createImportAgent";
077: msc.invoke(on, "createImportAgent", params, signatures);
078: } else {
079: Properties destP = new Properties();
080:
081: addPropertyString(srcP, "nickname", destP);
082: addPropertyString(srcP, "character-set", destP);
083: addPropertyBoolean(srcP, "is-ssl", destP);
084: addPropertyString(srcP, "server", destP);
085: addPropertyString(srcP, "port", destP);
086: addPropertyString(srcP, "instance", destP);
087: if (!addPropertyBoolean(srcP, "is-compass301x", destP)) {
088: addPropertyString(srcP, "uri", destP);
089: addPropertyString(srcP, "database", destP);
090: }
091: addPropertyString(srcP, "user", destP);
092: addPropertyString(srcP, "password", destP);
093: if (addPropertyBoolean(srcP, "is-search-query", destP)) {
094: addPropertyString(srcP, "scope", destP);
095: addPropertyString(srcP, "view-attributes", destP);
096: addPropertyString(srcP, "view-hits", destP);
097: }
098: addPropertyString(srcP, "last-collection-time", destP);
099: addPropertyString(srcP, "timeout", destP);
100:
101: Object[] params = { destP, getOption("database") };
102: String[] signatures = { "java.util.Properties",
103: "java.lang.String" };
104: method = "createImportAgent";
105: msc.invoke(on, "createImportAgent", params, signatures);
106: }
107: } catch (InstanceNotFoundException ie) {
108: throw new CommandException(getLocalizedString(
109: ERROR_MBEAN_INSTANCE_NOT_FOUND,
110: new Object[] { method }), ie);
111: } catch (MBeanException me) {
112: throw new CommandException(getLocalizedString(
113: ERROR_JMX_INVOKE, new Object[] { method }), me);
114: } catch (ReflectionException re) {
115: throw new CommandException(getLocalizedString(
116: ERROR_MBEAN_REFLECTION_ERROR,
117: new Object[] { method }), re);
118: } catch (CommandException ce) {
119: throw ce;
120: } catch (Exception e) {
121: throw new CommandException(
122: getLocalizedString(COMMAND_FAILED), e);
123: } finally {
124: closeMBeanServerConnection();
125: }
126: }
127:
128: private String addPropertyString(Properties srcP,
129: String optionName, Properties destP) {
130: if (srcP.containsKey(optionName)) {
131: String value = (String) srcP.getProperty(optionName);
132: destP.setProperty(optionName, value);
133: return value;
134: } else {
135: return "";
136: }
137: }
138:
139: private boolean addPropertyBoolean(Properties srcP,
140: String optionName, Properties destP) {
141: if (srcP.containsKey(optionName)) {
142: String value = (String) srcP.getProperty(optionName);
143: destP.setProperty(optionName, value);
144: return (value.equals("true")) ? true : false;
145: } else {
146: return false;
147: }
148: }
149:
150: }
|