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.*;
013: import java.lang.reflect.Array;
014: import java.util.*;
015: import java.util.regex.Matcher;
016: import java.util.regex.Pattern;
017: import java.io.IOException;
018:
019: public class GetMonitoringAttributes extends MonitoringBaseCommand {
020: private class CLIInput {
021: public boolean interactive = false;
022: public String nameProperties = null;
023: public String objectName = null;
024: public String type = null;
025: public boolean verbose = false;
026:
027: public String attributeNameRegExp = null;
028:
029: CLIInput() {
030: interactive = getBooleanOption(CLI_OPTION_INTERACTIVE);
031: nameProperties = getOption(CLI_OPTION_NAME_PROPERTIES);
032: objectName = getOption(CLI_OPTION_OBJECT_NAME);
033: type = getOption(CLI_OPTION_TYPE);
034: verbose = getBooleanOption(CLI_OPTION_VERBOSE);
035:
036: String operandValues[] = getOperandValues();
037: if (operandValues != null)
038: attributeNameRegExp = operandValues[0];
039: }
040: }
041:
042: private Map getAttributeNameValues(String attributeNamePrefix,
043: Object array) {
044: Map result = new TreeMap();
045:
046: int arrayLength = 0;
047: try {
048: arrayLength = Array.getLength(array);
049: } catch (IllegalArgumentException t) {
050: result.put(attributeNamePrefix, array);
051: return result;
052: }
053:
054: for (int j = 0; j < arrayLength; j++) {
055: result.putAll(getAttributeNameValues(attributeNamePrefix
056: + "." + j, Array.get(array, j)));
057: }
058:
059: return result;
060: }
061:
062: private Map getAttributeNameValues(String attributeName,
063: int dimension, Object array, Class arrayElementType) {
064: Map result = new TreeMap();
065:
066: int[] dimensions = new int[dimension];
067: Object[] arrayObjects = new Object[dimension];
068: dimensions[0] = Array.getLength(array);
069: arrayObjects[0] = Array.get(array, 0);
070: for (int i = 1; i < dimension; i++) {
071: dimensions[i] = Array.getLength(arrayObjects[i - 1]);
072: arrayObjects[i] = Array.get(arrayObjects[i - 1], 0);
073: }
074:
075: Object arrayInner = Array.newInstance(arrayElementType,
076: dimensions);
077: arrayInner = array;
078:
079: for (int i = 0; i < dimensions[0]; i++) {
080: result.putAll(getAttributeNameValues(attributeName + "."
081: + i, Array.get(arrayInner, i)));
082: }
083:
084: return result;
085: }
086:
087: private Map getAttributeNameValues(String attributeName,
088: CompositeData compositeData) {
089: Map result = new TreeMap();
090:
091: CompositeType compositeType = compositeData.getCompositeType();
092: Set itemNames = compositeType.keySet();
093: for (Iterator iterator = itemNames.iterator(); iterator
094: .hasNext();) {
095: String itemName = (String) iterator.next();
096: OpenType itemType = compositeType.getType(itemName);
097: Object itemValue = compositeData.get(itemName);
098:
099: if (itemType instanceof ArrayType) {
100: ArrayType arrayType = (ArrayType) itemType;
101:
102: int dimension = arrayType.getDimension();
103: String className = arrayType.getClassName();
104: String elementType = className.substring(dimension,
105: dimension + 1);
106: if (elementType.equals("B")) {
107: result.putAll(getAttributeNameValues(attributeName
108: + "." + itemName, dimension, itemValue,
109: byte.class));
110: } else if (elementType.equals("C")) {
111: result.putAll(getAttributeNameValues(attributeName
112: + "." + itemName, dimension, itemValue,
113: char.class));
114: } else if (elementType.equals("D")) {
115: result.putAll(getAttributeNameValues(attributeName
116: + "." + itemName, dimension, itemValue,
117: double.class));
118: } else if (elementType.equals("F")) {
119: result.putAll(getAttributeNameValues(attributeName
120: + "." + itemName, dimension, itemValue,
121: float.class));
122: } else if (elementType.equals("I")) {
123: result.putAll(getAttributeNameValues(attributeName
124: + "." + itemName, dimension, itemValue,
125: int.class));
126: } else if (elementType.equals("J")) {
127: result.putAll(getAttributeNameValues(attributeName
128: + "." + itemName, dimension, itemValue,
129: long.class));
130: } else if (elementType.equals("L")) {
131: try {
132: Class arrayElementClass = Class
133: .forName(className);
134: result
135: .putAll(getAttributeNameValues(
136: attributeName + "." + itemName,
137: dimension, itemValue,
138: arrayElementClass));
139: } catch (ClassNotFoundException cnfe) {
140: result.putAll(getAttributeNameValues(
141: attributeName + "." + itemName,
142: dimension, itemValue, Object.class));
143: }
144: } else if (elementType.equals("S")) {
145: result.putAll(getAttributeNameValues(attributeName
146: + "." + itemName, dimension, itemValue,
147: short.class));
148: } else if (elementType.equals("Z")) {
149: result.putAll(getAttributeNameValues(attributeName
150: + "." + itemName, dimension, itemValue,
151: boolean.class));
152: } else {
153: }
154: } else if (itemType instanceof CompositeType) {
155: result.putAll(getAttributeNameValues(attributeName
156: + "." + itemName, (CompositeData) itemValue));
157: } else if (itemType instanceof SimpleType) {
158: result.put(attributeName + "." + itemName, itemValue);
159: } else if (itemType instanceof TabularType) {
160: result.putAll(getAttributeNameValues(attributeName
161: + "." + itemName, (TabularData) itemValue));
162: }
163: }
164:
165: return result;
166: }
167:
168: private String getKeyValuesPrefix(OpenType openType, Object value) {
169: if (openType instanceof ArrayType) {
170: return "ArrayTypeKey"; // not supported, doesn't make sense
171: } else if (openType instanceof CompositeType) {
172: return "CompositeTypeKey"; // not supported, doesn't make sense
173: } else if (openType instanceof SimpleType) {
174: return value.toString();
175: } else if (openType instanceof TabularType) {
176: return "TabularTypeKey"; // not supported, doesn't make sense
177: }
178:
179: return "UnknownOpenType"; // should not reach here
180: }
181:
182: private String getKeyValuesPrefix(List indexNames,
183: CompositeData compositeData) {
184: StringBuffer result = new StringBuffer();
185: for (Iterator iterator = indexNames.iterator(); iterator
186: .hasNext();) {
187: String itemName = (String) iterator.next();
188:
189: Object itemValue = compositeData.get(itemName);
190: OpenType itemType = compositeData.getCompositeType()
191: .getType(itemName);
192:
193: result.append(".").append(
194: getKeyValuesPrefix(itemType, itemValue));
195: }
196:
197: return result.toString();
198: }
199:
200: private Map getAttributeNameValues(String attributeName,
201: TabularData tabularData) {
202: Map result = new TreeMap();
203:
204: TabularType tabularType = tabularData.getTabularType();
205: List indexNames = tabularType.getIndexNames();
206: Collection rows = tabularData.values();
207: for (Iterator iterator = rows.iterator(); iterator.hasNext();) {
208: CompositeData row = (CompositeData) iterator.next();
209: result.putAll(getAttributeNameValues(attributeName
210: + getKeyValuesPrefix(indexNames, row), row));
211: }
212:
213: return result;
214: }
215:
216: protected Map getAttributeNameValues(ObjectName objectName)
217: throws CommandException {
218: Map result = new TreeMap();
219: MBeanInfo mBeanInfo = null;
220: try {
221: mBeanInfo = getCommandClient().getMBeanInfo(objectName);
222: } catch (ReflectionException e) {
223: throwError(e);
224: } catch (IOException e) {
225: throwError(e);
226: } catch (InstanceNotFoundException e) {
227: throwError(e);
228: } catch (MBeanException e) {
229: throwError(e);
230: }
231:
232: MBeanAttributeInfo[] mBeanAttributeInfos = mBeanInfo
233: .getAttributes();
234: for (int i = 0; i < mBeanAttributeInfos.length; i++) {
235: MBeanAttributeInfo mBeanAttributeInfo = mBeanAttributeInfos[i];
236:
237: if (mBeanAttributeInfo.isReadable()) {
238: String attributeName = mBeanAttributeInfo.getName();
239: String attributeType = mBeanAttributeInfo.getType();
240:
241: try {
242: if (attributeType.startsWith("[")) {
243: int dimension = attributeType.lastIndexOf("[") + 1;
244: Object itemValue = getCommandClient()
245: .getAttribute(objectName, attributeName);
246:
247: String elementType = attributeType.substring(
248: dimension, dimension + 1);
249: if (elementType.equals("B")) {
250: result.putAll(getAttributeNameValues(
251: attributeName, dimension,
252: itemValue, byte.class));
253: } else if (elementType.equals("C")) {
254: result.putAll(getAttributeNameValues(
255: attributeName, dimension,
256: itemValue, char.class));
257: } else if (elementType.equals("D")) {
258: result.putAll(getAttributeNameValues(
259: attributeName, dimension,
260: itemValue, double.class));
261: } else if (elementType.equals("F")) {
262: result.putAll(getAttributeNameValues(
263: attributeName, dimension,
264: itemValue, float.class));
265: } else if (elementType.equals("I")) {
266: result.putAll(getAttributeNameValues(
267: attributeName, dimension,
268: itemValue, int.class));
269: } else if (elementType.equals("J")) {
270: result.putAll(getAttributeNameValues(
271: attributeName, dimension,
272: itemValue, long.class));
273: } else if (elementType.equals("L")) {
274: String arrayElementType = attributeType
275: .substring(dimension + 1,
276: attributeType.length()
277: - ";".length());
278: try {
279: Class arrayElementClass = Class
280: .forName(arrayElementType);
281: result.putAll(getAttributeNameValues(
282: attributeName, dimension,
283: itemValue, arrayElementClass));
284: } catch (ClassNotFoundException cnfe) {
285: result.putAll(getAttributeNameValues(
286: attributeName, dimension,
287: itemValue, Object.class));
288: }
289: } else if (elementType.equals("S")) {
290: result.putAll(getAttributeNameValues(
291: attributeName, dimension,
292: itemValue, short.class));
293: } else if (elementType.equals("Z")) {
294: result.putAll(getAttributeNameValues(
295: attributeName, dimension,
296: itemValue, boolean.class));
297: } else {
298: }
299: } else if (attributeType
300: .equals("javax.management.openmbean.CompositeData")) {
301: CompositeData compositeData = (CompositeData) getCommandClient()
302: .getAttribute(objectName, attributeName);
303: result.putAll(getAttributeNameValues(
304: attributeName, compositeData));
305: } else if (attributeType
306: .equals("javax.management.openmbean.TabularData")) {
307: TabularData tabularData = (TabularData) getCommandClient()
308: .getAttribute(objectName, attributeName);
309: result.putAll(getAttributeNameValues(
310: attributeName, tabularData));
311: } else {
312: Object value = getCommandClient().getAttribute(
313: objectName, attributeName);
314: result.put(attributeName, value);
315: }
316: } catch (Throwable t) {
317: System.err.println(objectName);
318: System.err.println(getLocalizedString(
319: MONITORING_PREFIX + WARNING
320: + COULD_NOT_GET_ATTRIBUTE,
321: new Object[] { attributeName }));
322: }
323: }
324: }
325:
326: return result;
327: }
328:
329: private StringBuffer getMBeanAttributes(ObjectName on,
330: Pattern attributeNamePattern) throws CommandException {
331: StringBuffer sb = new StringBuffer();
332:
333: Map nameValues = getAttributeNameValues(on);
334: Set names = nameValues.keySet();
335: Iterator iterator = names.iterator();
336: while (iterator.hasNext()) {
337: String name = (String) iterator.next();
338: Matcher matcher = attributeNamePattern.matcher(name);
339: if (matcher.matches())
340: sb.append(LINE_SEPARATOR).append(TAB).append(name)
341: .append(EQUAL_TO).append(nameValues.get(name));
342: }
343:
344: if (sb.length() == 0) {
345: sb.append(LINE_SEPARATOR).append(TAB).append(
346: getMessage(NO_MBEAN_ATTRIBUTES_FOUND, null));
347: }
348:
349: return sb;
350: }
351:
352: private String getMBeanAttributes(CLIInput cliInput)
353: throws CommandException {
354: StringBuffer sb = new StringBuffer();
355:
356: ObjectName onPattern = getObjectNamePattern(
357: cliInput.objectName, cliInput.type,
358: cliInput.nameProperties);
359: Pattern attributeNamePattern = getAttributeNamePattern(cliInput.attributeNameRegExp);
360: if (cliInput.verbose) {
361: CLILogger.getInstance().printMessage(
362: getMessage(
363: QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
364: new Object[] { onPattern }));
365: CLILogger
366: .getInstance()
367: .printMessage(
368: getMessage(
369: FILTERING_MBEAN_ATTRIBUTE_NAMES_WITH_PATTERN,
370: new Object[] { attributeNamePattern }));
371: }
372:
373: Set objectNames = null;
374: try {
375: objectNames = getCommandClient()
376: .queryNames(onPattern, null);
377: } catch (ReflectionException e) {
378: throwError(e);
379: } catch (IOException e) {
380: throwError(e);
381: } catch (InstanceNotFoundException e) {
382: throwError(e);
383: } catch (MBeanException e) {
384: throwError(e);
385: }
386:
387: if (objectNames.isEmpty()) {
388: sb.append(getMessage(NO_MBEANS_FOUND, null));
389: } else {
390: Iterator iterator = objectNames.iterator();
391: ObjectName on = null;
392: while (iterator.hasNext()) {
393: on = (ObjectName) iterator.next();
394: if (cliInput.interactive) {
395: if (!voteForContinue(on).booleanValue()) {
396: continue;
397: }
398: }
399:
400: if (sb.length() != 0) {
401: sb.append(LINE_SEPARATOR);
402: }
403: sb.append(on);
404:
405: sb.append(getMBeanAttributes(on, attributeNamePattern));
406: }
407: }
408:
409: return sb.toString();
410: }
411:
412: private void printCLIInput(CLIInput cliInput) {
413: if (cliInput.verbose) {
414: CLILogger.getInstance().printMessage(
415: getMessage(MBEAN_OBJECT_NAME_PATTERN,
416: new Object[] { cliInput.objectName }));
417: CLILogger.getInstance().printMessage(
418: getMessage(MBEAN_TYPE,
419: new Object[] { cliInput.type }));
420: CLILogger.getInstance().printMessage(
421: getMessage(MBEAN_NAME_PROPERTIES,
422: new Object[] { cliInput.nameProperties }));
423: CLILogger
424: .getInstance()
425: .printMessage(
426: getMessage(
427: ATTRIBUTE_NAME_REGEXP,
428: new Object[] { cliInput.attributeNameRegExp }));
429: }
430: }
431:
432: public void runCommand() throws CommandValidationException,
433: CommandException {
434: CLIInput cliInput = null;
435: Boolean connected = Boolean.FALSE;
436:
437: try {
438: gearUp();
439:
440: cliInput = new CLIInput();
441: printCLIInput(cliInput);
442:
443: connect(cliInput.verbose);
444: connected = Boolean.TRUE;
445:
446: String output = getMBeanAttributes(cliInput);
447: CLILogger.getInstance().printMessage(output);
448: } finally {
449: if (connected.booleanValue()) {
450: disconnect(cliInput.verbose);
451: }
452: }
453: }
454: }
|