001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: * <p/>
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.admin.cli.commands.logging;
013:
014: import com.sun.enterprise.cli.framework.CLILogger;
015: import com.sun.enterprise.cli.framework.CommandException;
016: import com.sun.enterprise.cli.framework.CommandValidationException;
017: import com.sun.portal.admin.common.util.AdminClientUtil;
018:
019: import javax.management.ObjectName;
020: import javax.management.MalformedObjectNameException;
021: import javax.management.InstanceNotFoundException;
022: import javax.management.MBeanException;
023: import javax.management.MBeanServerConnection;
024: import java.util.Iterator;
025: import java.util.LinkedList;
026: import java.util.Vector;
027: import java.util.logging.Level;
028:
029: public class SetLoggerCommand extends LoggingBaseCommand {
030:
031: // Options specific to set-logger
032: protected static final String OPT_FILE = "file";
033: protected static final String OPT_STACK_TRACE = "stack-trace";
034: protected static final String OPT_PARENT = "parent";
035:
036: // Error suffixes
037: private static final String NO_FILE_OPTION = "setlogger.option.nofile";
038: private static final String FILE_OPTION = "setlogger.option.file";
039:
040: // variables to hold the values of the above Options
041: private boolean file;
042: private boolean stackTrace;
043: private boolean parent;
044:
045: protected boolean getFile() {
046: return file;
047: }
048:
049: protected boolean getStackTrace() {
050: return stackTrace;
051: }
052:
053: protected boolean getParent() {
054: return parent;
055: }
056:
057: private void validateSetOptions() throws CommandValidationException {
058: file = getBooleanOption(OPT_FILE);
059: stackTrace = getBooleanOption(OPT_STACK_TRACE);
060: parent = getBooleanOption(OPT_PARENT);
061:
062: // if --file option is not specified --stack-trace or --parent options are invalid
063: if (!getFile()) {
064: if (getStackTrace() || getParent()) {
065: throw new CommandValidationException(
066: getLocalizedString(LOGGING_PREFIX
067: + NO_FILE_OPTION));
068: }
069: } else {
070: // if --file option is specified both --stack-trace and --parent options cannot be specified
071: if (getStackTrace() && getParent()) {
072: throw new CommandValidationException(
073: getLocalizedString(LOGGING_PREFIX + FILE_OPTION));
074: }
075: }
076: }
077:
078: private void checkLevelValidity() throws CommandException {
079: try {
080: Level.parse(getLevel());
081: } catch (IllegalArgumentException iae) {
082: if (isVerbose()) {
083: StringBuffer sb = new StringBuffer();
084: sb
085: .append("The valid levels are SEVERE, WARNING, CONFIG, INFO, FINE, FINER, FINEST");
086: CLILogger.getInstance().printMessage(sb.toString());
087: }
088: throw new CommandException(getLocalizedString(
089: LOGGING_PREFIX + INVALID_LEVEL,
090: new Object[] { getLevel() }));
091: }
092: }
093:
094: public void runCommand() throws CommandValidationException,
095: CommandException {
096:
097: try {
098:
099: validateOptions();
100: validateSetOptions();
101: checkLevelValidity();
102:
103: //get Mbean Server Connection based on uid/pwd
104: MBeanServerConnection msc = getMBeanServerConnection(
105: getUserId(), getPassword(), getHost());
106:
107: LinkedList path = new LinkedList();
108: path.addFirst(getDomainId());
109: path.addFirst("portalLogConfigurator");
110:
111: ObjectName objName = AdminClientUtil
112: .getResourceMBeanObjectName(
113: "PortalDomain.PortalLogConfigurator", path);
114:
115: Vector result = null;
116: // If --file is specified, redirect the logger to a separate file
117: if (getFile()) {
118: if (getStackTrace()) {
119: // If the --stack-trace is specified, enable stack-trace
120: // Disable logInParent
121: Object[] params = { getComponent(),
122: getComponentId(), getInstance(),
123: getLoggerName(), getLevel(),
124: new Boolean(getStackTrace()), Boolean.FALSE };
125:
126: String[] signature = { "java.lang.String",
127: "java.lang.String", "java.lang.String",
128: "java.lang.String", "java.lang.String",
129: "java.lang.Boolean", "java.lang.Boolean" };
130:
131: //does not go to parent
132: if (isVerbose()) {
133: StringBuffer sb = new StringBuffer(
134: "Setting a separate log file for the logger: ");
135: sb.append(getLoggerName());
136: sb.append(" at the level: ");
137: sb.append(getLevel());
138: sb.append(LINE_SEPARATOR);
139: sb
140: .append("The content logged does not go to the parent log");
141: sb.append(LINE_SEPARATOR);
142: sb.append("The stack trace is logged");
143: CLILogger.getInstance().printMessage(
144: sb.toString());
145: }
146: msc.invoke(objName, "setSeparateLogFile", params,
147: signature);
148:
149: } else if (getParent()) {
150: // if --parent is specified, log the content to parent log file as well, no stacktrace will be logged
151: // Disable stackTrace
152: Object[] params = { getComponent(),
153: getComponentId(), getInstance(),
154: getLoggerName(), getLevel(), Boolean.FALSE,
155: new Boolean(getParent()) };
156:
157: String[] signature = { "java.lang.String",
158: "java.lang.String", "java.lang.String",
159: "java.lang.String", "java.lang.String",
160: "java.lang.Boolean", "java.lang.Boolean" };
161:
162: //no stacktrace
163: if (isVerbose()) {
164: StringBuffer sb = new StringBuffer(
165: "Setting a separate log file for the logger: ");
166: sb.append(getLoggerName());
167: sb.append(" at the level: ");
168: sb.append(getLevel());
169: sb.append(LINE_SEPARATOR);
170: sb
171: .append("The content is logged both to this log and also to the parent log");
172: sb.append(LINE_SEPARATOR);
173: sb
174: .append("The stack trace is not logged if the level is INFO and below");
175: CLILogger.getInstance().printMessage(
176: sb.toString());
177: }
178: msc.invoke(objName, "setSeparateLogFile", params,
179: signature);
180: } else {
181: // if neither --stack-trace nor --parent is specified
182: Object[] params = { getComponent(),
183: getComponentId(), getInstance(),
184: getLoggerName(), getLevel(), Boolean.FALSE,
185: Boolean.FALSE };
186:
187: String[] signature = { "java.lang.String",
188: "java.lang.String", "java.lang.String",
189: "java.lang.String", "java.lang.String",
190: "java.lang.Boolean", "java.lang.Boolean" };
191:
192: //no stacktrace, does not go to parent
193: if (isVerbose()) {
194: StringBuffer sb = new StringBuffer(
195: "Setting a separate log file for the logger: ");
196: sb.append(getLoggerName());
197: sb.append(" at the level: ");
198: sb.append(getLevel());
199: sb.append(LINE_SEPARATOR);
200: sb
201: .append("The content is not logged to the parent log");
202: sb.append(LINE_SEPARATOR);
203: sb.append("The stack trace is not logged");
204: CLILogger.getInstance().printMessage(
205: sb.toString());
206: }
207: msc.invoke(objName, "setSeparateLogFile", params,
208: signature);
209: }
210: } else {
211: // If --file is not specified, set the level for the logger
212: Object[] params = { getComponent(), getComponentId(),
213: getInstance(), getLoggerName(), getLevel() };
214:
215: String[] signature = { "java.lang.String",
216: "java.lang.String", "java.lang.String",
217: "java.lang.String", "java.lang.String" };
218:
219: //no stacktrace, does not go to parent
220: if (isVerbose()) {
221: StringBuffer sb = new StringBuffer(
222: "Setting the level: ");
223: sb.append(getLevel());
224: sb.append(" for the logger: ");
225: sb.append(getLoggerName());
226: CLILogger.getInstance().printMessage(sb.toString());
227: }
228: msc.invoke(objName, "setLevel", params, signature);
229: }
230:
231: if (result != null) {
232: StringBuffer outputBuffer = new StringBuffer();
233: Iterator itr = result.iterator();
234: while (itr.hasNext()) {
235: outputBuffer.append(itr.next());
236: outputBuffer.append(LINE_SEPARATOR);
237: outputBuffer.append(LINE_SEPARATOR);
238: }
239:
240: CLILogger.getInstance().printMessage(
241: outputBuffer.toString());
242: }
243:
244: } catch (InstanceNotFoundException ie) {
245: logger.log(Level.SEVERE, "PSALI_CSPACCL0001", ie);
246: throw new CommandException(getLocalizedString(
247: ERROR_MBEAN_REFLECTION_ERROR,
248: new Object[] { "setLevel" }), null);
249: } catch (MBeanException me) {
250: logger.log(Level.SEVERE, "PSALI_CSPACCL0001", me);
251: throwError(me);
252: } catch (MalformedObjectNameException mle) {
253: logger.log(Level.SEVERE, "PSALI_CSPACCL0001", mle);
254: throw new CommandException(
255: getLocalizedString(ERROR_OBJECT_NAME), null);
256: } catch (CommandException ce) {
257: logger.log(Level.SEVERE, "PSALI_CSPACCL0001", ce);
258: throw ce;
259: } catch (CommandValidationException cve) {
260: logger.log(Level.SEVERE, "PSALI_CSPACCL0001", cve);
261: throw cve;
262: } catch (Exception ex) {
263: logger.log(Level.SEVERE, "PSALI_CSPACCL0001", ex);
264: throw new CommandException(
265: getLocalizedString(COMMAND_FAILED), null);
266: } finally {
267: closeMBeanServerConnection();
268: }
269: }
270: }
|