Bug 1024853 - Enhancements to "Programmatically Load Classes and Resources in a Deployment" doc
Enhancements to "Programmatically Load Classes and Resources in a Deployment"...
Status: CLOSED CURRENTRELEASE
Product: JBoss Enterprise Application Platform 6
Classification: JBoss
Component: Documentation (Show other bugs)
6.2.0
Unspecified Unspecified
unspecified Severity unspecified
: GA
: EAP 6.2.0
Assigned To: sgilda
Russell Dickenson
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2013-10-30 10:04 EDT by David M. Lloyd
Modified: 2014-09-29 14:31 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Build Name: 14875, Development Guide-6.2-1 Build Date: 30-10-2013 10:11:06 Topic ID: 24376-549480 [Latest]
Last Closed: 2013-12-15 11:48:47 EST
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description David M. Lloyd 2013-10-30 10:04:19 EDT
Title: Programmatically Load Classes and Resources in a Deployment

Describe the issue:
A couple of minor problems.

Suggestions for improvement:

1. Class targetClass = Class.forName("TargetClass", true, this.getClass().getClassLoader()); <-- should include a package name.

2. In "Load All Instances of a Single Resource", the text "use the Class.getClassLoader().getResources(String resourceName) method, where resource name is the fully qualified path the the resource" should be changed to "use the Class.getClassLoader().getResources(String resourceName) method, where *resourceName* is the fully qualified path *of* the resource" (emphasis added) ("resourceName" is a literal parameter name)

3. In "Load All Instances of a Single Resource" ... " returns an Enumeration of URL objects" is correct but the code sample then uses an incorrect construct (probably my fault, sorry) consisting of a URL[].  Change this code:

  URL[] urls = MyClass.class.getClassLoader().getResources("full/path/to/resource");
  while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      ...

To:

  Enumeration<URL> urls = MyClass.class.getClassLoader().getResources("full/path/to/resource");
  while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      ...

4. The last section "Load a Class File From the Class Loader" is incorrect.  Change this code:

  InputStream inputStream = TargetClass.class.getResourceAsStream(TargetClass.class.getSimpleName() + ".class");

To:

  InputStream inputStream = TargetClass.class.getResourceAsStream(TargetClass.class.getSimpleName().replace('.', '/') + ".class");

5. In the "Load a Class File From the Class Loader" I also recommend changing this:

  InputStream inputStream = TargetClass.class.getClassLoader().getResourceAsStream("TargetClass".replace('.', '/') + ".class");

To:

  String className = "com.yourcompany.TargetClass";
  InputStream inputStream = TargetClass.class.getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");

...because in the given example the replacement is not necessary (the class name has no '.' in it), and in any case if you already have the class name it would be more logical to simply do the replacement by hand yielding "com/yourcompany/TargetClass.class" for example.
Comment 1 sgilda 2013-10-30 11:37:47 EDT
Good catches. Thanks!

Made the changes to topic 24376.
Comment 4 Pavel Jelinek 2013-11-14 04:13:10 EST
Verified. 
There is one more typo and I created new BZ for it. See https://bugzilla.redhat.com/show_bug.cgi?id=1030262

Note You need to log in before you can comment on or make changes to this bug.