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: * @(#)JbiListComponentConfigurationTask.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 JbiListComponentConfigurationTask 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 value of property componentName. */
068: private String mComponentName = null;
069:
070: /** Holds Param Nested elements */
071: private List mParamList;
072:
073: /**
074: * Getter for property componentName.
075: * @return Value of property componentName.
076: */
077: public String getComponentName() {
078: return this .mComponentName;
079: }
080:
081: /**
082: * Setter for property componentName.
083: * @param componentName name of the component.
084: */
085: public void setComponentName(String componentName) {
086: this .mComponentName = componentName;
087: }
088:
089: /**
090: * Central logic for listing component configuration, component logger,
091: * application variables and application configuration
092: * @param componentName component name
093: */
094: private void executeListComponentConfiguration(String componentName)
095: throws BuildException {
096: try {
097: String target = getValidTarget();
098: List tmpParamList = this .getParamList();
099: List paramList = new ArrayList(tmpParamList);
100:
101: // Check the list, remove the entry with empty name value
102: Iterator iter = tmpParamList.iterator();
103: while (iter.hasNext()) {
104: Param param = (Param) iter.next();
105: if (param != null) {
106: String paramName = param.getName();
107: if ((paramName != null)
108: && (paramName.trim().compareTo("") == 0)) {
109: paramList.remove(param);
110: }
111: }
112: }
113:
114: // Print the component configuration
115: this
116: .logDebug("Executing List Component Configuration ....");
117:
118: Properties paramsMap = this .getJBIAdminCommands()
119: .getComponentConfiguration(componentName, target);
120: printComponentConfiguration(componentName, paramList,
121: paramsMap, target);
122: } catch (Exception ex) {
123: processTaskException(ex);
124: }
125: }
126:
127: /** executes the install task. Ant Task framework calls this method to
128: * excute the task.
129: * @throws BuildException if error or exception occurs.
130: */
131: public void executeTask() throws BuildException {
132: this
133: .logDebug("Executing List Component Configuration Task....");
134: String compName = getComponentName();
135:
136: executeListComponentConfiguration(compName);
137: }
138:
139: /**
140: * returns i18n key. tasks implement this method.
141: * @return i18n key for the success status
142: */
143: protected String getTaskFailedStatusI18NKey() {
144: return FAILED_STATUS_KEY;
145: }
146:
147: /**
148: * returns i18n key. tasks implement this method.
149: * @return i18n key for the failed status
150: */
151: protected String getTaskSuccessStatusI18NKey() {
152: return SUCCESS_STATUS_KEY;
153: }
154:
155: /**
156: * returns i18n key
157: * @return the i18n key
158: */
159: protected String getEmptyQueryResultI18NKey(boolean isRuntime) {
160: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.empty"
161: : "jbi.ui.ant.print.jbi.comp.config.info.empty";
162: }
163:
164: /**
165: * returns i18n key
166: * @return the i18n key
167: */
168: protected String getQueryResultHeaderI18NKey(boolean isRuntime) {
169: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.header"
170: : "jbi.ui.ant.print.jbi.comp.config.info.header";
171: }
172:
173: /**
174: * returns i18n key
175: * @return the i18n key
176: */
177: protected String getQueryResultHeaderSeparatorI18NKey(
178: boolean isRuntime) {
179: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.header.separator"
180: : "jbi.ui.ant.print.jbi.comp.config.info.header.separator";
181: }
182:
183: /**
184: * returns i18n key
185: * @return the i18n key
186: */
187: protected String getQueryResultPageSeparatorI18NKey(
188: boolean isRuntime) {
189: return isRuntime ? "jbi.ui.ant.print.jbi.config.info.separator"
190: : "jbi.ui.ant.print.jbi.comp.config.info.separator";
191: }
192:
193: /**
194: * Printing component application variables
195: * @param compName component name
196: * @param params the paramters to be printed
197: * @param target target name
198: */
199: protected void printComponentConfiguration(String compName,
200: List paramList, Properties returnProps, String target) {
201: this
202: .logDebug("Printing Component Configuration .... for component="
203: + compName + " on target=" + target);
204:
205: boolean isRuntime = false;
206: String header = getI18NBundle().getMessage(
207: getQueryResultHeaderI18NKey(isRuntime),
208: new String[] { compName, target });
209: String headerSeparator = getI18NBundle().getMessage(
210: getQueryResultHeaderSeparatorI18NKey(isRuntime));
211: String pageSeparator = getI18NBundle().getMessage(
212: getQueryResultPageSeparatorI18NKey(isRuntime));
213: String emptyResult = getI18NBundle().getMessage(
214: getEmptyQueryResultI18NKey(isRuntime));
215:
216: StringWriter stringWriter = new StringWriter();
217: PrintWriter msgWriter = new PrintWriter(stringWriter);
218:
219: msgWriter.println(headerSeparator);
220: msgWriter.println(header);
221: msgWriter.println(headerSeparator);
222:
223: if (returnProps == null || returnProps.size() <= 0) {
224: msgWriter.println(emptyResult);
225: msgWriter.println(pageSeparator);
226: } else if (paramList == null || paramList.size() <= 0) {
227: SortedSet keys = new TreeSet(returnProps.keySet());
228:
229: for (Object key : keys) {
230: String name = (String) key;
231: String value = returnProps.getProperty(name, "");
232: String param = getI18NBundle().getMessage(
233: "jbi.ui.ant.print.jbi.comp.config.param", name,
234: value);
235: msgWriter.println(param);
236: }
237: msgWriter.println(pageSeparator);
238: } else {
239: Iterator itr = paramList.iterator();
240: while (itr.hasNext()) {
241: String name = "" + ((Param) itr.next()).getName();
242: String value = returnProps.getProperty(name);
243: if (value != null) {
244: String param = getI18NBundle().getMessage(
245: "jbi.ui.ant.print.jbi.comp.config.param",
246: name, value);
247: msgWriter.println(param);
248: } else {
249: String msg = getI18NBundle()
250: .getMessage(
251: "jbi.ui.ant.print.jbi.comp.config.param.not.retrieved",
252: name);
253: msgWriter.println(msg);
254: }
255: }
256: msgWriter.println(pageSeparator);
257: }
258:
259: msgWriter.close();
260: printMessage(stringWriter.getBuffer().toString());
261: }
262:
263: /**
264: * returns param element list
265: * @return param List
266: */
267: protected List getParamList() {
268: if (this .mParamList == null) {
269: this .mParamList = new ArrayList();
270: }
271: return this .mParamList;
272: }
273:
274: /**
275: * factory method for creating the nested element <param>
276: * @return Param object
277: */
278: public Param createParam() {
279: Param param = new Param();
280: this.getParamList().add(param);
281: return param;
282: }
283: }
|