KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > tools > loader > AbstractDeployable


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.tools.loader;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import java.util.zip.ZipInputStream JavaDoc;
33 import javax.enterprise.deploy.model.DDBean JavaDoc;
34 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
35 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
36 import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
37 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
38
39 import org.apache.geronimo.deployment.tools.DDBeanRootImpl;
40
41 /**
42  *
43  *
44  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
45  */

46 public abstract class AbstractDeployable implements DeployableObject JavaDoc {
47     private final URL JavaDoc moduleURL;
48     private final ModuleType JavaDoc type;
49     private final DDBeanRoot JavaDoc root;
50     private final ClassLoader JavaDoc rootCL;
51     private final List JavaDoc entries;
52
53     protected AbstractDeployable(ModuleType JavaDoc type, URL JavaDoc moduleURL, String JavaDoc rootDD) throws DDBeanCreateException {
54         this.type = type;
55         this.moduleURL = moduleURL;
56         rootCL = new URLClassLoader JavaDoc(new URL JavaDoc[] {moduleURL}, Thread.currentThread().getContextClassLoader());
57         root = new DDBeanRootImpl(this, rootCL.getResource(rootDD));
58
59         // @todo make this work with unpacked
60
entries = new ArrayList JavaDoc();
61         InputStream JavaDoc is = null;
62         try {
63             is = moduleURL.openStream();
64             ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(new BufferedInputStream JavaDoc(is));
65             ZipEntry JavaDoc entry;
66             while ((entry = zis.getNextEntry()) != null) {
67                 entries.add(entry.getName());
68             }
69         } catch (IOException JavaDoc e) {
70             throw (DDBeanCreateException) new DDBeanCreateException("Unable to create list of entries").initCause(e);
71         } finally {
72             if (is != null) {
73                 try {
74                     is.close();
75                 } catch (IOException JavaDoc e1) {
76                     // ignore
77
}
78             }
79         }
80     }
81
82     public ModuleType JavaDoc getType() {
83         return type;
84     }
85
86     public DDBeanRoot JavaDoc getDDBeanRoot() {
87         return root;
88     }
89
90     public DDBeanRoot JavaDoc getDDBeanRoot(String JavaDoc filename) throws FileNotFoundException JavaDoc, DDBeanCreateException {
91         try {
92             return new DDBeanRootImpl(null, new URL JavaDoc(moduleURL, filename));
93         } catch (MalformedURLException JavaDoc e) {
94             throw (DDBeanCreateException) new DDBeanCreateException("Unable to construct URL for "+filename).initCause(e);
95         }
96     }
97
98     public DDBean JavaDoc[] getChildBean(String JavaDoc xpath) {
99         return root.getChildBean(xpath);
100     }
101
102     public String JavaDoc[] getText(String JavaDoc xpath) {
103         return root.getText(xpath);
104     }
105
106     public Enumeration JavaDoc entries() {
107         return Collections.enumeration(entries);
108     }
109
110     public InputStream JavaDoc getEntry(String JavaDoc name) {
111         return rootCL.getResourceAsStream(name);
112     }
113
114     protected ClassLoader JavaDoc getModuleLoader() {
115         return rootCL;
116     }
117
118     public Class JavaDoc getClassFromScope(String JavaDoc className) {
119         try {
120             return getModuleLoader().loadClass(className);
121         } catch (ClassNotFoundException JavaDoc e) {
122             // spec does not allow an Exception
123
return null;
124         }
125     }
126
127     public String JavaDoc getModuleDTDVersion() {
128         throw new UnsupportedOperationException JavaDoc();
129     }
130 }
131
Popular Tags