KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > plugin > packaging > PackageBuilder


1 /**
2  *
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.geronimo.plugin.packaging;
18
19 import java.io.File JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import javax.management.MalformedObjectNameException JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25
26 import org.apache.geronimo.gbean.GBeanData;
27 import org.apache.geronimo.kernel.KernelRegistry;
28 import org.apache.geronimo.kernel.KernelFactory;
29 import org.apache.geronimo.kernel.Kernel;
30 import org.apache.geronimo.kernel.config.ConfigurationManager;
31 import org.apache.geronimo.kernel.config.ConfigurationUtil;
32
33 /**
34  * JellyBean that builds a Geronimo Configuration using the local Mavem
35  * infrastructure.
36  *
37  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
38  */

39 public class PackageBuilder {
40     private static final String JavaDoc KERNEL_NAME = "geronimo.maven";
41     /**
42      * The name of the GBean that will load dependencies from the Maven repository.
43      */

44     private static final ObjectName JavaDoc REPOSITORY_NAME;
45
46     /**
47      * The name of the GBean that will load Configurations from the Maven repository.
48      */

49     private static final ObjectName JavaDoc CONFIGSTORE_NAME;
50
51     private static final String JavaDoc[] ARG_TYPES = {
52         File JavaDoc.class.getName(),
53         File JavaDoc.class.getName(),
54         File JavaDoc.class.getName(),
55         Boolean.TYPE.getName(),
56         String JavaDoc.class.getName(),
57         String JavaDoc.class.getName(),
58         String JavaDoc.class.getName(),
59     };
60
61     static {
62         try {
63             REPOSITORY_NAME = new ObjectName JavaDoc(KERNEL_NAME + ":name=Repository");
64             CONFIGSTORE_NAME = new ObjectName JavaDoc(KERNEL_NAME + ":name=MavenConfigStore,j2eeType=ConfigurationStore");
65         } catch (MalformedObjectNameException JavaDoc e) {
66             throw new ExceptionInInitializerError JavaDoc(e.getMessage());
67         }
68     }
69
70     private File JavaDoc repository;
71     private URI JavaDoc deploymentConfig;
72     private ObjectName JavaDoc deployerName;
73
74     private File JavaDoc planFile;
75     private File JavaDoc moduleFile;
76     private File JavaDoc packageFile;
77     private String JavaDoc mainClass;
78     private String JavaDoc classPath;
79     private String JavaDoc endorsedDirs;
80
81     public File JavaDoc getRepository() {
82         return repository;
83     }
84
85     /**
86      * Set the location of the Maven repository; typically ${maven.repo.local}
87      *
88      * @param repository the location of the Maven repository
89      */

90     public void setRepository(File JavaDoc repository) {
91         this.repository = repository;
92     }
93
94     public String JavaDoc getDeploymentConfig() {
95         return deploymentConfig.toString();
96     }
97
98     /**
99      * Set the id of the Configuration to use to perform the packaging.
100      *
101      * @param deploymentConfig the id of the Configuration performing the deployment
102      */

103     public void setDeploymentConfig(String JavaDoc deploymentConfig) {
104         this.deploymentConfig = URI.create(deploymentConfig);
105     }
106
107     public String JavaDoc getDeployerName() {
108         return deployerName.toString();
109     }
110
111     /**
112      * Set the name of the GBean that is the Deployer.
113      *
114      * @param deployerName the name of the Deployer GBean
115      */

116     public void setDeployerName(String JavaDoc deployerName) {
117         try {
118             this.deployerName = new ObjectName JavaDoc(deployerName);
119         } catch (MalformedObjectNameException JavaDoc e) {
120             throw new IllegalArgumentException JavaDoc("deployerName is not a valid ObjectName: " + deployerName);
121         }
122     }
123
124     public File JavaDoc getPlanFile() {
125         return planFile;
126     }
127
128     /**
129      * Set the File that is the deployment plan.
130      *
131      * @param planFile the deployment plan
132      */

133     public void setPlanFile(File JavaDoc planFile) {
134         this.planFile = planFile;
135     }
136
137     public File JavaDoc getModuleFile() {
138         return moduleFile;
139     }
140
141     /**
142      * Set the File that is the module being deployed.
143      *
144      * @param moduleFile the module to deploy
145      */

146     public void setModuleFile(File JavaDoc moduleFile) {
147         this.moduleFile = moduleFile;
148     }
149
150     public File JavaDoc getPackageFile() {
151         return packageFile;
152     }
153
154     /**
155      * Set the File where the Configuration will be stored; normally the artifact being produced.
156      *
157      * @param packageFile the package file to produce
158      */

159     public void setPackageFile(File JavaDoc packageFile) {
160         this.packageFile = packageFile;
161     }
162
163     public String JavaDoc getMainClass() {
164         return mainClass;
165     }
166
167     /**
168      * Set the name of the class containing the main method for a executable configuration.
169      *
170      * @param mainClass
171      */

172     public void setMainClass(String JavaDoc mainClass) {
173         this.mainClass = mainClass;
174     }
175
176     public String JavaDoc getClassPath() {
177         return classPath;
178     }
179
180     public void setClassPath(String JavaDoc classPath) {
181         this.classPath = classPath;
182     }
183
184     public String JavaDoc getEndorsedDirs() {
185         return endorsedDirs;
186     }
187
188     public void setEndorsedDirs(String JavaDoc endorsedDirs) {
189         this.endorsedDirs = endorsedDirs;
190     }
191
192     public void execute() throws Exception JavaDoc {
193         Kernel kernel = createKernel();
194
195         // start the Configuration we're going to use for this deployment
196
ConfigurationManager configMgr = ConfigurationUtil.getConfigurationManager(kernel);
197         if (!configMgr.isLoaded(deploymentConfig)) {
198             List JavaDoc configs = configMgr.loadRecursive(deploymentConfig);
199             for (int i = 0; i < configs.size(); i++) {
200                 ObjectName JavaDoc configName = (ObjectName JavaDoc) configs.get(i);
201                 kernel.startRecursiveGBean(configName);
202             }
203         }
204
205         ObjectName JavaDoc deployer = locateDeployer(kernel);
206         invokeDeployer(kernel, deployer);
207         System.out.println("Generated package " + packageFile);
208     }
209
210     /**
211      * Create a Geronimo Kernel to contain the deployment configurations.
212      */

213     private Kernel createKernel() throws Exception JavaDoc {
214         Kernel kernel = KernelRegistry.getKernel(KERNEL_NAME);
215         if (kernel != null) {
216             return kernel;
217         }
218
219         kernel = KernelFactory.newInstance().createKernel(KERNEL_NAME);
220         kernel.boot();
221
222         bootDeployerSystem(kernel);
223
224         return kernel;
225     }
226
227     /**
228      * Boot the in-Maven deployment system.
229      * This contains just Repository and ConfigurationStore GBeans that map to
230      * the local maven installation.
231      */

232     private void bootDeployerSystem(Kernel kernel) throws Exception JavaDoc {
233         ClassLoader JavaDoc cl = PackageBuilder.class.getClassLoader();
234         GBeanData repoGBean = new GBeanData(REPOSITORY_NAME, MavenRepository.GBEAN_INFO);
235         repoGBean.setAttribute("root", repository);
236
237         GBeanData storeGBean = new GBeanData(CONFIGSTORE_NAME, MavenConfigStore.GBEAN_INFO);
238         storeGBean.setReferencePattern("Repository", REPOSITORY_NAME);
239
240         kernel.loadGBean(repoGBean, cl);
241         kernel.startGBean(REPOSITORY_NAME);
242         kernel.loadGBean(storeGBean, cl);
243         kernel.startGBean(CONFIGSTORE_NAME);
244     }
245
246     /**
247      * Locate a Deployer GBean matching the deployerName pattern.
248      *
249      * @param kernel the kernel to search.
250      * @return the ObjectName of the Deployer GBean
251      * @throws IllegalStateException if there is not exactly one GBean matching the deployerName pattern
252      */

253     private ObjectName JavaDoc locateDeployer(Kernel kernel) {
254         Iterator JavaDoc i = kernel.listGBeans(deployerName).iterator();
255         if (!i.hasNext()) {
256             throw new IllegalStateException JavaDoc("No deployer found matching deployerName: " + deployerName);
257         }
258         ObjectName JavaDoc deployer = (ObjectName JavaDoc) i.next();
259         if (i.hasNext()) {
260             throw new IllegalStateException JavaDoc("Multiple deployers found matching deployerName: " + deployerName);
261         }
262         return deployer;
263     }
264
265     private List JavaDoc invokeDeployer(Kernel kernel, ObjectName JavaDoc deployer) throws Exception JavaDoc {
266         Object JavaDoc[] args = {planFile, moduleFile, packageFile, Boolean.FALSE, mainClass, classPath, endorsedDirs};
267         return (List JavaDoc) kernel.invoke(deployer, "deploy", args, ARG_TYPES);
268     }
269 }
270
Popular Tags