001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Florent BENOIT & Ludovic BERT
022: * --------------------------------------------------------------------------
023: * $Id: JURLs.java 4804 2004-05-25 15:13:29Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.server;
026:
027: import java.util.Enumeration;
028: import java.util.Vector;
029: import java.io.File;
030: import java.net.URL;
031: import java.net.MalformedURLException;
032:
033: /**
034: * This implements an utility class that list the URLs of the jars to load at
035: * the launch time of the JOnAS server.
036: * @author Ludovic Bert
037: * @author Florent Benoit
038: */
039: public class JURLs extends Vector {
040:
041: /**
042: * Construct an empty JURLs.
043: */
044: public JURLs() {
045: super ();
046: }
047:
048: /**
049: * Add the specified file or directory to this JURLs. Note that in the case
050: * of a directory this method add only the content of this one.
051: * @param file the specified file or directory to add to this JURLs.
052: * @throws MalformedURLException to indicate that a malformed URL has
053: * occured.
054: */
055: public void add(File file) throws MalformedURLException {
056: add(file, null, null, null);
057: }
058:
059: /**
060: * Add the specified directory to this JURLs. Note that this method add the
061: * URL of the specified directory and not the content of this directory.
062: * @param file the directory to add to this JURLs.
063: * @throws MalformedURLException to indicate that a malformed URL has
064: * occured.
065: */
066: public void addDir(File file) throws MalformedURLException {
067: if (!file.exists() || !file.isDirectory()) {
068: String err = "Warning: Ressource " + file.getName();
069: err += " cannot be loaded : The directory does not exist";
070: System.out.println(err);
071: } else {
072: if (!contains(file.toURL())) {
073: add(file.toURL());
074: }
075: }
076: }
077:
078: /**
079: * Add the content of the specified directory to this JURLs, by using the
080: * specified filter.
081: * @param file the directory to add to this JURLs.
082: * @param filter the filter to use to add the content of this directory,
083: * null if not use the filter.
084: * @throws MalformedURLException to indicate that a malformed URL has
085: * occured.
086: */
087: public void add(File file, String filter)
088: throws MalformedURLException {
089: if (file.isDirectory()) {
090: add(file, filter, null, null);
091: } else {
092: String err = "Warning: Ressource " + file.getName();
093: err += " cannot be loaded : It is not a directory";
094: System.out.println(err);
095: }
096: }
097:
098: /**
099: * Add the content of the specified directory to this JURLs if the contained
100: * file not starts with the specified prefix.
101: * @param file the directory to add to this JURLs.
102: * @param prefix the prefix to ignore, null if not use the prefix.
103: * @throws MalformedURLException to indicate that a malformed URL has
104: * occured.
105: */
106: public void addNotStartWith(File file, String prefix)
107: throws MalformedURLException {
108:
109: if (file.isDirectory()) {
110: add(file, null, prefix, null);
111: } else {
112: String 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 file, String suffix)
127: throws MalformedURLException {
128:
129: if (file.isDirectory()) {
130: add(file, null, null, suffix);
131: } else {
132: String err = "Warning: Ressource " + file.getName();
133: err += " cannot be loaded : It is not a directory";
134: System.out.println(err);
135: }
136: }
137:
138: /**
139: * Add the content of the specified directory to this JURLs by using a file
140: * filter and if the contained file not starts with the prefix and not ends
141: * with the suffix.
142: * @param file the directory to add to this JURLs.
143: * @param filter the filter to use to add the content of this directory,
144: * null if not use the filter.
145: * @param prefix the prefix to ignore, null if not use the prefix.
146: * @param suffix the suffix to ignore, null if not use the suffix.
147: * @throws MalformedURLException to indicate that a malformed URL has
148: * occured.
149: */
150: public void add(File file, String filter, String prefix,
151: String suffix) throws MalformedURLException {
152: if (!file.exists()) {
153: String err = "Warning: Ressource " + file.getPath();
154: err += " cannot be loaded : The file or directory does not exist";
155: err += "(Check your environment variable)";
156: System.out.println(err);
157: } else {
158: if (file.isFile()) {
159: if (!isMatching(file, prefix, suffix)
160: && !contains(file.toURL())) {
161: // System.out.println("Adding URL "+file.toURL()); // DEBUG
162: add(file.toURL());
163: }
164: } else {
165: File[] childrenFiles = null;
166: if (filter != null) {
167: childrenFiles = file.listFiles(new JFileFilter(
168: filter));
169: } else {
170: childrenFiles = file.listFiles();
171: }
172: for (int i = 0; i < childrenFiles.length; i++) {
173: add(childrenFiles[i], filter, prefix, suffix);
174: }
175: }
176: }
177: }
178:
179: /**
180: * Return true if only if the file starts with the specified prefix or ends
181: * with the specified suffix.
182: * @param file the file to match.
183: * @param prefix the prefix to use for the matching, null if do not use the
184: * prefix.
185: * @param suffix the suffix to use for the matching, null if do not use the
186: * suffix.
187: * @return true if only if the file starts with the specified prefix or ends
188: * with the specified suffix.
189: */
190: private boolean isMatching(File file, String prefix, String suffix) {
191: String fileName = file.getName();
192: if (prefix == null) {
193: if (suffix == null) {
194: return false;
195: } else {
196: return fileName.endsWith(suffix);
197: }
198: } else {
199: if (suffix == null) {
200: return fileName.startsWith(prefix);
201: } else {
202: return fileName.startsWith(prefix)
203: || fileName.endsWith(suffix);
204: }
205: }
206: }
207:
208: /**
209: * Merge the specified JURls to this JURLs.
210: * @param jurl the JURls to merge with this JURLs.
211: */
212: public void merge(JURLs jurl) {
213: for (Enumeration e = jurl.elements(); e.hasMoreElements();) {
214: URL url = (URL) e.nextElement();
215: if (!contains(url)) {
216: add(url);
217: }
218: }
219: }
220:
221: /**
222: * Remove the specified file of this JURLs
223: * @param file the file to be removed
224: * @throws MalformedURLException to indicate that a malformed URL has
225: * occured.
226: */
227: public void remove(File file) throws MalformedURLException {
228: if (file.exists()) {
229: remove(file.toURL());
230: } else {
231: String err = "Warning: Ressource " + file.getName();
232: err += " cannot be removed : It doesn't exist";
233: System.out.println(err);
234: }
235: }
236:
237: /**
238: * Return an array containing all the URLs of this JURLs.
239: * @return an array containing all the URLs of this JURLs.
240: */
241: public URL[] toURLs() {
242: return (URL[]) super .toArray(new URL[elementCount]);
243: }
244: }
|