001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JbiListComponentLoggersTask.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.ant;
030:
031: import com.sun.jbi.ui.common.JBIManagementMessage;
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.IOException;
035: import java.io.PrintWriter;
036: import java.io.StringWriter;
037: import java.util.ArrayList;
038: import java.util.Enumeration;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042: import java.util.Properties;
043: import java.util.Set;
044: import java.util.SortedSet;
045: import java.util.TreeSet;
046: import java.util.TreeMap;
047: import java.util.logging.Level;
048: import java.util.StringTokenizer;
049: import java.util.Vector;
050: import org.apache.tools.ant.BuildException;
051:
052: /** This class is an ant task for displaying the runtime configuration or component
053: * configuration.
054: *
055: * @author Sun Microsystems, Inc.
056: */
057: public class JbiListComponentLoggersTask extends JbiTargetTask {
058: /** logger success msg key */
059: private static final String LOGGER_SUCCESS_STATUS_KEY = "jbi.ui.ant.list.logger.successful";
060: /** logger failure msg key */
061: private static final String LOGGER_FAILED_STATUS_KEY = "jbi.ui.ant.list.logger.failed";
062:
063: /** INSTANCE ERROR PROPERTY */
064: private static final String INSTANCE_ERROR_PROP = "com.sun.jbi.cluster.instance.error";
065:
066: /** Configuration Name Key */
067: private static final String APP_CONFIG_NAME_KEY = "configurationName";
068:
069: /** Holds value of property componentName. */
070: private String mComponentName = null;
071:
072: /** Holds Nested elements of <logger>*/
073: private List mLoggerList;
074:
075: /**
076: * Getter for property componentName.
077: * @return Value of property componentName.
078: */
079: public String getComponentName() {
080: return this .mComponentName;
081: }
082:
083: /**
084: * Setter for property componentName.
085: * @param componentName name of the component.
086: */
087: public void setComponentName(String componentName) {
088: this .mComponentName = componentName;
089: }
090:
091: /**
092: * Central logic for listing component configuration, component logger,
093: * application variables and application configuration
094: * @param componentName component name
095: */
096: private void executeListComponentLoggers(String componentName)
097: throws BuildException {
098: try {
099: String target = getValidTarget();
100: List tmpLoggerList = this .getLoggerList();
101: List loggerList = new ArrayList(tmpLoggerList);
102:
103: // Check the list, remove the entry with empty name value
104: Iterator iter = tmpLoggerList.iterator();
105: while (iter.hasNext()) {
106: Logger logger = (Logger) iter.next();
107: if (logger != null) {
108: String loggerName = logger.getName();
109: if ((loggerName != null)
110: && (loggerName.trim().compareTo("") == 0)) {
111: loggerList.remove(logger);
112: }
113: }
114: }
115:
116: logDebug("Before calling getJBIAdminCommands().getComponentLoggerLevels, the target is: "
117: + target);
118: Map<String, Level> resultLevels = this
119: .getJBIAdminCommands().getComponentLoggerLevels(
120: componentName, target, target);
121: logDebug("After calling getJBIAdminCommands().getComponentLoggerLevels, the target is: "
122: + target);
123: if (resultLevels == null) {
124: String errMsg = createFailedFormattedJbiAdminResult(
125: "jbi.ui.ant.task.error.no.logger.resolved",
126: null);
127: throw new BuildException(errMsg);
128: }
129:
130: if (loggerList.size() == 0) {
131: printComponentLoggerLevels(componentName, resultLevels,
132: target);
133: } else {
134: Iterator it = loggerList.iterator();
135: TreeMap<String, Level> theLevels = new TreeMap<String, Level>();
136: while (it.hasNext()) {
137: Logger logger = (Logger) it.next();
138: String loggerName = logger.getName();
139:
140: if ((loggerName != null)
141: && (loggerName.compareTo("") != 0)) {
142: Level theLevel = resultLevels.get(logger
143: .getName());
144: if (theLevel == null) {
145: String errMsg = createFailedFormattedJbiAdminResult(
146: "jbi.ui.ant.task.error.no.logger.resolved",
147: null);
148: throw new BuildException(errMsg);
149: }
150:
151: theLevels.put(logger.getName(), theLevel);
152: }
153: }
154: printComponentLoggerLevels(componentName, theLevels,
155: target);
156: }
157: } catch (Exception ex) {
158: processTaskException(ex);
159: }
160: }
161:
162: /** executes the install task. Ant Task framework calls this method to
163: * excute the task.
164: * @throws BuildException if error or exception occurs.
165: */
166: public void executeTask() throws BuildException {
167: this .logDebug("Executing List Configuration Task....");
168: String compName = getComponentName();
169:
170: executeListComponentLoggers(compName);
171: }
172:
173: /**
174: * returns i18n key. tasks implement this method.
175: * @return i18n key for the success status
176: */
177: protected String getTaskFailedStatusI18NKey() {
178: return LOGGER_FAILED_STATUS_KEY;
179: }
180:
181: /**
182: * returns i18n key. tasks implement this method.
183: * @return i18n key for the failed status
184: */
185: protected String getTaskSuccessStatusI18NKey() {
186: return LOGGER_SUCCESS_STATUS_KEY;
187: }
188:
189: /**
190: * returns i18n key
191: * @return the i18n key
192: */
193: protected String getEmptyQueryResultI18NKey(boolean isRuntime) {
194: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.empty"
195: : "jbi.ui.ant.print.jbi.comp.config.info.empty";
196: }
197:
198: /**
199: * returns i18n key
200: * @return the i18n key
201: */
202: protected String getLoggerEmptyQueryResultI18NKey(boolean isRuntime) {
203: return isRuntime ? "jbi.ui.ant.print.jbi.logger.info.empty"
204: : "jbi.ui.ant.print.jbi.comp.logger.info.empty";
205: }
206:
207: /**
208: * returns i18n key
209: * @return the i18n key
210: */
211: protected String getQueryResultHeaderI18NKey(boolean isRuntime) {
212: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.header"
213: : "jbi.ui.ant.print.jbi.comp.config.info.header";
214: }
215:
216: /**
217: * returns i18n key
218: * @return the i18n key
219: */
220: protected String getLoggerQueryResultHeaderI18NKey(boolean isRuntime) {
221: return isRuntime ? "jbi.ui.ant.print.jbi.logger.info.header"
222: : "jbi.ui.ant.print.jbi.comp.logger.info.header";
223: }
224:
225: /**
226: * returns i18n key
227: * @return the i18n key
228: */
229: protected String getQueryResultHeaderSeparatorI18NKey(
230: boolean isRuntime) {
231: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.header.separator"
232: : "jbi.ui.ant.print.jbi.comp.config.info.header.separator";
233: }
234:
235: /**
236: * returns i18n key
237: * @return the i18n key
238: */
239: protected String getQueryResultPageSeparatorI18NKey(
240: boolean isRuntime) {
241: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.separator"
242: : "jbi.ui.ant.print.jbi.comp.config.info.separator";
243: }
244:
245: protected void printComponentLoggerLevels(String compName,
246: Map<String, Level> theMap, String target) {
247: this .logDebug("Printing Component Logger Levels....");
248: if (theMap == null) {
249: this
250: .logDebug("List Component Logger Level map returns NULL");
251: } else {
252: this .logDebug("Size of Component Logger Level map: "
253: + theMap.size());
254: }
255: boolean isRuntime = false;
256: String header = getI18NBundle().getMessage(
257: getLoggerQueryResultHeaderI18NKey(isRuntime),
258: new String[] { compName, target });
259: String headerSeparator = getI18NBundle().getMessage(
260: getQueryResultHeaderSeparatorI18NKey(isRuntime));
261: String pageSeparator = getI18NBundle().getMessage(
262: getQueryResultPageSeparatorI18NKey(isRuntime));
263:
264: String emptyResult = getI18NBundle().getMessage(
265: getLoggerEmptyQueryResultI18NKey(isRuntime));
266:
267: StringWriter stringWriter = new StringWriter();
268: PrintWriter msgWriter = new PrintWriter(stringWriter);
269:
270: msgWriter.println(headerSeparator);
271: msgWriter.println(header);
272: msgWriter.println(headerSeparator);
273:
274: if (theMap == null || theMap.size() <= 0) {
275: msgWriter.println(emptyResult);
276: msgWriter.println(pageSeparator);
277: } else {
278: // sort the keys and display for deterministic output
279: SortedSet keys = new TreeSet(theMap.keySet());
280: for (Object key : keys) {
281: String loggerName = (String) key;
282: Level level = (Level) theMap.get(loggerName);
283: String param = getI18NBundle()
284: .getMessage(
285: "jbi.ui.ant.print.jbi.config.param",
286: loggerName,
287: (level == null) ? "" : level
288: .getLocalizedName());
289: msgWriter.println(param);
290: }
291: msgWriter.println(pageSeparator);
292: }
293:
294: msgWriter.close();
295: printMessage(stringWriter.getBuffer().toString());
296:
297: }
298:
299: /**
300: * returns param element list
301: * @return param List
302: */
303: protected List getLoggerList() {
304: if (this .mLoggerList == null) {
305: this .mLoggerList = new ArrayList();
306: }
307: return this .mLoggerList;
308: }
309:
310: /**
311: * factory method for creating the nested element <param>
312: * @return Logger Object
313: */
314: public Logger createLogger() {
315: Logger logger = new Logger();
316: this.getLoggerList().add(logger);
317: return logger;
318: }
319: }
|