Hi,

Jay SenSharma

Jay SenSharma

WebLogic 10.3.1 (11g) provides a new excellent feature of resource reloading (or we can say resource filtering). This feature provides us the facility to reload the non-class resources to be filtered while loading these resources in to the WebLogic container.
To utilize this feature of WebLogic Server we can use a new tag added as part of WebLogic 10.3.1 “weblogic-application.xml” file <prefer-application-resources>

A Sample application here demonstrate that if a different version of JAR file which is already loaded as part of WebLogic Class path (which contains some resources like XMLs, XSDs, Properties…etc) then how can an application instruct the WebLogic Container to load this JAR resources from the Application AND NOT from the WebLogic class path.

Step 1).  Develop a JAR file which contains a JAR File “Test.jar” which contains a XML file “log3j.xml” This may be any XML file (for Example)like below

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>

<log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/”>
<appender>
<param value=”‘.’yyyy-MM-dd”/>
<param value=”/apps/logsDir/SOAEngine/ServiceOnDemand/SOAEngineLogs.log”/>
<param value=”true”/>
<layout>
<param value=”%d [%t] %p – %m%n”/>
</layout>
</appender>
<root>
<priority value =”debug”/>
<appender-ref ref=”appender”/>
</root>
</log4j:configuration>

Step 2). Develop a Web Application with following “index.jsp” file

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<%@ page contentType=”text/html;charset=windows-1252″%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″/>
<title>index</title>
</head>
<body>
<a href=”resourceTest.jsp”>Click Here For Resource Test</a>
</body>
</html>

Step 3). Write an other JSP file “resourceTest.jsp” inside resource.war  web application  like following:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<%@ page contentType=”text/html;charset=windows-1252″%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″/>
<title>resourceTest</title>
</head>
<body>
getResource(“log4j.xml”)  outout:
<BR><BR>
<%
java.net.URL url=Thread.currentThread().getContextClassLoader().getResource(“log4j.xml”);
out.println(“<h3>Got log4j.html in url: “+url);
%>
<HR>
getResource<b>s</b>(“log4j.xml”)  outout:
<%
java.util.Enumeration en=Thread.currentThread().getContextClassLoader().getResources(“log4j.xml”);
while(en.hasMoreElements())
{
java.net.URL tempUrl =(java.net.URL)en.nextElement();
out.println(“<BR><h3>Got log4j.html in url: “+tempUrl);
}
%>
</body>
</html>

Step 4). Web.xml will be emprt like following :

<?xml version = ‘1.0’ encoding = ‘windows-1252’?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” si:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” version=”2.5″ xmlns=”http://java.sun.com/xml/ns/javaee”/>

Step 5). Now Create an EAR file that contains the “Test.jar” file inside the “APP-INF/lib” directory and a WebApplication “resource.war” which contains “resourceTest.jsp” and “index.jsp”.

We need to write the following entry in “application.xml” file.
<?xml version=”1.0″ encoding=”UTF-8″?>
<application xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” version=”1.4″>
<description>Resource Loading Test</description>
<module>
<web>
<web-uri>resources</web-uri>
<context-root>resources</context-root>
</web>
</module>
</application>

Step 6). The Most important step to test resource Filtering …is to provide a “weblogic-application.xml” file with the following entry in it.

<?xml version=’1.0′ encoding=’ISO-8859-1′?>
<weblogic-application xmlns=”http://xmlns.oracle.com/weblogic/weblogic-application” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.0/weblogic-application.xsd”>

<prefer-application-resources>
<resource-name>log4j.xml</resource-name>
</prefer-application-resources>

</weblogic-application>

Step 7). Now add the “Test.jar” file in the Server CLASSPATH somewhere at the beginning or at the end of the class path and then deploy the Application then see the difference in the output of “resource Test.jsp” page…When  the following tag is available in “weblogic-application.xml” and when it is now available as part of “weblogic-application.xml” file.

<prefer-application-resources>
<resource-name>log4j.xml</resource-name>
</prefer-application-resources>

Thanks

Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.