1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
|
/*
* @(#)ClassLoadHelper.java
*
* Copyright (C) 2000,,2003 2002 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package net.sourceforge.groboutils.util.classes.v1;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
/**
* Utility class for loading classes and creating instances. Much of the
* basic operation have been ripped from
* <tt>net.groboutils.util.classes.v1.ClassUtil</tt> in the GroboUtils package.
* If the helper's class loader is <tt>null</tt>, then it will use the Thread's
* context ClassLoader.
* <P>
* Note that resource loading is very tricky. Finding the right classloader
* and right methods to invoke is JDK dependent.
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @version $Date: 2003/02/10 22:52:36 $
* @since March 16, 2002
*/
public class ClassLoadHelper
{
private static final Logger LOG = Logger.getLogger(
SPILoader.class.getName() );
private static Method M_GET_CONTEXT_CLASSLOADER =
discoverContextClassloaderMethod();
private static Method M_GET_RESOURCES =
discoverGetResourcesMethod();
private static Method M_GET_SYSTEM_RESOURCES =
discoverGetSystemResourcesMethod();
private ClassLoader classLoader = null;
/**
* Default constructor - will use the Thread's context class loader for
* each class discovery.
*/
public ClassLoadHelper()
{
this( (ClassLoader)null );
}
/**
* Use the given class's classloader.
*
* @param clazz the class to pull the classloader from.
* @exception NullPointerException if <tt>clazz</tt> is <tt>null</tt>.
*/
public ClassLoadHelper( Class clazz )
{
this( clazz.getClassLoader() );
}
/**
* Loads the helper with the given class loader. If the given class loader
* is <tt>null</tt>, then it will use the Thread's context class loader.
*
* @param cl the classloader to pull all requested classes from, or
* it will use the thread's context class loader if <tt>cl</tt> is
* <tt>null</tt>.
* @see java.lang.Thread#getContextClassLoader()
*/
public ClassLoadHelper( ClassLoader cl )
{
this.classLoader = cl;
}
//----------------------------
// Public methods
/**
* Loads the requested class from the helper's classloader, and returns
* the Class instance, or <tt>null</tt> if the class could not be
* found.
*
* @param name the name of the class to load.
* @return the discovered Class, or <tt>null</tt> if it could not be found.
*/
public Class getClass( String name )
{
return getClass( name, true );
}
/**
* Loads the requested class from the helper's classloader, and returns
* the Class instance, or <tt>null</tt> if the class could not be
* found.
*
* @param name the name of the class to load.
* @param swallowExceptions <tt>true</tt> if this method is to return
* @return the discovered Class, or <tt>null</tt> if it could not be found
* and <tt>swallowExceptions</tt> is true.
* @exception IllegalStateException if there was an error during
* initialization and <tt>swallowExceptions</tt> is <tt>false</tt>.
*/
public Class getClass( String name, boolean swallowExceptions )
{
Class c = null;
if (name != null)
{
Throwable error = null;
try
{
c = loadClass( name );
}
catch (ClassNotFoundException cnfe)
{
error = cnfe;
}
catch (LinkageError le)
{
error = le;
}
catch (IllegalArgumentException iae)
{
error = iae;
}
if (error != null)
{
LOG.info( "getClass( "+name+" ) threw exception", error );
if (!swallowExceptions)
{
throw new IllegalStateException( error.toString() );
}
}
}
else
{
c = null;
}
return c;
}
/**
* Creates a new instance of the class with the given <tt>className</tt>
* using the default constructor. If there was an error during the
* creation, such as the class was not found, the class does not have
* a default constructor, or the constructor threw an exception, then
* <tt>null</tt> is returned.
*
* @param className the name of the class to create an instance.
* @return the new instance, or <tt>null</tt> if there was a problem.
* @see #getClass( String )
* @see #createObject( String, boolean )
* @see #createObject( Class )
* @see #createObject( Class, boolean )
*/
public Object createObject( String className )
{
return createObject( getClass( className ), true );
}
/**
* Creates a new instance of the class with the given <tt>className</tt>
* using the default constructor. If there was an error during the
* creation, such as the class was not found, the class does not have
* a default constructor, or the constructor threw an exception, then an
* IllegalStateException will be thrown only if <tt>swallowExceptions</tt>
* is <tt>false</tt>; otherwise, <tt>null</tt> will be returned.
*
* @param className the name of the class to create an instance.
* @param swallowExceptions <tt>true</tt> if this method is to return
* <tt>null</tt> on any exceptions, or <tt>false</tt> if it should
* throw an IllegalStateException on any error.
* @return the new instance.
* @exception IllegalStateException if there was an error during
* initialization and <tt>swallowExceptions</tt> is <tt>false</tt>.
* @see #getClass( String )
* @see #createObject( String )
* @see #createObject( Class )
* @see #createObject( Class, boolean )
*/
public Object createObject( String className, boolean swallowExceptions )
{
return createObject( getClass( className, swallowExceptions ),
swallowExceptions );
}
/**
* Creates an Object from the given Class, using its default constructor.
* All creation exceptions are swallowed. If the object could not
* be created, then <tt>null</tt> is returned.
*
* @param c the Class object from which a new instance will be created
* using its default constructor.
* @return the instantiated object, or <tt>null</tt> if <tt>c</tt> is
* <tt>null</tt>, or if there was an error during initialization.
*/
public Object createObject( Class c )
{
return createObject( c, true );
}
/**
* Creates an Object from the given Class, using its default constructor.
* If there was an error during the
* creation, such as the class was not found, the class does not have
* a default constructor, or the constructor threw an exception, then an
* IllegalStateException will be thrown only if <tt>swallowExceptions</tt>
* is <tt>false</tt>; otherwise, <tt>null</tt> will be returned.
*
* @param c the Class object from which a new instance will be created
* using its default constructor.
* @param swallowExceptions <tt>true</tt> if this method is to return
* <tt>null</tt> on any exceptions, or <tt>false</tt> if it should
* throw an IllegalStateException on any error.
* @return the instantiated object, or <tt>null</tt> if <tt>c</tt> is
* <tt>null</tt>, or if there was an error during initialization and
* <tt>swallowExceptions</tt> is <tt>true</tt>.
* @exception IllegalStateException if there was an error during
* initialization and <tt>swallowExceptions</tt> is <tt>false</tt>.
*/
public Object createObject( Class c, boolean swallowExceptions )
{
if (c == null)
{
return null;
}
Throwable error = null;
Object obj = null;
try
{
obj = c.newInstance();
}
catch (InstantiationException ie)
{
error = ie;
}
catch (IllegalAccessException iae)
{
error = iae;
}
catch (NoSuchMethodError nsme)
{
error = nsme;
}
if (error != null)
{
LOG.info( "createObject( "+c+" ) threw exception", error );
if (!swallowExceptions)
{
// Note about error.getMessage(): JDK 1.1 may have a 'null'
// error message thrown above. Some test cases have
// encountered this.
throw new IllegalStateException( "Could not instantiate " +
c.getName() + ": " + error.toString() );
}
}
// else
return obj;
}
/**
* Loads an object using the {@link #createObject( String, boolean )}
* method above, using the given System property's value as the
* class name. If the System property is not defined, then it resorts to
* the default class.
*
* @param propertyClassName the System Property name, whose value will be
* used as a fully-qualified Class name to load and instantiate and
* return.
* @param defaultClass if the System Property <tt>propertyClassName</tt>
* is not defined, then this will be the class to instantiate and
* return.
* @param swallowExceptions <tt>true</tt> if this method is to return
* <tt>null</tt> on any exceptions, or <tt>false</tt> if it should
* throw an IllegalStateException on any error.
* @return the instantiated class.
* @exception IllegalStateException if there was an error during
* initialization and <tt>swallowExceptions</tt> is <tt>false</tt>.
*/
public Object createObjectFromProperty( String propertyClassName,
Class defaultClass, boolean swallowExceptions )
{
return createObjectFromProperty( propertyClassName, defaultClass,
null, swallowExceptions );
}
/**
* Loads an object using the {@link #createObject( String, boolean )}
* method above, using the given Hashtable's property's value as the
* class name. If the Hashtable property is not defined, then it resorts to
* the default class. If the Hashtable is <tt>null</tt>, then the
* System property will be used instead.
*
* @param propertyClassName the System Property name, whose value will be
* used as a fully-qualified Class name to load and instantiate and
* return.
* @param defaultClass if the System Property <tt>propertyClassName</tt>
* is not defined, then this will be the class to instantiate and
* return.
* @param properties a Hashtable of String -> String mappings.
* @param swallowExceptions <tt>true</tt> if this method is to return
* <tt>null</tt> on any exceptions, or <tt>false</tt> if it should
* throw an IllegalStateException on any error.
* @return the instantiated class.
* @exception IllegalStateException if there was an error during
* initialization and <tt>swallowExceptions</tt> is <tt>false</tt>.
*/
public Object createObjectFromProperty( String propertyClassName,
Class defaultClass, Hashtable properties,
boolean swallowExceptions )
{
Object o = null;
String cname = null;
if (properties == null)
{
cname = System.getProperty( propertyClassName );
}
else
{
cname = (String)properties.get( propertyClassName );
}
if (cname == null)
{
o = createObject( defaultClass, swallowExceptions );
}
else
{
o = createObject( cname, swallowExceptions );
}
return o;
}
/**
* Loads a resource with the given name, using the correct ClassLoader.
* Does not swallow exceptions. See the JDK documentation on resources
* (they are pretty much files that are in the classpath of the
* classloader). Yes, this can be used successfully to get a class file
* (well, JDK 1.1 throws a SecurityException if this is attempted).
*
* @param name absolute referece to the expected resource.
* @return the resource as an InputStream, which may possibly be
* <tt>null</tt>.
* @exception IOException if an I/O error occurs.
* @see java.lang.ClassLoader#getResource( String )
* @see java.lang.ClassLoader#getResourceAsStream( String )
*/
public InputStream getResourceAsStream( String name )
throws IOException
{
ClassLoader cl = getClassLoader();
InputStream is = null;
if (cl == null)
{
name = getAbsoluteResourceName( name );
is = this.getClass().getResourceAsStream( name );
}
else
{
is = cl.getResourceAsStream( name );
}
return is;
}
/**
* Loads a resource with the given name, using the correct ClassLoader.
* Does not swallow exceptions. See the JDK documentation on resources
* (they are pretty much files that are in the classpath of the
* classloader). Yes, this can be used successfully to get a class file
* (well, JDK 1.1 throws a SecurityException if this is attempted).
*
* @param name absolute referece to the expected resource.
* @return the resource name as an URL, which may possibly be
* <tt>null</tt>.
* @exception IOException if an I/O error occurs.
* @see java.lang.ClassLoader#getResource( String )
* @see java.lang.ClassLoader#getResourceAsStream( String )
*/
public URL getResource( String name )
throws IOException
{
return getResource( name, getClassLoader() );
}
/**
* Loads a resource with the given name, using the given ClassLoader.
* Does not swallow exceptions. See the JDK documentation on resources
* (they are pretty much files that are in the classpath of the
* classloader). Yes, this can be used successfully to get a class file
* (well, JDK 1.1 throws a SecurityException if this is attempted).
*
* @param name absolute referece to the expected resource.
* @param cl the classloader to load the reference from.
* @return the resource name as an URL, which may possibly be
* <tt>null</tt>.
* @exception IOException if an I/O error occurs.
* @see java.lang.ClassLoader#getResource( String )
* @see java.lang.ClassLoader#getResourceAsStream( String )
*/
public URL getResource( String name, ClassLoader cl )
throws IOException
{
LOG.debug("Enter getResource( "+name+" )");
URL url = null;
if (cl != null)
{
// JDK 1.2+ can be awfully fickle. Allow for an alternative
// if this fails, through the use of the next IF statement.
url = cl.getResource( name );
}
if (url == null)
{
name = getAbsoluteResourceName( name );
url = this.getClass().getResource( name );
}
LOG.debug("Exit getResource( "+name+" ) = '"+url+"'");
return url;
}
/**
* Loads a resource with the given name, using the correct ClassLoader.
* Does not swallow exceptions. See the JDK documentation on resources
* (they are pretty much files that are in the classpath of the
* classloader). Yes, this can be used successfully to get a class file
* (well, JDK 1.1 throws a SecurityException if this is attempted).
*
* @param name absolute referece to the expected resource.
* @return the resource name as an URL, which may possibly be
* <tt>null</tt>.
* @exception IOException if an I/O error occurs.
* @see java.lang.ClassLoader#getResource( String )
* @see java.lang.ClassLoader#getResourceAsStream( String )
*/
public URL getSystemResource( String name )
throws IOException
{
LOG.debug("Enter getSystemResource( "+name+" )");
URL url = ClassLoader.getSystemResource( name );
LOG.debug("Exit getSystemResource( "+name+" ) = '"+url+"'");
return url;
}
/**
* Loads a resource with the given name, using the correct ClassLoader.
* Does not swallow exceptions. See the JDK documentation on resources
* (they are pretty much files that are in the classpath of the
* classloader). Yes, this can be used successfully to get a class file
* (well, JDK 1.1 throws a SecurityException if this is attempted).
*
* @param name absolute referece to the expected resource.
* @return the list of resource URLs, which may NOT be <tt>null</tt>
* (implementation ensures it is not null).
* @exception IOException if an I/O error occurs.
* @see java.lang.ClassLoader#getResource( String )
* @see java.lang.ClassLoader#getResources( String )
* @see java.lang.ClassLoader#getResourceAsStream( String )
*/
public Enumeration getResources( String name )
throws IOException
{
return getResources( name, getClassLoader() );
}
/**
* Loads a resource with the given name, using the correct ClassLoader.
* Does not swallow exceptions. See the JDK documentation on resources
* (they are pretty much files that are in the classpath of the
* classloader). Yes, this can be used successfully to get a class file
* (well, JDK 1.1 throws a SecurityException if this is attempted).
*
* @param name absolute referece to the expected resource.
* @param cl the classloader to load the references from.
* @return a non-null list of resource URLs for the resource name.
* @exception IOException if an I/O error occurs.
* @see java.lang.ClassLoader#getResource( String )
* @see java.lang.ClassLoader#getResources( String )
* @see java.lang.ClassLoader#getResourceAsStream( String )
*/
public Enumeration getResources( String name, ClassLoader cl )
throws IOException
{
Enumeration enum = null;
if (M_GET_RESOURCES != null && cl != null)
{
try
{
LOG.debug("Getting resources for "+name);
enum = (Enumeration)M_GET_RESOURCES.invoke( cl,
new Object[] { name } );
//LOG.debug("Found resources "+enum);
}
catch (java.lang.reflect.InvocationTargetException ite)
{
Throwable t = ite.getTargetException();
if (t instanceof IOException)
{
throw (IOException)t;
}
// else
LOG.info( "getResources( "+name+" ) threw exception.", t );
enum = null;
}
catch (Exception e)
{
LOG.info( "invoke on getResources( "+name+
" ) threw exception.", e );
enum = null;
}
}
// Yes, for invalid resources this will be incredibly inefficient.
// However, we want to be as robust as possible for those resource
// names that ARE valid. JDK 1.2+ can be sooo fickle.
if (enum == null || !enum.hasMoreElements())
{
LOG.debug("Resource enum is null or contains nothing.");
// Try system resources next
enum = getSystemResources( name );
if (enum == null || !enum.hasMoreElements())
{
// Try a single resource last
Vector v = new Vector();
URL url = getResource( name, cl );
if (url != null)
{
LOG.debug( "classloader getResource returned "+url );
v.addElement( url );
}
// else give up
enum = v.elements();
}
}
return enum;
}
/**
* Get the resource associated with the given name from the System
* classloader. This will never return <tt>null</tt>.
*
* @param name absolute referece to the expected resource.
* @return a non-null list of URLs matching the resource.
* @exception IOException if an I/O error occurs.
* @see java.lang.ClassLoader#getResource( String )
* @see java.lang.ClassLoader#getResources( String )
* @see java.lang.ClassLoader#getResourceAsStream( String )
*/
public Enumeration getSystemResources( String name )
throws IOException
{
Enumeration enum = null;
if (M_GET_SYSTEM_RESOURCES != null)
{
try
{
LOG.debug("Getting system resources for "+name);
// static method
enum = (Enumeration)M_GET_SYSTEM_RESOURCES.invoke( null,
new Object[] { name } );
//LOG.debug("Found system resources "+enum);
}
catch (java.lang.reflect.InvocationTargetException ite)
{
Throwable t = ite.getTargetException();
if (t instanceof IOException)
{
throw (IOException)t;
}
// else
LOG.info( "getSystemResources( "+name+
" ) threw exception.", t );
enum = null;
}
catch (Exception e)
{
LOG.info( "invoke on getResources( "+name+
" ) threw exception.", e );
enum = null;
}
}
// Yes, for invalid resources this will be incredibly inefficient.
// However, we want to be as robust as possible for those resource
// names that ARE valid.
if (enum == null || !enum.hasMoreElements())
{
LOG.debug("Resource enum is null or contains nothing.");
Vector v = new Vector();
URL url = getSystemResource( name );
if (url != null)
{
LOG.debug( "classloader getSystemResource returned "+url );
v.addElement( url );
}
// else give up
enum = v.elements();
}
return enum;
}
//----------------------------
// Protected methods
/**
* Gets the correct class loader. May return null.
*
* @return the ClassLoader
*/
protected ClassLoader getClassLoader()
{
ClassLoader cl = this.classLoader;
if (cl == null)
{
cl = getThreadClassLoader( Thread.currentThread() );
if (cl == null)
{
// JDK 1.1 may return NULL here.
cl = this.getClass().getClassLoader();
}
}
return cl;
}
/**
* Loads a class with the given name, using the correct ClassLoader.
* Does not swallow exceptions.
*
* @exception ClassNotFoundException if the class name is not known by the
* class loader.
* @exception LinkageError if there was a basic class loader error.
* @exception IllegalArgumentException if the class doesn't smell right to
* JDK 1.1.
*/
protected Class loadClass( String name )
throws ClassNotFoundException, LinkageError,
IllegalArgumentException
{
ClassLoader cl = getClassLoader();
Class c = null;
if (name != null)
{
if (cl == null)
{
c = Class.forName( name );
}
else
{
c = cl.loadClass( name );
}
}
return c;
}
/**
* Use reflection to get the thread (context) class loader.
*/
protected static ClassLoader getThreadClassLoader( Thread t )
{
ClassLoader cl = null;
if (M_GET_CONTEXT_CLASSLOADER != null)
{
try
{
cl = (ClassLoader)M_GET_CONTEXT_CLASSLOADER.invoke(t, null);
}
catch (Exception e)
{
cl = null;
}
}
return cl;
}
/**
*
*/
protected static Method discoverContextClassloaderMethod()
{
Method m;
try
{
Class c = Thread.class;
m = c.getDeclaredMethod( "getContextClassLoader", new Class[0] );
}
catch (Exception e)
{
// discovery method: exception is expected where this is not
// supoorted.
// LOG.info( "discoverContextClassloaderMethod() threw exception.",
// e );
m = null;
}
return m;
}
/**
*
*/
protected static Method discoverGetResourcesMethod()
{
Method m;
try
{
Class c = ClassLoader.class;
m = c.getDeclaredMethod( "getResources",
new Class[] { String.class } );
}
catch (Exception e)
{
// discovery method: exception is expected where this is not
// supoorted.
// LOG.info( "discoverGetResourcesMethod() threw exception.",
// e );
m = null;
}
return m;
}
/**
*
*/
protected static Method discoverGetSystemResourcesMethod()
{
Method m;
try
{
Class c = ClassLoader.class;
m = c.getDeclaredMethod( "getSystemResources",
new Class[] { String.class } );
}
catch (Exception e)
{
// discovery method: exception is expected where this is not
// supoorted.
// LOG.info( "discoverGetSystemResourcesMethod() threw exception.",
// e );
m = null;
}
return m;
}
/**
*
*/
protected String getAbsoluteResourceName( String name )
{
// Using 'Class' getResourceAsStream, which is relative to its
// package, whereas the ClassLoader is an absolute name. So,
// ensure that the name is absolute to be compatible.
if (name != null && name.length() > 0 && name.charAt( 0 ) != '/')
{
name = '/' + name;
}
return name;
}
}
|