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.io.BufferedReader;
013: import java.io.IOException;
014: import java.io.InputStreamReader;
015: import java.util.*;
016: import java.util.regex.Matcher;
017: import java.util.regex.Pattern;
018:
019: public class LogMonitoringAttributes extends GetMonitoringAttributes {
020: private class CLIInput {
021: public boolean interactive = false;
022: public String nameProperties = null;
023: public String objectName = null;
024: public int period;
025: public String type = null;
026: public boolean verbose = false;
027:
028: public String attributeNameRegExp = null;
029:
030: CLIInput() {
031: interactive = getBooleanOption(CLI_OPTION_INTERACTIVE);
032: nameProperties = getOption(CLI_OPTION_NAME_PROPERTIES);
033: objectName = getOption(CLI_OPTION_OBJECT_NAME);
034: period = getIntegerOption(CLI_OPTION_PERIOD);
035: type = getOption(CLI_OPTION_TYPE);
036: verbose = getBooleanOption(CLI_OPTION_VERBOSE);
037:
038: String operandValues[] = getOperandValues();
039: if (operandValues != null)
040: attributeNameRegExp = operandValues[0];
041: }
042: }
043:
044: private Set getMBeanObjectNames(CLIInput cliInput)
045: throws CommandException {
046: Set result = new HashSet();
047:
048: ObjectName onPattern = getObjectNamePattern(
049: cliInput.objectName, cliInput.type,
050: cliInput.nameProperties);
051: if (cliInput.verbose) {
052: CLILogger.getInstance().printMessage(
053: getMessage(
054: QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN,
055: new Object[] { onPattern }));
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: CLILogger.getInstance().printMessage(
074: 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: if (cliInput.interactive) {
081: if (!voteForContinue(on).booleanValue()) {
082: continue;
083: }
084: }
085:
086: result.add(on);
087: }
088: }
089:
090: return result;
091: }
092:
093: private class GetAttributesThread extends Thread {
094: private CLIInput cliInput;
095: private Set objectNames;
096: private Pattern attributeNamePattern;
097: private Boolean dumpHeaders;
098:
099: public GetAttributesThread(CLIInput cliInput, Set objectNames) {
100: dumpHeaders = Boolean.TRUE;
101:
102: this .cliInput = cliInput;
103: cliInput.interactive = false;
104:
105: this .objectNames = objectNames;
106:
107: attributeNamePattern = getAttributeNamePattern(cliInput.attributeNameRegExp);
108: if (cliInput.verbose) {
109: CLILogger
110: .getInstance()
111: .printMessage(
112: getMessage(
113: FILTERING_MBEAN_ATTRIBUTE_NAMES_WITH_PATTERN,
114: new Object[] { attributeNamePattern }));
115: }
116: }
117:
118: private void showMessage() {
119: System.err.println(getLocalizedString(MONITORING_PREFIX
120: + PROMPT_EXIT_MESSAGE,
121: new Object[] { getLocalizedString(MONITORING_PREFIX
122: + INPUT_EXIT_COMMAND) }));
123: }
124:
125: public void run() {
126: Boolean interrupted = Boolean.FALSE;
127: Boolean connected = Boolean.FALSE;
128: while (true) {
129: try {
130: if (interrupted.booleanValue()) {
131: break;
132: }
133:
134: try {
135: connect(cliInput.verbose);
136: connected = Boolean.TRUE;
137:
138: if (objectNames == null) {
139: objectNames = getMBeanObjectNames(cliInput);
140: }
141:
142: StringBuffer sb = new StringBuffer();
143: for (Iterator iterator = objectNames.iterator(); iterator
144: .hasNext();) {
145: ObjectName objectName = (ObjectName) iterator
146: .next();
147:
148: Map nameValues = getAttributeNameValues(objectName);
149: Set names = nameValues.keySet();
150:
151: if (dumpHeaders.booleanValue()) {
152: sb.append(objectName);
153: for (Iterator iterator2 = names
154: .iterator(); iterator2
155: .hasNext();) {
156: String name = (String) iterator2
157: .next();
158: Matcher matcher = attributeNamePattern
159: .matcher(name);
160: if (matcher.matches()) {
161: sb.append("|").append(name);
162: }
163: }
164: sb.append(LINE_SEPARATOR);
165: }
166:
167: long time = Calendar.getInstance()
168: .getTimeInMillis();
169: sb.append(time);
170: for (Iterator iterator2 = names.iterator(); iterator2
171: .hasNext();) {
172: String name = (String) iterator2.next();
173: Matcher matcher = attributeNamePattern
174: .matcher(name);
175: if (matcher.matches()) {
176: sb.append("|").append(
177: nameValues.get(name));
178: }
179: }
180:
181: if (objectNames.size() == 1) {
182: dumpHeaders = Boolean.FALSE;
183: } else {
184: sb.append(LINE_SEPARATOR);
185: }
186: }
187:
188: CLILogger.getInstance().printMessage(
189: sb.toString());
190: } catch (CommandException ce) {
191: System.err
192: .println(getLocalizedString(MONITORING_PREFIX
193: + WARNING
194: + SERVER_INSTANCE_MAY_BE_DOWN));
195: } finally {
196: if (connected.booleanValue()) {
197: try {
198: disconnect(cliInput.verbose);
199: } catch (CommandException e) {
200: System.err
201: .println(getLocalizedString(MONITORING_PREFIX
202: + WARNING
203: + SERVER_INSTANCE_MAY_BE_DOWN));
204: }
205: }
206: }
207:
208: showMessage();
209: Thread.sleep(cliInput.period * 1000);
210: } catch (InterruptedException ie) {
211: interrupted = Boolean.TRUE;
212: }
213: }
214: }
215: }
216:
217: private class InputThread extends Thread {
218: private Thread getAttributesThread;
219:
220: public InputThread(GetAttributesThread getAttributesThread) {
221: this .getAttributesThread = getAttributesThread;
222: }
223:
224: public void run() {
225: String exitCommand = getLocalizedString(MONITORING_PREFIX
226: + INPUT_EXIT_COMMAND);
227: String exitConfirmYes = getLocalizedString(MONITORING_PREFIX
228: + INPUT_EXIT_CONFIRM_YES);
229:
230: BufferedReader br = new BufferedReader(
231: new InputStreamReader(System.in));
232: while (true) {
233: try {
234: String line = br.readLine();
235:
236: if (line.equalsIgnoreCase(exitCommand)) {
237: System.err
238: .println(getLocalizedString(MONITORING_PREFIX
239: + PROMPT_EXIT_CONFIRMATION_MESSAGE));
240: line = br.readLine();
241:
242: if (line.toLowerCase().startsWith(
243: exitConfirmYes)) {
244: getAttributesThread.interrupt();
245: break;
246: }
247: }
248: } catch (IOException e) {
249: }
250: }
251: }
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: CLILogger.getInstance().printMessage(
272: getMessage(LOG_PERIOD, new Object[] { new Integer(
273: cliInput.period) }));
274: }
275: }
276:
277: public void runCommand() throws CommandValidationException,
278: CommandException {
279: gearUp();
280:
281: CLIInput cliInput = new CLIInput();
282: printCLIInput(cliInput);
283:
284: Set objectNames = null;
285: Boolean connected = Boolean.FALSE;
286: try {
287: connect(cliInput.verbose);
288: connected = Boolean.TRUE;
289:
290: objectNames = getMBeanObjectNames(cliInput);
291: } finally {
292: if (connected.booleanValue()) {
293: disconnect(cliInput.verbose);
294: }
295: }
296:
297: GetAttributesThread getAttributesThread = new GetAttributesThread(
298: cliInput, objectNames);
299: InputThread inputThread = new InputThread(getAttributesThread);
300: getAttributesThread.start();
301: inputThread.start();
302: try {
303: inputThread.join();
304: getAttributesThread.join();
305: } catch (InterruptedException e) {
306: throwWarning(e);
307: }
308: }
309: }
|