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: import javax.management.RuntimeOperationsException;
027:
028: // PS Admin Common
029: import com.sun.portal.admin.common.util.AdminClientUtil;
030:
031: // CLI framework
032: import com.sun.enterprise.cli.framework.*;
033:
034: // Base Class
035: import com.sun.portal.admin.cli.commands.GenericCommand;
036:
037: /**
038: * This class implements the psadmin create-search-rd subcommand. The
039: * create-search-rd subcommand calls the SearchServerMBean and creates a
040: * new search resource description.
041: */
042: public class CreateResourceDescriptionCommand extends GenericCommand {
043:
044: /**
045: * An abstract method that executes the command
046: * @throws CommandException
047: * @throws CommandValidationException
048: */
049: public void runCommand() throws CommandException,
050: CommandValidationException {
051: validateOptions();
052: validateSearchServerID();
053: String method = "";
054:
055: try {
056: LinkedList path = new LinkedList();
057: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
058: path.addFirst(getSearchServerId());
059: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
060: AdminClientUtil.SEARCHSERVER_MBEAN_TYPE, path);
061:
062: Object[] params = { readFile("file"), getOption("database") };
063: String[] signatures = { "[B", "java.lang.String" };
064: method = "createResourceDescription";
065:
066: MBeanServerConnection msc = getMBeanServerConnection(
067: getUserId(), getPassword(), getHost());
068: msc.invoke(on, "createResourceDescription", params,
069: signatures);
070: } catch (InstanceNotFoundException ie) {
071: throw new CommandException(getLocalizedString(
072: ERROR_MBEAN_INSTANCE_NOT_FOUND,
073: new Object[] { method }), ie);
074: } catch (MBeanException me) {
075: throw new CommandException(getLocalizedString(
076: ERROR_JMX_INVOKE, new Object[] { method }), me);
077: } catch (ReflectionException re) {
078: throw new CommandException(getLocalizedString(
079: ERROR_MBEAN_REFLECTION_ERROR,
080: new Object[] { method }), re);
081: } catch (CommandException ce) {
082: throw ce;
083: } catch (Exception e) {
084: throw new CommandException(
085: getLocalizedString(COMMAND_FAILED), e);
086: } finally {
087: closeMBeanServerConnection();
088: }
089: }
090:
091: private byte[] readFile(String optionName) throws Exception {
092: FileInputStream fis = new FileInputStream(getOption(optionName));
093:
094: ByteArrayOutputStream baos = new ByteArrayOutputStream();
095: byte[] buf = new byte[1024];
096: int count = 0;
097: while ((count = fis.read(buf)) != -1) {
098: baos.write(buf, 0, count);
099: }
100:
101: fis.close();
102: return baos.toByteArray();
103: }
104:
105: }
|