1 2 24 25 26 27 28 29 package com.lutris.classloader; 30 31 import java.io.File ; 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.util.zip.ZipFile ; 39 40 import com.lutris.logging.LogChannel; 41 42 156 public class ClassPathEntry { 157 161 162 164 165 private URL entryURL = null; 166 167 168 private ZipFile zipFile = null; 169 170 171 private boolean loggingEnabled = false; 172 173 174 private LogChannel logChannel; 176 178 179 private int logLevel; 181 183 184 private boolean isZipFile; 185 186 188 211 public ClassPathEntry(String entry, LogChannel loadLogChannel) { 213 this(convertEntryToURL(entry), loadLogChannel); 215 } 218 219 235 public ClassPathEntry(File entry, LogChannel loadLogChannel) { 237 this(convertEntryToURL(entry), loadLogChannel); 239 } 242 243 260 public ClassPathEntry(URL entry, LogChannel loadLogChannel) { 262 267 try { 268 this.logChannel = loadLogChannel; 270 if (logChannel != null) { 272 this.logLevel = logChannel.getLevel(MultiClassLoader.LOG_LEVEL); 274 loggingEnabled = logChannel.isEnabled(logLevel); 276 } 285 this.entryURL = new URL (cleanUpURL(entry).toString()); 286 } catch (MalformedURLException mue) { 287 if (loggingEnabled) { 288 logChannel.write(logLevel, "Illegal class path entry: " + entry); 290 } 292 this.entryURL = null; 293 } 294 initIsZipFile(); 295 } 296 297 299 304 public URL getURL() { 305 return entryURL; 306 } 307 308 317 public String getName() { 318 return entryURL.getFile(); 320 } 321 322 329 public String getLocation() { 330 if (entryURL.getPort() != -1) { 331 return entryURL.getProtocol() + "://" + entryURL.getHost() + ":" + 332 entryURL.getPort() + "/"; 333 } else { 334 return entryURL.getProtocol() + "://" + entryURL.getHost() + "/"; 335 } 336 } 337 338 343 public String toString() { 344 if (entryURL != null) { 345 return entryURL.toString(); 346 } else { 347 return "null"; 348 } 349 } 350 351 360 public Resource getResource(String name) { 361 name = convertSlashes(name); 362 Resource resource = null; 367 try { 368 if (isDirectory() && isLocal()) { 369 resource = new LocalDirResource(name, this, logChannel); 371 } else if (isZipFile() && isLocal()) { 373 resource = new LocalZipResource(name, this, logChannel); 375 } else if (isDirectory() && isRemote()) { 377 if (loggingEnabled) { 380 logChannel.write(logLevel, "Cannot get remote directory resource."); 382 } 384 resource = null; 385 } else if (isZipFile() && isRemote()) { 386 if (loggingEnabled) { 389 logChannel.write(logLevel, "Cannot get remote jar file resource."); 391 } 393 resource = null; 394 } 395 } catch (FileNotFoundException e) { 396 if (loggingEnabled) { 397 logChannel.write(logLevel, "File not found: " + name 401 + ": " + e.getClass().getName() + ": " 403 + e.getMessage()); 404 } 405 resource = null; 406 } 407 return resource; 408 } 409 410 416 public boolean isZipFile() { 417 return isZipFile; 418 } 420 421 424 private void initIsZipFile() { 425 isZipFile = (toString().endsWith(".jar") || toString().endsWith(".zip")); 426 } 427 428 435 public boolean isDirectory() { 436 return (!isZipFile()); 437 } 438 439 445 public boolean isLocal() { 446 String host = entryURL.getHost(); 447 if (host.equals("") || host.equalsIgnoreCase("localhost") || 448 host.equals("127.0.0.1")) { 449 return true; 450 } 451 return false; 452 } 453 454 461 public boolean isRemote() { 462 return (!isLocal()); 463 } 464 465 472 public boolean equals(Object o) { 473 if (o instanceof ClassPathEntry) { 474 return entryURL.equals(((ClassPathEntry)o).getURL()); 475 } 476 return false; 477 } 478 479 486 public ZipFile getZipFile() { 487 if (zipFile == null) { 488 return doGetZipFile(); 489 } 490 return zipFile; 491 } 492 493 500 private synchronized ZipFile doGetZipFile() { 501 if (zipFile == null) { 502 if (isZipFile() && isLocal()) { 503 try { 504 zipFile = new ZipFile (getName()); 505 } catch (IOException e) { 506 if (loggingEnabled) { 507 logChannel.write(logLevel, "Cannot create zip file " + getName() + ".", e); 509 } 512 zipFile = null; 513 } 514 } else { 515 zipFile = null; 516 } 517 } 518 return zipFile; 519 } 520 522 530 private static URL cleanUpURL(URL url) { 531 URL newURL = null; 532 try { 533 String urlString = url.toString(); 535 if (!urlString.endsWith("/") && !urlString.endsWith(".jar") && 536 !urlString.endsWith(".zip")) { 537 newURL = new URL (convertSlashes(url.toString() + "/")); 538 } else { 539 newURL = new URL (convertSlashes(url.toString())); 540 } 541 } catch (MalformedURLException e) { 542 newURL = null; 545 } 546 return newURL; 547 } 548 549 558 private static URL convertEntryToURL(Object object) { 559 if (object instanceof java.net.URL ) { 560 return (URL )object; 561 } else if (object instanceof java.lang.String ) { 562 return convertEntryToURL((String )object); 563 } else if (object instanceof java.io.File ) { 564 return convertEntryToURL(object.toString()); 565 } else { 566 return null; 569 } 570 } 571 572 578 private static URL convertEntryToURL(String string) { 579 try { 580 return new URL (string); 581 } catch (MalformedURLException e) { 582 try { 583 String absPath = (new File (string)).getAbsolutePath(); 584 absPath = absPath.replace(File.separatorChar, '/'); 587 if (!absPath.startsWith("/")) { 590 absPath = "/" + absPath; 591 } 592 return new URL ("file://" + absPath); 593 } catch (MalformedURLException e2) { 594 return null; 596 } 597 } 598 } 599 600 607 private static String convertSlashes(String string) { 608 string.replace(File.separatorChar, '/'); 609 return string.replace('\\', '/'); 610 } 611 } 612 | Popular Tags |