KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ear > lib > EarClassPathManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: EarClassPathManager.java,v 1.5 2004/04/19 14:28:04 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.ear.lib;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.jar.Attributes JavaDoc;
36 import java.util.jar.JarFile JavaDoc;
37 import java.util.jar.Manifest JavaDoc;
38
39 /**
40  * JOnAS Ear class path manager class. This class provides a way for managing
41  * the class-path dependency libraries.
42  * @author Florent Benoit
43  * @author Ludovic Bert
44  * @author Nicolas Van Caneghem <nicolas.vancaneghem@openpricer.com>Allow the
45  * deployment of an exploded ear
46  */

47 public class EarClassPathManager {
48
49     /**
50      * Urls of the class path manager.
51      */

52     private URL JavaDoc[] urls = null;
53
54     /**
55      * List of jars that must be parsed.
56      */

57     private JarList toParse = null;
58
59     /**
60      * List of jars that have been parsed.
61      */

62     private JarList parsed = null;
63
64     /**
65      * List of jars which are libraries.
66      */

67     private JarList libraries = null;
68
69     /**
70      * List of the ejb jars.
71      */

72     private JarList ejbs = null;
73
74     /**
75      * List of the war files.
76      */

77     private JarList wars = null;
78
79     /**
80      * List of the client jars.
81      */

82     private JarList clients = null;
83
84     /**
85      * Directory of the ear
86      */

87     private URL JavaDoc directory = null;
88
89     /**
90      * Construct an instance of a EarClassPathManager
91      * @param ejbs JarList of ejb-jar
92      * @param wars JarList of war
93      * @param directory URL of the directory
94      * @throws EarClassPathManagerException if we can't create the manager
95      */

96     public EarClassPathManager(JarList ejbs, JarList wars, URL JavaDoc directory) throws EarClassPathManagerException {
97
98         if ((ejbs == null) || (wars == null) || (directory == null)) {
99             throw new EarClassPathManagerException("The constructor EarClassPathManager can't accept null parameters");
100         }
101
102         //check protocol
103
if (!directory.getProtocol().equalsIgnoreCase("file")) {
104             throw new EarClassPathManagerException("Only the file:/ URL can be used");
105         }
106         this.ejbs = ejbs;
107         this.wars = wars;
108         this.clients = new JarList();
109         this.directory = directory;
110     }
111
112     /**
113      * Construct an instance of a EarClassPathManager
114      * @param clients JarList of the clients
115      * @param directory URL of the directory
116      * @throws EarClassPathManagerException if we can't create the manager
117      */

118     public EarClassPathManager(JarList clients, URL JavaDoc directory) throws EarClassPathManagerException {
119
120         if ((clients == null) || (directory == null)) {
121             throw new EarClassPathManagerException("The constructor EarClassPathManager can't accept null parameters");
122         }
123
124         //check protocol
125
if (!directory.getProtocol().equalsIgnoreCase("file")) {
126             throw new EarClassPathManagerException("Only the file:/ URL can be used");
127         }
128         this.ejbs = new JarList();
129         this.wars = new JarList();
130         this.clients = clients;
131         this.directory = directory;
132     }
133
134     /**
135      * Get the class-path from the MANIFEST.MF file of the specified archive.
136      * @param url the URL of the JAR file which contains the MANIFEST file.
137      * @return the class-path from the MANIFEST.MF file of the specified
138      * archive.
139      * @throws IOException if creation of a file based upon the url failed
140      * @throws EarClassPathManagerException if it failed
141      */

142     private JarList getManifestClassPath(URL JavaDoc url) throws EarClassPathManagerException, IOException JavaDoc {
143
144         if (url == null) {
145             throw new EarClassPathManagerException("JarList.getManifestClassPath : The url parameter can't be null");
146         }
147
148         Manifest JavaDoc manifest = null;
149
150         if (new File JavaDoc(url.getFile()).isDirectory()) {
151             File JavaDoc manifestFile = new File JavaDoc(url.getFile() + File.separator + JarFile.MANIFEST_NAME);
152             if (manifestFile.exists()) {
153                 manifest = new Manifest JavaDoc(new FileInputStream JavaDoc(manifestFile));
154             }
155
156         } else {
157             //Construct a JarFile in order to access to the manifest
158
// IOException it if failed
159
JarFile JavaDoc jarFile = new JarFile JavaDoc(url.getFile());
160
161             //get manifest from the jarFile
162
manifest = jarFile.getManifest();
163         }
164
165         //classpath
166
String JavaDoc classPath = null;
167
168         //Only if a manifest is found
169
if (manifest != null) {
170             //get attributes (classpath)
171
Attributes JavaDoc attributes = manifest.getMainAttributes();
172             classPath = attributes.getValue(Attributes.Name.CLASS_PATH);
173         }
174
175         //New JarList
176
JarList jarList = null;
177
178         //The jarList will be Empty if classpath is null or populate with
179
// classpath entries
180
if (classPath != null) {
181             jarList = new JarList(new StringTokenizer JavaDoc(classPath));
182         } else {
183             jarList = new JarList();
184         }
185
186         //Return the list
187
return jarList;
188     }
189
190     /**
191      * Get the list of the URLs.
192      * @return the list of the URLs.
193      * @throws EarClassPathManagerException if we can't resolve the path
194      */

195     public URL JavaDoc[] getResolvedClassPath() throws EarClassPathManagerException {
196
197         //If we don't have already compute
198
if (urls == null) {
199             resolveClassPath();
200         }
201
202         //return the cache
203
return urls;
204     }
205
206     /**
207      * Resolve the class-path dependencies of WAR and JAR files.
208      * @throws EarClassPathManagerException if it failed
209      */

210     private void resolveClassPath() throws EarClassPathManagerException {
211
212         //Set the list to parsed
213
toParse = new JarList();
214
215         //Set the list already parsed
216
parsed = new JarList();
217
218         //Set the list of libraries
219
libraries = new JarList();
220
221         //add the ejbs, wars and clients to this list
222
toParse.merge(ejbs);
223         toParse.merge(wars);
224         toParse.merge(clients);
225
226         //dependencies list
227
JarList lstOfFilesDep = new JarList();
228
229         //Url of the current filename
230
URL JavaDoc depUrl = null;
231
232         //While there are elements to analyse
233
while (toParse.size() > 0) {
234
235             //File to look for Manifest
236
String JavaDoc fileName = (String JavaDoc) toParse.firstElement();
237
238             if (fileName.endsWith("/")) {
239                 throw new EarClassPathManagerException("In j2ee application, Class-Path with directory is forbidden. '"
240                         + fileName + "' is not authorized.");
241             }
242             try {
243                 //Get dependency entries
244
depUrl = new URL JavaDoc(directory.toExternalForm() + "/" + fileName);
245                 lstOfFilesDep = getManifestClassPath(depUrl);
246             } catch (MalformedURLException JavaDoc mue) {
247                 lstOfFilesDep.removeAllElements();
248                 throw new EarClassPathManagerException("Error while trying to get the url for "
249                         + directory.toExternalForm() + File.separator + fileName + " : " + mue.getMessage());
250             } catch (IOException JavaDoc ioe) {
251                 lstOfFilesDep.removeAllElements();
252                 throw new EarClassPathManagerException("Error while reading manifest file from the file " + fileName
253                         + " : " + ioe.getMessage());
254             }
255
256             String JavaDoc parentDir = new File JavaDoc(fileName).getParent();
257             //subDirectory (the parent dir or "")
258
String JavaDoc subDir = null;
259             if (parentDir != null) {
260                 subDir = parentDir;
261             } else {
262                 subDir = "";
263             }
264
265             //Set the relative path of EAR / file
266
lstOfFilesDep.setRelativePath(subDir);
267
268             //Merge the list
269
toParse.merge(lstOfFilesDep);
270
271             //Add the parsed file
272
parsed.add(fileName);
273
274             //Add to the libraries if it's not an EJB or a WEB application
275
if (isALibrary(fileName)) {
276                 libraries.add(fileName);
277             }
278
279             //Remove the fileName
280
toParse.remove(fileName);
281
282         }
283
284         //We've got the list of files, its the JarList : parsed
285
try {
286             urls = libraries.getURLs(directory.toExternalForm());
287         } catch (JarListException e) {
288             throw new EarClassPathManagerException(
289                     "Error while geting the URLs of the jars files which must be loaded at the EAR level");
290
291         }
292
293     }
294
295     /**
296      * Check if the file is a library , ie : - It's not an EJB Jar. - It's not a
297      * War.
298      * @param fileName name of the file to check
299      * @return true if it's not either an ejbjar either a war file. (a library).
300      */

301     private boolean isALibrary(String JavaDoc fileName) {
302         return (!ejbs.contains(fileName) && !wars.contains(fileName));
303     }
304
305 }
Popular Tags