View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 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  
18  package org.apache.geronimo.deployment.tools.loader;
19  
20  import java.io.BufferedInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.net.URLClassLoader;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Enumeration;
30  import java.util.List;
31  import java.util.zip.ZipEntry;
32  import java.util.zip.ZipInputStream;
33  import javax.enterprise.deploy.model.DDBean;
34  import javax.enterprise.deploy.model.DDBeanRoot;
35  import javax.enterprise.deploy.model.DeployableObject;
36  import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
37  import javax.enterprise.deploy.shared.ModuleType;
38  
39  import org.apache.geronimo.deployment.tools.DDBeanRootImpl;
40  
41  /**
42   * 
43   * 
44   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
45   */
46  public abstract class AbstractDeployable implements DeployableObject {
47      private final URL moduleURL;
48      private final ModuleType type;
49      private final DDBeanRoot root;
50      private final ClassLoader rootCL;
51      private final List entries;
52  
53      protected AbstractDeployable(ModuleType type, URL moduleURL, String rootDD) throws DDBeanCreateException {
54          this.type = type;
55          this.moduleURL = moduleURL;
56          rootCL = new URLClassLoader(new URL[] {moduleURL}, Thread.currentThread().getContextClassLoader());
57          root = new DDBeanRootImpl(this, rootCL.getResource(rootDD));
58  
59          // @todo make this work with unpacked
60          entries = new ArrayList();
61          InputStream is = null;
62          try {
63              is = moduleURL.openStream();
64              ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
65              ZipEntry entry;
66              while ((entry = zis.getNextEntry()) != null) {
67                  entries.add(entry.getName());
68              }
69          } catch (IOException 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 e1) {
76                      // ignore
77                  }
78              }
79          }
80      }
81  
82      public ModuleType getType() {
83          return type;
84      }
85  
86      public DDBeanRoot getDDBeanRoot() {
87          return root;
88      }
89  
90      public DDBeanRoot getDDBeanRoot(String filename) throws FileNotFoundException, DDBeanCreateException {
91          try {
92              return new DDBeanRootImpl(null, new URL(moduleURL, filename));
93          } catch (MalformedURLException e) {
94              throw (DDBeanCreateException) new DDBeanCreateException("Unable to construct URL for "+filename).initCause(e);
95          }
96      }
97  
98      public DDBean[] getChildBean(String xpath) {
99          return root.getChildBean(xpath);
100     }
101 
102     public String[] getText(String xpath) {
103         return root.getText(xpath);
104     }
105 
106     public Enumeration entries() {
107         return Collections.enumeration(entries);
108     }
109 
110     public InputStream getEntry(String name) {
111         return rootCL.getResourceAsStream(name);
112     }
113 
114     protected ClassLoader getModuleLoader() {
115         return rootCL;
116     }
117 
118     public Class getClassFromScope(String className) {
119         try {
120             return getModuleLoader().loadClass(className);
121         } catch (ClassNotFoundException e) {
122             // spec does not allow an Exception
123             return null;
124         }
125     }
126 
127     public String getModuleDTDVersion() {
128         throw new UnsupportedOperationException();
129     }
130 }