1 26 27 package org.objectweb.jonas.server; 28 29 import java.util.Enumeration ; 30 import java.util.Vector ; 31 import java.io.File ; 32 import java.net.URL ; 33 import java.net.MalformedURLException ; 34 35 41 public class JURLs extends Vector { 42 43 46 public JURLs() { 47 super(); 48 } 49 50 57 public void add(File file) throws MalformedURLException { 58 add(file, null, null, null); 59 } 60 61 68 public void addDir(File file) throws MalformedURLException { 69 if (!file.exists() || !file.isDirectory()) { 70 String 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 89 public void add(File file, String filter) throws MalformedURLException { 90 if (file.isDirectory()) { 91 add(file, filter, null, null); 92 } else { 93 String err = "Warning: Ressource " + file.getName(); 94 err += " cannot be loaded : It is not a directory"; 95 System.out.println(err); 96 } 97 } 98 99 107 public void addNotStartWith(File file, String prefix) 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 126 public void addNotEndWith(File file, String suffix) throws MalformedURLException { 127 128 if (file.isDirectory()) { 129 add(file, null, null, suffix); 130 } else { 131 String err = "Warning: Ressource " + file.getName(); 132 err += " cannot be loaded : It is not a directory"; 133 System.out.println(err); 134 } 135 } 136 137 149 public void add(File file, String filter, String prefix, String suffix) throws MalformedURLException { 150 if (!file.exists()) { 151 String 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 add(file.toURL()); 160 } 161 } else { 162 File [] 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 186 private boolean isMatching(File file, String prefix, String suffix) { 187 String 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 207 public void merge(JURLs jurl) { 208 for (Enumeration e = jurl.elements(); e.hasMoreElements();) { 209 URL url = (URL ) e.nextElement(); 210 if (!contains(url)) { 211 add(url); 212 } 213 } 214 } 215 216 222 public void remove(File file) throws MalformedURLException { 223 if (file.exists()) { 224 remove(file.toURL()); 225 } else { 226 String err = "Warning: Ressource " + file.getName(); 227 err += " cannot be removed : It doesn't exist"; 228 System.out.println(err); 229 } 230 } 231 232 236 public URL [] toURLs() { 237 return (URL []) super.toArray(new URL [elementCount]); 238 } 239 } | Popular Tags |