KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > server > JURLs


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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: JURLs.java,v 1.4 2004/05/25 15:13:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.server;
28
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.io.File JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34
35 /**
36  * This implements an utility class that list the URLs of the jars to load at
37  * the launch time of the JOnAS server.
38  * @author Ludovic Bert
39  * @author Florent Benoit
40  */

41 public class JURLs extends Vector JavaDoc {
42
43     /**
44      * Construct an empty JURLs.
45      */

46     public JURLs() {
47         super();
48     }
49
50     /**
51      * Add the specified file or directory to this JURLs. Note that in the case
52      * of a directory this method add only the content of this one.
53      * @param file the specified file or directory to add to this JURLs.
54      * @throws MalformedURLException to indicate that a malformed URL has
55      * occured.
56      */

57     public void add(File JavaDoc file) throws MalformedURLException JavaDoc {
58         add(file, null, null, null);
59     }
60
61     /**
62      * Add the specified directory to this JURLs. Note that this method add the
63      * URL of the specified directory and not the content of this directory.
64      * @param file the directory to add to this JURLs.
65      * @throws MalformedURLException to indicate that a malformed URL has
66      * occured.
67      */

68     public void addDir(File JavaDoc file) throws MalformedURLException JavaDoc {
69         if (!file.exists() || !file.isDirectory()) {
70             String JavaDoc err = "Warning: Ressource " + file.getName();
71             err += " cannot be loaded : The directory does not exist";
72             System.out.println(err);
73         } else {
74             if (!contains(file.toURL())) {
75                 add(file.toURL());
76             }
77         }
78     }
79
80     /**
81      * Add the content of the specified directory to this JURLs, by using the
82      * specified filter.
83      * @param file the directory to add to this JURLs.
84      * @param filter the filter to use to add the content of this directory,
85      * null if not use the filter.
86      * @throws MalformedURLException to indicate that a malformed URL has
87      * occured.
88      */

89     public void add(File JavaDoc file, String JavaDoc filter) throws MalformedURLException JavaDoc {
90         if (file.isDirectory()) {
91             add(file, filter, null, null);
92         } else {
93             String JavaDoc err = "Warning: Ressource " + file.getName();
94             err += " cannot be loaded : It is not a directory";
95             System.out.println(err);
96         }
97     }
98
99     /**
100      * Add the content of the specified directory to this JURLs if the contained
101      * file not starts with the specified prefix.
102      * @param file the directory to add to this JURLs.
103      * @param prefix the prefix to ignore, null if not use the prefix.
104      * @throws MalformedURLException to indicate that a malformed URL has
105      * occured.
106      */

107     public void addNotStartWith(File JavaDoc file, String JavaDoc prefix) throws MalformedURLException JavaDoc {
108
109         if (file.isDirectory()) {
110             add(file, null, prefix, null);
111         } else {
112             String JavaDoc err = "Warning: Ressource " + file.getName();
113             err += " cannot be loaded : It is not a directory";
114             System.out.println(err);
115         }
116     }
117
118     /**
119      * Add the content of the specified directory to this JURLs if the contained
120      * file not ends with the specified suffix.
121      * @param file the directory to add to this JURLs.
122      * @param suffix the suffix to ignore, null if not use the suffix.
123      * @throws MalformedURLException to indicate that a malformed URL has
124      * occured.
125      */

126     public void addNotEndWith(File JavaDoc file, String JavaDoc suffix) throws MalformedURLException JavaDoc {
127
128         if (file.isDirectory()) {
129             add(file, null, null, suffix);
130         } else {
131             String JavaDoc err = "Warning: Ressource " + file.getName();
132             err += " cannot be loaded : It is not a directory";
133             System.out.println(err);
134         }
135     }
136
137     /**
138      * Add the content of the specified directory to this JURLs by using a file
139      * filter and if the contained file not starts with the prefix and not ends
140      * with the suffix.
141      * @param file the directory to add to this JURLs.
142      * @param filter the filter to use to add the content of this directory,
143      * null if not use the filter.
144      * @param prefix the prefix to ignore, null if not use the prefix.
145      * @param suffix the suffix to ignore, null if not use the suffix.
146      * @throws MalformedURLException to indicate that a malformed URL has
147      * occured.
148      */

149     public void add(File JavaDoc file, String JavaDoc filter, String JavaDoc prefix, String JavaDoc suffix) throws MalformedURLException JavaDoc {
150         if (!file.exists()) {
151             String JavaDoc err = "Warning: Ressource " + file.getPath();
152             err += " cannot be loaded : The file or directory does not exist";
153             err += "(Check your environment variable)";
154             System.out.println(err);
155         } else {
156             if (file.isFile()) {
157                 if (!isMatching(file, prefix, suffix) && !contains(file.toURL())) {
158                     // System.out.println("Adding URL "+file.toURL()); // DEBUG
159
add(file.toURL());
160                 }
161             } else {
162                 File JavaDoc[] childrenFiles = null;
163                 if (filter != null) {
164                     childrenFiles = file.listFiles(new JFileFilter(filter));
165                 } else {
166                     childrenFiles = file.listFiles();
167                 }
168                 for (int i = 0; i < childrenFiles.length; i++) {
169                     add(childrenFiles[i], filter, prefix, suffix);
170                 }
171             }
172         }
173     }
174
175     /**
176      * Return true if only if the file starts with the specified prefix or ends
177      * with the specified suffix.
178      * @param file the file to match.
179      * @param prefix the prefix to use for the matching, null if do not use the
180      * prefix.
181      * @param suffix the suffix to use for the matching, null if do not use the
182      * suffix.
183      * @return true if only if the file starts with the specified prefix or ends
184      * with the specified suffix.
185      */

186     private boolean isMatching(File JavaDoc file, String JavaDoc prefix, String JavaDoc suffix) {
187         String JavaDoc fileName = file.getName();
188         if (prefix == null) {
189             if (suffix == null) {
190                 return false;
191             } else {
192                 return fileName.endsWith(suffix);
193             }
194         } else {
195             if (suffix == null) {
196                 return fileName.startsWith(prefix);
197             } else {
198                 return fileName.startsWith(prefix) || fileName.endsWith(suffix);
199             }
200         }
201     }
202
203     /**
204      * Merge the specified JURls to this JURLs.
205      * @param jurl the JURls to merge with this JURLs.
206      */

207     public void merge(JURLs jurl) {
208         for (Enumeration JavaDoc e = jurl.elements(); e.hasMoreElements();) {
209             URL JavaDoc url = (URL JavaDoc) e.nextElement();
210             if (!contains(url)) {
211                 add(url);
212             }
213         }
214     }
215
216     /**
217      * Remove the specified file of this JURLs
218      * @param file the file to be removed
219      * @throws MalformedURLException to indicate that a malformed URL has
220      * occured.
221      */

222     public void remove(File JavaDoc file) throws MalformedURLException JavaDoc {
223         if (file.exists()) {
224             remove(file.toURL());
225         } else {
226             String JavaDoc err = "Warning: Ressource " + file.getName();
227             err += " cannot be removed : It doesn't exist";
228             System.out.println(err);
229         }
230     }
231
232     /**
233      * Return an array containing all the URLs of this JURLs.
234      * @return an array containing all the URLs of this JURLs.
235      */

236     public URL JavaDoc[] toURLs() {
237         return (URL JavaDoc[]) super.toArray(new URL JavaDoc[elementCount]);
238     }
239 }
Popular Tags