KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > DeploymentContext


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;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedHashMap JavaDoc;
34 import java.util.LinkedHashSet JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.jar.Attributes JavaDoc;
41 import java.util.jar.JarFile JavaDoc;
42 import java.util.jar.Manifest JavaDoc;
43 import java.util.zip.ZipEntry JavaDoc;
44 import java.util.zip.ZipFile JavaDoc;
45
46 import org.apache.geronimo.common.DeploymentException;
47 import org.apache.geronimo.deployment.util.DeploymentUtil;
48 import org.apache.geronimo.gbean.AbstractName;
49 import org.apache.geronimo.gbean.AbstractNameQuery;
50 import org.apache.geronimo.gbean.GBeanData;
51 import org.apache.geronimo.gbean.GBeanInfo;
52 import org.apache.geronimo.gbean.ReferencePatterns;
53 import org.apache.geronimo.gbean.GReferenceInfo;
54 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
55 import org.apache.geronimo.kernel.GBeanNotFoundException;
56 import org.apache.geronimo.kernel.Naming;
57 import org.apache.geronimo.kernel.config.Configuration;
58 import org.apache.geronimo.kernel.config.ConfigurationData;
59 import org.apache.geronimo.kernel.config.ConfigurationManager;
60 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
61 import org.apache.geronimo.kernel.config.NoSuchConfigException;
62 import org.apache.geronimo.kernel.repository.Artifact;
63 import org.apache.geronimo.kernel.repository.Environment;
64
65 /**
66  * @version $Rev:385232 $ $Date: 2006-12-01 19:57:29 -0500 (Fri, 01 Dec 2006) $
67  */

