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: * @(#)JbiUpdateApplicationConfigurationTask.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 JbiUpdateApplicationConfigurationTask extends
051: JbiTargetTask {
052: /**
053: * appconfig success msg key
054: */
055: private static final String APPCONFIG_SUCCESS_STATUS_KEY = "jbi.ui.ant.set.appconfig.successful";
056:
057: /**
058: * appconfig failure msg key
059: */
060: private static final String APPCONFIG_FAILED_STATUS_KEY = "jbi.ui.ant.set.appconfig.failed";
061:
062: /**
063: * appconfig success msg key
064: */
065: private static final String APPCONFIG_PARTIAL_SUCCESS_STATUS_KEY = "jbi.ui.ant.set.appconfig.partial.success";
066:
067: /** Holds Param Nested elements */
068: private List mParamList = null;
069:
070: /** Holds Params File **/
071: private String mParamsFile = null;
072:
073: /** Holds value of property componentName. */
074: private String mComponentName = null;
075:
076: /** Holds value of property name. */
077: private String mName = null;
078:
079: /**
080: * Getter for property componentName.
081: * @return Value of property componentName.
082: */
083: public String getComponentName() {
084: return this .mComponentName;
085: }
086:
087: /**
088: * Setter for property componentName.
089: * @param componentName name of the component.
090: */
091: public void setComponentName(String componentName) {
092: this .mComponentName = componentName;
093: }
094:
095: /**
096: * Getter for property name (i.e, the name of application configuration).
097: * @return Value of property name.
098: */
099: public String getName() {
100: return this .mName;
101: }
102:
103: /**
104: * Setter for property name (i.e, the name of application configuration).
105: * @param name of the application configuration.
106: */
107: public void setName(String name) {
108: this .mName = name;
109: }
110:
111: /** Getter for property Params.
112: * @return Value of property Params.
113: *
114: */
115: public String getParams() {
116: return this .mParamsFile;
117: }
118:
119: /**
120: * Sets the params file location to the absolute filename of the
121: * given file. If the value of this attribute is an absolute path, it
122: * is left unchanged (with / and \ characters converted to the
123: * current platforms conventions). Otherwise it is taken as a path
124: * relative to the project's basedir and expanded.
125: * @param paramsFile path to set
126: */
127:
128: public void setParams(String paramsFile) {
129: this .mParamsFile = paramsFile;
130: }
131:
132: private void debugPrintParams(Properties params) {
133: if (params == null) {
134: this .logDebug("Set Configuration params are NULL");
135: return;
136: }
137: StringWriter stringWriter = new StringWriter();
138: PrintWriter out = new PrintWriter(stringWriter);
139: params.list(out);
140: out.close();
141: this .logDebug(stringWriter.getBuffer().toString());
142: }
143:
144: private String createFormatedSuccessJbiResultMessage(
145: String i18nKey, Object[] args) {
146:
147: String msgCode = getI18NBundle().getMessage(i18nKey + ".ID");
148: String msg = getI18NBundle().getMessage(i18nKey, args);
149:
150: String jbiResultXml = JBIResultXmlBuilder.getInstance()
151: .createJbiResultXml("JBI_ANT_TASK_SET_CONFIG",
152: JBIResultXmlBuilder.SUCCESS_RESULT,
153: JBIResultXmlBuilder.INFO_MSG_TYPE, msgCode,
154: msg, args, null);
155:
156: JBIManagementMessage mgmtMsg = null;
157: mgmtMsg = JBIManagementMessage
158: .createJBIManagementMessage(jbiResultXml);
159: return (mgmtMsg != null) ? mgmtMsg.getMessage() : msg;
160: }
161:
162: private void executeUpdateApplicationConfiguration(
163: String componentName) throws BuildException {
164:
165: try {
166: // Go throught the static/constant configuration paramter elements
167: String appConfigName = this .getName();
168:
169: String target = getValidTarget();
170:
171: if ((componentName == null)
172: || (componentName.compareTo("") == 0)) {
173: String errMsg = createFailedFormattedJbiAdminResult(
174: "jbi.ui.ant.task.error.nullCompName", null);
175: throw new BuildException(errMsg);
176: }
177:
178: if ((appConfigName == null)
179: || (appConfigName.compareTo("") == 0)) {
180: String errMsg = createFailedFormattedJbiAdminResult(
181: "jbi.ui.ant.set.appconfig.no.name.error", null);
182: throw new BuildException(errMsg);
183: }
184:
185: this
186: .logDebug("Executing Set Application Configuration ....");
187:
188: List paramList = this .getParamList();
189: Properties appConfigParamProps = new Properties();
190:
191: Iterator itr = paramList.iterator();
192: while (itr.hasNext()) {
193: Param param = (Param) itr.next();
194: if (("" + param.getName()).compareTo("") != 0) {
195: if ((param.getValue() != null)
196: && (param.getValue().compareTo("") != 0)) {
197: appConfigParamProps.setProperty(
198: param.getName(), param.getValue());
199: } else if ((param.getAppVariable() != null)
200: && (param.getAppVariable().compareTo("") != 0)) {
201: appConfigParamProps
202: .setProperty(param.getName(), "$"
203: + param.getValue());
204: } else {
205: // Default one
206: appConfigParamProps.setProperty(
207: param.getName(), param.getValue());
208: }
209: } else {
210: this .logDebug("the input param name is empty.");
211: }
212: }
213:
214: if (appConfigParamProps.size() == 0) {
215: this
216: .logDebug("No nested param element is found in input.");
217: }
218:
219: String paramsFile = this .getParams();
220: if ((paramsFile != null) && (paramsFile.compareTo("") != 0)) {
221: appConfigParamProps.putAll(loadParamsFromFile(new File(
222: paramsFile)));
223: } else {
224: this
225: .logDebug("No File based Parameters passed to set application configuration Task via nested params element");
226: }
227:
228: if (appConfigParamProps.size() == 0) {
229: String msg = createFailedFormattedJbiAdminResult(
230: "jbi.ui.ant.list.no.input.appconfig.param.data.found",
231: null);
232: throw new BuildException(msg, getLocation());
233: }
234:
235: this .logDebug("component name: " + componentName
236: + " Application Configuration name: "
237: + appConfigName + " target: " + target);
238: debugPrintParams(appConfigParamProps);
239: String rtnXml = this .getJBIAdminCommands()
240: .setApplicationConfiguration(componentName, target,
241: appConfigName, appConfigParamProps);
242:
243: JBIManagementMessage mgmtMsg = JBIManagementMessage
244: .createJBIManagementMessage(rtnXml);
245: if (mgmtMsg.isFailedMsg()) {
246: throwBuildException(mgmtMsg);
247: } else {
248: // print success message
249: printTaskSuccess(mgmtMsg);
250: }
251: } catch (Exception ex) {
252: processTaskException(ex);
253: }
254: }
255:
256: /** executes the install task. Ant Task framework calls this method to
257: * excute the task.
258: * @throws BuildException if error or exception occurs.
259: */
260: public void executeTask() throws BuildException {
261: this .logDebug("Executing Set Configuration Task....");
262: String compName = getComponentName();
263:
264: executeUpdateApplicationConfiguration(compName);
265: }
266:
267: /**
268: * returns i18n key. tasks implement this method.
269: * @return i18n key for the success status
270: */
271: protected String getTaskFailedStatusI18NKey() {
272: return APPCONFIG_FAILED_STATUS_KEY;
273: }
274:
275: /**
276: * returns i18n key. tasks implement this method.
277: * @return i18n key for the failed status
278: */
279: protected String getTaskSuccessStatusI18NKey() {
280: return APPCONFIG_SUCCESS_STATUS_KEY;
281: }
282:
283: /**
284: * return i18n key for the partial success
285: * @return i18n key for the partial success
286: */
287: protected String getTaskPartialSuccessStatusI18NKey() {
288: return APPCONFIG_PARTIAL_SUCCESS_STATUS_KEY;
289: }
290:
291: /**
292: * returns appconfig element list
293: * @return Paramter List
294: */
295: protected List getParamList() {
296: if (this .mParamList == null) {
297: this .mParamList = new ArrayList();
298: }
299: return this .mParamList;
300: }
301:
302: /**
303: * load properties from a file
304: * @return the Loaded properties
305: * @param file file to load
306: * @throws BuildException on error
307: */
308: protected Properties loadParamsFromFile(File file)
309: throws BuildException {
310: String absFilePath = null;
311: String fileName = null;
312: if (file != null) {
313: absFilePath = file.getAbsolutePath();
314: fileName = file.getName();
315: }
316: if (file == null || !file.exists()) {
317: String msg = createFailedFormattedJbiAdminResult(
318: "jbi.ui.ant.task.error.config.params.file.not.exist",
319: new Object[] { fileName });
320: throw new BuildException(msg, getLocation());
321: }
322:
323: if (file.isDirectory()) {
324: String msg = createFailedFormattedJbiAdminResult(
325: "jbi.ui.ant.task.error.config.params.file.is.directory",
326: null);
327: throw new BuildException(msg, getLocation());
328: }
329:
330: Properties props = new Properties();
331: this .logDebug("Loading " + file.getAbsolutePath());
332: try {
333: FileInputStream fis = new FileInputStream(file);
334: try {
335: props.load(fis);
336: } finally {
337: if (fis != null) {
338: fis.close();
339: }
340: }
341: return props;
342: } catch (IOException ex) {
343: throw new BuildException(ex, getLocation());
344: }
345: }
346:
347: /**
348: * factory method for creating the nested element <param>
349: * @return Param Object
350: */
351: public Param createParam() {
352: Param param = new Param();
353: this.getParamList().add(param);
354: return param;
355: }
356: }
|