Bug 1024853

Summary: Enhancements to "Programmatically Load Classes and Resources in a Deployment" doc
Product: [JBoss] JBoss Enterprise Application Platform 6 Reporter: David M. Lloyd <david.lloyd>
Component: DocumentationAssignee: sgilda
Status: CLOSED CURRENTRELEASE QA Contact: Russell Dickenson <rdickens>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.2.0CC: pjelinek
Target Milestone: GA   
Target Release: EAP 6.2.0   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
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 16:48:47 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description David M. Lloyd 2013-10-30 14:04:19 UTC
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 15:37:47 UTC
Good catches. Thanks!

Made the changes to topic 24376.

Comment 4 Pavel Jelinek 2013-11-14 09:13:10 UTC
Verified. 
There is one more typo and I created new BZ for it. See https://bugzilla.redhat.com/show_bug.cgi?id=1030262