001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.admin.cli.commands.monitoring;
006:
007: import com.sun.enterprise.cli.framework.CommandException;
008: import com.sun.enterprise.cli.framework.CommandValidationException;
009: import com.sun.enterprise.cli.framework.CLILogger;
010:
011: import javax.management.*;
012: import javax.management.openmbean.SimpleType;
013: import java.math.BigDecimal;
014: import java.math.BigInteger;
015: import java.text.DateFormat;
016: import java.text.ParseException;
017: import java.util.Iterator;
018: import java.util.Map;
019: import java.util.Set;
020: import java.util.TreeMap;
021: import java.io.IOException;
022:
023: public class SetMonitoringAttributes extends MonitoringBaseCommand {
024: private class CLIInput {
025: public boolean interactive = false;
026: public String nameProperties = null;
027: public String objectName = null;
028: public String type = null;
029: public boolean verbose = false;
030:
031: public String operandValues[];
032:
033: CLIInput() {
034: interactive = getBooleanOption(CLI_OPTION_INTERACTIVE);
035: nameProperties = getOption(CLI_OPTION_NAME_PROPERTIES);
036: objectName = getOption(CLI_OPTION_OBJECT_NAME);
037: type = getOption(CLI_OPTION_TYPE);
038: verbose = getBooleanOption(CLI_OPTION_VERBOSE);
039:
040: operandValues = getOperandValues();
041: }
042: }
043:
044: private StringBuffer setAttributeValues(ObjectName objectName,
045: Map nameValues) throws CommandException {
046: StringBuffer sb = new StringBuffer();
047:
048: MBeanInfo mBeanInfo = null;
049: try {
050: mBeanInfo = getCommandClient().getMBeanInfo(objectName);
051: } catch (ReflectionException e) {
052: throwError(e);
053: } catch (IOException e) {
054: throwError(e);
055: } catch (InstanceNotFoundException e) {
056: throwError(e);
057: } catch (MBeanException e) {
058: throwError(e);
059: }
060:
061: MBeanAttributeInfo[] mBeanAttributeInfos = mBeanInfo
062: .getAttributes();
063: for (int i = 0; i < mBeanAttributeInfos.length; i++) {
064: MBeanAttributeInfo mBeanAttributeInfo = mBeanAttributeInfos[i];
065: if (mBeanAttributeInfo.isWritable()) {
066: String attributeName = mBeanAttributeInfo.getName();
067: String attributeType = mBeanAttributeInfo.getType();
068:
069: if (attributeType.startsWith("[")) {
070: // Ignore, setting Array/ArrayElements via CLI is not supported.
071: } else if (attributeType
072: .equals("javax.management.openmbean.CompositeData")) {
073: // Ignore, setting CompositeData via CLI is not supported.
074: } else if (attributeType
075: .equals("javax.management.openmbean.TabularData")) {
076: // Ignore, setting TabularData via CLI is not supported.
077: } else {
078: if (nameValues.containsKey(attributeName)) {
079: String attributeValue = (String) nameValues
080: .get(attributeName);
081:
082: Object value = null;
083: if (attributeType.equals(SimpleType.BIGDECIMAL
084: .getClassName())) {
085: try {
086: value = new BigDecimal(attributeValue);
087: } catch (NumberFormatException e) {
088: throwError(e);
089: }
090: } else if (attributeType
091: .equals(SimpleType.BIGINTEGER
092: .getClassName())) {
093: try {
094: value = new BigInteger(attributeValue);
095: } catch (NumberFormatException e) {
096: throwError(e);
097: }
098: } else if (attributeType
099: .equals(SimpleType.BOOLEAN
100: .getClassName())) {
101: value = new Boolean(attributeValue);
102: } else if (attributeType.equals(SimpleType.BYTE
103: .getClassName())) {
104: try {
105: value = new Byte(attributeValue);
106: } catch (NumberFormatException e) {
107: throwError(e);
108: }
109: } else if (attributeType
110: .equals(SimpleType.CHARACTER
111: .getClassName())) {
112: value = new Character(attributeValue
113: .charAt(0));
114: } else if (attributeType.equals(SimpleType.DATE
115: .getClassName())) {
116: try {
117: value = DateFormat.getDateInstance()
118: .parse(attributeValue);
119: } catch (ParseException e) {
120: throwError(e);
121: }
122: } else if (attributeType
123: .equals(SimpleType.DOUBLE
124: .getClassName())) {
125: try {
126: value = new Double(attributeValue);
127: } catch (NumberFormatException e) {
128: throwError(e);
129: }
130: } else if (attributeType
131: .equals(SimpleType.FLOAT.getClassName())) {
132: try {
133: value = new Float(attributeValue);
134: } catch (NumberFormatException e) {
135: throwError(e);
136: }
137: } else if (attributeType
138: .equals(SimpleType.INTEGER
139: .getClassName())) {
140: try {
141: value = new Integer(attributeValue);
142: } catch (NumberFormatException e) {
143: throwError(e);
144: }
145: } else if (attributeType.equals(SimpleType.LONG
146: .getClassName())) {
147: try {
148: value = new Long(attributeValue);
149: } catch (NumberFormatException e) {
150: throwError(e);
151: }
152: } else if (attributeType
153: .equals(SimpleType.OBJECTNAME
154: .getClassName())) {
155: try {
156: value = new ObjectName(attributeValue);
157: } catch (MalformedObjectNameException e) {
158: throwError(e);
159: }
160: } else if (attributeType
161: .equals(SimpleType.SHORT.getClassName())) {
162: try {
163: value = new Short(attributeValue);
164: } catch (NumberFormatException e) {
165: throwError(e);
166: }
167: } else if (attributeType
168: .equals(SimpleType.STRING
169: .getClassName())) {
170: value = attributeValue;
171: } else if (attributeType.equals(SimpleType.VOID
172: .getClassName())) {
173: ; // attribute being void! impossible!!
174: }
175:
176: Attribute attribute = new Attribute(
177: attributeName, value);
178: try {
179: getCommandClient().setAttribute(objectName,
180: attribute);
181: sb
182: .append(LINE_SEPARATOR)
183: .append(TAB)
184: .append(
185: getMessage(
186: ATTRIBUTE_VALUE_SET_AS,
187: new Object[] {
188: attributeName,
189: value }));
190: } catch (ReflectionException e) {
191: throwError(e);
192: } catch (IOException e) {
193: throwError(e);
194: } catch (InstanceNotFoundException e) {
195: throwError(e);
196: } catch (MBeanException e) {
197: throwError(e);
198: }
199: }
200: }
201: }
202: }
203:
204: if (sb.length() == 0) {
205: sb.append(LINE_SEPARATOR).append(TAB).append(
206: getMessage(NO_MBEAN_ATTRIBUTES_FOUND, null));
207: }
208:
209: return sb;
210: }
211:
212: private Map getNameValues(String[] operandValues) {
213: Map nameValues = new TreeMap();
214:
215: for (int i = 0; i < operandValues.length;) {
216: String name = operandValues[i++];
217:
218: String value = null;
219: if (i < operandValues.length) {
220: value = operandValues[i++];
221: }
222:
223: nameValues.put(name, value);
224: }
225:
226: return nameValues;
227: }
228:
229: private String setMBeanAttributes(CLIInput cliInput)
230: throws CommandException {
231: StringBuffer sb = new StringBuffer();
232:
233: ObjectName onPattern = getObjectNamePattern(
234: cliInput.objectName, cliInput.type,
235: cliInput.nameProperties);
236: if (cliInput.verbose) {
237: CLILogger.getInstance().printMessage(
238: getMessage(
239: QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
240: new Object[] { onPattern }));
241: }
242:
243: Set objectNames = null;
244: try {
245: objectNames = getCommandClient()
246: .queryNames(onPattern, null);
247: } catch (ReflectionException e) {
248: throwError(e);
249: } catch (IOException e) {
250: throwError(e);
251: } catch (InstanceNotFoundException e) {
252: throwError(e);
253: } catch (MBeanException e) {
254: throwError(e);
255: }
256:
257: if (objectNames.isEmpty()) {
258: sb.append(getMessage(NO_MBEANS_FOUND, null));
259: } else {
260: Map nameValues = getNameValues(cliInput.operandValues);
261:
262: Iterator iterator = objectNames.iterator();
263: ObjectName on = null;
264: while (iterator.hasNext()) {
265: on = (ObjectName) iterator.next();
266: if (cliInput.interactive) {
267: if (!voteForContinue(on).booleanValue()) {
268: continue;
269: }
270: }
271:
272: if (sb.length() != 0) {
273: sb.append(LINE_SEPARATOR);
274: }
275: sb.append(on);
276:
277: sb.append(setAttributeValues(on, nameValues));
278: }
279: }
280:
281: return sb.toString();
282: }
283:
284: private void printCLIInput(CLIInput cliInput) {
285: if (cliInput.verbose) {
286: CLILogger.getInstance().printMessage(
287: getMessage(MBEAN_OBJECT_NAME_PATTERN,
288: new Object[] { cliInput.objectName }));
289: CLILogger.getInstance().printMessage(
290: getMessage(MBEAN_TYPE,
291: new Object[] { cliInput.type }));
292: CLILogger.getInstance().printMessage(
293: getMessage(MBEAN_NAME_PROPERTIES,
294: new Object[] { cliInput.nameProperties }));
295: }
296: }
297:
298: public void runCommand() throws CommandValidationException,
299: CommandException {
300: CLIInput cliInput = null;
301: Boolean connected = Boolean.FALSE;
302:
303: try {
304: gearUp();
305:
306: cliInput = new CLIInput();
307: printCLIInput(cliInput);
308:
309: connect(cliInput.verbose);
310: connected = Boolean.TRUE;
311:
312: String output = setMBeanAttributes(cliInput);
313: CLILogger.getInstance().printMessage(output);
314: } finally {
315: if (connected.booleanValue()) {
316: disconnect(cliInput.verbose);
317: }
318: }
319: }
320: }
|