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 DeleteMonitoringMBeans 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 mBeanNameRegExp = 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: mBeanNameRegExp = operandValues[0];
038: }
039: }
040:
041: private String deleteMBeans(CLIInput cliInput)
042: throws CommandException {
043: StringBuffer sb = new StringBuffer();
044:
045: ObjectName onPattern = getObjectNamePattern(null,
046: cliInput.type, cliInput.nameProperties);
047: Pattern mBeanNamePattern = getMBeanNamePattern(cliInput.mBeanNameRegExp);
048: if (cliInput.verbose) {
049: CLILogger.getInstance().printMessage(
050: getMessage(
051: QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
052: new Object[] { onPattern }));
053: CLILogger.getInstance().printMessage(
054: getMessage(
055: FILTERING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
056: new Object[] { mBeanNamePattern }));
057: }
058:
059: Set objectNames = null;
060: try {
061: objectNames = getCommandClient()
062: .queryNames(onPattern, null);
063: } catch (ReflectionException e) {
064: throwError(e);
065: } catch (IOException e) {
066: throwError(e);
067: } catch (InstanceNotFoundException e) {
068: throwError(e);
069: } catch (MBeanException e) {
070: throwError(e);
071: }
072:
073: if (objectNames.isEmpty()) {
074: sb.append(getMessage(NO_MBEANS_FOUND, null));
075: } else {
076: Iterator iterator = objectNames.iterator();
077: ObjectName on = null;
078: while (iterator.hasNext()) {
079: on = (ObjectName) iterator.next();
080: Matcher matcher = mBeanNamePattern.matcher(on
081: .toString());
082: if (matcher.matches()) {
083: if (cliInput.interactive) {
084: if (!voteForContinue(on).booleanValue()) {
085: continue;
086: }
087: }
088:
089: if (sb.length() != 0) {
090: sb.append(LINE_SEPARATOR);
091: }
092: sb.append(on);
093:
094: try {
095: getCommandClient().unregisterMBean(on);
096: sb.append(LINE_SEPARATOR).append(TAB).append(
097: getMessage(MBEAN_UNREGISTERED, null));
098: } catch (ReflectionException e) {
099: throwError(e);
100: } catch (IOException e) {
101: throwError(e);
102: } catch (InstanceNotFoundException e) {
103: throwError(e);
104: } catch (MBeanException e) {
105: throwError(e);
106: }
107: }
108: }
109: }
110:
111: if (sb.length() == 0) {
112: sb.append(LINE_SEPARATOR).append(TAB).append(
113: getMessage(NO_MBEANS_UNREGISTERED, null));
114: }
115:
116: return sb.toString();
117: }
118:
119: private void printCLIInput(CLIInput cliInput) {
120: if (cliInput.verbose) {
121: CLILogger.getInstance().printMessage(
122: getMessage(MBEAN_OBJECT_NAME_PATTERN,
123: new Object[] { cliInput.objectName }));
124: CLILogger.getInstance().printMessage(
125: getMessage(MBEAN_TYPE,
126: new Object[] { cliInput.type }));
127: CLILogger.getInstance().printMessage(
128: getMessage(MBEAN_NAME_PROPERTIES,
129: new Object[] { cliInput.nameProperties }));
130: CLILogger.getInstance().printMessage(
131: getMessage(MBEAN_NAME_REGEXP,
132: new Object[] { cliInput.mBeanNameRegExp }));
133: }
134: }
135:
136: public void runCommand() throws CommandValidationException,
137: CommandException {
138: CLIInput cliInput = null;
139: Boolean connected = Boolean.FALSE;
140:
141: try {
142: gearUp();
143:
144: cliInput = new CLIInput();
145: printCLIInput(cliInput);
146:
147: connect(cliInput.verbose);
148: connected = Boolean.TRUE;
149:
150: String output = deleteMBeans(cliInput);
151: CLILogger.getInstance().printMessage(output);
152: } finally {
153: if (connected.booleanValue()) {
154: disconnect(cliInput.verbose);
155: }
156: }
157: }
158: }
|