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