java.lang.NoSuchFieldException: resourceEntries struts2

Keywords: Programming Tomcat Java Apache Struts

1. About using struts2 to report java.lang.NoSuchFieldException: resourceEntries in the project.

When we use Tomcat version 7.0.64 and below, there will be no problem. When we use tomcat 7.0.65, we will report this question. Many of the answers we see online are about tomcat 8 and so on.

The actual problems began at tomcat 7.0.65. The root cause of this problem is that. From 64 to 65, the class org apache catalina loader WebappClassLoader. java was abstracted, and the class org apache catalina loader WebappClassLoader. java was abstracted.

2. There is a problem in clearing tomcat caches in struts 2 xwork package com opensymphony xwork2 util LocalizedTextUtil. java. The problem is this clearMap in early summer. getDeclaredField, which comes with java, cannot retrieve the attributes of the parent class. So there will always be a mistake here.

The latest version of these two methods:

private static void clearTomcatCache() {
        ClassLoader loader = getCurrentThreadContextClassLoader();
        // no need for compilation here.
        Class cl = loader.getClass();
        try {
            if ("org.apache.catalina.loader.WebappClassLoader".equals(cl.getName())) {
                clearMap(cl, loader, TOMCAT_RESOURCE_ENTRIES_FIELD);
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("class loader " + cl.getName() + " is not tomcat loader.");
                }
            }
        } catch (NoSuchFieldException nsfe) {
            if ("org.apache.catalina.loader.WebappClassLoaderBase".equals(cl.getSuperclass().getName())) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Base class #0 doesn't contain '#1' field, trying with parent!", nsfe, cl.getName(), TOMCAT_RESOURCE_ENTRIES_FIELD);
                }
                try {
                    clearMap(cl.getSuperclass(), loader, TOMCAT_RESOURCE_ENTRIES_FIELD);
                } catch (Exception e) {
                    if (LOG.isWarnEnabled()) {
                        LOG.warn("Couldn't clear tomcat cache using #0", e, cl.getSuperclass().getName());
                    }
                }
            }
        } catch (Exception e) {
            if (LOG.isWarnEnabled()) {
        	    LOG.warn("Couldn't clear tomcat cache", e, cl.getName());
            }
        }
    }
    private static 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);

        synchronized (cache) {
            Class ccl = cache.getClass();
            Method clearMethod = ccl.getMethod("clear");
            clearMethod.invoke(cache);
        }
    }

3. When we encounter this problem, we are often dealing with old projects, many of which have to use the new version of tomcat because of scanning in addition to vulnerabilities. We can choose to upgrade the version of struts 2, but the upgrade of the version will bring many unexpected problems. There are three ways ahead of us: first, use the latest version of struts 2.2, take out this class, and re-examine these two methods. (3) Change these two methods and put them in the jar package.

Reference resources:

FieldUtils.getAllFields() under the apache commons lang3 package can obtain all (public, protected, default, private) attributes of classes and parent classes.

 

https://blog.csdn.net/wangjun5159/article/details/79289244

Posted by blues on Tue, 22 Jan 2019 09:42:12 -0800