JURLs.java

00001 
00027 package org.objectweb.jonas.server;
00028 
00029 import java.util.Enumeration;
00030 import java.util.Vector;
00031 import java.io.File;
00032 import java.net.URL;
00033 import java.net.MalformedURLException;
00034 
00041 public class JURLs extends Vector {
00042 
00046     public JURLs() {
00047         super();
00048     }
00049 
00057     public void add(File file) throws MalformedURLException {
00058         add(file, null, null, null);
00059     }
00060 
00068     public void addDir(File file) throws MalformedURLException {
00069         if (!file.exists() || !file.isDirectory()) {
00070             String err = "Warning: Ressource " + file.getName();
00071             err += " cannot be loaded : The directory does not exist";
00072             System.out.println(err);
00073         } else {
00074             if (!contains(file.toURL())) {
00075                 add(file.toURL());
00076             }
00077         }
00078     }
00079 
00089     public void add(File file, String filter) throws MalformedURLException {
00090         if (file.isDirectory()) {
00091             add(file, filter, null, null);
00092         } else {
00093             String err = "Warning: Ressource " + file.getName();
00094             err += " cannot be loaded : It is not a directory";
00095             System.out.println(err);
00096         }
00097     }
00098 
00107     public void addNotStartWith(File file, String prefix) throws MalformedURLException {
00108 
00109         if (file.isDirectory()) {
00110             add(file, null, prefix, null);
00111         } else {
00112             String err = "Warning: Ressource " + file.getName();
00113             err += " cannot be loaded : It is not a directory";
00114             System.out.println(err);
00115         }
00116     }
00117 
00126     public void addNotEndWith(File file, String suffix) throws MalformedURLException {
00127 
00128         if (file.isDirectory()) {
00129             add(file, null, null, suffix);
00130         } else {
00131             String err = "Warning: Ressource " + file.getName();
00132             err += " cannot be loaded : It is not a directory";
00133             System.out.println(err);
00134         }
00135     }
00136 
00149     public void add(File file, String filter, String prefix, String suffix) throws MalformedURLException {
00150         if (!file.exists()) {
00151             String err = "Warning: Ressource " + file.getPath();
00152             err += " cannot be loaded : The file or directory does not exist";
00153             err += "(Check your environment variable)";
00154             System.out.println(err);
00155         } else {
00156             if (file.isFile()) {
00157                 if (!isMatching(file, prefix, suffix) && !contains(file.toURL())) {
00158                     // System.out.println("Adding URL "+file.toURL()); // DEBUG
00159                     add(file.toURL());
00160                 }
00161             } else {
00162                 File[] childrenFiles = null;
00163                 if (filter != null) {
00164                     childrenFiles = file.listFiles(new JFileFilter(filter));
00165                 } else {
00166                     childrenFiles = file.listFiles();
00167                 }
00168                 for (int i = 0; i < childrenFiles.length; i++) {
00169                     add(childrenFiles[i], filter, prefix, suffix);
00170                 }
00171             }
00172         }
00173     }
00174 
00186     private boolean isMatching(File file, String prefix, String suffix) {
00187         String fileName = file.getName();
00188         if (prefix == null) {
00189             if (suffix == null) {
00190                 return false;
00191             } else {
00192                 return fileName.endsWith(suffix);
00193             }
00194         } else {
00195             if (suffix == null) {
00196                 return fileName.startsWith(prefix);
00197             } else {
00198                 return fileName.startsWith(prefix) || fileName.endsWith(suffix);
00199             }
00200         }
00201     }
00202 
00207     public void merge(JURLs jurl) {
00208         for (Enumeration e = jurl.elements(); e.hasMoreElements();) {
00209             URL url = (URL) e.nextElement();
00210             if (!contains(url)) {
00211                 add(url);
00212             }
00213         }
00214     }
00215 
00222     public void remove(File file) throws MalformedURLException {
00223         if (file.exists()) {
00224             remove(file.toURL());
00225         } else {
00226             String err = "Warning: Ressource " + file.getName();
00227             err += " cannot be removed : It doesn't exist";
00228             System.out.println(err);
00229         }
00230     }
00231 
00236     public URL[] toURLs() {
00237         return (URL[]) super.toArray(new URL[elementCount]);
00238     }
00239 }

Generated on Tue Feb 15 15:05:24 2005 for JOnAS by  doxygen 1.3.9.1