68 public class DeploymentContext {
69     private final File JavaDoc baseDir;
70     private final File JavaDoc inPlaceConfigurationDir;
71     private final ResourceContext resourceContext;
72     private final byte[] buffer = new byte[4096];
73     private final Map JavaDoc childConfigurationDatas = new LinkedHashMap JavaDoc();
74     private final ConfigurationManager configurationManager;
75     private final Configuration configuration;
76     private final Naming naming;
77     private final List JavaDoc additionalDeployment = new ArrayList JavaDoc();
78     protected final AbstractName moduleName;
79
80     public DeploymentContext(File JavaDoc baseDir, File JavaDoc inPlaceConfigurationDir, Environment environment, AbstractName moduleName, ConfigurationModuleType moduleType, Naming naming, ConfigurationManager configurationManager, Collection JavaDoc repositories) throws DeploymentException {
81         this(baseDir, inPlaceConfigurationDir, environment, moduleName, moduleType, naming, createConfigurationManager(configurationManager, repositories));
82     }
83
84     public DeploymentContext(File JavaDoc baseDir, File JavaDoc inPlaceConfigurationDir, Environment environment, AbstractName moduleName, ConfigurationModuleType moduleType, Naming naming, ConfigurationManager configurationManager) throws DeploymentException {
85         if (baseDir == null) throw new NullPointerException JavaDoc("baseDir is null");
86         if (environment == null) throw new NullPointerException JavaDoc("environment is null");
87         if (moduleType == null) throw new NullPointerException JavaDoc("type is null");
88         if (configurationManager == null) throw new NullPointerException JavaDoc("configurationManager is null");
89
90         if (!baseDir.exists()) {
91             baseDir.mkdirs();
92         }
93         this.baseDir = baseDir;
94
95         this.inPlaceConfigurationDir = inPlaceConfigurationDir;
96
97         this.moduleName = moduleName;
98
99         this.naming = naming;
100
101         this.configuration = createTempConfiguration(environment, moduleType, baseDir, inPlaceConfigurationDir, configurationManager, naming);
102
103         this.configurationManager = configurationManager;
104         
105         if (null == inPlaceConfigurationDir) {
106             resourceContext = new CopyResourceContext(configuration, baseDir);
107         } else {
108             resourceContext = new InPlaceResourceContext(configuration, inPlaceConfigurationDir);
109         }
110     }
111
112     private static ConfigurationManager createConfigurationManager(ConfigurationManager configurationManager, Collection JavaDoc repositories) {
113         return new DeploymentConfigurationManager(configurationManager, repositories);
114     }
115
116     private static Configuration createTempConfiguration(Environment environment, ConfigurationModuleType moduleType, File JavaDoc baseDir, File JavaDoc inPlaceConfigurationDir, ConfigurationManager configurationManager, Naming naming) throws DeploymentException {
117         try {
118             configurationManager.loadConfiguration(new ConfigurationData(moduleType, null, null, null, environment, baseDir, inPlaceConfigurationDir, naming));
119             return configurationManager.getConfiguration(environment.getConfigId());
120         } catch (Exception JavaDoc e) {
121             throw new DeploymentException("Unable to create configuration for deployment", e);
122         }
123     }
124
125     public ConfigurationManager getConfigurationManager() {
126         return configurationManager;
127     }
128
129     public Artifact getConfigID() {
130         return configuration.getId();
131     }
132
133     public File JavaDoc getBaseDir() {
134         return baseDir;
135     }
136
137     public File JavaDoc getInPlaceConfigurationDir() {
138         return inPlaceConfigurationDir;
139     }
140
141     public Naming getNaming() {
142         return naming;
143     }
144
145     public GBeanData addGBean(String JavaDoc name, GBeanInfo gbeanInfo) throws GBeanAlreadyExistsException {
146         if (name == null) throw new NullPointerException JavaDoc("name is null");
147         if (gbeanInfo == null) throw new NullPointerException JavaDoc("gbean is null");
148         GBeanData gbean = new GBeanData(gbeanInfo);
149         configuration.addGBean(name, gbean);
150         return gbean;
151     }
152
153     public void addGBean(GBeanData gbean) throws GBeanAlreadyExistsException {
154         if (gbean == null) throw new NullPointerException JavaDoc("gbean is null");
155         if (gbean.getAbstractName() == null) throw new NullPointerException JavaDoc("gbean.getAbstractName() is null");
156         configuration.addGBean(gbean);
157     }
158
159     public void removeGBean(AbstractName name) throws GBeanNotFoundException {
160         if (name == null) throw new NullPointerException JavaDoc("name is null");
161         configuration.removeGBean(name);
162     }
163
164     public Set JavaDoc getGBeanNames() {
165         return new HashSet JavaDoc(configuration.getGBeans().keySet());
166     }
167
168     /**
169      * @deprecated use findGBeans(pattern)
170      */

171     public Set JavaDoc listGBeans(AbstractNameQuery pattern) {
172         return findGBeans(pattern);
173     }
174
175     public AbstractName findGBean(AbstractNameQuery pattern) throws GBeanNotFoundException {
176         return configuration.findGBean(pattern);
177     }
178
179     public AbstractName findGBean(Set JavaDoc patterns) throws GBeanNotFoundException {
180         return configuration.findGBean(patterns);
181     }
182
183     public LinkedHashSet JavaDoc findGBeans(AbstractNameQuery pattern) {
184         return configuration.findGBeans(pattern);
185     }
186
187     public LinkedHashSet JavaDoc findGBeans(Set JavaDoc patterns) {
188         return configuration.findGBeans(patterns);
189     }
190
191     public GBeanData getGBeanInstance(AbstractName name) throws GBeanNotFoundException {
192         Map JavaDoc gbeans = configuration.getGBeans();
193         GBeanData gbeanData = (GBeanData) gbeans.get(name);
194         if (gbeanData == null) {
195             throw new GBeanNotFoundException(name);
196         }
197         return gbeanData;
198     }
199
200     /**
201      * Add a packed jar file into the deployment context and place it into the
202      * path specified in the target path. The newly added packed jar is added
203      * to the classpath of the configuration.
204      *
205      * @param targetPath where the packed jar file should be placed
206      * @param jarFile the jar file to copy
207      * @throws IOException if there's a problem copying the jar file
208      */

209     public void addIncludeAsPackedJar(URI JavaDoc targetPath, JarFile JavaDoc jarFile) throws IOException JavaDoc {
210         resourceContext.addIncludeAsPackedJar(targetPath, jarFile);
211     }
212
213     /**
214      * Add a ZIP file entry into the deployment context and place it into the
215      * path specified in the target path. The newly added entry is added
216      * to the classpath of the configuration.
217      *
218      * @param targetPath where the ZIP file entry should be placed
219      * @param zipFile the ZIP file
220      * @param zipEntry the ZIP file entry
221      * @throws IOException if there's a problem copying the ZIP entry
222      */

223     public void addInclude(URI JavaDoc targetPath, ZipFile JavaDoc zipFile, ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
224         resourceContext.addInclude(targetPath, zipFile, zipEntry);
225     }
226
227     /**
228      * Add a file into the deployment context and place it into the
229      * path specified in the target path. The newly added file is added
230      * to the classpath of the configuration.
231      *
232      * @param targetPath where the file should be placed
233      * @param source the URL of file to be copied
234      * @throws IOException if there's a problem copying the ZIP entry
235      */

236     public void addInclude(URI JavaDoc targetPath, URL JavaDoc source) throws IOException JavaDoc {
237         resourceContext.addInclude(targetPath, source);
238     }
239
240     /**
241      * Add a file into the deployment context and place it into the
242      * path specified in the target path. The newly added file is added
243      * to the classpath of the configuration.
244      *
245      * @param targetPath where the file should be placed
246      * @param source the file to be copied
247      * @throws IOException if there's a problem copying the ZIP entry
248      */

249     public void addInclude(URI JavaDoc targetPath, File JavaDoc source) throws IOException JavaDoc {
250         resourceContext.addInclude(targetPath, source);
251     }
252
253     /**
254      * Import the classpath from a jar file's manifest. The imported classpath
255      * is crafted relative to <code>moduleBaseUri</code>.
256      *
257      * @param moduleFile the jar file from which the manifest is obtained.
258      * @param moduleBaseUri the base for the imported classpath
259      * @throws DeploymentException if there is a problem with the classpath in
260      * the manifest
261      */

262     public void addManifestClassPath(JarFile JavaDoc moduleFile, URI JavaDoc moduleBaseUri) throws DeploymentException {
263         Manifest JavaDoc manifest;
264         try {
265             manifest = moduleFile.getManifest();
266         } catch (IOException JavaDoc e) {
267             throw new DeploymentException("Could not read manifest: " + moduleBaseUri);
268         }
269
270         if (manifest == null) {
271             return;
272         }
273         String JavaDoc manifestClassPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
274         if (manifestClassPath == null) {
275             return;
276         }
277
278         for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(manifestClassPath, " "); tokenizer.hasMoreTokens();) {
279             String JavaDoc path = tokenizer.nextToken();
280
281             URI JavaDoc pathUri;
282             try {
283                 pathUri = new URI JavaDoc(path);
284             } catch (URISyntaxException JavaDoc e) {
285                 throw new DeploymentException("Invalid manifest classpath entry: module=" + moduleBaseUri + ", path=" + path);
286             }
287
288             if (!pathUri.getPath().endsWith(".jar")) {
289                 throw new DeploymentException("Manifest class path entries must end with the .jar extension (J2EE 1.4 Section 8.2): module=" + moduleBaseUri);
290             }
291             if (pathUri.isAbsolute()) {
292                 throw new DeploymentException("Manifest class path entries must be relative (J2EE 1.4 Section 8.2): moduel=" + moduleBaseUri);
293             }
294
295             try {
296                 URI JavaDoc targetUri = moduleBaseUri.resolve(pathUri);
297                 if (targetUri.getPath().endsWith("/")) throw new IllegalStateException JavaDoc("target path must not end with a '/' character: " + targetUri);
298                 configuration.addToClassPath(targetUri.toString());
299             } catch (IOException JavaDoc e) {
300                 throw new DeploymentException(e);
301             }
302         }
303     }
304
305     public void addClass(URI JavaDoc targetPath, String JavaDoc fqcn, byte[] bytes) throws IOException JavaDoc, URISyntaxException JavaDoc {
306         if (!targetPath.getPath().endsWith("/")) throw new IllegalStateException JavaDoc("target path must end with a '/' character: " + targetPath);
307
308         String JavaDoc classFileName = fqcn.replace('.', '/') + ".class";
309
310         File JavaDoc targetFile = getTargetFile(new URI JavaDoc(targetPath.toString() + classFileName));
311         addFile(targetFile, new ByteArrayInputStream JavaDoc(bytes));
312
313         configuration.addToClassPath(targetPath.toString());
314     }
315
316     public void addFile(URI JavaDoc targetPath, ZipFile JavaDoc zipFile, ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
317         resourceContext.addFile(targetPath, zipFile, zipEntry);
318     }
319
320     public void addFile(URI JavaDoc targetPath, URL JavaDoc source) throws IOException JavaDoc {
321         resourceContext.addFile(targetPath, source);
322     }
323
324     public void addFile(URI JavaDoc targetPath, File JavaDoc source) throws IOException JavaDoc {
325         resourceContext.addFile(targetPath, source);
326     }
327
328     public void addFile(URI JavaDoc targetPath, String JavaDoc source) throws IOException JavaDoc {
329         resourceContext.addFile(targetPath, source);
330     }
331
332     private void addFile(File JavaDoc targetFile, InputStream JavaDoc source) throws IOException JavaDoc {
333         targetFile.getParentFile().mkdirs();
334         OutputStream JavaDoc out = null;
335         try {
336             out = new FileOutputStream JavaDoc(targetFile);
337             int count;
338             while ((count = source.read(buffer)) > 0) {
339                 out.write(buffer, 0, count);
340             }
341         } finally {
342             DeploymentUtil.close(out);
343         }
344     }
345
346     public File JavaDoc getTargetFile(URI JavaDoc targetPath) {
347         return resourceContext.getTargetFile(targetPath);
348     }
349
350     public ClassLoader JavaDoc getClassLoader() throws DeploymentException {
351         return configuration.getConfigurationClassLoader();
352     }
353
354     public Configuration getConfiguration() {
355         return configuration;
356     }
357
358     public void flush() throws IOException JavaDoc{
359         resourceContext.flush();
360     }
361
362     public void close() throws IOException JavaDoc, DeploymentException {
363         if (configurationManager != null) {
364             try {
365                 configurationManager.unloadConfiguration(configuration.getId());
366             } catch (NoSuchConfigException ignored) {
367                 //ignore
368
}
369         }
370     }
371
372     public void addChildConfiguration(String JavaDoc moduleName, ConfigurationData configurationData) {
373         childConfigurationDatas.put(moduleName, configurationData);
374     }
375
376     public ConfigurationData getConfigurationData() throws DeploymentException {
377         List JavaDoc failures = verify();
378         if (!failures.isEmpty()) {
379             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
380             for (Iterator JavaDoc iterator = failures.iterator(); iterator.hasNext();) {
381                 String JavaDoc failure = (String JavaDoc) iterator.next();
382                 if (message.length() > 0) message.append("\n");
383                 message.append(failure);
384             }
385             throw new DeploymentException(message.toString());
386         }
387
388         ArrayList JavaDoc gbeans = new ArrayList JavaDoc(configuration.getGBeans().values());
389         Collections.sort(gbeans, new GBeanData.PriorityComparator());
390         ConfigurationData configurationData = new ConfigurationData(configuration.getModuleType(),
391                 new LinkedHashSet JavaDoc(configuration.getClassPath()),
392                 gbeans,
393                 childConfigurationDatas,
394                 configuration.getEnvironment(),
395                 baseDir,
396                 inPlaceConfigurationDir,
397                 naming);
398
399         for (Iterator JavaDoc iterator = additionalDeployment.iterator(); iterator.hasNext();) {
400             ConfigurationData ownedConfiguration = (ConfigurationData) iterator.next();
401             configurationData.addOwnedConfigurations(ownedConfiguration.getId());
402         }
403
404         return configurationData;
405     }
406
407     public void addAdditionalDeployment(ConfigurationData configurationData) {
408         additionalDeployment.add(configurationData);
409     }
410
411     public List JavaDoc getAdditionalDeployment() {
412         return additionalDeployment;
413     }
414
415     public AbstractName getModuleName() {
416         return moduleName;
417     }
418
419     public List JavaDoc verify() throws DeploymentException {
420         List JavaDoc failures = new ArrayList JavaDoc();
421         for (Iterator JavaDoc iterator = configuration.getGBeans().entrySet().iterator(); iterator.hasNext();) {
422             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
423             AbstractName name = (AbstractName) entry.getKey();
424             GBeanData gbean = (GBeanData) entry.getValue();
425
426             for (Iterator JavaDoc iterator1 = gbean.getReferences().entrySet().iterator(); iterator1.hasNext();) {
427                 Map.Entry JavaDoc referenceEntry = (Map.Entry JavaDoc) iterator1.next();
428                 String JavaDoc referenceName = (String JavaDoc) referenceEntry.getKey();
429                 ReferencePatterns referencePatterns = (ReferencePatterns) referenceEntry.getValue();
430
431                 String JavaDoc failure = verifyReference(gbean, referenceName, referencePatterns);
432                 if (failure != null) {
433                     failures.add(failure);
434                 }
435             }
436
437             for (Iterator JavaDoc iterator1 = gbean.getDependencies().iterator(); iterator1.hasNext();) {
438                 ReferencePatterns referencePatterns = (ReferencePatterns) iterator1.next();
439                 String JavaDoc failure = verifyDependency(name, referencePatterns);
440                 if (failure != null) {
441                     failures.add(failure);
442                 }
443             }
444         }
445         return failures;
446     }
447
448     private String JavaDoc verifyReference(GBeanData gbean, String JavaDoc referenceName, ReferencePatterns referencePatterns) {
449         GReferenceInfo referenceInfo = gbean.getGBeanInfo().getReference(referenceName);
450
451         // if there is no reference info we can't verify
452
if (referenceInfo == null) return null;
453
454         // A collection valued reference doesn't need to be verified
455
if (referenceInfo.getProxyType().equals(Collection JavaDoc.class.getName())) return null;
456
457         String JavaDoc message = isVerifyReference(referencePatterns);
458         if (message != null) {
459             return "Unable to resolve reference \"" + referenceName + "\" in gbean " +
460                     gbean.getAbstractName() + " to a gbean matching the pattern " + referencePatterns.getPatterns() + "due to: " + message;
461         }
462         return null;
463     }
464
465     private String JavaDoc verifyDependency(AbstractName name, ReferencePatterns referencePatterns) {
466         String JavaDoc message = isVerifyReference(referencePatterns);
467         if (message != null) {
468             return "Unable to resolve dependency in gbean " + name +
469                     " to a gbean matching the pattern " + referencePatterns.getPatterns() + "due to: " + message;
470         }
471
472         return null;
473     }
474
475     private String JavaDoc isVerifyReference(ReferencePatterns referencePatterns) {
476         // we can't verify a resolved reference since it will have a specific artifact already set...
477
// hopefully the deployer won't generate bad resolved references
478
if (referencePatterns.isResolved()) return null;
479
480         // Do not verify the reference if it has an explicit depenency on another artifact, because it it likely
481
// that the other artifact is not in the "environment" (if it were you wouldn't use the long form)
482
Set JavaDoc patterns = referencePatterns.getPatterns();
483         for (Iterator JavaDoc iterator = patterns.iterator(); iterator.hasNext();) {
484             AbstractNameQuery query = (AbstractNameQuery) iterator.next();
485             if (query.getArtifact() != null) return null;
486         }
487
488         // attempt to find the bean
489
try {
490             findGBean(patterns);
491             return null;
492         } catch (GBeanNotFoundException e) {
493             return e.getMessage();
494         }
495     }
496 }
497
Popular Tags