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: * @(#)JbiDeleteApplicationVariablesTask.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.Vector;
043: import java.util.TreeMap;
044: import java.util.Properties;
045: import org.apache.tools.ant.BuildException;
046:
047: /** This class is an ant task for updating service engine or binding component.
048: *
049: * @author Sun Microsystems, Inc.
050: */
051: public class JbiDeleteApplicationVariablesTask extends JbiTargetTask {
052: /**
053: * appvariable success msg key
054: */
055: private static final String APPVARIABLE_SUCCESS_STATUS_KEY = "jbi.ui.ant.delete.appvariable.successful";
056:
057: /**
058: * appvariable failure msg key
059: */
060: private static final String APPVARIABLE_FAILED_STATUS_KEY = "jbi.ui.ant.delete.appvariable.failed";
061:
062: /**
063: * appvariable success msg key
064: */
065: private static final String APPVARIABLE_PARTIAL_SUCCESS_STATUS_KEY = "jbi.ui.ant.delete.appvariable.partial.success";
066:
067: /** Holds AppVariable Nested elements */
068: private List mAppVariableList;
069:
070: /** Holds value of property componentName. */
071: private String mComponentName = null;
072:
073: /**
074: * Getter for property componentName.
075: * @return Value of property componentName.
076: */
077: public String getComponentName() {
078: return this .mComponentName;
079: }
080:
081: /**
082: * Setter for property componentName.
083: * @param componentName name of the component.
084: */
085: public void setComponentName(String componentName) {
086: this .mComponentName = componentName;
087: }
088:
089: private void debugPrintParams(Properties params) {
090: if (params == null) {
091: this .logDebug("Set Configuration params are NULL");
092: return;
093: }
094: StringWriter stringWriter = new StringWriter();
095: PrintWriter out = new PrintWriter(stringWriter);
096: params.list(out);
097: out.close();
098: this .logDebug(stringWriter.getBuffer().toString());
099: }
100:
101: private String createFormatedSuccessJbiResultMessage(
102: String i18nKey, Object[] args) {
103:
104: String msgCode = getI18NBundle().getMessage(i18nKey + ".ID");
105: String msg = getI18NBundle().getMessage(i18nKey, args);
106:
107: String jbiResultXml = JBIResultXmlBuilder.getInstance()
108: .createJbiResultXml("JBI_ANT_TASK_SET_CONFIG",
109: JBIResultXmlBuilder.SUCCESS_RESULT,
110: JBIResultXmlBuilder.INFO_MSG_TYPE, msgCode,
111: msg, args, null);
112:
113: JBIManagementMessage mgmtMsg = null;
114: mgmtMsg = JBIManagementMessage
115: .createJBIManagementMessage(jbiResultXml);
116: return (mgmtMsg != null) ? mgmtMsg.getMessage() : msg;
117: }
118:
119: private void executeDeleteApplicationVariables(
120: List appVariableList, String compName, String target)
121: throws Exception, BuildException {
122: // Go throught the appvariable elements
123: this .logDebug("Delete application variables, component name: "
124: + compName + " target: " + target);
125: Properties varsProps = new Properties();
126:
127: Iterator itr = appVariableList.iterator();
128: ArrayList<String> deletedNames = new ArrayList<String>();
129: while (itr.hasNext()) {
130: AppVariable appVar = (AppVariable) itr.next();
131: String appVarName = "" + appVar.getName();
132: if (appVarName.compareTo("") == 0) {
133: String msg = createFailedFormattedJbiAdminResult(
134: "jbi.ui.ant.list.no.input.appvariable.data.found",
135: null);
136: throw new BuildException(msg, getLocation());
137: }
138:
139: this .logDebug("The deleted applicaiton variable is: "
140: + appVarName);
141: /* The name filed could contain multiple variable names,
142: separated by " ", need to break and get them */
143: String[] appVarNames = ("" + appVarName).split("\\s");
144: for (int i = 0; i < appVarNames.length; ++i) {
145: deletedNames.add(appVarNames[i]);
146: }
147: }
148:
149: String[] deletedStrs = deletedNames.toArray(new String[0]);
150: for (int i = 0; i < deletedStrs.length; ++i) {
151: this .logDebug("Variable name to be deleted: "
152: + deletedStrs[i]);
153: }
154:
155: String rtnXml = this .getJBIAdminCommands()
156: .deleteApplicationVariables(compName, target,
157: (String[]) deletedNames.toArray(new String[0]));
158:
159: JBIManagementMessage mgmtMsg = JBIManagementMessage
160: .createJBIManagementMessage(rtnXml);
161: if (mgmtMsg.isFailedMsg()) {
162: throw new Exception(rtnXml);
163: }
164:
165: printTaskSuccess(mgmtMsg);
166: }
167:
168: /** executes the install task. Ant Task framework calls this method to
169: * excute the task.
170: * @throws BuildException if error or exception occurs.
171: */
172: public void executeTask() throws BuildException {
173: try {
174: String compName = getComponentName();
175: String target = getValidTarget();
176: List appVariableList = this .getAppVariableList();
177:
178: if ((compName == null) || (compName.compareTo("") == 0)) {
179: String errMsg = createFailedFormattedJbiAdminResult(
180: "jbi.ui.ant.task.error.nullCompName", null);
181: throw new BuildException(errMsg);
182: }
183:
184: this
185: .logDebug("Executing delete application variable Task....");
186: executeDeleteApplicationVariables(appVariableList,
187: compName, target);
188: } catch (Exception ex) {
189: processTaskException(ex);
190: }
191: }
192:
193: /**
194: * returns i18n key. tasks implement this method.
195: * @return i18n key for the success status
196: */
197: protected String getTaskFailedStatusI18NKey() {
198: return APPVARIABLE_FAILED_STATUS_KEY;
199: }
200:
201: /**
202: * returns i18n key. tasks implement this method.
203: * @return i18n key for the failed status
204: */
205: protected String getTaskSuccessStatusI18NKey() {
206: return APPVARIABLE_SUCCESS_STATUS_KEY;
207: }
208:
209: /**
210: * return i18n key for the partial success
211: * @return i18n key for the partial success
212: */
213: protected String getTaskPartialSuccessStatusI18NKey() {
214: return APPVARIABLE_PARTIAL_SUCCESS_STATUS_KEY;
215: }
216:
217: /**
218: * returns AppVariable element list
219: * @return AppVariable List
220: */
221: protected List getAppVariableList() {
222: if (this .mAppVariableList == null) {
223: this .mAppVariableList = new ArrayList();
224: }
225: return this .mAppVariableList;
226: }
227:
228: /**
229: * factory method for creating the nested element <AppVariable>
230: * @return AppVariable Object
231: */
232: public AppVariable createAppVariable() {
233: AppVariable appVar = new AppVariable();
234: this.getAppVariableList().add(appVar);
235: return appVar;
236: }
237: }
|