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