1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| /**
* Recharge tout les bundles
*/
public boolean reloadBundles() {
boolean ret = false;
try {
clearMap(ResourceBundle.class, null, "cacheList");
// now, for the true and utter hack, if we're running in tomcat,
// clear
// it's class loader resource cache as well.
ret = clearTomcatCache();
} catch (Exception e) {
if (LOG.isErrorEnabled()) {
LOG.error("Could not reload resource bundles", e);
}
}
return ret;
}
/**
* Efface le cache (les ressources) du classLoader de Tomcat
*/
private boolean clearTomcatCache() {
boolean ret = false;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
// no need for compilation here.
Class cl = loader.getClass();
try {
if ("org.apache.catalina.loader.WebappClassLoader".equals(cl.getName())) {
clearMap(cl, loader, "resourceEntries");
} else {
if (LOG.isWarnEnabled()) {
LOG.warn("class loader " + cl.getName()
+ " is not tomcat loader.");
}
}
ret = true;
} catch (Exception e) {
if (LOG.isErrorEnabled()) {
LOG.error("couldn't clear tomcat cache", e);
}
}
return ret;
}
private void clearMap(Class cl, Object obj, String name)
throws NoSuchFieldException, IllegalAccessException,
NoSuchMethodException, InvocationTargetException {
Field field = cl.getDeclaredField(name);
field.setAccessible(true);
Object cache = field.get(obj);
Class ccl = cache.getClass();
Method sizeMethod = ccl.getMethod("size", (Class[]) null);
if (LOG.isInfoEnabled()) {
LOG.info(name + ": size before clear: "
+ sizeMethod.invoke(cache, (Object[]) null));
}
Method clearMethod = ccl.getMethod("clear", (Class[]) null);
clearMethod.invoke(cache, (Object[]) null);
if (LOG.isInfoEnabled()) {
LOG.info(name + ": size after clear: "
+ sizeMethod.invoke(cache, (Object[]) null));
}
} |
Partager