001: /**
002: * $Id: VersionCommand.java,v 1.6 2005/11/07 17:40:17 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.io.*;
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.InstanceNotFoundException;
025: import javax.management.MBeanException;
026: import javax.management.MalformedObjectNameException;
027: import javax.management.ReflectionException;
028:
029: //PS Admin Common
030: import com.sun.portal.fabric.util.FileUtil;
031: import com.sun.portal.fabric.util.os.OSTasksFactory;
032: import com.sun.portal.fabric.util.os.OSTasks;
033:
034: //PS Admin Common
035: import com.sun.portal.admin.common.util.AdminClientUtil;
036: import com.sun.portal.admin.common.PSMBeanException;
037:
038: /**
039: * This class implements the psadmin version subcommand.
040: */
041:
042: public class VersionCommand extends AdminBaseCommand {
043:
044: static final String fs = File.separator;
045: private String jarFullPath;
046:
047: // subcommand specific options
048: private static final String OPT_PATCHES = "patches";
049: private static final String OPT_VERBOSE = "verbose";
050: private static final String OPT_DISPLAY = "display";
051: private static final String OPT_JARFILE = "jar";
052:
053: /**
054: * The version subcommand
055: */
056: public VersionCommand() {
057: super ();
058: }
059:
060: public boolean validateOptions() throws CommandValidationException {
061: return super .validateOptions();
062:
063: }
064:
065: public void validateJar() throws CommandException {
066:
067: String jarFilePath = getOption(OPT_JARFILE);
068: if (jarFilePath != null) {
069: File f = new File(jarFilePath);
070: jarFullPath = f.getAbsolutePath();
071:
072: if (!f.exists()) {
073: String token[] = { jarFullPath };
074: throw new CommandException(getLocalizedString(
075: ERROR_FILE_NOT_FOUND, token));
076:
077: }
078: }
079: }
080:
081: /**
082: * An abstract method that Executes the command
083: * @throws CommandException
084: */
085: public void runCommand() throws CommandException,
086: CommandValidationException {
087:
088: validateOptions();
089: validateJar();
090:
091: String operation = "";
092:
093: try {
094:
095: // Get the MBean server connection
096: MBeanServerConnection msc = getMBeanServerConnection(
097: getUserId(), getPassword(), getHost());
098:
099: // Setting the params and signature
100: Boolean patch = new Boolean(getBooleanOption(OPT_PATCHES));
101: Boolean patchVerbose = new Boolean(
102: getBooleanOption(OPT_VERBOSE));
103: Boolean display = new Boolean(getBooleanOption(OPT_DISPLAY));
104:
105: Object[] params = { display, jarFullPath, patch,
106: patchVerbose };
107: String[] signature = { "java.lang.Boolean",
108: "java.lang.String", "java.lang.Boolean",
109: "java.lang.Boolean" };
110: operation = "getPortalVersion";
111:
112: // Get the Domain MBean object
113: ObjectName objName = AdminClientUtil
114: .getPortalDomainMBeanObjectName(getDomainId());
115:
116: // Invoke the create portal method on the domain MBean
117: String out = (String) msc.invoke(objName, operation,
118: params, signature);
119:
120: CLILogger.getInstance().printMessage(out);
121: } catch (InstanceNotFoundException ie) {
122: logger.log(Level.SEVERE, "PSALI_CSPACC0005", ie);
123: throw new CommandException(getLocalizedString(
124: ERROR_MBEAN_INSTANCE_NOT_FOUND,
125: new Object[] { operation }), ie);
126: } catch (MBeanException me) {
127: logger.log(Level.SEVERE, "PSALI_CSPACC0006", me);
128: boolean isPE = me.getCause() instanceof PSMBeanException;
129: if (isPE) {
130: PSMBeanException pe = (PSMBeanException) me.getCause();
131: String key = null;
132: Object tokens[] = pe.getTokens();
133:
134: if (pe.getErrorKey() != null) {
135: key = pe.getErrorKey();
136: CLILogger.getInstance().printMessage(
137: getLocalizedString(key, tokens));
138: } else {
139: throw new CommandException(getLocalizedString(
140: ERROR_JMX_INVOKE,
141: new Object[] { operation }), pe);
142: }
143: } else {
144: throw new CommandException(getLocalizedString(
145: ERROR_JMX_INVOKE, new Object[] { operation }),
146: me);
147: }
148: } catch (ReflectionException re) {
149: logger.log(Level.SEVERE, "PSALI_CSPACC0007", re);
150: throw new CommandException(getLocalizedString(
151: ERROR_MBEAN_REFLECTION_ERROR,
152: new Object[] { operation }), re);
153: } catch (MalformedObjectNameException mle) {
154: logger.log(Level.SEVERE, "PSALI_CSPACC0004", mle);
155: throw new CommandException(
156: getLocalizedString(ERROR_OBJECT_NAME), mle);
157: } catch (CommandException ce) {
158: logger.log(Level.SEVERE, "PSALI_CSPACC0008", ce);
159: throw ce;
160: } catch (Exception ex) {
161: logger.log(Level.SEVERE, "PSALI_CSPACC0010", ex);
162: throw new CommandException(
163: getLocalizedString(COMMAND_FAILED), ex);
164: } finally {
165: closeMBeanServerConnection();
166: }
167: }
168: }
|