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 java.util.Iterator;
013: import java.util.Set;
014: import java.util.regex.Matcher;
015: import java.util.regex.Pattern;
016: import java.io.IOException;
017:
018: public class GetMonitoringMBeanNames extends MonitoringBaseCommand {
019: private class CLIInput {
020: public String nameProperties = null;
021: public String objectName = null;
022: public String type = null;
023: public boolean verbose;
024:
025: public String mBeanNameRegExp = null;
026:
027: CLIInput() {
028: nameProperties = getOption(CLI_OPTION_NAME_PROPERTIES);
029: objectName = getOption(CLI_OPTION_OBJECT_NAME);
030: type = getOption(CLI_OPTION_TYPE);
031: verbose = getBooleanOption(CLI_OPTION_VERBOSE);
032:
033: String operandValues[] = getOperandValues();
034: if (operandValues != null)
035: mBeanNameRegExp = operandValues[0];
036: }
037: }
038:
039: private String getMBeanNames(CLIInput cliInput)
040: throws CommandException {
041: StringBuffer sb = new StringBuffer();
042:
043: ObjectName onPattern = getObjectNamePattern(
044: cliInput.objectName, cliInput.type,
045: cliInput.nameProperties);
046: Pattern mBeanNamePattern = getMBeanNamePattern(cliInput.mBeanNameRegExp);
047: if (cliInput.verbose) {
048: CLILogger.getInstance().printMessage(
049: getMessage(
050: QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
051: new Object[] { onPattern }));
052: CLILogger.getInstance().printMessage(
053: getMessage(
054: FILTERING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
055: new Object[] { mBeanNamePattern }));
056: }
057:
058: Set objectNames = null;
059: try {
060: objectNames = getCommandClient()
061: .queryNames(onPattern, null);
062: } catch (ReflectionException e) {
063: throwError(e);
064: } catch (IOException e) {
065: throwError(e);
066: } catch (InstanceNotFoundException e) {
067: throwError(e);
068: } catch (MBeanException e) {
069: throwError(e);
070: }
071:
072: if (objectNames.isEmpty()) {
073: sb.append(getMessage(NO_MBEANS_FOUND, null));
074: } else {
075: Iterator iterator = objectNames.iterator();
076: while (iterator.hasNext()) {
077: ObjectName on = (ObjectName) iterator.next();
078: Matcher matcher = mBeanNamePattern.matcher(on
079: .toString());
080: if (matcher.matches()) {
081: if (sb.length() != 0) {
082: sb.append(LINE_SEPARATOR);
083: }
084: sb.append(on);
085: }
086: }
087: }
088:
089: return sb.toString();
090: }
091:
092: private void printCLIInput(CLIInput cliInput) {
093: if (cliInput.verbose) {
094: CLILogger.getInstance().printMessage(
095: getMessage(MBEAN_OBJECT_NAME_PATTERN,
096: new Object[] { cliInput.objectName }));
097: CLILogger.getInstance().printMessage(
098: getMessage(MBEAN_TYPE,
099: new Object[] { cliInput.type }));
100: CLILogger.getInstance().printMessage(
101: getMessage(MBEAN_NAME_PROPERTIES,
102: new Object[] { cliInput.nameProperties }));
103: CLILogger.getInstance().printMessage(
104: getMessage(MBEAN_NAME_REGEXP,
105: new Object[] { cliInput.mBeanNameRegExp }));
106: }
107: }
108:
109: public void runCommand() throws CommandValidationException,
110: CommandException {
111: CLIInput cliInput = null;
112: Boolean connected = Boolean.FALSE;
113:
114: try {
115: gearUp();
116:
117: cliInput = new CLIInput();
118: printCLIInput(cliInput);
119:
120: connect(cliInput.verbose);
121: connected = Boolean.TRUE;
122:
123: String output = getMBeanNames(cliInput);
124: CLILogger.getInstance().printMessage(output);
125: } finally {
126: if (connected.booleanValue()) {
127: disconnect(cliInput.verbose);
128: }
129: }
130: }
131: }
|