KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > management > task > InstallComponentTask


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.management.task;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.apache.servicemix.jbi.framework.AdminCommandsServiceMBean;
28 import org.apache.tools.ant.BuildException;
29
30 /**
31  * Install a Component
32  *
33  * @version $Revision: 427204 $
34  */

35 public class InstallComponentTask extends JbiTask {
36     
37     private String JavaDoc file; //file to install
38
private String JavaDoc paramsFile;
39     private List JavaDoc nestedParams;
40     
41     /**
42      * @return Returns the file.
43      */

44     public String JavaDoc getFile() {
45         return file;
46     }
47     /**
48      * @param file The file to set.
49      */

50     public void setFile(String JavaDoc file) {
51         this.file = file;
52     }
53     
54     public String JavaDoc getParams() {
55         return paramsFile;
56     }
57     public void setParams(String JavaDoc paramsFile) {
58         this.paramsFile = paramsFile;
59     }
60     
61     public Param createParam() {
62         Param p = new Param();
63         if (nestedParams == null) {
64             nestedParams = new ArrayList JavaDoc();
65         }
66         nestedParams.add(p);
67         return p;
68     }
69     
70     /**
71      * execute the task
72      * @throws BuildException
73      */

74     public void doExecute(AdminCommandsServiceMBean acs) throws Exception JavaDoc {
75         if (file == null){
76             throw new BuildException("null file - file should be an archive");
77         }
78         if (!file.endsWith(".zip") && !file.endsWith(".jar")) {
79             throw new BuildException("file: " + file + " is not an archive");
80         }
81         File JavaDoc archive = new File JavaDoc(file);
82         String JavaDoc location = archive.getAbsolutePath();
83         if (!archive.isFile()) {
84             // if it's not a file, assume it's a url and pass it along
85
location = file;
86         }
87         Properties JavaDoc props = getProperties();
88         acs.installComponent(location, props);
89     }
90     
91     private Properties JavaDoc getProperties() throws IOException JavaDoc {
92         Properties JavaDoc props = new Properties JavaDoc();
93         if (paramsFile != null) {
94             props.load(new FileInputStream JavaDoc(paramsFile));
95         }
96         if (nestedParams != null) {
97             for (Iterator JavaDoc iter = nestedParams.iterator(); iter.hasNext();) {
98                 Param p = (Param) iter.next();
99                 props.setProperty(p.getName(), p.getValue());
100             }
101         }
102         return props;
103     }
104     
105     public static class Param {
106         private String JavaDoc name;
107         private String JavaDoc value;
108         public String JavaDoc getName() {
109             return name;
110         }
111         public void setName(String JavaDoc name) {
112             this.name = name;
113         }
114         public String JavaDoc getValue() {
115             return value;
116         }
117         public void setValue(String JavaDoc value) {
118             this.value = value;
119         }
120     }
121     
122 }
Popular Tags