KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > service > ServiceConfigBuilder


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
18 package org.apache.geronimo.deployment.service;
19
20 import java.beans.PropertyEditorManager JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31
32 import org.apache.geronimo.common.DeploymentException;
33 import org.apache.geronimo.deployment.ConfigurationBuilder;
34 import org.apache.geronimo.deployment.DeploymentContext;
35 import org.apache.geronimo.deployment.ModuleIDBuilder;
36 import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
37 import org.apache.geronimo.deployment.NamespaceDrivenBuilderCollection;
38 import org.apache.geronimo.deployment.util.DeploymentUtil;
39 import org.apache.geronimo.deployment.xbeans.ArtifactType;
40 import org.apache.geronimo.deployment.xbeans.EnvironmentType;
41 import org.apache.geronimo.deployment.xbeans.ModuleDocument;
42 import org.apache.geronimo.deployment.xbeans.ModuleType;
43 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
44 import org.apache.geronimo.gbean.AbstractName;
45 import org.apache.geronimo.gbean.GBeanInfo;
46 import org.apache.geronimo.gbean.GBeanInfoBuilder;
47 import org.apache.geronimo.kernel.Kernel;
48 import org.apache.geronimo.kernel.Naming;
49 import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
50 import org.apache.geronimo.kernel.config.ConfigurationManager;
51 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
52 import org.apache.geronimo.kernel.config.ConfigurationStore;
53 import org.apache.geronimo.kernel.config.ConfigurationUtil;
54 import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
55 import org.apache.geronimo.kernel.repository.Artifact;
56 import org.apache.geronimo.kernel.repository.ArtifactResolver;
57 import org.apache.geronimo.kernel.repository.Environment;
58 import org.apache.geronimo.kernel.repository.Repository;
59 import org.apache.xmlbeans.XmlCursor;
60 import org.apache.xmlbeans.XmlException;
61 import org.apache.xmlbeans.XmlObject;
62
63 /**
64  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
65  */

