001: /**
002: * $Id: GenericCommand.java,v 1.8 2005/11/07 17:40:16 cathywu Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.cli.commands;
014:
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017:
018: //CLI framework
019: import com.sun.enterprise.cli.framework.*;
020:
021: //JMX
022: import javax.management.MBeanServerConnection;
023: import javax.management.ObjectName;
024: import javax.management.MBeanException;
025: import javax.management.ReflectionException;
026: import javax.management.MalformedObjectNameException;
027: import javax.management.InstanceNotFoundException;
028: import javax.management.RuntimeErrorException;
029:
030: //PS Admin
031: import com.sun.portal.admin.common.PSMBeanException;
032:
033: /**
034: * This is a generic command that gets the JMX connection, and invoke
035: * the MBean operation that is defined for the command.
036: */
037: public class GenericCommand extends AdminBaseCommand {
038: /**
039: * An abstract method that validates the options
040: * on the specification in the xml properties file
041: * This method verifies for the correctness of number of
042: * operands and if all the required options are supplied by the client.
043: * @return boolean returns true if success else returns false
044: */
045: public boolean validateOptions() throws CommandValidationException {
046: return super .validateOptions();
047: }
048:
049: /**
050: * An abstract method that Executes the command
051: * @throws CommandException
052: */
053: public void runCommand() throws CommandException,
054: CommandValidationException {
055: if (!validateOptions()) {
056: throw new CommandValidationException(
057: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
058: }
059:
060: if (getOption(OPT_PORTAL_ID) != null) {
061: validatePortalId();
062: }
063:
064: if (getOption(OPT_SEARCHSERVER_ID) != null) {
065: validateSearchServerID();
066: }
067:
068: if (getInstanceId() != null
069: && !getName().equals("create-portal")
070: && !getName().equals("create-instance")) {
071: validateInstance();
072: }
073:
074: MBeanServerConnection mbsc = getMBeanServerConnection(
075: getUserId(), getPassword(), getHost());
076: String operationName = null;
077:
078: try {
079: final ObjectName objectName = getObjectName();
080: final Object[] params = getParamsInfo();
081: final String[] types = getTypesInfo();
082: operationName = getOperationName();
083:
084: Object returnValue = mbsc.invoke(objectName, operationName,
085: params, types);
086: handleReturnValue(returnValue);
087: } catch (InstanceNotFoundException ie) {
088: logger.log(Level.SEVERE, "PSALI_CSPACC0005", ie);
089: throw new CommandException(getLocalizedString(
090: ERROR_MBEAN_INSTANCE_NOT_FOUND,
091: new Object[] { operationName }), ie);
092: } catch (MBeanException me) {
093: logger.log(Level.SEVERE, "PSALI_CSPACC0006", me);
094: boolean pe = me.getCause() instanceof PSMBeanException;
095: if (getBooleanOption(OPT_DEBUG)) {
096: if (pe) {
097: String dbgMsg = me.getCause().getMessage();
098: if (dbgMsg != null) {
099: CLILogger.getInstance().printMessage(dbgMsg);
100: }
101: }
102: }
103: if (pe) {
104: PSMBeanException pme = (PSMBeanException) me.getCause();
105: if (((PSMBeanException) me.getCause()).getTokens() != null) {
106: throw new CommandException(getLocalizedString(pme
107: .getErrorKey(), pme.getTokens()), pme);
108: } else {
109: throw new CommandException(getLocalizedString(pme
110: .getErrorKey()), me);
111: }
112: } else {
113: throw new CommandException(getLocalizedString(
114: ERROR_JMX_INVOKE,
115: new Object[] { operationName }), me);
116: }
117: } catch (ReflectionException re) {
118: logger.log(Level.SEVERE, "PSALI_CSPACC0007", re);
119: throw new CommandException(getLocalizedString(
120: ERROR_MBEAN_REFLECTION_ERROR,
121: new Object[] { operationName }), re);
122: } catch (RuntimeErrorException rte) {
123: logger.log(Level.SEVERE, "PSALI_CSPACC0009", rte);
124: throw new CommandException(getLocalizedString(
125: ERROR_JMX_RUNTIME_ERROR,
126: new Object[] { operationName }), rte);
127: } catch (CommandException ce) {
128: logger.log(Level.SEVERE, "PSALI_CSPACC0008", ce);
129: throw ce;
130: } catch (Exception ex) {
131: logger.log(Level.SEVERE, "PSALI_CSPACC0010", ex);
132: throw new CommandException(
133: getLocalizedString(COMMAND_FAILED), ex);
134: } finally {
135: closeMBeanServerConnection();
136: }
137: }
138: }
|