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.util.*;
014: import java.util.regex.Matcher;
015: import java.util.regex.Pattern;
016: import java.io.IOException;
017:
018: public class GetMonitoringAttributeNames extends MonitoringBaseCommand {
019: private class CLIInput {
020: public boolean interactive = false;
021: public String nameProperties = null;
022: public String objectName = null;
023: public String type = null;
024: public boolean verbose = false;
025:
026: public String attributeNameRegExp = null;
027:
028: CLIInput() {
029: interactive = getBooleanOption(CLI_OPTION_INTERACTIVE);
030: nameProperties = getOption(CLI_OPTION_NAME_PROPERTIES);
031: objectName = getOption(CLI_OPTION_OBJECT_NAME);
032: type = getOption(CLI_OPTION_TYPE);
033: verbose = getBooleanOption(CLI_OPTION_VERBOSE);
034:
035: String operandValues[] = getOperandValues();
036: if (operandValues != null)
037: attributeNameRegExp = operandValues[0];
038: }
039: }
040:
041: private Set getAttributeNames(String attributeName,
042: CompositeData compositeData) {
043: Set result = new TreeSet();
044:
045: CompositeType compositeType = compositeData.getCompositeType();
046: Set itemNames = compositeType.keySet();
047: for (Iterator iterator = itemNames.iterator(); iterator
048: .hasNext();) {
049: String itemName = (String) iterator.next();
050: OpenType itemType = compositeType.getType(itemName);
051: Object itemValue = compositeData.get(itemName);
052:
053: if (itemType instanceof ArrayType) {
054: ArrayType arrayType = (ArrayType) itemType;
055: String className = arrayType.getClassName();
056: int dimension = className.lastIndexOf("[") + 1;
057: StringBuffer nameSuffix = new StringBuffer();
058: for (int j = 0; j < dimension; j++) {
059: nameSuffix.append(".").append("<index>");
060: }
061: result.add(attributeName + "." + itemName + nameSuffix);
062: } else if (itemType instanceof CompositeType) {
063: result.addAll(getAttributeNames(attributeName + "."
064: + itemName, (CompositeData) itemValue));
065: } else if (itemType instanceof SimpleType) {
066: result.add(attributeName + "." + itemName);
067: } else if (itemType instanceof TabularType) {
068: result.addAll(getAttributeNames(attributeName + "."
069: + itemName, (TabularData) itemValue));
070: }
071: }
072:
073: return result;
074: }
075:
076: private Set getAttributeNames(String attributeName,
077: TabularData tabularData) {
078: Set result = new TreeSet();
079:
080: TabularType tabularType = tabularData.getTabularType();
081: List indexNames = tabularType.getIndexNames();
082: StringBuffer indexNamesSuffix = new StringBuffer();
083: for (Iterator iterator = indexNames.iterator(); iterator
084: .hasNext();) {
085: indexNamesSuffix.append(".").append("<").append(
086: iterator.next()).append(">");
087: }
088:
089: Collection rows = tabularData.values();
090: for (Iterator iterator = rows.iterator(); iterator.hasNext();) {
091: CompositeData row = (CompositeData) iterator.next();
092: result.addAll(getAttributeNames(attributeName
093: + indexNamesSuffix, row));
094: }
095:
096: return result;
097: }
098:
099: private Set getAttributeNames(ObjectName objectName)
100: throws CommandException {
101: Set result = new TreeSet();
102: MBeanInfo mBeanInfo = null;
103: try {
104: mBeanInfo = getCommandClient().getMBeanInfo(objectName);
105: } catch (ReflectionException e) {
106: throwError(e);
107: } catch (IOException e) {
108: throwError(e);
109: } catch (InstanceNotFoundException e) {
110: throwError(e);
111: } catch (MBeanException e) {
112: throwError(e);
113: }
114:
115: MBeanAttributeInfo[] mBeanAttributeInfos = mBeanInfo
116: .getAttributes();
117: for (int i = 0; i < mBeanAttributeInfos.length; i++) {
118: MBeanAttributeInfo mBeanAttributeInfo = mBeanAttributeInfos[i];
119:
120: String attributeName = mBeanAttributeInfo.getName();
121: String attributeType = mBeanAttributeInfo.getType();
122:
123: if (attributeType.startsWith("[")) {
124: int dimension = attributeType.lastIndexOf("[") + 1;
125: StringBuffer nameSuffix = new StringBuffer();
126: for (int j = 0; j < dimension; j++) {
127: nameSuffix.append(".").append("<index>");
128: }
129: result.add(attributeName + nameSuffix);
130: } else if (attributeType
131: .equals("javax.management.openmbean.CompositeData")) {
132: CompositeData compositeData = null;
133: try {
134: compositeData = (CompositeData) getCommandClient()
135: .getAttribute(objectName, attributeName);
136: } catch (ReflectionException e) {
137: throwError(e);
138: } catch (IOException e) {
139: throwError(e);
140: } catch (InstanceNotFoundException e) {
141: throwError(e);
142: } catch (MBeanException e) {
143: throwError(e);
144: }
145: result.addAll(getAttributeNames(attributeName,
146: compositeData));
147: } else if (attributeType
148: .equals("javax.management.openmbean.TabularData")) {
149: TabularData tabularData = null;
150: try {
151: tabularData = (TabularData) getCommandClient()
152: .getAttribute(objectName, attributeName);
153: } catch (ReflectionException e) {
154: throwError(e);
155: } catch (IOException e) {
156: throwError(e);
157: } catch (InstanceNotFoundException e) {
158: throwError(e);
159: } catch (MBeanException e) {
160: throwError(e);
161: }
162: result.addAll(getAttributeNames(attributeName,
163: tabularData));
164: } else {
165: result.add(attributeName);
166: }
167: }
168:
169: return result;
170: }
171:
172: private StringBuffer getMBeanAttributeNames(ObjectName on,
173: Pattern attributeNamePattern) throws CommandException {
174: StringBuffer sb = new StringBuffer();
175:
176: Set names = getAttributeNames(on);
177: Iterator iterator = names.iterator();
178: while (iterator.hasNext()) {
179: String name = (String) iterator.next();
180: Matcher matcher = attributeNamePattern.matcher(name);
181: if (matcher.matches())
182: sb.append(LINE_SEPARATOR).append(TAB).append(name);
183: }
184:
185: if (sb.length() == 0) {
186: sb.append(LINE_SEPARATOR).append(TAB).append(
187: getMessage(NO_MBEAN_ATTRIBUTES_FOUND, null));
188: }
189:
190: return sb;
191: }
192:
193: private String getMBeanAttributeNames(CLIInput cliInput)
194: throws CommandException {
195: StringBuffer sb = new StringBuffer();
196:
197: ObjectName onPattern = getObjectNamePattern(
198: cliInput.objectName, cliInput.type,
199: cliInput.nameProperties);
200: Pattern attributeNamePattern = getAttributeNamePattern(cliInput.attributeNameRegExp);
201: if (cliInput.verbose) {
202: CLILogger.getInstance().printMessage(
203: getMessage(
204: QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
205: new Object[] { onPattern }));
206: CLILogger
207: .getInstance()
208: .printMessage(
209: getMessage(
210: FILTERING_MBEAN_ATTRIBUTE_NAMES_WITH_PATTERN,
211: new Object[] { attributeNamePattern }));
212: }
213:
214: Set objectNames = null;
215: try {
216: objectNames = getCommandClient()
217: .queryNames(onPattern, null);
218: } catch (ReflectionException e) {
219: throwError(e);
220: } catch (IOException e) {
221: throwError(e);
222: } catch (InstanceNotFoundException e) {
223: throwError(e);
224: } catch (MBeanException e) {
225: throwError(e);
226: }
227:
228: if (objectNames.isEmpty()) {
229: sb.append(getMessage(NO_MBEANS_FOUND, null));
230: } else {
231: Iterator iterator = objectNames.iterator();
232: ObjectName on = null;
233: while (iterator.hasNext()) {
234: on = (ObjectName) iterator.next();
235: if (cliInput.interactive) {
236: if (!voteForContinue(on).booleanValue()) {
237: continue;
238: }
239: }
240:
241: if (sb.length() != 0) {
242: sb.append(LINE_SEPARATOR);
243: }
244: sb.append(on);
245:
246: sb.append(getMBeanAttributeNames(on,
247: attributeNamePattern));
248: }
249: }
250:
251: return sb.toString();
252: }
253:
254: private void printCLIInput(CLIInput cliInput) {
255: if (cliInput.verbose) {
256: CLILogger.getInstance().printMessage(
257: getMessage(MBEAN_OBJECT_NAME_PATTERN,
258: new Object[] { cliInput.objectName }));
259: CLILogger.getInstance().printMessage(
260: getMessage(MBEAN_TYPE,
261: new Object[] { cliInput.type }));
262: CLILogger.getInstance().printMessage(
263: getMessage(MBEAN_NAME_PROPERTIES,
264: new Object[] { cliInput.nameProperties }));
265: CLILogger
266: .getInstance()
267: .printMessage(
268: getMessage(
269: ATTRIBUTE_NAME_REGEXP,
270: new Object[] { cliInput.attributeNameRegExp }));
271: }
272: }
273:
274: public void runCommand() throws CommandValidationException,
275: CommandException {
276: CLIInput cliInput = null;
277: Boolean connected = Boolean.FALSE;
278:
279: try {
280: gearUp();
281:
282: cliInput = new CLIInput();
283: printCLIInput(cliInput);
284:
285: connect(cliInput.verbose);
286: connected = Boolean.TRUE;
287:
288: String output = getMBeanAttributeNames(cliInput);
289: CLILogger.getInstance().printMessage(output);
290: } finally {
291: if (connected.booleanValue()) {
292: disconnect(cliInput.verbose);
293: }
294: }
295: }
296: }
|