1 2 24 25 package com.lutris.classloader; 26 27 import java.io.File ; 29 import java.net.URL ; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 import com.lutris.logging.LogChannel; 34 import com.lutris.util.FatalExceptionError; 35 36 46 class ClassPath { 47 48 50 51 private Vector classPath = null; 52 53 54 private boolean loggingEnabled = false; 55 56 57 private LogChannel logChannel; 59 61 62 private int logLevel; 64 66 68 75 public ClassPath(LogChannel loadLogChannel) { 77 this((Vector )null, loadLogChannel); 79 } 82 83 91 public ClassPath(String [] entries, LogChannel loadLogChannel) { 93 this(convertArrayToVector(entries, loadLogChannel), loadLogChannel); 95 } 98 99 106 public ClassPath(File [] entries, LogChannel loadLogChannel) { 108 this(convertArrayToVector(entries, loadLogChannel), loadLogChannel); 110 } 113 114 122 public ClassPath(URL [] entries, LogChannel loadLogChannel) { 124 this(convertArrayToVector(entries, loadLogChannel), loadLogChannel); 126 } 129 130 132 140 private ClassPath(Vector entries, LogChannel loadLogChannel) { 142 147 classPath = new Vector (); 148 set(entries); 149 150 logChannel = loadLogChannel; 152 if (logChannel != null) { 154 logLevel = logChannel.getLevel(MultiClassLoader.LOG_LEVEL); 156 loggingEnabled = logChannel.isEnabled(logLevel); 158 } 160 } 169 170 172 179 public void set(String [] entries) { 180 set(convertArrayToVector(entries, logChannel)); 182 } 184 185 191 public void set(File [] entries) { 192 set(convertArrayToVector(entries, logChannel)); 194 } 196 197 204 public void set(URL [] entries) { 205 set(convertArrayToVector(entries, logChannel)); 207 } 209 210 217 public void add(String [] entries) { 218 add(convertArrayToVector(entries, logChannel)); 220 } 222 223 229 public void add(File [] entries) { 230 add(convertArrayToVector(entries, logChannel)); 232 } 234 235 242 public void add(URL [] entries) { 243 add(convertArrayToVector(entries, logChannel)); 245 } 247 248 254 public void clear() { 255 for (int i=0; i<classPath.capacity(); i++){ 256 try { 257 ClassPathEntry cpe = (ClassPathEntry)classPath.elementAt(i); 258 if (cpe.isZipFile()){ 259 cpe.getZipFile().close(); 260 } 261 }catch(Exception ex){} 262 } 263 classPath.removeAllElements(); 264 } 265 266 271 public int getLength() { 272 return classPath.size(); 273 } 274 275 282 public Enumeration getPath() { 283 return classPath.elements(); 284 } 285 286 296 public Resource getResource(String name) { 297 298 if (name == null) { 299 throw new NullPointerException ("Null resource name passed to " + 300 "getResource() for class path, " + this); 301 } 302 Resource resource = null; 303 for (int i = 0; i < classPath.size(); i++) { 304 ClassPathEntry entry = null; 305 entry = (ClassPathEntry)classPath.elementAt(i); 306 307 if (loggingEnabled) { 308 logChannel.write(logLevel, " checking: \"" + entry.getName() 310 + "\""); 312 } 313 resource = entry.getResource(name); 314 if (resource != null) { 315 if (loggingEnabled) { 316 logChannel.write(logLevel, " found: " + name); 318 } 320 return resource; 321 } 322 } 323 if (loggingEnabled) { 324 logChannel.write(logLevel, " not found: " + name); 326 } 328 return null; 329 } 330 331 332 334 342 private void set(Vector entries) { 343 347 classPath.removeAllElements(); 348 add(entries); 349 } 350 351 359 private void add(Vector entries) { 360 364 if (entries != null) { 365 for (int i = 0; i < entries.size(); i++) { 366 classPath.insertElementAt(entries.elementAt(i), i); 367 } 368 } 369 } 370 371 379 private static Vector convertArrayToVector(Object [] array, 380 LogChannel loadLogChannel) { 382 if (array != null) { 385 Vector vector = new Vector (); 386 for (int i = 0; i < array.length; i++) { 387 Object object = array[i]; 388 ClassPathEntry entry = null; 389 if (object instanceof String ) { 390 entry = new ClassPathEntry((String )object, loadLogChannel); 392 } else if (object instanceof File ) { 394 entry = new ClassPathEntry((File )object, loadLogChannel); 396 } else if (object instanceof URL ) { 398 entry = new ClassPathEntry((URL )object, loadLogChannel); 400 } else { 402 throw new FatalExceptionError(new ClassCastException ( 405 "Type, " + object.getClass() + ", is not supported. " + 406 "Expecting a String, File, or URL.")); 407 } 408 if (entry != null && ! vector.contains(entry)) { 409 vector.addElement(entry); 410 } 411 } 412 return vector; 413 } 414 return null; 415 } 416 } 417 418 | Popular Tags |