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: * @(#)JbiListRuntimeConfigurationTask.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 JbiListRuntimeConfigurationTask extends JbiTargetTask {
058: /** success msg key */
059: private static final String SUCCESS_STATUS_KEY = "jbi.ui.ant.list.configuration.successful";
060: /** failure msg key */
061: private static final String FAILED_STATUS_KEY = "jbi.ui.ant.list.configuration.failed";
062: /** logger success msg key */
063:
064: /** INSTANCE ERROR PROPERTY */
065: private static final String INSTANCE_ERROR_PROP = "com.sun.jbi.cluster.instance.error";
066:
067: /** Holds Param Nested elements */
068: private List mParamList;
069:
070: /**
071: * Logic for listing runtime configuration
072: */
073:
074: private void executeListRuntimeConfiguration()
075: throws BuildException {
076: this .logDebug("Executing List Runtime Configuration ....");
077: try {
078: String target = getValidTarget();
079: List tmpParamList = this .getParamList();
080: List paramList = new ArrayList(tmpParamList);
081:
082: // Check the list, remove the entry with empty name value
083: Iterator iter = tmpParamList.iterator();
084: while (iter.hasNext()) {
085: Param param = (Param) iter.next();
086: if (param != null) {
087: String paramName = param.getName();
088: if ((paramName != null)
089: && (paramName.trim().compareTo("") == 0)) {
090: paramList.remove(param);
091: }
092: }
093: }
094:
095: Properties returnProps = this .getJBIAdminCommands()
096: .getRuntimeConfiguration(target);
097: printRuntimeConfiguration(paramList, returnProps);
098: } catch (Exception ex) {
099: processTaskException(ex);
100: }
101: }
102:
103: /** executes the install task. Ant Task framework calls this method to
104: * excute the task.
105: * @throws BuildException if error or exception occurs.
106: */
107: public void executeTask() throws BuildException {
108: this .logDebug("Executing List Configuration Task....");
109:
110: executeListRuntimeConfiguration();
111: }
112:
113: /**
114: * returns i18n key. tasks implement this method.
115: * @return i18n key for the success status
116: */
117: protected String getTaskFailedStatusI18NKey() {
118: return FAILED_STATUS_KEY;
119: }
120:
121: /**
122: * returns i18n key. tasks implement this method.
123: * @return i18n key for the failed status
124: */
125: protected String getTaskSuccessStatusI18NKey() {
126: return SUCCESS_STATUS_KEY;
127: }
128:
129: /**
130: * returns i18n key
131: * @return the i18n key
132: */
133: protected String getEmptyQueryResultI18NKey(boolean isRuntime) {
134: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.empty"
135: : "jbi.ui.ant.print.jbi.comp.config.info.empty";
136: }
137:
138: /**
139: * returns i18n key
140: * @return the i18n key
141: */
142: protected String getQueryResultHeaderI18NKey(boolean isRuntime) {
143: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.header"
144: : "jbi.ui.ant.print.jbi.comp.config.info.header";
145: }
146:
147: /**
148: * returns i18n key
149: * @return the i18n key
150: */
151: protected String getQueryResultHeaderSeparatorI18NKey(
152: boolean isRuntime) {
153: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.header.separator"
154: : "jbi.ui.ant.print.jbi.comp.config.info.header.separator";
155: }
156:
157: /**
158: * returns i18n key
159: * @return the i18n key
160: */
161: protected String getQueryResultPageSeparatorI18NKey(
162: boolean isRuntime) {
163: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.separator"
164: : "jbi.ui.ant.print.jbi.comp.config.info.separator";
165: }
166:
167: /**
168: * Printing runtime configuration
169: * @param params the parameters to be printed
170: */
171: protected void printRuntimeConfiguration(List paramList,
172: Properties returnProps) {
173: this .logDebug("Printing Rimtime Configuration ....");
174: boolean isRuntime = true;
175: String header = getI18NBundle().getMessage(
176: getQueryResultHeaderI18NKey(isRuntime),
177: getValidTarget());
178: String headerSeparator = getI18NBundle().getMessage(
179: getQueryResultHeaderSeparatorI18NKey(isRuntime));
180: String pageSeparator = getI18NBundle().getMessage(
181: getQueryResultPageSeparatorI18NKey(isRuntime));
182: String emptyResult = getI18NBundle().getMessage(
183: getEmptyQueryResultI18NKey(isRuntime));
184:
185: StringWriter stringWriter = new StringWriter();
186: PrintWriter msgWriter = new PrintWriter(stringWriter);
187:
188: msgWriter.println(headerSeparator);
189: msgWriter.println(header);
190: msgWriter.println(headerSeparator);
191:
192: if ((returnProps == null) || (returnProps.size() <= 0)) {
193: msgWriter.println(emptyResult);
194: msgWriter.println(pageSeparator);
195: } else if ((paramList == null) || (paramList.size() <= 0)) {
196: // sort the keys and display for deterministic output
197: SortedSet keys = new TreeSet(returnProps.keySet());
198: for (Object key : keys) {
199: String name = (String) key;
200: String value = returnProps.getProperty(name, "");
201: String param = getI18NBundle().getMessage(
202: "jbi.ui.ant.print.jbi.config.param", name,
203: value);
204: msgWriter.println(param);
205: }
206: msgWriter.println(pageSeparator);
207: } else {
208: Iterator itr = paramList.iterator();
209: while (itr.hasNext()) {
210: String name = "" + ((Param) itr.next()).getName();
211: String value = returnProps.getProperty(name);
212: if (value != null) {
213: String msg = getI18NBundle().getMessage(
214: "jbi.ui.ant.print.jbi.config.param", name,
215: value);
216: msgWriter.println(msg);
217: } else {
218: String msg = getI18NBundle()
219: .getMessage(
220: "jbi.ui.ant.print.jbi.config.param.not.retrieved",
221: name);
222: msgWriter.println(msg);
223: }
224: }
225: msgWriter.println(pageSeparator);
226: }
227:
228: msgWriter.close();
229: printMessage(stringWriter.getBuffer().toString());
230:
231: }
232:
233: /**
234: * returns param element list
235: * @return param List
236: */
237: protected List getParamList() {
238: if (this .mParamList == null) {
239: this .mParamList = new ArrayList();
240: }
241: return this .mParamList;
242: }
243:
244: /**
245: * factory method for creating the nested element <param>
246: * @return Logger Object
247: */
248: public Param createParam() {
249: Param param = new Param();
250: this.getParamList().add(param);
251: return param;
252: }
253: }
|