001: /**
002: * $Id: SetAttributeCommand.java,v 1.10 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: //CLI framework
016: import com.sun.enterprise.cli.framework.*;
017:
018: //JMX
019: import javax.management.MBeanServerConnection;
020: import javax.management.ObjectName;
021: import javax.management.MBeanException;
022: import javax.management.ReflectionException;
023: import javax.management.MalformedObjectNameException;
024: import javax.management.InstanceNotFoundException;
025:
026: import com.sun.portal.admin.common.util.AdminUtil;
027: import com.sun.portal.admin.common.PSMBeanException;
028:
029: import java.util.Collections;
030: import java.util.List;
031: import java.util.Vector;
032: import java.util.Iterator;
033: import java.util.Map;
034: import java.util.logging.Logger;
035: import java.util.logging.Level;
036:
037: import com.sun.portal.admin.common.AttrOptionConstants;
038:
039: public class SetAttributeCommand 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 operation = "";
053:
054: try {
055: String component = getOption(AttrOptionConstants.OPT_COMPONENT);
056: List values = null;
057: Vector v = getOperands();
058: if (v == null || v.isEmpty()) {
059: values = Collections.EMPTY_LIST;
060: } else {
061: values = getMultiValueFromString((String) v
062: .firstElement());
063: }
064:
065: validateSetOptions(values, getOptions(), component);
066:
067: MBeanServerConnection mbsc = getMBeanServerConnection(
068: getUserId(), getPassword(), getHost());
069:
070: ObjectName objectName = portalId == null ? AdminUtil
071: .getPortalDomainMBeanObjectName(getDomainId())
072: : AdminUtil.getPortalMBeanObjectName(getDomainId(),
073: portalId);
074:
075: Map optMap = getOptions();
076: //Process the options to convert the add and remove option values
077: //from a single string to multivalued List
078: //process add if it exists
079: if (optMap.containsKey(AttrOptionConstants.OPT_ADD)) {
080: List optValues = getMultiValueOption(AttrOptionConstants.OPT_ADD);
081: //remove the original key-value since it is key-string pair
082: optMap.remove(AttrOptionConstants.OPT_ADD);
083: //then put the new key-list pair into the map
084: optMap.put(AttrOptionConstants.OPT_ADD, optValues);
085: }
086: //process remove if it exists
087: if (optMap.containsKey(AttrOptionConstants.OPT_REMOVE)) {
088: List optValues = getMultiValueOption(AttrOptionConstants.OPT_REMOVE);
089: optMap.remove(AttrOptionConstants.OPT_REMOVE);
090: optMap.put(AttrOptionConstants.OPT_REMOVE, optValues);
091: }
092:
093: optMap.put("operation", "set");
094: // Setting the params and signature
095: Object[] params = { values, optMap };
096: String[] signature = { "java.util.List", "java.util.Map" };
097: operation = "setAttribute";
098:
099: mbsc.invoke(objectName, operation, params, signature);
100:
101: } catch (InstanceNotFoundException ie) {
102: logger.log(Level.SEVERE, "PSALI_CSPACC0005", ie);
103: throw new CommandException(getLocalizedString(
104: ERROR_MBEAN_INSTANCE_NOT_FOUND,
105: new Object[] { operation }), ie);
106: } catch (MBeanException me) {
107: logger.log(Level.SEVERE, "PSALI_CSPACC0006", me);
108: boolean psmbe = me.getCause() instanceof PSMBeanException;
109: if (getBooleanOption(OPT_DEBUG)) {
110: if (psmbe) {
111: String dbgMsg = me.getCause().getMessage();
112: if (dbgMsg != null) {
113: CLILogger.getInstance().printMessage(dbgMsg);
114: }
115: }
116: }
117: if (psmbe) {
118: throw new CommandException(
119: getLocalizedString(((PSMBeanException) me
120: .getCause()).getErrorKey()), me);
121: } else {
122: throw new CommandException(
123: getLocalizedString(ERROR_JMX_INVOKE), me);
124: }
125: } catch (ReflectionException re) {
126: logger.log(Level.SEVERE, "PSALI_CSPACC0007", re);
127: throw new CommandException(
128: getLocalizedString(ERROR_JMX_INVOKE), re);
129: } catch (CommandException ce) {
130: logger.log(Level.SEVERE, "PSALI_CSPACC0008", ce);
131: throw ce;
132: } catch (CommandValidationException cve) {
133: logger.log(Level.SEVERE, "PSALI_CSPACC0013", cve);
134: throw cve;
135: } catch (Exception ex) {
136: logger.log(Level.SEVERE, "PSALI_CSPACC0010", ex);
137: throw new CommandException(
138: getLocalizedString(COMMAND_FAILED), ex);
139: } finally {
140: closeMBeanServerConnection();
141: }
142: }
143:
144: private void validateSetOptions(List values, Map optionsMap,
145: String component) throws CommandValidationException {
146: validateBasicOptions(optionsMap);
147: validateBasicSetOptions(values, optionsMap);
148: if ("desktop".equals(component)) {
149: validateDesktopOptions(optionsMap);
150: } else if ("subscriptions".equals(component)) {
151: validateSubscriptionsOptions(optionsMap);
152: }
153: //Component owners to add more calls here as required
154:
155: }
156:
157: private void validateBasicOptions(Map optionsMap)
158: throws CommandValidationException {
159: //validate the basic options
160: boolean global = Boolean
161: .valueOf(
162: (String) optionsMap
163: .get(AttrOptionConstants.OPT_GLOBAL))
164: .booleanValue();
165: boolean org = Boolean.valueOf(
166: (String) optionsMap.get(AttrOptionConstants.OPT_ORG))
167: .booleanValue();
168: boolean dn = optionsMap.containsKey(AttrOptionConstants.OPT_DN);
169: if (global) {
170: //global is not allowed with org and dn options
171: if (org || dn) {
172: throw new CommandValidationException(
173: getLocalizedString("error.psadmin.validation.combination.1"));
174: }
175: }
176: }
177:
178: private void validateBasicSetOptions(List values, Map optionsMap)
179: throws CommandValidationException {
180: boolean inherit = Boolean.valueOf(
181: (String) optionsMap
182: .get(AttrOptionConstants.OPT_INHERIT))
183: .booleanValue();
184: boolean add = optionsMap
185: .containsKey(AttrOptionConstants.OPT_ADD);
186: boolean remove = optionsMap
187: .containsKey(AttrOptionConstants.OPT_REMOVE);
188: boolean dn = optionsMap.containsKey(AttrOptionConstants.OPT_DN);
189:
190: //The following logic can be simplified by not testing everything
191: //in every if. Instead the tests can grow shorter as execution
192: //falls thru the ifs. But none of the tests are very expensive
193: //and this way these can be separated out easily later, if needed.
194: if (inherit) {
195: //if inherit is true, then all others are invalid
196: if (add || remove || values.size() != 0) {
197: //inherit is not allowed with other options or operand
198: throw new CommandValidationException(
199: getLocalizedString("error.psadmin.validation.combination.2"));
200: }
201: if (!dn) {
202: //dn must be specified with inherit
203: throw new CommandValidationException(
204: getLocalizedString("error.psadmin.validation.combination.6"));
205: }
206: } else if (add || remove) {
207: //if add is specified, then only remove is valid
208: //if remove is specified, then only add is valid
209: //if add and remove are given, then all others are invalid
210: if (inherit || !values.isEmpty()) {
211: //add/remove is not allowed with other options or operand
212: throw new CommandValidationException(
213: getLocalizedString("error.psadmin.validation.combination.3"));
214: }
215: } else if (values.size() == 1) {
216: //if values are given as operand then, all others are invalid
217: if (inherit || add || remove) {
218: //operand is not allowed with other options
219: throw new CommandValidationException(
220: getLocalizedString("error.psadmin.validation.combination.4"));
221: }
222: } else if (values.isEmpty()) {
223: //if neither any options nor values, then its invalid
224: if (!inherit && !add && !remove) {
225: //neither options nor operands specified
226: throw new CommandValidationException(
227: getLocalizedString("error.psadmin.validation.combination.5"));
228: }
229: }
230: }
231:
232: private void validateDesktopOptions(Map map)
233: throws CommandValidationException {
234:
235: String portalId = getOption(OPT_PORTAL_ID);
236:
237: if (portalId == null) {
238: throw new CommandValidationException(
239: getLocalizedString("error.psadmin.portalid.not.specified"));
240: }
241:
242: //Following check is required if we have org as valid option for cli
243: boolean org = Boolean.valueOf(
244: (String) map.get(AttrOptionConstants.OPT_ORG))
245: .booleanValue();
246: if (org) {
247: throw new CommandValidationException(
248: getLocalizedString("error.psadmin.org.option.not.valid.for.desktop"));
249: }
250:
251: }
252:
253: private void validateSubscriptionsOptions(Map map)
254: throws CommandValidationException {
255:
256: String portalId = getOption(OPT_PORTAL_ID);
257:
258: if (portalId == null) {
259: throw new CommandValidationException(
260: getLocalizedString("error.psadmin.portalid.not.specified"));
261: }
262: }
263:
264: }
|