66 public class ServiceConfigBuilder implements ConfigurationBuilder {
67     private final Environment defaultEnvironment;
68     private final Collection JavaDoc repositories;
69
70     private static final QName JavaDoc MODULE_QNAME = ModuleDocument.type.getDocumentElementName();
71     public static final String JavaDoc SERVICE_MODULE = "ServiceModule";
72     private final Naming naming;
73     private final ConfigurationManager configurationManager;
74     private final NamespaceDrivenBuilderCollection serviceBuilders;
75
76     public ServiceConfigBuilder(Environment defaultEnvironment, Collection JavaDoc repositories, Naming naming) {
77         this(defaultEnvironment, repositories, Collections.EMPTY_LIST, naming, null);
78     }
79
80     public ServiceConfigBuilder(Environment defaultEnvironment, Collection JavaDoc repositories, Collection JavaDoc serviceBuilders, Kernel kernel) {
81         this(defaultEnvironment, repositories, serviceBuilders, kernel.getNaming(), ConfigurationUtil.getConfigurationManager(kernel));
82     }
83
84     public ServiceConfigBuilder(Environment defaultEnvironment, Collection JavaDoc repositories, Collection JavaDoc serviceBuilders, Naming naming) {
85         this(defaultEnvironment, repositories, serviceBuilders, naming, null);
86     }
87
88     private ServiceConfigBuilder(Environment defaultEnvironment, Collection JavaDoc repositories, Collection JavaDoc serviceBuilders, Naming naming, ConfigurationManager configurationManager) {
89         this.naming = naming;
90         this.configurationManager = configurationManager;
91
92         EnvironmentBuilder environmentBuilder = new EnvironmentBuilder();
93         this.defaultEnvironment = defaultEnvironment;
94
95         this.repositories = repositories;
96         this.serviceBuilders = new NamespaceDrivenBuilderCollection(serviceBuilders, GBeanBuilder.SERVICE_QNAME);
97     }
98
99     public Object JavaDoc getDeploymentPlan(File JavaDoc planFile, JarFile JavaDoc jarFile, ModuleIDBuilder idBuilder) throws DeploymentException {
100         if (planFile == null && jarFile == null) {
101             return null;
102         }
103
104         try {
105             XmlObject xmlObject;
106             if (planFile != null) {
107                 xmlObject = XmlBeansUtil.parse(planFile.toURL(), getClass().getClassLoader());
108             } else {
109                 URL JavaDoc path = DeploymentUtil.createJarURL(jarFile, "META-INF/geronimo-service.xml");
110                 try {
111                     xmlObject = XmlBeansUtil.parse(path, getClass().getClassLoader());
112                 } catch (FileNotFoundException JavaDoc e) {
113                     // It has a JAR but no plan, and nothing at META-INF/geronimo-service.xml,
114
// therefore it's not a service deployment
115
return null;
116                 }
117             }
118             if(xmlObject == null) {
119                 return null;
120             }
121
122             XmlCursor cursor = xmlObject.newCursor();
123             try {
124                 cursor.toFirstChild();
125                 if (!MODULE_QNAME.equals(cursor.getName())) {
126                     return null;
127                 }
128             } finally {
129                 cursor.dispose();
130             }
131             ModuleDocument moduleDoc;
132             if (xmlObject instanceof ModuleDocument) {
133                 moduleDoc = (ModuleDocument) xmlObject;
134             } else {
135                 moduleDoc = (ModuleDocument) xmlObject.changeType(ModuleDocument.type);
136             }
137             XmlBeansUtil.validateDD(moduleDoc);
138             // If there's no artifact ID and we won't be able to figure one out later, use the plan file name. Bit of a hack.
139
if(jarFile == null && (moduleDoc.getModule().getEnvironment() == null ||
140                         moduleDoc.getModule().getEnvironment().getModuleId() == null ||
141                         moduleDoc.getModule().getEnvironment().getModuleId().getArtifactId() == null)) {
142                 if(moduleDoc.getModule().getEnvironment() == null) {
143                     moduleDoc.getModule().addNewEnvironment();
144                 }
145                 if(moduleDoc.getModule().getEnvironment().getModuleId() == null) {
146                     moduleDoc.getModule().getEnvironment().addNewModuleId();
147                 }
148                 String JavaDoc name = planFile.getName();
149                 int pos = name.lastIndexOf('.');
150                 if(pos > -1) {
151                     name = name.substring(0, pos);
152                 }
153                 moduleDoc.getModule().getEnvironment().getModuleId().setArtifactId(name);
154             }
155             return moduleDoc.getModule();
156         } catch (XmlException e) {
157             throw new DeploymentException("Could not parse xml in plan", e);
158         } catch (IOException JavaDoc e) {
159             throw new DeploymentException("no plan at " + planFile, e);
160         }
161     }
162
163     public Artifact getConfigurationID(Object JavaDoc plan, JarFile JavaDoc module, ModuleIDBuilder idBuilder) throws IOException JavaDoc, DeploymentException {
164         ModuleType configType = (ModuleType) plan;
165         EnvironmentType environmentType = configType.getEnvironment();
166         Environment environment = EnvironmentBuilder.buildEnvironment(environmentType, defaultEnvironment);
167         idBuilder.resolve(environment, module == null ? "" : new File JavaDoc(module.getName()).getName(), "car");
168         if(!environment.getConfigId().isResolved()) {
169             throw new IllegalStateException JavaDoc("Service Module ID is not fully populated ("+environment.getConfigId()+")");
170         }
171         return environment.getConfigId();
172     }
173
174     public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object JavaDoc plan, JarFile JavaDoc jar, Collection JavaDoc configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws IOException JavaDoc, DeploymentException {
175         ModuleType configType = (ModuleType) plan;
176
177         return buildConfiguration(inPlaceDeployment, configId, configType, jar, configurationStores, artifactResolver, targetConfigurationStore);
178     }
179
180     public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, ModuleType moduleType, JarFile JavaDoc jar, Collection JavaDoc configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws DeploymentException, IOException JavaDoc {
181         ArtifactType type = moduleType.getEnvironment().isSetModuleId() ? moduleType.getEnvironment().getModuleId() : moduleType.getEnvironment().addNewModuleId();
182         type.setArtifactId(configId.getArtifactId());
183         type.setGroupId(configId.getGroupId());
184         type.setType(configId.getType());
185         type.setVersion(configId.getVersion().toString());
186         Environment environment = EnvironmentBuilder.buildEnvironment(moduleType.getEnvironment(), defaultEnvironment);
187         if(!environment.getConfigId().isResolved()) {
188             throw new IllegalStateException JavaDoc("Module ID should be fully resolved by now (not "+environment.getConfigId()+")");
189         }
190         File JavaDoc outfile;
191         try {
192             outfile = targetConfigurationStore.createNewConfigurationDir(configId);
193         } catch (ConfigurationAlreadyExistsException e) {
194             throw new DeploymentException(e);
195         }
196
197         DeploymentContext context = null;
198         try {
199             ConfigurationManager configurationManager = this.configurationManager;
200             if (configurationManager == null) {
201                 configurationManager = new SimpleConfigurationManager(configurationStores, artifactResolver, repositories);
202             }
203
204             AbstractName moduleName = naming.createRootName(configId, configId.toString(), SERVICE_MODULE);
205             context = new DeploymentContext(outfile,
206                     inPlaceDeployment && null != jar ? DeploymentUtil.toFile(jar) : null,
207                     environment,
208                     moduleName,
209                     ConfigurationModuleType.SERVICE,
210                     naming,
211                     configurationManager,
212                     repositories);
213             if(jar != null) {
214                 File JavaDoc file = new File JavaDoc(jar.getName());
215                 context.addIncludeAsPackedJar(URI.create(file.getName()), jar);
216             }
217
218             serviceBuilders.build(moduleType, context, context);
219             return context;
220         } catch (DeploymentException de) {
221             cleanupAfterFailedBuild(context, outfile);
222             throw de;
223         } catch (IOException JavaDoc ie) {
224             cleanupAfterFailedBuild(context, outfile);
225             throw ie;
226         } catch (RuntimeException JavaDoc re) {
227             cleanupAfterFailedBuild(context, outfile);
228             throw re;
229         } catch (Error JavaDoc e) {
230             cleanupAfterFailedBuild(context, outfile);
231             throw e;
232         }
233     }
234
235     private void cleanupAfterFailedBuild(DeploymentContext context, File JavaDoc directory) {
236         try {
237             if (context !=null) {
238                 context.close();
239             }
240         } catch (DeploymentException de) {
241             // ignore error on cleanup
242
} catch (IOException JavaDoc ioe) {
243             // ignore error on cleanu
244
}
245         if (directory != null) {
246             DeploymentUtil.recursiveDelete(directory);
247         }
248     }
249
250     public static final GBeanInfo GBEAN_INFO;
251
252     static {
253         PropertyEditorManager.registerEditor(Environment.class, EnvironmentBuilder.class);
254
255         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(ServiceConfigBuilder.class, CONFIG_BUILDER);
256
257         infoBuilder.addInterface(ConfigurationBuilder.class);
258
259         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true);
260         infoBuilder.addReference("Repository", Repository.class, "Repository");
261         infoBuilder.addReference("ServiceBuilders", NamespaceDrivenBuilder.class, "ModuleBuilder");
262         infoBuilder.addAttribute("kernel", Kernel.class, false, false);
263
264         infoBuilder.setConstructor(new String JavaDoc[]{"defaultEnvironment", "Repository", "ServiceBuilders", "kernel"});
265
266         GBEAN_INFO = infoBuilder.getBeanInfo();
267     }
268
269     public static GBeanInfo getGBeanInfo() {
270         return GBEAN_INFO;
271     }
272
273 }
274
Popular Tags