View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.geronimo.ews.ws4j2ee.module;
18  
19  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileNotFoundException;
24  import java.io.InputStream;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.net.URLClassLoader;
28  import java.util.ArrayList;
29  
30  /***
31   * @author Srinath Perera(hemapani@opensource.lk)
32   */
33  public class DirModule implements Module {
34      protected InputStream wscfFile;
35      protected InputStream webddfile;
36      protected InputStream ejbJarfile;
37      private String location;
38      private ClassLoader parentCL;
39  
40      /***
41       * @param jarFile
42       * @throws GenerationFault
43       */
44      public DirModule(String location, ClassLoader parentCL)
45              throws GenerationFault {
46          File file = null;
47          this.parentCL = parentCL;
48          this.location = location;
49          try {
50              file = new File(location + "/WEB-INF/webservices.xml");
51              if (file.exists()) {
52                  wscfFile = new FileInputStream(file);
53              } else {
54                  file = new File(location + "/META-INF/webservices.xml");
55                  if (file.exists()) {
56                      wscfFile = new FileInputStream(file);
57                  } else {
58                      wscfFile = new FileInputStream(new File(location + "/webservices.xml"));
59                  }
60              }
61              findDDs();
62          } catch (FileNotFoundException e) {
63              e.printStackTrace();
64              throw GenerationFault.createGenerationFault(e);
65          }
66      }
67  
68      /***
69       * @param jarFile
70       * @throws GenerationFault
71       */
72      public DirModule(File wscfDDFile)
73              throws GenerationFault {
74          try {
75              wscfFile = new FileInputStream(wscfDDFile);
76              File parent = wscfDDFile.getParentFile();
77              String parentName = parent.getAbsolutePath();
78              if (parentName.endsWith("WEB-INF")
79                      || parentName.endsWith("WEB-INF/")
80                      || parentName.endsWith("WEB-INF")
81                      || parentName.endsWith("META-INF/")) {
82                  this.location = parent.getParentFile().getAbsolutePath();
83              } else {
84                  this.location = parent.getAbsolutePath();
85              }
86              findDDs();
87          } catch (FileNotFoundException e) {
88              e.printStackTrace();
89              throw GenerationFault.createGenerationFault(e);
90          }
91      }
92  
93      public void findDDs() throws GenerationFault {
94          try {
95              File file = new File(location + "/META-INF/ejb-jar.xml");
96              if (file.exists()) {
97                  ejbJarfile = new FileInputStream(file);
98              } else {
99                  file = new File(location + "/ejb-jar.xml");
100                 if (file.exists()) {
101                     ejbJarfile = new FileInputStream(file);
102                 }
103             }
104             file = new File(location + "WEB-INF/web.xml");
105             if (file.exists()) {
106                 webddfile = new FileInputStream(file);
107             } else {
108                 file = new File(location + "/web.xml");
109                 if (file.exists()) {
110                     webddfile = new FileInputStream(file);
111                 }
112             }
113             if (wscfFile == null)
114                 throw new GenerationFault("wscf file must not be null");
115         } catch (FileNotFoundException e) {
116             e.printStackTrace();
117             throw GenerationFault.createGenerationFault(e);
118         }
119     }
120 
121     public ClassLoader getClassLoaderWithPackageLoaded() throws GenerationFault {
122         try {
123             File file = new File(location);
124             return new URLClassLoader(new URL[]{file.toURL()}, parentCL);
125         } catch (MalformedURLException e) {
126             e.printStackTrace();
127             throw GenerationFault.createGenerationFault(e);
128         }
129     }
130 
131     public ArrayList getClassPathElements() throws GenerationFault {
132         ArrayList elements = new ArrayList();
133         elements.add(new File(location));
134         return elements;
135     }
136 
137     public InputStream getEjbJarfile() throws GenerationFault {
138         return ejbJarfile;
139     }
140 
141     public InputStream getWebddfile() throws GenerationFault {
142         return webddfile;
143     }
144 
145     public InputStream getWscfFile() throws GenerationFault {
146         return wscfFile;
147     }
148 
149     public void setEjbJarfile(InputStream stream) throws GenerationFault {
150         this.ejbJarfile = stream;
151     }
152 
153     public void setWebddfile(InputStream stream) throws GenerationFault {
154         this.webddfile = stream;
155     }
156 
157     public void setWscfFile(InputStream stream) throws GenerationFault {
158         this.wscfFile = stream;
159     }
160 
161     /* (non-Javadoc)
162      * @see org.apache.geronimo.ews.ws4j2ee.utils.packager.load.PackageModule#findFileInModule(java.lang.String)
163      */
164     public InputStream findFileInModule(String path) throws GenerationFault {
165         try {
166             File file = new File(location + "/" + path);
167             if (file.exists())
168                 new FileInputStream(file);
169             return null;
170         } catch (FileNotFoundException e) {
171             e.printStackTrace();
172             throw GenerationFault.createGenerationFault(e);
173         }
174     }
175 
176 }