Author: rjung Date: Thu Apr 22 13:08:01 2010 New Revision: 936823 URL: http://svn.apache.org/viewvc?rev=936823&view=rev Log: Add property "searchExternalFirst" to WebappLoader: if set the external repositories will be searched before the WEB-INF ones. Default (false) is unchanged behaviour. Expose the new property via JMX and document it..
Modified: tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java tomcat/trunk/java/org/apache/catalina/loader/mbeans-descriptors.xml tomcat/trunk/webapps/docs/config/loader.xml Modified: tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java?rev=936823&r1=936822&r2=936823&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java (original) +++ tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java Thu Apr 22 13:08:01 2010 @@ -410,6 +410,11 @@ public class WebappClassLoader protected boolean hasExternalRepositories = false; /** + * Search external repositories first + */ + protected boolean searchExternalFirst = false; + + /** * need conversion for properties files */ protected boolean needConvert = false; @@ -535,7 +540,21 @@ public class WebappClassLoader this.antiJARLocking = antiJARLocking; } - + /** + * @return Returns the searchExternalFirst. + */ + public boolean getSearchExternalFirst() { + return searchExternalFirst; + } + + /** + * @param searchExternalFirst Whether external repositories should be searched first + */ + public void setSearchExternalFirst(boolean searchExternalFirst) { + this.searchExternalFirst = searchExternalFirst; + } + + /** * If there is a Java SecurityManager create a read FilePermission * or JndiPermission for the file directory path. @@ -1065,22 +1084,37 @@ public class WebappClassLoader try { if (log.isTraceEnabled()) log.trace(" findClassInternal(" + name + ")"); - try { - clazz = findClassInternal(name); - } catch(ClassNotFoundException cnfe) { - if (!hasExternalRepositories) { - throw cnfe; - } - } catch(AccessControlException ace) { - log.warn("WebappClassLoader.findClassInternal(" + name - + ") security exception: " + ace.getMessage(), ace); - throw new ClassNotFoundException(name, ace); - } catch (RuntimeException e) { - if (log.isTraceEnabled()) - log.trace(" -->RuntimeException Rethrown", e); - throw e; + if (hasExternalRepositories && searchExternalFirst) { + try { + clazz = super.findClass(name); + } catch(AccessControlException ace) { + log.warn("WebappClassLoader.findClassInternal(" + name + + ") security exception: " + ace.getMessage(), ace); + throw new ClassNotFoundException(name, ace); + } catch (RuntimeException e) { + if (log.isTraceEnabled()) + log.trace(" -->RuntimeException Rethrown", e); + throw e; + } } - if ((clazz == null) && hasExternalRepositories) { + if ((clazz == null)) { + try { + clazz = findClassInternal(name); + } catch(ClassNotFoundException cnfe) { + if (!hasExternalRepositories || searchExternalFirst) { + throw cnfe; + } + } catch(AccessControlException ace) { + log.warn("WebappClassLoader.findClassInternal(" + name + + ") security exception: " + ace.getMessage(), ace); + throw new ClassNotFoundException(name, ace); + } catch (RuntimeException e) { + if (log.isTraceEnabled()) + log.trace(" -->RuntimeException Rethrown", e); + throw e; + } + } + if ((clazz == null) && hasExternalRepositories && !searchExternalFirst) { try { clazz = super.findClass(name); } catch(AccessControlException ace) { @@ -1138,21 +1172,26 @@ public class WebappClassLoader URL url = null; - ResourceEntry entry = resourceEntries.get(name); - if (entry == null) { - if (securityManager != null) { - PrivilegedAction<ResourceEntry> dp = - new PrivilegedFindResourceByName(name, name); - entry = AccessController.doPrivileged(dp); - } else { - entry = findResourceInternal(name, name); + if (hasExternalRepositories && searchExternalFirst) + url = super.findResource(name); + + if (url == null) { + ResourceEntry entry = resourceEntries.get(name); + if (entry == null) { + if (securityManager != null) { + PrivilegedAction<ResourceEntry> dp = + new PrivilegedFindResourceByName(name, name); + entry = AccessController.doPrivileged(dp); + } else { + entry = findResourceInternal(name, name); + } + } + if (entry != null) { + url = entry.source; } - } - if (entry != null) { - url = entry.source; } - if ((url == null) && hasExternalRepositories) + if ((url == null) && hasExternalRepositories && !searchExternalFirst) url = super.findResource(name); if (log.isDebugEnabled()) { @@ -1188,6 +1227,16 @@ public class WebappClassLoader int i; + // Adding the results of a call to the superclass + if (hasExternalRepositories && searchExternalFirst) { + + Enumeration<URL> otherResourcePaths = super.findResources(name); + + while (otherResourcePaths.hasMoreElements()) { + result.addElement(otherResourcePaths.nextElement()); + } + + } // Looking at the repositories for (i = 0; i < repositoriesLength; i++) { try { @@ -1223,7 +1272,7 @@ public class WebappClassLoader } // Adding the results of a call to the superclass - if (hasExternalRepositories) { + if (hasExternalRepositories && !searchExternalFirst) { Enumeration<URL> otherResourcePaths = super.findResources(name); @@ -3178,4 +3227,3 @@ public class WebappClassLoader } - Modified: tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java?rev=936823&r1=936822&r2=936823&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java (original) +++ tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java Thu Apr 22 13:08:01 2010 @@ -196,6 +196,12 @@ public class WebappLoader extends Lifecy private ArrayList<String> loaderRepositories = null; + /** + * Whether we should search the external repositories first + */ + private boolean searchExternalFirst = false; + + // ------------------------------------------------------------- Properties @@ -331,6 +337,23 @@ public class WebappLoader extends Lifecy } + /** + * @return Returns searchExternalFirst. + */ + public boolean getSearchExternalFirst() { + return searchExternalFirst; + } + + /** + * @param searchExternalFirst Whether external repositories should be searched first + */ + public void setSearchExternalFirst(boolean searchExternalFirst) { + this.searchExternalFirst = searchExternalFirst; + if (classLoader != null) { + classLoader.setSearchExternalFirst(searchExternalFirst); + } + } + // --------------------------------------------------------- Public Methods @@ -589,6 +612,7 @@ public class WebappLoader extends Lifecy classLoader = createClassLoader(); classLoader.setResources(container.getResources()); classLoader.setDelegate(this.delegate); + classLoader.setSearchExternalFirst(searchExternalFirst); if (container instanceof StandardContext) { classLoader.setAntiJARLocking( ((StandardContext) container).getAntiJARLocking()); Modified: tomcat/trunk/java/org/apache/catalina/loader/mbeans-descriptors.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/mbeans-descriptors.xml?rev=936823&r1=936822&r2=936823&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/mbeans-descriptors.xml (original) +++ tomcat/trunk/java/org/apache/catalina/loader/mbeans-descriptors.xml Thu Apr 22 13:08:01 2010 @@ -38,6 +38,10 @@ description="The reloadable flag for this Loader" type="boolean"/> + <attribute name="searchExternalFirst" + description="The searchExternalFirst flag for this Loader" + type="boolean"/> + <attribute name="repositories" description="Extra repositories managed by this loader" type="[Ljava.lang.String;"/> @@ -133,6 +137,10 @@ description="The antiJARLocking flag for this Loader" type="boolean"/> + <attribute name="searchExternalFirst" + description="The searchExternalFirst flag for this Loader" + type="boolean"/> + <attribute name="uRLs" description="The URLs of this loader" type="[Ljava.net.URL;"/> Modified: tomcat/trunk/webapps/docs/config/loader.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/loader.xml?rev=936823&r1=936822&r2=936823&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/config/loader.xml (original) +++ tomcat/trunk/webapps/docs/config/loader.xml Thu Apr 22 13:08:01 2010 @@ -130,6 +130,12 @@ <code>org.apache.catalina.loader.WebappClassLoader</code>.</p> </attribute> + <attribute name="searchExternalFirst" required="false"> + <p>Set to <code>true</code> if you want repositories outside + of <code>WEB-INF/classes</code> and <code>WEB-INF/lib</code> to + be searched first. Default value is <code>false</code>.</p> + </attribute> + </attributes> </subsection> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org