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: * @(#)JbiInstallComponentTask.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 java.io.File;
032: import java.io.FileInputStream;
033: import java.io.IOException;
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Properties;
038: import org.apache.tools.ant.BuildException;
039:
040: /** This class is an ant task for installing service engine or binding component.
041: *
042: * @author Sun Microsystems, Inc.
043: */
044: public class JbiInstallComponentTask extends JbiTargetTask {
045: /**
046: * success msg key
047: */
048: private static final String SUCCESS_STATUS_KEY = "jbi.ui.ant.install.successful";
049: /**
050: * failure msg key
051: */
052: private static final String FAILED_STATUS_KEY = "jbi.ui.ant.install.failed";
053: /** Holds value of property installFile. */
054: private File mInstallFile;
055:
056: /** Holds Param Nested elements */
057: private List mParamList;
058:
059: /** Holds Params File **/
060: private File mParamsFile = null;
061:
062: /** Holds value of property componentName. */
063: private String mComponentName = null;
064:
065: /** Getter for property componentName.
066: * @return Value of property componentName.
067: *
068: */
069: public String getName() {
070: return this .mComponentName;
071: }
072:
073: /**
074: * Setter for property componentName.
075: * @param name component name.
076: */
077: public void setName(String name) {
078: this .mComponentName = name;
079: }
080:
081: /** Getter for property component installer zip file.
082: * @return Value of property installer zip file.
083: *
084: */
085: public File getFile() {
086: return this .mInstallFile;
087: }
088:
089: /** Setter for property installer zip file.
090: * @param file New value of property installer zip file.
091: *
092: */
093: public void setFile(File file) {
094: this .mInstallFile = file;
095: }
096:
097: /** Getter for property componentJar.
098: * @return Value of property componentJar.
099: *
100: */
101: public File getParams() {
102: return this .mParamsFile;
103: }
104:
105: /**
106: * Sets the params file location to the absolute filename of the
107: * given file. If the value of this attribute is an absolute path, it
108: * is left unchanged (with / and \ characters converted to the
109: * current platforms conventions). Otherwise it is taken as a path
110: * relative to the project's basedir and expanded.
111: * @param paramsFile path to set
112: */
113:
114: public void setParams(File paramsFile) {
115: this .mParamsFile = paramsFile;
116: }
117:
118: /**
119: * validates the file
120: */
121: protected void validateInstallFile(File installFile)
122: throws BuildException {
123: if (installFile == null) {
124: throwTaskBuildException("jbi.ui.ant.install.error.comp.archive.file.path.null");
125: }
126:
127: if (installFile.getPath().trim().length() <= 0) {
128: throwTaskBuildException("jbi.ui.ant.install.error.comp.archive.file.path.required");
129: }
130:
131: if (!installFile.exists()) {
132:
133: throwTaskBuildException(
134: "jbi.ui.ant.install.error.comp.archive.file.not.exist",
135: installFile.getName());
136: }
137:
138: if (installFile.isDirectory()) {
139: throwTaskBuildException("jbi.ui.ant.install.error.comp.archive.file.is.directory");
140: }
141:
142: }
143:
144: /**
145: * validate compName with valid target
146: * @param compName name of the component in the repository
147: * @param target value, can not be domain
148: */
149: protected void validateInstallFromDomainAttributes(String compName,
150: String target) throws BuildException {
151: if (compName.trim().length() == 0) {
152: throwTaskBuildException("jbi.ui.ant.install.from.domain.error.comp.name.required");
153: }
154: if ("domain".equals(target)) {
155: throwTaskBuildException("jbi.ui.ant.install.error.comp.invalid.target.with.name.attrib");
156: }
157: }
158:
159: /** executes the install task. Ant Task framework calls this method to
160: * excute the task.
161: * @throws BuildException if error or exception occurs.
162: */
163: public void executeTask() throws BuildException {
164: String installFileAbsolutePath = null;
165: boolean installFromDoamin = false;
166:
167: String compName = getName();
168:
169: File installFile = getFile();
170:
171: String target = getValidTarget();
172:
173: if (compName == null && installFile == null) {
174: throwTaskBuildException("jbi.ui.ant.install.error.comp.name.or.file.required");
175: }
176:
177: if (compName != null && installFile != null) {
178: throwTaskBuildException("jbi.ui.ant.install.error.comp.name.and.file.set");
179: }
180:
181: if (compName != null) {
182: validateInstallFromDomainAttributes(compName, target);
183: installFromDoamin = true;
184: } else {
185: validateInstallFile(installFile);
186: installFileAbsolutePath = installFile.getAbsolutePath();
187: }
188:
189: try {
190: String result = null;
191: Properties params = this .getParamsAsProperties();
192: if (installFromDoamin) {
193: result = this .getJBIAdminCommands()
194: .installComponentFromDomain(compName, params,
195: target);
196: } else {
197: result = this .getJBIAdminCommands().installComponent(
198: installFileAbsolutePath, params, target);
199: }
200:
201: printTaskSuccess(result);
202:
203: } catch (Exception ex) {
204: processTaskException(ex);
205: }
206:
207: }
208:
209: /**
210: * returns i18n key. tasks implement this method.
211: * @return i18n key for the success status
212: */
213: protected String getTaskFailedStatusI18NKey() {
214: return FAILED_STATUS_KEY;
215: }
216:
217: /**
218: * returns i18n key. tasks implement this method.
219: * @return i18n key for the failed status
220: */
221: protected String getTaskSuccessStatusI18NKey() {
222: return SUCCESS_STATUS_KEY;
223: }
224:
225: /**
226: * returns param element list
227: * @return Paramter List
228: */
229: protected List getParamList() {
230: if (this .mParamList == null) {
231: this .mParamList = new ArrayList();
232: }
233: return this .mParamList;
234: }
235:
236: /**
237: * load properties from a file
238: * @return the Loaded properties
239: * @param file file to load
240: * @throws BuildException on error
241: */
242: protected Properties loadParamsFromFile(File file)
243: throws BuildException {
244: String absFilePath = null;
245: String fileName = null;
246: if (file != null) {
247: absFilePath = file.getAbsolutePath();
248: fileName = file.getName();
249: }
250: if (file == null || !file.exists()) {
251: String msg = createFailedFormattedJbiAdminResult(
252: "jbi.ui.ant.task.error.params.file.not.exist",
253: new Object[] { fileName });
254: throw new BuildException(msg, getLocation());
255: }
256:
257: if (file.isDirectory()) {
258: String msg = createFailedFormattedJbiAdminResult(
259: "jbi.ui.ant.task.error.params.file.is.directory",
260: null);
261: throw new BuildException(msg, getLocation());
262: }
263:
264: Properties props = new Properties();
265: this .logDebug("Loading " + file.getAbsolutePath());
266: try {
267: FileInputStream fis = new FileInputStream(file);
268: try {
269: props.load(fis);
270: } finally {
271: if (fis != null) {
272: fis.close();
273: }
274: }
275: return props;
276: } catch (IOException ex) {
277: throw new BuildException(ex, getLocation());
278: }
279: }
280:
281: /**
282: * converts the Params list to the properties object with <name><value> pair
283: * @return Properties object that will be passed to the install method of UIMBean
284: */
285: protected Properties getParamsAsProperties() throws BuildException {
286: Properties props = new Properties();
287: // add params from the nested param elements
288: for (Iterator itr = getParamList().iterator(); itr.hasNext();) {
289: Param param = (Param) itr.next();
290: // non null name is gaurenteed.
291: String name = param.getName().trim();
292: // non null value is gaurenteed.
293: String value = param.getValue().trim();
294: if (name.length() <= 0 || value.length() <= 0) {
295: String msg = createFailedFormattedJbiAdminResult(
296: "jbi.ui.ant.task.error.param.invalid",
297: new Object[] { name, value });
298: throw new BuildException(msg, getLocation());
299: }
300: if (name.length() > 0) {
301: props.setProperty(name, value);
302: } else {
303: // log warning
304: this
305: .logDebug("Blank parameter name passed to the install task using <param> nested element.");
306: }
307: }
308:
309: Properties paramsProps = null;
310:
311: paramsProps = null;
312: File paramsFile = this .getParams();
313: if (paramsFile != null) {
314: paramsProps = this .loadParamsFromFile(paramsFile);
315: props.putAll(paramsProps);
316: } else {
317: this
318: .logDebug("No File based Parameters passed to installer Task via nested params element");
319: }
320:
321: return props;
322: }
323:
324: /**
325: * factory method for creating the nested element <param>
326: * @return Param Object
327: */
328: public Param createParam() {
329: Param param = new Param();
330: this .getParamList().add(param);
331: return param;
332: }
333:
334: /**
335: * configuration parameters nested element
336: */
337: public class Param {
338:
339: /**
340: * Holds value of property name.
341: */
342: private String mName;
343:
344: /**
345: * Holds value of property value.
346: */
347: private String mValue;
348:
349: /**
350: * default public constructor
351: */
352: public Param() {
353: this .mName = "";
354: this .mValue = "";
355: }
356:
357: /**
358: * Getter for property name.
359: * @return Value of property name.
360: */
361: public String getName() {
362: return this .mName;
363: }
364:
365: /**
366: * Setter for property name.
367: * @param name New value of property name.
368: */
369: public void setName(String name) {
370: this .mName = name;
371: }
372:
373: /**
374: * Getter for property value.
375: * @return Value of property value.
376: */
377: public String getValue() {
378: return this .mValue;
379: }
380:
381: /**
382: * Setter for property value.
383: * @param value New value of property value.
384: */
385: public void setValue(String value) {
386: this.mValue = value;
387: }
388:
389: }
390:
391: }
|