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