001: /**
002: * $Id: GetAttributeCommand.java,v 1.10 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:
029: import com.sun.portal.admin.common.util.AdminUtil;
030: import com.sun.portal.admin.common.AttrOptionConstants;
031: import com.sun.portal.admin.cli.display.AttributeDisplayHandlerFactory;
032: import com.sun.portal.admin.cli.display.AttributeDisplayHandler;
033: import com.sun.portal.admin.common.PSMBeanException;
034:
035: import java.util.List;
036: import java.util.Map;
037: import java.util.Iterator;
038:
039: public class GetAttributeCommand extends GenericCommand {
040: public void runCommand() throws CommandException,
041: CommandValidationException {
042: if (!validateOptions()) {
043: throw new CommandValidationException(
044: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
045: }
046:
047: String portalId = getOption(OPT_PORTAL_ID);
048: if (portalId != null) {
049: validatePortalId();
050: }
051:
052: String component = getOption(AttrOptionConstants.OPT_COMPONENT);
053: validateGetOptions(getOptions(), component);
054:
055: MBeanServerConnection mbsc = getMBeanServerConnection(
056: getUserId(), getPassword(), getHost());
057:
058: String operation = "";
059:
060: try {
061: ObjectName objectName = portalId == null ? AdminUtil
062: .getPortalDomainMBeanObjectName(getDomainId())
063: : AdminUtil.getPortalMBeanObjectName(getDomainId(),
064: portalId);
065:
066: Map optMap = getOptions();
067: optMap.put("operation", "get");
068: // Setting the params and signature
069: Object[] params = { optMap };
070: String[] signature = { "java.util.Map" };
071: operation = "getAttribute";
072:
073: Object returnValue = mbsc.invoke(objectName, operation,
074: params, signature);
075:
076: AttributeDisplayHandler attrHandler = AttributeDisplayHandlerFactory
077: .getAttrDisplayHandler(component);
078: if (attrHandler != null) {
079: attrHandler.handleReturnValue((List) returnValue);
080: } else {
081: handleReturnValue(returnValue);
082: }
083: } catch (InstanceNotFoundException ie) {
084: logger.log(Level.SEVERE, "PSALI_CSPACC0005", ie);
085: throw new CommandException(getLocalizedString(
086: ERROR_MBEAN_INSTANCE_NOT_FOUND,
087: new Object[] { operation }), ie);
088: } catch (MBeanException me) {
089: logger.log(Level.SEVERE, "PSALI_CSPACC0006", me);
090: boolean psmbe = me.getCause() instanceof PSMBeanException;
091: if (getBooleanOption(OPT_DEBUG)) {
092: if (psmbe) {
093: String dbgMsg = me.getCause().getMessage();
094: if (dbgMsg != null) {
095: CLILogger.getInstance().printMessage(dbgMsg);
096: }
097: }
098: }
099: if (psmbe) {
100: throw new CommandException(
101: getLocalizedString(((PSMBeanException) me
102: .getCause()).getErrorKey()), me);
103: } else {
104: throw new CommandException(getLocalizedString(
105: ERROR_JMX_INVOKE, new Object[] { operation }),
106: me);
107: }
108: } catch (ReflectionException re) {
109: logger.log(Level.SEVERE, "PSALI_CSPACC0007", re);
110: throw new CommandException(getLocalizedString(
111: ERROR_MBEAN_REFLECTION_ERROR,
112: new Object[] { operation }), re);
113: } catch (Exception ex) {
114: logger.log(Level.SEVERE, "PSALI_CSPACC0010", ex);
115: throw new CommandException(
116: getLocalizedString(COMMAND_FAILED), ex);
117: } finally {
118: closeMBeanServerConnection();
119: }
120: }
121:
122: private void validateGetOptions(Map optionsMap, String component)
123: throws CommandValidationException {
124: validateBasicOptions(optionsMap);
125: if ("desktop".equals(component)) {
126: validateDesktopOptions(optionsMap);
127: } else if ("subscriptions".equals(component)) {
128: validateSubscriptionsOptions(optionsMap);
129: }
130: //Component owners to add more calls here as required
131: }
132:
133: private void validateBasicOptions(Map optionsMap)
134: throws CommandValidationException {
135: //validate the basic options
136: boolean global = Boolean
137: .valueOf(
138: (String) optionsMap
139: .get(AttrOptionConstants.OPT_GLOBAL))
140: .booleanValue();
141: boolean org = Boolean.valueOf(
142: (String) optionsMap.get(AttrOptionConstants.OPT_ORG))
143: .booleanValue();
144: boolean dn = optionsMap.containsKey(AttrOptionConstants.OPT_DN);
145: if (global) {
146: //global is not allowed with org and dn options
147: if (org || dn) {
148: throw new CommandValidationException(
149: getLocalizedString("error.psadmin.validation.combination.1"));
150: }
151: }
152: }
153:
154: private void validateDesktopOptions(Map map)
155: throws CommandValidationException {
156:
157: String portalId = getOption(OPT_PORTAL_ID);
158:
159: if (portalId == null) {
160: throw new CommandValidationException(
161: getLocalizedString("error.psadmin.portalid.not.specified"));
162: }
163:
164: //Following check is required if we have org as valid option for cli
165: boolean org = Boolean.valueOf(
166: (String) map.get(AttrOptionConstants.OPT_ORG))
167: .booleanValue();
168: if (org) {
169: throw new CommandValidationException(
170: getLocalizedString("error.psadmin.org.option.not.valid.for.desktop"));
171: }
172:
173: }
174:
175: private void validateSubscriptionsOptions(Map map)
176: throws CommandValidationException {
177:
178: String portalId = getOption(OPT_PORTAL_ID);
179:
180: if (portalId == null) {
181: throw new CommandValidationException(
182: getLocalizedString("error.psadmin.portalid.not.specified"));
183: }
184: }
185: }
|