KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > geronimo > ServiceMixConfigBuilder


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.geronimo;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.jar.JarFile JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29
30 import javax.management.MalformedObjectNameException JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.geronimo.common.DeploymentException;
35 import org.apache.geronimo.deployment.ConfigurationBuilder;
36 import org.apache.geronimo.deployment.DeploymentContext;
37 import org.apache.geronimo.deployment.ModuleIDBuilder;
38 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
39 import org.apache.geronimo.deployment.util.DeploymentUtil;
40 import org.apache.geronimo.gbean.AbstractName;
41 import org.apache.geronimo.gbean.AbstractNameQuery;
42 import org.apache.geronimo.gbean.GBeanData;
43 import org.apache.geronimo.gbean.GBeanInfo;
44 import org.apache.geronimo.gbean.GBeanInfoBuilder;
45 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
46 import org.apache.geronimo.kernel.Kernel;
47 import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
48 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
49 import org.apache.geronimo.kernel.config.ConfigurationStore;
50 import org.apache.geronimo.kernel.config.ConfigurationUtil;
51 import org.apache.geronimo.kernel.repository.Artifact;
52 import org.apache.geronimo.kernel.repository.ArtifactResolver;
53 import org.apache.geronimo.kernel.repository.Environment;
54 import org.apache.geronimo.kernel.repository.ImportType;
55 import org.apache.geronimo.kernel.repository.Repository;
56 import org.apache.geronimo.kernel.repository.Version;
57 import org.apache.servicemix.jbi.deployment.Descriptor;
58 import org.apache.servicemix.jbi.deployment.DescriptorFactory;
59 import org.apache.servicemix.jbi.deployment.ServiceUnit;
60 import org.apache.servicemix.jbi.deployment.SharedLibraryList;
61
62 public class ServiceMixConfigBuilder implements ConfigurationBuilder {
63
64     private static final Log log = LogFactory.getLog(ServiceMixConfigBuilder.class);
65
66     private final Environment defaultEnvironment;
67
68     private final Collection JavaDoc repositories;
69
70     private final Kernel kernel;
71
72     public static final GBeanInfo GBEAN_INFO;
73
74     static {
75         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(ServiceMixConfigBuilder.class, NameFactory.CONFIG_BUILDER);
76         infoFactory.addInterface(ConfigurationBuilder.class);
77         infoFactory.addAttribute("defaultEnvironment", Environment.class, true, true);
78         infoFactory.addAttribute("kernel", Kernel.class, false);
79         infoFactory.addReference("Repositories", Repository.class, "Repository");
80         infoFactory.setConstructor(new String JavaDoc[] { "defaultEnvironment", "Repositories", "kernel" });
81         GBEAN_INFO = infoFactory.getBeanInfo();
82     }
83
84     public static GBeanInfo getGBeanInfo() {
85         return GBEAN_INFO;
86     }
87
88     public ServiceMixConfigBuilder(Environment defaultEnvironment, Collection JavaDoc repositories, Kernel kernel) {
89         this.defaultEnvironment = defaultEnvironment;
90         this.repositories = repositories;
91         this.kernel = kernel;
92     }
93
94     /**
95      * Builds a deployment plan specific to this builder from a planFile and/or
96      * module if this builder can process it.
97      *
98      * @param planFile
99      * the deployment plan to examine; can be null
100      * @param module
101      * the URL of the module to examine; can be null
102      * @return the deployment plan, or null if this builder can not handle the
103      * module
104      * @throws org.apache.geronimo.common.DeploymentException
105      * if there was a problem with the configuration
106      */

107     public Object JavaDoc getDeploymentPlan(File JavaDoc planFile, JarFile JavaDoc module, ModuleIDBuilder idBuilder)
108                     throws DeploymentException {
109         log.debug("Checking for ServiceMix deployment.");
110         System.err.println("Checking for ServiceMix deployment.");
111         if (module == null) {
112             return null;
113         }
114
115         // Check that the jbi descriptor is present
116
try {
117             URL JavaDoc url = DeploymentUtil.createJarURL(module, "META-INF/jbi.xml");
118             Descriptor descriptor = DescriptorFactory.buildDescriptor(url);
119             if (descriptor == null) {
120                 return null;
121             }
122             DescriptorFactory.checkDescriptor(descriptor);
123             return descriptor;
124         } catch (Exception JavaDoc e) {
125             log.debug("Not a ServiceMix deployment: no jbi.xml found.", e);
126             // no jbi.xml, not for us
127
return null;
128         }
129     }
130
131     /**
132      * Checks what configuration URL will be used for the provided module.
133      *
134      * @param plan
135      * the deployment plan
136      * @param module
137      * the module to build
138      * @return the ID that will be used for the Configuration
139      * @throws IOException
140      * if there was a problem reading or writing the files
141      * @throws org.apache.geronimo.common.DeploymentException
142      * if there was a problem with the configuration
143      */

144     public Artifact getConfigurationID(Object JavaDoc plan, JarFile JavaDoc module, ModuleIDBuilder idBuilder) throws IOException JavaDoc,
145                     DeploymentException {
146         Descriptor descriptor = (Descriptor) plan;
147         if (descriptor.getComponent() != null) {
148             return new Artifact("servicemix-components", descriptor.getComponent().getIdentification().getName(),
149                             (Version) null, "car");
150         } else if (descriptor.getServiceAssembly() != null) {
151             return new Artifact("servicemix-assemblies", descriptor.getServiceAssembly().getIdentification().getName(),
152                             (Version) null, "car");
153         } else if (descriptor.getSharedLibrary() != null) {
154             return new Artifact("servicemix-libraries", descriptor.getSharedLibrary().getIdentification().getName(),
155                             descriptor.getSharedLibrary().getVersion(), "car");
156         } else {
157             throw new DeploymentException("Unable to construct configuration ID " + module.getName()
158                             + ": unrecognized jbi package. Should be a component, assembly or library.");
159         }
160     }
161
162     /**
163      * Build a configuration from a local file
164      *
165      * @param inPlaceDeployment
166      * @param configId
167      * @param plan
168      * @param earFile
169      * @param configurationStores
170      * @param artifactResolver
171      * @param targetConfigurationStore
172      * @return the DeploymentContext information
173      * @throws IOException
174      * if there was a problem reading or writing the files
175      * @throws org.apache.geronimo.common.DeploymentException
176      * if there was a problem with the configuration
177      */

178     public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object JavaDoc plan,
179                     JarFile JavaDoc jarFile, Collection JavaDoc configurationStores, ArtifactResolver artifactResolver,
180                     ConfigurationStore targetConfigurationStore) throws IOException JavaDoc, DeploymentException {
181         if (plan == null) {
182             log.warn("Expected a Descriptor but received null");
183             return null;
184         }
185         if (plan instanceof Descriptor == false) {
186             log.warn("Expected a Descriptor but received a " + plan.getClass().getName());
187             return null;
188         }
189         File JavaDoc configurationDir;
190         try {
191             configurationDir = targetConfigurationStore.createNewConfigurationDir(configId);
192         } catch (ConfigurationAlreadyExistsException e) {
193             throw new DeploymentException(e);
194         }
195
196         Environment environment = new Environment();
197         environment.setConfigId(configId);
198         EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
199
200         DeploymentContext context = null;
201         try {
202             Descriptor descriptor = (Descriptor) plan;
203             context = new DeploymentContext(configurationDir,
204                             inPlaceDeployment ? DeploymentUtil.toFile(jarFile) : null, environment,
205                             ConfigurationModuleType.SERVICE, kernel.getNaming(), ConfigurationUtil
206                                             .getConfigurationManager(kernel), repositories);
207             if (descriptor.getComponent() != null) {
208                 buildComponent(descriptor, context, jarFile);
209             } else if (descriptor.getServiceAssembly() != null) {
210                 buildServiceAssembly(descriptor, context, jarFile);
211             } else if (descriptor.getSharedLibrary() != null) {
212                 buildSharedLibrary(descriptor, context, jarFile);
213             } else {
214                 throw new IllegalStateException JavaDoc("Invalid jbi descriptor");
215             }
216         } catch (Exception JavaDoc e) {
217             if (context != null) {
218                 context.close();
219             }
220             DeploymentUtil.recursiveDelete(configurationDir);
221             throw new DeploymentException("Unable to deploy", e);
222         }
223
224         return context;
225     }
226
227     protected void buildComponent(Descriptor descriptor, DeploymentContext context, JarFile JavaDoc module) throws Exception JavaDoc {
228         Environment environment = context.getConfiguration().getEnvironment();
229         // Unzip the component
230
File JavaDoc targetDir = new File JavaDoc(context.getBaseDir(), "install");
231         targetDir.mkdirs();
232         unzip(context, module, new URI JavaDoc("install/"));
233         // Create workspace dir
234
File JavaDoc workDir = new File JavaDoc(context.getBaseDir(), "workspace");
235         workDir.mkdirs();
236         // Create the bootstrap and perform installation
237
// TODO: Create the bootstrap and perform installation
238
// Add classpath entries
239
if ("self-first".equals(descriptor.getComponent().getComponentClassLoaderDelegation())) {
240             context.getConfiguration().getEnvironment().setInverseClassLoading(true);
241         }
242         SharedLibraryList[] slList = descriptor.getComponent().getSharedLibraries();
243         if (slList != null) {
244             for (int i = 0; i < slList.length; i++) {
245                 Artifact sl = new Artifact("servicemix-libraries", slList[i].getName(), slList[i].getVersion(), "car");
246                 environment.addDependency(sl, ImportType.CLASSES);
247             }
248         }
249         if (descriptor.getComponent().getComponentClassPath() != null) {
250             String JavaDoc[] pathElements = descriptor.getComponent().getComponentClassPath().getPathElements();
251             if (pathElements != null) {
252                 for (int i = 0; i < pathElements.length; i++) {
253                     // We can not add includes directly, so move the file and
254
// include it
255
File JavaDoc include = new File JavaDoc(targetDir, pathElements[i]);
256                     File JavaDoc temp = new File JavaDoc(workDir, pathElements[i]);
257                     if (!include.isFile()) {
258                         throw new Exception JavaDoc("Classpath element '" + pathElements[i] + "' not found");
259                     }
260                     temp.getParentFile().mkdirs();
261                     include.renameTo(temp);
262                     context.addInclude(new URI JavaDoc("install/").resolve(pathElements[i]), temp);
263                     temp.delete();
264                 }
265             }
266         }
267         // Create the JBI deployment managed object
268
Properties JavaDoc props = new Properties JavaDoc();
269         props.put("jbiType", "JBIComponent");
270         props.put("name", descriptor.getComponent().getIdentification().getName());
271         AbstractName name = new AbstractName(environment.getConfigId(), props);
272         GBeanData gbeanData = new GBeanData(name, Component.GBEAN_INFO);
273         gbeanData.setAttribute("name", descriptor.getComponent().getIdentification().getName());
274         gbeanData.setAttribute("description", descriptor.getComponent().getIdentification().getDescription());
275         gbeanData.setAttribute("type", descriptor.getComponent().getType());
276         gbeanData.setAttribute("className", descriptor.getComponent().getComponentClassName());
277         gbeanData.setReferencePattern("container", getContainerObjectName());
278         context.addGBean(gbeanData);
279     }
280
281     protected void buildServiceAssembly(Descriptor descriptor, DeploymentContext context, JarFile JavaDoc module)
282                     throws Exception JavaDoc {
283         Environment environment = context.getConfiguration().getEnvironment();
284         // Unzip the component
285
File JavaDoc targetDir = new File JavaDoc(context.getBaseDir(), "install");
286         targetDir.mkdirs();
287         unzip(context, module, new URI JavaDoc("install/"));
288         // Unzip SUs
289
ServiceUnit[] sus = descriptor.getServiceAssembly().getServiceUnits();
290         for (int i = 0; i < sus.length; i++) {
291             String JavaDoc name = sus[i].getIdentification().getName();
292             String JavaDoc zip = sus[i].getTarget().getArtifactsZip();
293             String JavaDoc comp = sus[i].getTarget().getComponentName();
294             unzip(context, new JarFile JavaDoc(new File JavaDoc(targetDir, zip)), new URI JavaDoc("sus/" + comp + "/" + name + "/"));
295             // Deploy the SU on the component
296
// TODO: deploy
297
// Add component config as a dependency
298
Artifact sl = new Artifact("servicemix-components", comp, (Version) null, "car");
299             environment.addDependency(sl, ImportType.ALL);
300         }
301         // Create the JBI deployment managed object
302
Properties JavaDoc props = new Properties JavaDoc();
303         props.put("jbiType", "JBIServiceAssembly");
304         props.put("name", descriptor.getServiceAssembly().getIdentification().getName());
305         AbstractName name = new AbstractName(environment.getConfigId(), props);
306         GBeanData gbeanData = new GBeanData(name, ServiceAssembly.GBEAN_INFO);
307         gbeanData.setAttribute("name", descriptor.getServiceAssembly().getIdentification().getName());
308         gbeanData.setReferencePattern("container", getContainerObjectName());
309         for (int i = 0; i < sus.length; i++) {
310             String JavaDoc comp = sus[i].getTarget().getComponentName();
311             gbeanData.addDependency(getComponentName(comp));
312         }
313         context.addGBean(gbeanData);
314     }
315
316     protected void buildSharedLibrary(Descriptor descriptor, DeploymentContext context, JarFile JavaDoc module)
317                     throws Exception JavaDoc {
318         Environment environment = context.getConfiguration().getEnvironment();
319         // Unzip the SL
320
File JavaDoc targetDir = new File JavaDoc(context.getBaseDir(), "install");
321         targetDir.mkdirs();
322         unzip(context, module, new URI JavaDoc("install/"));
323         // Create workspace dir
324
File JavaDoc workDir = new File JavaDoc(context.getBaseDir(), "workspace");
325         workDir.mkdirs();
326         // Add classpath entries
327
if ("self-first".equals(descriptor.getSharedLibrary().getClassLoaderDelegation())) {
328             context.getConfiguration().getEnvironment().setInverseClassLoading(true);
329         }
330         if (descriptor.getSharedLibrary().getSharedLibraryClassPath() != null) {
331             String JavaDoc[] pathElements = descriptor.getSharedLibrary().getSharedLibraryClassPath().getPathElements();
332             if (pathElements != null) {
333                 for (int i = 0; i < pathElements.length; i++) {
334                     log.debug("Processing pathElements[" + i + "]: " + pathElements[i]);
335                     // We can not add includes directly, so move the file and
336
// include it
337
File JavaDoc include = new File JavaDoc(targetDir, pathElements[i]);
338                     File JavaDoc temp = new File JavaDoc(workDir, pathElements[i]);
339                     if (!include.isFile()) {
340                         throw new Exception JavaDoc("Classpath element '" + pathElements[i] + "' not found");
341                     }
342                     temp.getParentFile().mkdirs();
343                     include.renameTo(temp);
344                     context.addInclude(new URI JavaDoc("install/").resolve(pathElements[i]), temp);
345                     temp.delete();
346                 }
347             } else {
348                 log.debug("SharedLibrary().getSharedLibraryClassPath().getPathElements() is null");
349             }
350         } else {
351             log.debug("SharedLibrary().getSharedLibraryClassPath() is null");
352         }
353         // Create the JBI deployment managed object
354
Properties JavaDoc props = new Properties JavaDoc();
355         props.put("jbiType", "JBISharedLibrary");
356         props.put("name", descriptor.getSharedLibrary().getIdentification().getName());
357         AbstractName name = new AbstractName(environment.getConfigId(), props);
358         GBeanData gbeanData = new GBeanData(name, SharedLibrary.GBEAN_INFO);
359         gbeanData.setAttribute("name", descriptor.getSharedLibrary().getIdentification().getName());
360         gbeanData.setAttribute("description", descriptor.getSharedLibrary().getIdentification().getDescription());
361         gbeanData.setReferencePattern("container", getContainerObjectName());
362         context.addGBean(gbeanData);
363     }
364
365     protected void unzip(DeploymentContext context, JarFile JavaDoc module, URI JavaDoc targetUri) throws IOException JavaDoc {
366         Enumeration JavaDoc entries = module.entries();
367         while (entries.hasMoreElements()) {
368             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
369             URI JavaDoc target = targetUri.resolve(entry.getName());
370             context.addFile(target, module, entry);
371         }
372     }
373
374     protected AbstractName getContainerObjectName() {
375         AbstractNameQuery query = new AbstractNameQuery(Container.class.getName());
376         Set JavaDoc names = kernel.listGBeans(query);
377         return (AbstractName) names.iterator().next();
378     }
379
380     protected AbstractNameQuery getComponentName(String JavaDoc name) throws MalformedObjectNameException JavaDoc {
381         URI JavaDoc uri = URI.create("servicemix-components/" + name + "//car?jbiType=JBIComponent");
382         return new AbstractNameQuery(uri);
383     }
384
385 }
386
Popular Tags