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.ObjectName;
012: import javax.management.ReflectionException;
013: import javax.management.InstanceNotFoundException;
014: import javax.management.MBeanException;
015: import javax.management.MBeanInfo;
016: import javax.management.MBeanAttributeInfo;
017: import javax.management.MBeanOperationInfo;
018: import javax.management.MBeanParameterInfo;
019: import javax.management.MBeanNotificationInfo;
020: import java.util.regex.Pattern;
021: import java.util.regex.Matcher;
022: import java.util.Set;
023: import java.util.Iterator;
024: import java.io.IOException;
025:
026: public class GetMBeansInformation extends MonitoringBaseCommand {
027: private class CLIInput {
028: public boolean interactive = false;
029: public String nameProperties = null;
030: public String objectName = null;
031: public String type = null;
032: public boolean verbose = false;
033:
034: public String mBeanNameRegExp = null;
035:
036: CLIInput() {
037: interactive = getBooleanOption(CLI_OPTION_INTERACTIVE);
038: nameProperties = getOption(CLI_OPTION_NAME_PROPERTIES);
039: objectName = getOption(CLI_OPTION_OBJECT_NAME);
040: type = getOption(CLI_OPTION_TYPE);
041: verbose = getBooleanOption(CLI_OPTION_VERBOSE);
042:
043: String[] operandValues = getOperandValues();
044: if (operandValues != null) {
045: mBeanNameRegExp = operandValues[0];
046: }
047: }
048: }
049:
050: private StringBuffer getMBeanDescription(String description) {
051: StringBuffer sb = new StringBuffer();
052:
053: sb.append(LINE_SEPARATOR).append(getMessage(DESCRIPTION, null));
054: sb.append(LINE_SEPARATOR);
055: sb.append(LINE_SEPARATOR).append(description);
056: sb.append(LINE_SEPARATOR);
057:
058: return sb;
059: }
060:
061: private StringBuffer getMBeanAttributesInformation(
062: MBeanAttributeInfo[] mBeanAttributeInfos) {
063: StringBuffer sb = new StringBuffer();
064:
065: sb.append(LINE_SEPARATOR).append(
066: getMessage(MBEAN_ATTRIBUTES, null));
067: if (mBeanAttributeInfos != null) {
068: if (mBeanAttributeInfos.length == 0) {
069: sb.append(LINE_SEPARATOR).append(
070: getMessage(MBEAN_NO_ATTRIBUTES_FOUND, null));
071: sb.append(LINE_SEPARATOR);
072: } else {
073: for (int i = 0; i < mBeanAttributeInfos.length; i++) {
074: MBeanAttributeInfo mBeanAttributeInfo = mBeanAttributeInfos[i];
075:
076: sb.append(LINE_SEPARATOR).append(
077: getMessage(NAME,
078: new Object[] { mBeanAttributeInfo
079: .getName() }));
080: sb.append(LINE_SEPARATOR).append(
081: getMessage(DESCRIPTION_PROMPT,
082: new Object[] { mBeanAttributeInfo
083: .getDescription() }));
084: sb.append(LINE_SEPARATOR).append(
085: getMessage(TYPE,
086: new Object[] { mBeanAttributeInfo
087: .getType() }));
088: sb
089: .append(LINE_SEPARATOR)
090: .append(
091: getMessage(
092: MBEAN_ATTRIBUTE_READABLE,
093: new Object[] { mBeanAttributeInfo
094: .isReadable() ? Boolean.TRUE
095: : Boolean.FALSE }));
096: sb
097: .append(LINE_SEPARATOR)
098: .append(
099: getMessage(
100: MBEAN_ATTRIBUTE_WRITABLE,
101: new Object[] { mBeanAttributeInfo
102: .isWritable() ? Boolean.TRUE
103: : Boolean.FALSE }));
104: sb.append(LINE_SEPARATOR);
105: }
106: }
107: } else {
108: sb.append(LINE_SEPARATOR).append(
109: getMessage(MBEAN_NO_ATTRIBUTES_FOUND, null));
110: sb.append(LINE_SEPARATOR);
111: }
112:
113: return sb;
114: }
115:
116: private StringBuffer getOperationSignatureInformation(
117: MBeanParameterInfo[] mBeanParameterInfos) {
118: StringBuffer sb = new StringBuffer();
119:
120: if (mBeanParameterInfos != null) {
121: if (mBeanParameterInfos.length == 0) {
122: sb.append(LINE_SEPARATOR).append(TAB).append(
123: getMessage(MBEAN_OPERATION_NO_PARAMETERS_FOUND,
124: null));
125: sb.append(LINE_SEPARATOR);
126: } else {
127: for (int i = 0; i < mBeanParameterInfos.length; i++) {
128: MBeanParameterInfo mBeanParameterInfo = mBeanParameterInfos[i];
129:
130: sb.append(LINE_SEPARATOR).append(TAB).append(
131: getMessage(NAME,
132: new Object[] { mBeanParameterInfo
133: .getName() }));
134: sb.append(LINE_SEPARATOR).append(TAB).append(
135: getMessage(DESCRIPTION_PROMPT,
136: new Object[] { mBeanParameterInfo
137: .getDescription() }));
138: sb.append(LINE_SEPARATOR).append(TAB).append(
139: getMessage(TYPE,
140: new Object[] { mBeanParameterInfo
141: .getType() }));
142: sb.append(LINE_SEPARATOR);
143: }
144: }
145: } else {
146: sb.append(LINE_SEPARATOR).append(TAB).append(
147: getMessage(MBEAN_OPERATION_NO_PARAMETERS_FOUND,
148: null));
149: sb.append(LINE_SEPARATOR);
150: }
151:
152: return sb;
153: }
154:
155: private StringBuffer getMBeanOperationsInformation(
156: MBeanOperationInfo[] mBeanOperationInfos) {
157: StringBuffer sb = new StringBuffer();
158:
159: sb.append(LINE_SEPARATOR).append(
160: getMessage(MBEAN_OPERATIONS, null));
161: if (mBeanOperationInfos != null) {
162: if (mBeanOperationInfos.length == 0) {
163: sb.append(LINE_SEPARATOR).append(
164: getMessage(MBEAN_NO_OPERATIONS_FOUND, null));
165: sb.append(LINE_SEPARATOR);
166: } else {
167: for (int i = 0; i < mBeanOperationInfos.length; i++) {
168: MBeanOperationInfo mBeanOperationInfo = mBeanOperationInfos[i];
169:
170: sb.append(LINE_SEPARATOR).append(
171: getMessage(NAME,
172: new Object[] { mBeanOperationInfo
173: .getName() }));
174: sb.append(LINE_SEPARATOR).append(
175: getMessage(DESCRIPTION_PROMPT,
176: new Object[] { mBeanOperationInfo
177: .getDescription() }));
178: sb.append(LINE_SEPARATOR).append(
179: getMessage(TYPE,
180: new Object[] { mBeanOperationInfo
181: .getReturnType() }));
182:
183: sb.append(LINE_SEPARATOR)
184: .append(
185: getMessage(
186: MBEAN_OPERATION_SIGNATURE,
187: null));
188: sb
189: .append(getOperationSignatureInformation(mBeanOperationInfo
190: .getSignature()));
191: sb.append(LINE_SEPARATOR);
192: }
193: }
194: } else {
195: sb.append(LINE_SEPARATOR).append(
196: getMessage(MBEAN_NO_OPERATIONS_FOUND, null));
197: sb.append(LINE_SEPARATOR);
198: }
199:
200: return sb;
201: }
202:
203: private String getNotificationTypes(String[] notificationTypes) {
204: StringBuffer sb = new StringBuffer();
205:
206: for (int i = 0; i < notificationTypes.length; i++) {
207: if (sb.length() != 0) {
208: sb.append(TAB);
209: }
210: sb.append(notificationTypes[i]);
211: }
212:
213: return sb.toString();
214: }
215:
216: private StringBuffer getMBeanNotificationsInformation(
217: MBeanNotificationInfo[] mBeanNotificationInfos) {
218: StringBuffer sb = new StringBuffer();
219:
220: sb.append(LINE_SEPARATOR).append(
221: getMessage(MBEAN_NOTIFICATIONS, null));
222: if (mBeanNotificationInfos != null) {
223: if (mBeanNotificationInfos.length == 0) {
224: sb.append(LINE_SEPARATOR).append(
225: getMessage(MBEAN_NO_NOTIFICATIONS_FOUND, null));
226: sb.append(LINE_SEPARATOR);
227: } else {
228: for (int i = 0; i < mBeanNotificationInfos.length; i++) {
229: MBeanNotificationInfo mBeanNotificationInfo = mBeanNotificationInfos[i];
230:
231: sb
232: .append(LINE_SEPARATOR)
233: .append(
234: getMessage(
235: NAME,
236: new Object[] { mBeanNotificationInfo
237: .getName() }));
238: sb
239: .append(LINE_SEPARATOR)
240: .append(
241: getMessage(
242: DESCRIPTION_PROMPT,
243: new Object[] { mBeanNotificationInfo
244: .getDescription() }));
245: sb
246: .append(LINE_SEPARATOR)
247: .append(
248: getMessage(
249: MBEAN_NOTIFICATION_TYPES,
250: new Object[] { getNotificationTypes(mBeanNotificationInfo
251: .getNotifTypes()) }));
252: sb.append(LINE_SEPARATOR);
253: }
254: }
255: } else {
256: sb.append(LINE_SEPARATOR).append(
257: getMessage(MBEAN_NO_NOTIFICATIONS_FOUND, null));
258: sb.append(LINE_SEPARATOR);
259: }
260:
261: return sb;
262: }
263:
264: private StringBuffer getMBeanInformation(ObjectName objectName)
265: throws CommandException {
266: StringBuffer sb = new StringBuffer();
267:
268: MBeanInfo mBeanInfo = null;
269: try {
270: mBeanInfo = getCommandClient().getMBeanInfo(objectName);
271: } catch (ReflectionException e) {
272: throwError(e);
273: } catch (IOException e) {
274: throwError(e);
275: } catch (InstanceNotFoundException e) {
276: throwError(e);
277: } catch (MBeanException e) {
278: throwError(e);
279: }
280:
281: sb.append(getMBeanDescription(mBeanInfo.getDescription()));
282: sb.append(getMBeanAttributesInformation(mBeanInfo
283: .getAttributes()));
284: sb.append(getMBeanOperationsInformation(mBeanInfo
285: .getOperations()));
286: sb.append(getMBeanNotificationsInformation(mBeanInfo
287: .getNotifications()));
288:
289: return sb;
290: }
291:
292: private String getMBeanInformation(CLIInput cliInput)
293: throws CommandException {
294: StringBuffer sb = new StringBuffer();
295:
296: ObjectName onPattern = getObjectNamePattern(
297: cliInput.objectName, cliInput.type,
298: cliInput.nameProperties);
299: Pattern mBeanNamePattern = getMBeanNamePattern(cliInput.mBeanNameRegExp);
300: if (cliInput.verbose) {
301: CLILogger.getInstance().printMessage(
302: getMessage(
303: QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
304: new Object[] { onPattern }));
305: CLILogger.getInstance().printMessage(
306: getMessage(
307: FILTERING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
308: new Object[] { mBeanNamePattern }));
309: }
310:
311: Set objectNames = null;
312: try {
313: objectNames = getCommandClient()
314: .queryNames(onPattern, null);
315: } catch (ReflectionException e) {
316: throwError(e);
317: } catch (IOException e) {
318: throwError(e);
319: } catch (InstanceNotFoundException e) {
320: throwError(e);
321: } catch (MBeanException e) {
322: throwError(e);
323: }
324:
325: if (objectNames.isEmpty()) {
326: sb.append(getMessage(NO_MBEANS_FOUND, null));
327: } else {
328: Iterator iterator = objectNames.iterator();
329: ObjectName on = null;
330: while (iterator.hasNext()) {
331: on = (ObjectName) iterator.next();
332: Matcher matcher = mBeanNamePattern.matcher(on
333: .toString());
334: if (matcher.matches()) {
335: if (cliInput.interactive) {
336: if (!voteForContinue(on).booleanValue()) {
337: continue;
338: }
339: }
340:
341: if (sb.length() != 0) {
342: sb.append(LINE_SEPARATOR);
343: }
344: sb.append(on);
345:
346: sb.append(getMBeanInformation(on));
347: }
348: }
349: }
350:
351: return sb.toString();
352: }
353:
354: private void printCLIInput(CLIInput cliInput) {
355: if (cliInput.verbose) {
356: CLILogger.getInstance().printMessage(
357: getMessage(MBEAN_OBJECT_NAME_PATTERN,
358: new Object[] { cliInput.objectName }));
359: CLILogger.getInstance().printMessage(
360: getMessage(MBEAN_TYPE,
361: new Object[] { cliInput.type }));
362: CLILogger.getInstance().printMessage(
363: getMessage(MBEAN_NAME_PROPERTIES,
364: new Object[] { cliInput.nameProperties }));
365: CLILogger.getInstance().printMessage(
366: getMessage(MBEAN_NAME_REGEXP,
367: new Object[] { cliInput.mBeanNameRegExp }));
368: }
369: }
370:
371: public void runCommand() throws CommandException,
372: CommandValidationException {
373: CLIInput cliInput = null;
374: Boolean connected = Boolean.FALSE;
375:
376: try {
377: gearUp();
378:
379: cliInput = new CLIInput();
380: printCLIInput(cliInput);
381:
382: connect(cliInput.verbose);
383: connected = Boolean.TRUE;
384:
385: String output = getMBeanInformation(cliInput);
386: CLILogger.getInstance().printMessage(output);
387: } finally {
388: if (connected.booleanValue()) {
389: disconnect(cliInput.verbose);
390: }
391: }
392: }
393: }
|