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: import com.sun.portal.admin.cli.commands.AdminBaseCommand;
011: import com.sun.portal.admin.common.util.AdminClientUtil;
012:
013: import javax.management.*;
014: import java.io.BufferedReader;
015: import java.io.IOException;
016: import java.io.InputStreamReader;
017: import java.util.Vector;
018: import java.util.LinkedList;
019: import java.util.regex.Pattern;
020:
021: public abstract class MonitoringBaseCommand extends AdminBaseCommand {
022: protected CommandClient getCommandClient() {
023: if (commandClient == null) {
024: if (isPortal) {
025: commandClient = new CommandClient();
026: } else {
027: commandClient = new SRACommandClient(this );
028: }
029: }
030:
031: return commandClient;
032: }
033:
034: protected void setCommandClient(CommandClient commandClient) {
035: this .commandClient = commandClient;
036: }
037:
038: protected void gearUp() throws CommandValidationException,
039: CommandException {
040: validateOptions();
041: getCommandClient().setMBeanServerConnection(
042: getMBeanServerConnection(getUserId(), getPassword(),
043: getHost()));
044: getCommandClient().setObjectName(getObjectName());
045: }
046:
047: private void connect() throws ReflectionException,
048: InstanceNotFoundException, IOException, MBeanException {
049: getCommandClient().connect();
050: }
051:
052: protected void connect(boolean verbose) throws CommandException {
053: if (verbose) {
054: CLILogger.getInstance().printMessage(
055: getMessage(CONNECTING, null));
056: }
057:
058: try {
059: connect();
060: } catch (ReflectionException e) {
061: throwError(e);
062: } catch (InstanceNotFoundException e) {
063: throw new CommandException(getMessage(
064: COULD_NOT_FIND_MBEAN_INSTANCE, null));
065: } catch (IOException e) {
066: throwError(e);
067: } catch (MBeanException e) {
068: throw new CommandException(getMessage(
069: COULD_NOT_CONNECT_TO_SERVER_INSTANCE, null));
070: }
071:
072: if (verbose) {
073: CLILogger.getInstance().printMessage(
074: getMessage(CONNECTING_DONE, null));
075: }
076: }
077:
078: private void disconnect() throws ReflectionException,
079: InstanceNotFoundException, IOException, MBeanException {
080: getCommandClient().disconnect();
081: }
082:
083: protected void disconnect(boolean verbose) throws CommandException {
084: if (verbose) {
085: CLILogger.getInstance().printMessage(
086: getMessage(DISCONNECTING, null));
087: }
088:
089: try {
090: disconnect();
091: } catch (ReflectionException e) {
092: throwWarning(e);
093: } catch (InstanceNotFoundException e) {
094: throwWarning(e);
095: } catch (IOException e) {
096: throwWarning(e);
097: } catch (MBeanException e) {
098: throwWarning(e);
099: }
100:
101: if (verbose) {
102: CLILogger.getInstance().printMessage(
103: getMessage(DISCONNECTING_DONE, null));
104: }
105: }
106:
107: private String[] vectorToArray(Vector v) {
108: if (v == null || v.isEmpty()) {
109: return null;
110: } else {
111: return (String[]) v.toArray(new String[v.size()]);
112: }
113: }
114:
115: protected String[] getOperandValues() {
116: return vectorToArray(getOperands());
117: }
118:
119: protected Boolean voteForContinue(ObjectName objectName) {
120: Boolean result = Boolean.FALSE;
121:
122: System.err.println(getLocalizedString(MONITORING_PREFIX
123: + PROMPT_CONTINUE_OPERATION_MESSAGE,
124: new Object[] { objectName }));
125: BufferedReader br = new BufferedReader(new InputStreamReader(
126: System.in));
127: try {
128: String line = br.readLine();
129: if (line.toLowerCase().startsWith(
130: getLocalizedString(MONITORING_PREFIX
131: + INPUT_CONTINUE_OPERATION_CONFIRM_YES))) {
132: result = Boolean.TRUE;
133: }
134: } catch (IOException ioe) {
135: }
136:
137: return result;
138: }
139:
140: public boolean validateOptions() throws CommandValidationException {
141: super .validateOptions();
142:
143: isPortal = true;
144: if (null != getOption(CLI_OPTION_PROXY_TYPE)) {
145: isPortal = false;
146: } else if (null == getOption(CLI_OPTION_PORTAL)) {
147: throw new CommandValidationException(getMessage(
148: NO_INSTANCE_SELECTED, null));
149: }
150:
151: return true;
152: }
153:
154: protected ObjectName getObjectName() throws CommandException {
155: if (isPortal) {
156: return super .getObjectName();
157: } else {
158: LinkedList path = new LinkedList();
159: path.addFirst(getDomainId());
160: path.addFirst("sramonitoring");
161: try {
162: return AdminClientUtil
163: .getResourceMBeanObjectName(
164: AdminClientUtil.SRA_MONITORING_MBEAN_TYPE,
165: path);
166: } catch (MalformedObjectNameException e) {
167: throw new CommandException(e);
168: }
169: }
170: }
171:
172: protected ObjectName getObjectNamePattern(String objectName,
173: String type, String nameProperties) throws CommandException {
174: if (objectName != null) {
175: try {
176: return new ObjectName(objectName);
177: } catch (MalformedObjectNameException e) {
178: throwError(e);
179: }
180: }
181:
182: StringBuffer sb = new StringBuffer();
183:
184: if (type != null) {
185: sb.append(",type=" + type);
186: }
187:
188: if (nameProperties != null) {
189: sb.append("," + nameProperties);
190: }
191:
192: try {
193: return new ObjectName(getCommandClient().getDefaultDomain()
194: + ":*" + sb.toString());
195: } catch (MalformedObjectNameException e) {
196: throwError(e);
197: } catch (ReflectionException e) {
198: throwError(e);
199: } catch (IOException e) {
200: throwError(e);
201: } catch (InstanceNotFoundException e) {
202: throwError(e);
203: } catch (MBeanException e) {
204: throwError(e);
205: } catch (AttributeNotFoundException e) {
206: throwError(e);
207: }
208:
209: return null;
210: }
211:
212: protected Pattern getMBeanNamePattern(String mBeanNameRegExp) {
213: StringBuffer sb = new StringBuffer();
214:
215: sb.append("[\\S]*:");
216: if (mBeanNameRegExp != null) {
217: sb.append(mBeanNameRegExp);
218: }
219: sb.append("[\\S]*");
220:
221: Pattern pattern = Pattern.compile(sb.toString());
222: return pattern;
223: }
224:
225: protected Pattern getAttributeNamePattern(String attributeNameRegExp) {
226: StringBuffer sb = new StringBuffer();
227:
228: sb.append("[\\S]*");
229: if (attributeNameRegExp != null) {
230: sb.append(attributeNameRegExp);
231: }
232: sb.append("[\\S]*");
233:
234: Pattern pattern = Pattern.compile(sb.toString());
235: return pattern;
236: }
237:
238: protected String getMessage(String messageId, Object[] params) {
239: return getLocalizedString(messageId, params);
240: }
241:
242: protected String getCauseMessages(Exception e) {
243: StringBuffer sb = new StringBuffer(e.getLocalizedMessage());
244: Throwable t = e.getCause();
245: while (t != null) {
246: sb.append(LINE_SEPARATOR).append("\t").append(
247: t.getLocalizedMessage());
248: t = t.getCause();
249: }
250:
251: return sb.toString();
252: }
253:
254: protected void throwError(Exception e) throws CommandException {
255: throw new CommandException(getMessage(
256: MONITORING_PREFIX + ERROR,
257: new Object[] { getCauseMessages(e) }));
258: }
259:
260: protected void throwWarning(Exception e) throws CommandException {
261: throw new CommandException(getMessage(MONITORING_PREFIX
262: + WARNING, new Object[] { getCauseMessages(e) }));
263: }
264:
265: protected CommandClient commandClient;
266: protected boolean isPortal;
267:
268: protected static final String CLI_OPTION_INTERACTIVE = "interactive";
269: protected static final String CLI_OPTION_NAME_PROPERTIES = "name-properties";
270: protected static final String CLI_OPTION_OBJECT_NAME = "object-name";
271: protected static final String CLI_OPTION_PERIOD = "period";
272: protected static final String CLI_OPTION_PORTAL = "portal";
273: protected static final String CLI_OPTION_PROXY_TYPE = "proxy-type";
274: protected static final String CLI_OPTION_TYPE = "type";
275: protected static final String CLI_OPTION_VERBOSE = "verbose";
276:
277: protected static final String LINE_SEPARATOR = System
278: .getProperty("line.separator");
279: protected static final String TAB = "\t";
280: protected static final String EQUAL_TO = " = ";
281:
282: protected static final String MONITORING_PREFIX = "monitoring.";
283:
284: protected static final String SUCCESS = "success";
285: protected static final String ERROR = "error";
286: protected static final String WARNING = "warning";
287:
288: protected static final String COULD_NOT_GET_ATTRIBUTE = ".could.not.get.attribute";
289:
290: protected static final String PROMPT_EXIT_MESSAGE = "prompt.exit.message";
291: protected static final String PROMPT_EXIT_CONFIRMATION_MESSAGE = "prompt.exit.confirmation.message";
292: protected static final String INPUT_EXIT_COMMAND = "input.exit.command";
293: protected static final String INPUT_EXIT_CONFIRM_YES = "input.exit.confirm.yes";
294: protected static final String SERVER_INSTANCE_MAY_BE_DOWN = ".server.instance.may.be.down";
295:
296: protected static final String PROMPT_CONTINUE_OPERATION_MESSAGE = "prompt.continue.operation.message";
297: protected static final String INPUT_CONTINUE_OPERATION_CONFIRM_YES = "input.continue.operation.confirm.yes";
298:
299: protected static final String CONNECTING = MONITORING_PREFIX
300: + "connecting";
301: protected static final String CONNECTING_DONE = MONITORING_PREFIX
302: + "connecting.done";
303: protected static final String DISCONNECTING = MONITORING_PREFIX
304: + "disconnecting";
305: protected static final String DISCONNECTING_DONE = MONITORING_PREFIX
306: + "disconnecting.done";
307:
308: protected static final String MBEAN_OBJECT_NAME_PATTERN = MONITORING_PREFIX
309: + "mbean.object.name.pattern";
310: protected static final String MBEAN_TYPE = MONITORING_PREFIX
311: + "mbean.type";
312: protected static final String MBEAN_NAME_PROPERTIES = MONITORING_PREFIX
313: + "mbean.name.properties";
314: protected static final String MBEAN_NAME_REGEXP = MONITORING_PREFIX
315: + "mbean.name.regexp";
316: protected static final String ATTRIBUTE_NAME_REGEXP = MONITORING_PREFIX
317: + "attribute.name.regexp";
318:
319: protected static final String QUERYING_MBEAN_OBJECT_NAMES_WITH_PATTERN = MONITORING_PREFIX
320: + "querying.mbean.object.names.with.pattern";
321: protected static final String FILTERING_MBEAN_OBJECT_NAMES_WITH_PATTERN = MONITORING_PREFIX
322: + "filtering.mbean.object.names.with.pattern";
323: protected static final String FILTERING_MBEAN_ATTRIBUTE_NAMES_WITH_PATTERN = MONITORING_PREFIX
324: + "filtering.mbean.attribute.names.with.pattern";
325:
326: protected static final String COULD_NOT_CONNECT_TO_SERVER_INSTANCE = MONITORING_PREFIX
327: + "could.not.connect.to.server.instance";
328: protected static final String COULD_NOT_FIND_MBEAN_INSTANCE = MONITORING_PREFIX
329: + "could.not.find.mbean.instance";
330:
331: protected static final String NO_INSTANCE_SELECTED = MONITORING_PREFIX
332: + "error.no.instance.selected";
333: protected static final String NO_MBEANS_FOUND = MONITORING_PREFIX
334: + "no.mbeans.found";
335: protected static final String NO_MBEAN_ATTRIBUTES_FOUND = MONITORING_PREFIX
336: + "no.mbean.attributes.found";
337: protected static final String ATTRIBUTE_VALUE_SET_AS = MONITORING_PREFIX
338: + "attribute.value.set.as";
339: protected static final String MBEAN_UNREGISTERED = MONITORING_PREFIX
340: + "mbean.unregistered";
341: protected static final String NO_MBEANS_UNREGISTERED = MONITORING_PREFIX
342: + "no.mbeans.unregistered";
343: protected static final String MBEAN_RESET = MONITORING_PREFIX
344: + "mbean.reset";
345: protected static final String MBEAN_RESET_NOT_SUPPORTED = MONITORING_PREFIX
346: + "mbean.reset.not.supported";
347: protected static final String MBEAN_RESET_INSTANCE_NOT_FOUND = MONITORING_PREFIX
348: + "mbean.reset.instance.not.found";
349: protected static final String NO_MBEANS_RESET = MONITORING_PREFIX
350: + "no.mbeans.reset";
351: protected static final String LOG_PERIOD = MONITORING_PREFIX
352: + "log.period";
353: protected static final String INVOKE_MBEAN_OPERATION_RETURN_VALUE = MONITORING_PREFIX
354: + "invoke.mbean.operation.return.value";
355: protected static final String INVOKE_MBEAN_OPERATION_NOT_SUPPORTED = MONITORING_PREFIX
356: + "invoke.mbean.operation.not.supported";
357: protected static final String DESCRIPTION = MONITORING_PREFIX
358: + "description";
359: protected static final String MBEAN_ATTRIBUTES = MONITORING_PREFIX
360: + "mbean.attributes";
361: protected static final String MBEAN_NO_ATTRIBUTES_FOUND = MONITORING_PREFIX
362: + "mbean.no.attributes.found";
363: protected static final String MBEAN_OPERATIONS = MONITORING_PREFIX
364: + "mbean.operations";
365: protected static final String MBEAN_NO_OPERATIONS_FOUND = MONITORING_PREFIX
366: + "mbean.no.operations.found";
367: protected static final String MBEAN_NOTIFICATIONS = MONITORING_PREFIX
368: + "mbean.notifications";
369: protected static final String MBEAN_NO_NOTIFICATIONS_FOUND = MONITORING_PREFIX
370: + "mbean.no.notifications.found";
371: protected static final String NAME = MONITORING_PREFIX + "name";
372: protected static final String DESCRIPTION_PROMPT = MONITORING_PREFIX
373: + "description.prompt";
374: protected static final String TYPE = MONITORING_PREFIX + "type";
375: protected static final String MBEAN_ATTRIBUTE_READABLE = MONITORING_PREFIX
376: + "mbean.attribute.readable";
377: protected static final String MBEAN_ATTRIBUTE_WRITABLE = MONITORING_PREFIX
378: + "mbean.attribute.writable";
379: protected static final String MBEAN_OPERATION_RETURN_TYPE = MONITORING_PREFIX
380: + "mbean.operation.return.type";
381: protected static final String MBEAN_OPERATION_SIGNATURE = MONITORING_PREFIX
382: + "mbean.operation.signature";
383: protected static final String MBEAN_OPERATION_NO_PARAMETERS_FOUND = MONITORING_PREFIX
384: + "mbean.operation.no.parameters.found";
385: protected static final String MBEAN_NOTIFICATION_TYPES = MONITORING_PREFIX
386: + "mbean.notification.types";
387: }
|