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: * @(#)JbiSetComponentConfigurationTask.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 com.sun.jbi.ui.common.JBIResultXmlBuilder;
033: import java.io.File;
034: import java.io.FileInputStream;
035: import java.io.IOException;
036: import java.io.PrintWriter;
037: import java.io.StringWriter;
038: import java.util.ArrayList;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042: import java.util.TreeMap;
043: import java.util.Properties;
044: import org.apache.tools.ant.BuildException;
045:
046: /** This class is an ant task for updating service engine or binding component.
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public class JbiSetComponentConfigurationTask extends JbiTargetTask {
051: /**
052: * success msg key
053: */
054: private static final String SUCCESS_STATUS_KEY = "jbi.ui.ant.set.configuration.successful";
055:
056: /**
057: * failure msg key
058: */
059: private static final String FAILED_STATUS_KEY = "jbi.ui.ant.set.configuration.failed";
060:
061: /**
062: * success msg key
063: */
064: private static final String PARTIAL_SUCCESS_STATUS_KEY = "jbi.ui.ant.set.configuration.partial.success";
065:
066: /** Holds Param Nested elements */
067: private List mParamList;
068:
069: /** Holds Params File **/
070: private String mParamsFile = null;
071:
072: /** Holds value of property componentName. */
073: private String mComponentName = null;
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: /** Getter for property Params.
092: * @return Value of property Params.
093: *
094: */
095: public String getParams() {
096: return this .mParamsFile;
097: }
098:
099: /**
100: * Sets the params file location to the absolute filename of the
101: * given file. If the value of this attribute is an absolute path, it
102: * is left unchanged (with / and \ characters converted to the
103: * current platforms conventions). Otherwise it is taken as a path
104: * relative to the project's basedir and expanded.
105: * @param paramsFile path to set
106: */
107:
108: public void setParams(String paramsFile) {
109: this .mParamsFile = paramsFile;
110: }
111:
112: private void debugPrintParams(Properties params) {
113: if (params == null) {
114: this .logDebug("Set Configuration params are NULL");
115: return;
116: }
117: StringWriter stringWriter = new StringWriter();
118: PrintWriter out = new PrintWriter(stringWriter);
119: params.list(out);
120: out.close();
121: this .logDebug(stringWriter.getBuffer().toString());
122: }
123:
124: private String createFormatedSuccessJbiResultMessage(
125: String i18nKey, Object[] args) {
126:
127: String msgCode = getI18NBundle().getMessage(i18nKey + ".ID");
128: String msg = getI18NBundle().getMessage(i18nKey, args);
129:
130: String jbiResultXml = JBIResultXmlBuilder.getInstance()
131: .createJbiResultXml("JBI_ANT_TASK_SET_CONFIG",
132: JBIResultXmlBuilder.SUCCESS_RESULT,
133: JBIResultXmlBuilder.INFO_MSG_TYPE, msgCode,
134: msg, args, null);
135:
136: JBIManagementMessage mgmtMsg = null;
137: mgmtMsg = JBIManagementMessage
138: .createJBIManagementMessage(jbiResultXml);
139: return (mgmtMsg != null) ? mgmtMsg.getMessage() : msg;
140: }
141:
142: private void executeSetComponentConfiguration(String componentName)
143: throws BuildException {
144:
145: try {
146: // Go throught the static/constant configuration paramter elements
147: Properties params = this .getParamsAsProperties();
148: if (params.size() == 0) {
149: String msg = createFailedFormattedJbiAdminResult(
150: "jbi.ui.ant.task.error.no.input.component.configuration.param.data.found",
151: null);
152: throw new BuildException(msg, getLocation());
153: }
154: debugPrintParams(params);
155:
156: String target = getValidTarget();
157:
158: this .logDebug("Executing Set Component Configuration ....");
159: this .logDebug("componentName: " + componentName
160: + " target: " + target + " params: " + params);
161:
162: String xmlString = this .getJBIAdminCommands()
163: .setComponentConfiguration(target, componentName,
164: params);
165: JBIManagementMessage mgmtMsg = JBIManagementMessage
166: .createJBIManagementMessage(xmlString);
167: if (mgmtMsg.isFailedMsg()) {
168: throw new Exception(xmlString);
169: } else {
170: // print success message
171: printTaskSuccess(mgmtMsg);
172: }
173: } catch (Exception ex) {
174: processTaskException(ex);
175: }
176: }
177:
178: /** executes the install task. Ant Task framework calls this method to
179: * excute the task.
180: * @throws BuildException if error or exception occurs.
181: */
182: public void executeTask() throws BuildException {
183: this .logDebug("Executing Set Component Configuration Task....");
184: String compName = getComponentName();
185:
186: executeSetComponentConfiguration(compName);
187: }
188:
189: /**
190: * returns i18n key. tasks implement this method.
191: * @return i18n key for the success status
192: */
193: protected String getTaskFailedStatusI18NKey() {
194: return FAILED_STATUS_KEY;
195: }
196:
197: /**
198: * returns i18n key. tasks implement this method.
199: * @return i18n key for the failed status
200: */
201: protected String getTaskSuccessStatusI18NKey() {
202: return SUCCESS_STATUS_KEY;
203: }
204:
205: /**
206: * return i18n key for the partial success
207: * @return i18n key for the partial success
208: */
209: protected String getTaskPartialSuccessStatusI18NKey() {
210: return PARTIAL_SUCCESS_STATUS_KEY;
211: }
212:
213: /**
214: * returns param element list
215: * @return Paramter List
216: */
217: protected List getParamList() {
218: if (this .mParamList == null) {
219: this .mParamList = new ArrayList();
220: }
221: return this .mParamList;
222: }
223:
224: /**
225: * load properties from a file
226: * @return the Loaded properties
227: * @param file file to load
228: * @throws BuildException on error
229: */
230: protected Properties loadParamsFromFile(File file)
231: throws BuildException {
232: String absFilePath = null;
233: String fileName = null;
234: if (file != null) {
235: absFilePath = file.getAbsolutePath();
236: fileName = file.getName();
237: }
238: if (file == null || !file.exists()) {
239: String msg = createFailedFormattedJbiAdminResult(
240: "jbi.ui.ant.task.error.config.params.file.not.exist",
241: new Object[] { fileName });
242: throw new BuildException(msg, getLocation());
243: }
244:
245: if (file.isDirectory()) {
246: String msg = createFailedFormattedJbiAdminResult(
247: "jbi.ui.ant.task.error.config.params.file.is.directory",
248: null);
249: throw new BuildException(msg, getLocation());
250: }
251:
252: Properties props = new Properties();
253: this .logDebug("Loading " + file.getAbsolutePath());
254: try {
255: FileInputStream fis = new FileInputStream(file);
256: try {
257: props.load(fis);
258: } finally {
259: if (fis != null) {
260: fis.close();
261: }
262: }
263: return props;
264: } catch (IOException ex) {
265: throw new BuildException(ex, getLocation());
266: }
267: }
268:
269: /**
270: * converts the Params list to the properties object with <name><value> pair
271: * @return Properties object that will be passed to the install method of UIMBean
272: */
273: protected Properties getParamsAsProperties() throws BuildException {
274: Properties props = new Properties();
275: // add params from the nested param elements
276: for (Iterator itr = getParamList().iterator(); itr.hasNext();) {
277: Param param = (Param) itr.next();
278: // non null name is gaurenteed.
279: String name = ("" + param.getName()).trim();
280: // non null value is gaurenteed.
281: String value = ("" + param.getValue()).trim();
282:
283: if (name.length() > 0) {
284: props.setProperty(name, value);
285: }
286: }
287:
288: Properties paramsProps = null;
289:
290: paramsProps = null;
291: String paramsFile = this .getParams();
292: if ((paramsFile != null) && (paramsFile.compareTo("") != 0)) {
293: paramsProps = this .loadParamsFromFile(new File(paramsFile));
294: props.putAll(paramsProps);
295: } else {
296: this
297: .logDebug("No File based Parameters passed to installer Task via nested params element");
298: }
299:
300: return props;
301: }
302:
303: /**
304: * factory method for creating the nested element <param>
305: * @return Param Object
306: */
307: public Param createParam() {
308: Param param = new Param();
309: this.getParamList().add(param);
310: return param;
311: }
312: }
|