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: * @(#)JbiSetRuntimeConfigurationTask.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 JbiSetRuntimeConfigurationTask extends JbiTargetTask {
051: /**
052: * success msg key
053: */
054: private static final String SUCCESS_STATUS_KEY = "jbi.ui.ant.set.configuration.successful";
055:
056: /**
057: * failure msg key
058: */
059: private static final String FAILED_STATUS_KEY = "jbi.ui.ant.set.configuration.failed";
060:
061: /**
062: * success msg key
063: */
064: private static final String PARTIAL_SUCCESS_STATUS_KEY = "jbi.ui.ant.set.configuration.partial.success";
065:
066: /** Holds Param Nested elements */
067: private List mParamList;
068:
069: /** Holds Params File **/
070: private String mParamsFile = null;
071:
072: /** Getter for property Params.
073: * @return Value of property Params.
074: *
075: */
076: public String getParams() {
077: return this .mParamsFile;
078: }
079:
080: /**
081: * Sets the params file location to the absolute filename of the
082: * given file. If the value of this attribute is an absolute path, it
083: * is left unchanged (with / and \ characters converted to the
084: * current platforms conventions). Otherwise it is taken as a path
085: * relative to the project's basedir and expanded.
086: * @param paramsFile path to set
087: */
088:
089: public void setParams(String paramsFile) {
090: this .mParamsFile = paramsFile;
091: }
092:
093: private void debugPrintParams(Properties params) {
094: if (params == null) {
095: this .logDebug("Set Configuration params are NULL");
096: return;
097: }
098:
099: StringWriter stringWriter = new StringWriter();
100: PrintWriter out = new PrintWriter(stringWriter);
101: params.list(out);
102: out.close();
103: this .logDebug(stringWriter.getBuffer().toString());
104: }
105:
106: private String createFormatedSuccessJbiResultMessage(
107: String i18nKey, Object[] args) {
108:
109: String msgCode = getI18NBundle().getMessage(i18nKey + ".ID");
110: String msg = getI18NBundle().getMessage(i18nKey, args);
111:
112: String jbiResultXml = JBIResultXmlBuilder.getInstance()
113: .createJbiResultXml("JBI_ANT_TASK_SET_CONFIG",
114: JBIResultXmlBuilder.SUCCESS_RESULT,
115: JBIResultXmlBuilder.INFO_MSG_TYPE, msgCode,
116: msg, args, null);
117:
118: JBIManagementMessage mgmtMsg = null;
119: mgmtMsg = JBIManagementMessage
120: .createJBIManagementMessage(jbiResultXml);
121: return (mgmtMsg != null) ? mgmtMsg.getMessage() : msg;
122: }
123:
124: private void executeSetRuntimeConfiguration() throws BuildException {
125:
126: this .logDebug("Executing Set Runtime Configuration ....");
127:
128: String target = getValidTarget();
129: boolean restartRequired = false;
130: try {
131: Properties params = this .getParamsAsProperties();
132: if (params.size() == 0) {
133: String msg = createFailedFormattedJbiAdminResult(
134: "jbi.ui.ant.task.error.no.input.runtine.configuration.param.data.found",
135: null);
136: throw new BuildException(msg, getLocation());
137: }
138: debugPrintParams(params);
139:
140: restartRequired = this .getJBIAdminCommands()
141: .setRuntimeConfiguration(params, target);
142: String i18nKey = restartRequired ? "jbi.ui.ant.task.info.config.restart.required"
143: : "jbi.ui.ant.task.info.config.done.on.target";
144: Object[] args = { target };
145:
146: this
147: .printTaskSuccess(createFormatedSuccessJbiResultMessage(
148: i18nKey, args));
149: } catch (Exception ex) {
150: processTaskException(ex);
151: }
152: }
153:
154: /** executes the install task. Ant Task framework calls this method to
155: * excute the task.
156: * @throws BuildException if error or exception occurs.
157: */
158: public void executeTask() throws BuildException {
159: this .logDebug("Executing Set Runtime Configuration Task....");
160:
161: executeSetRuntimeConfiguration();
162: }
163:
164: /**
165: * returns i18n key. tasks implement this method.
166: * @return i18n key for the success status
167: */
168: protected String getTaskFailedStatusI18NKey() {
169: return FAILED_STATUS_KEY;
170: }
171:
172: /**
173: * returns i18n key. tasks implement this method.
174: * @return i18n key for the failed status
175: */
176: protected String getTaskSuccessStatusI18NKey() {
177: return SUCCESS_STATUS_KEY;
178: }
179:
180: /**
181: * return i18n key for the partial success
182: * @return i18n key for the partial success
183: */
184: protected String getTaskPartialSuccessStatusI18NKey() {
185: return PARTIAL_SUCCESS_STATUS_KEY;
186: }
187:
188: /**
189: * returns param element list
190: * @return Paramter List
191: */
192: protected List getParamList() {
193: if (this .mParamList == null) {
194: this .mParamList = new ArrayList();
195: }
196: return this .mParamList;
197: }
198:
199: /**
200: * load properties from a file
201: * @return the Loaded properties
202: * @param file file to load
203: * @throws BuildException on error
204: */
205: protected Properties loadParamsFromFile(File file)
206: throws BuildException {
207: String absFilePath = null;
208: String fileName = null;
209: if (file != null) {
210: absFilePath = file.getAbsolutePath();
211: fileName = file.getName();
212: }
213: if (file == null || !file.exists()) {
214: String msg = createFailedFormattedJbiAdminResult(
215: "jbi.ui.ant.task.error.config.params.file.not.exist",
216: new Object[] { fileName });
217: throw new BuildException(msg, getLocation());
218: }
219:
220: if (file.isDirectory()) {
221: String msg = createFailedFormattedJbiAdminResult(
222: "jbi.ui.ant.task.error.config.params.file.is.directory",
223: null);
224: throw new BuildException(msg, getLocation());
225: }
226:
227: Properties props = new Properties();
228: this .logDebug("Loading " + file.getAbsolutePath());
229: try {
230: FileInputStream fis = new FileInputStream(file);
231: try {
232: props.load(fis);
233: } finally {
234: if (fis != null) {
235: fis.close();
236: }
237: }
238: return props;
239: } catch (IOException ex) {
240: throw new BuildException(ex, getLocation());
241: }
242: }
243:
244: /**
245: * converts the Params list to the properties object with <name><value> pair
246: * @return Properties object that will be passed to the install method of UIMBean
247: */
248: protected Properties getParamsAsProperties() throws BuildException {
249: Properties props = new Properties();
250: // add params from the nested param elements
251: for (Iterator itr = getParamList().iterator(); itr.hasNext();) {
252: Param param = (Param) itr.next();
253: // non null name is gaurenteed.
254: String name = ("" + param.getName()).trim();
255: // non null value is gaurenteed.
256: String value = ("" + param.getValue()).trim();
257:
258: if (name.length() > 0) {
259: props.setProperty(name, value);
260: }
261: }
262:
263: Properties paramsProps = null;
264:
265: paramsProps = null;
266: String paramsFile = this .getParams();
267: if ((paramsFile != null) && (paramsFile.compareTo("") != 0)) {
268: paramsProps = this .loadParamsFromFile(new File(paramsFile));
269: props.putAll(paramsProps);
270: } else {
271: this
272: .logDebug("No File based Parameters passed to installer Task via nested params element");
273: }
274:
275: return props;
276: }
277:
278: /**
279: * factory method for creating the nested element <param>
280: * @return Param Object
281: */
282: public Param createParam() {
283: Param param = new Param();
284: this.getParamList().add(param);
285: return param;
286: }
287: }
|