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