Refresh Properties at Run Time (Instead of Restarting the Server)

Refresh Properties at Run Time (Instead of Restarting the Server)

When developing with WCS, having to restart the server takes up a lot of time and really drags out the amount of time needed to test. If you follow IBM’s best practices, even sites with only one language still use properties files, and any changes to those typically means the servers needs restarted.

On a project using version 7 FeP 7, I got tired of having to restart all the time, and found code to refresh the properties at run time rather than having to restart the server.

However, after pasting the code into my JSP, I got the following no such field exception:

java.lang.NoSuchFieldException: cacheList
at java.lang.Class.getDeclaredFieldImpl(Native Method)
at java.lang.Class.getDeclaredField(Class.java:625)
at com.ibm._jsp._JustinRPennington._jspService(_JustinRPennington.java:414)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1700)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1635)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:149)

After a little tweaking, I was finally able to get the code to work and refresh the properties:

<%@ page import="java.util.*, java.lang.reflect.*"%>
<% 
Class klass = Class.forName("java.util.ResourceBundle");
Field field = klass.getDeclaredField("cache");
Field[] fieldz = klass.getDeclaredFields();
field.setAccessible(true);
WeakHashMap cache = (WeakHashMap)field.get(null);
cache.clear();
field.setAccessible(false);
%>

One solution is to add the code to a JSP and hit the URL directly to refresh the properties files. I usually add the code to the top of the page I am working on so that I can just refresh that page to see the latest changes. Just remember to remove the code before committing it!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *