getContextClassLoader().getResource(“”) return null on Weblogic while return path value on JBoss

By : nahab
Source: Stackoverflow.com
Question!

Our team make migration of app from JBoss 4.2 to Weblogic 10.3.4.

There is some logic in application that requires to know ear file name. For this purpose uses code below:

URL url = Thread.currentThread().getContextClassLoader().getResource("");

This code is executing during deploy phase during Spring initialization.

And on JBoss this works fine but on Weblogic getResource("") return null in this case.

So my question is why this does not work in Weblogic?


Additionaly I've created small test.jsp pages with only few lines of code that return value of Thread.currentThread().getContextClassLoader().getResource("") and this works on Weblogic. So I made small table to show this situation in more clear way:

+----------+-------------+------------------------------+
|          | on JSP page | during spring initialization |
+----------+-------------+------------------------------+
| JBoss    | works       | works                        |
| Weblogic | works       | returns null                 |
+----------+-------------+------------------------------+

Structure of application is next. getResource("") code is placed inside my.jar:

my.ear
|
+-my.war
  |
  +-WEB-INF
  | |
  | +-lib
  |   |
  |   +-my.jar
  |   +-org.springframework.aop-3.0.5.RELEASE.jar
  |   +-org.springframework.beans-3.0.5.RELEASE.jar
  |   +-org.springframework.context.support-3.0.5.RELEASE.jar
  |   +-org.springframework.context-3.0.5.RELEASE.jar
  |   +-org.springframework.core-3.0.5.RELEASE.jar
  |   +-other jars
  |
  +-test.jsp
  +-jsp pages    
By : nahab


Answers

The same issue I was facing when calling service from ear application on WLS 10.3.5. If you will give resource name with path Thread.currentThread().getContextClassLoader().getResource("META-INF/wsdls/test.wsdl"); this will give correct URL path like my.jar!META-INF/wsdls/test.wsdl. But not able to load wsdl.

By : Sanjay


on JBoss this works fine but on Weblogic getResource("") return null in this case.

This isn't altogether surprising, since getResource("") doesn't really make any sense. An empty String is not a resource path.

My guess is the JBoss is simply appending the argument on to the end of the base directory and then resolving that as a file, without doing any sanity checking on the value. Weblogic may be doing something more rigorous.

Whatever to reason, you need to find a more robust method to locate the base EAR directory, one that uses formal APIs in the manner in which the designers intended.

Edit: To my knowledge, the JavaEE APIs do not provide a way to do what you want. You're going to have to rely on appserver-specific logic, either using your getResource fudge, or querying e.g. JMX MBeans embedded in the server. You'd need to auto-detect the server type at runtime if you wanted your app to work on both - or change the app so that it doesn't need the information at all.

By : skaffman


This video can help you solving your question :)
By: admin