From f01f6e3cfe7361457f207fa05b4a57e511e9289c Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 20 Mar 2012 10:17:37 +0000 Subject: [PATCH] Merges Struts 2 ClassLoaderUtils into XWork 2 ClassLoaderUtil git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1302821 13f79535-47bb-0310-9956-ffa450edef68 --- .../struts2/config/PropertiesSettings.java | 17 ++- .../DefaultStaticContentLoader.java | 3 +- .../apache/struts2/dispatcher/Dispatcher.java | 6 +- .../struts2/dispatcher/FilterDispatcher.java | 42 +++--- .../struts2/dispatcher/ng/InitOperations.java | 15 +- .../apache/struts2/util/ClassLoaderUtils.java | 135 ------------------ .../org/apache/struts2/util/StrutsUtil.java | 3 +- .../views/velocity/StrutsResourceLoader.java | 8 +- .../struts2/views/xslt/XSLTResultTest.java | 31 ++-- .../codebehind/CodebehindUnknownHandler.java | 17 ++- .../config_browser/ListValidatorsAction.java | 4 +- .../convention/ConventionUnknownHandler.java | 28 ++-- .../convention/ConventionsServiceImpl.java | 17 ++- .../struts2/jsf/FacesSetupInterceptor.java | 32 ++--- .../xwork2/util/ClassLoaderUtil.java | 78 +++++----- 15 files changed, 163 insertions(+), 273 deletions(-) delete mode 100644 core/src/main/java/org/apache/struts2/util/ClassLoaderUtils.java diff --git a/core/src/main/java/org/apache/struts2/config/PropertiesSettings.java b/core/src/main/java/org/apache/struts2/config/PropertiesSettings.java index 16c1d1bc6..1b1162ff1 100644 --- a/core/src/main/java/org/apache/struts2/config/PropertiesSettings.java +++ b/core/src/main/java/org/apache/struts2/config/PropertiesSettings.java @@ -21,19 +21,18 @@ package org.apache.struts2.config; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Iterator; - -import org.apache.struts2.StrutsException; -import org.apache.struts2.util.ClassLoaderUtils; - +import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.util.location.LocatableProperties; import com.opensymphony.xwork2.util.location.Location; import com.opensymphony.xwork2.util.location.LocationImpl; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.StrutsException; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Iterator; /** @@ -54,7 +53,7 @@ class PropertiesSettings extends Settings { */ public PropertiesSettings(String name) { - URL settingsUrl = ClassLoaderUtils.getResource(name + ".properties", getClass()); + URL settingsUrl = ClassLoaderUtil.getResource(name + ".properties", getClass()); if (settingsUrl == null) { if (LOG.isDebugEnabled()) { diff --git a/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java b/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java index 5c7168f77..144cd882d 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java @@ -26,7 +26,6 @@ import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import org.apache.struts2.StrutsConstants; import org.apache.struts2.dispatcher.ng.HostConfig; -import org.apache.struts2.util.ClassLoaderUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -242,7 +241,7 @@ public class DefaultStaticContentLoader implements StaticContentLoader { String factoryName = filterConfig.getInitParameter("loggerFactory"); if (factoryName != null) { try { - Class cls = ClassLoaderUtils.loadClass(factoryName, this.getClass()); + Class cls = ClassLoaderUtil.loadClass(factoryName, this.getClass()); LoggerFactory fac = (LoggerFactory)cls.newInstance(); LoggerFactory.setLoggerFactory(fac); } catch (InstantiationException e) { diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index 7b21da8ae..f3fa16c3f 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -38,6 +38,7 @@ import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.ContainerBuilder; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.interceptor.Interceptor; +import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.util.FileManager; import com.opensymphony.xwork2.util.LocalizedTextUtil; import com.opensymphony.xwork2.util.ValueStack; @@ -61,7 +62,6 @@ import org.apache.struts2.dispatcher.mapper.ActionMapping; import org.apache.struts2.dispatcher.multipart.MultiPartRequest; import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; import org.apache.struts2.util.AttributeMap; -import org.apache.struts2.util.ClassLoaderUtils; import org.apache.struts2.util.ObjectFactoryDestroyable; import org.apache.struts2.views.freemarker.FreemarkerManager; @@ -73,6 +73,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -80,7 +81,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; -import java.util.Enumeration; /** * A utility class the actual dispatcher delegates most of its tasks to. Each instance @@ -347,7 +347,7 @@ public class Dispatcher { String[] classes = configProvs.split("\\s*[,]\\s*"); for (String cname : classes) { try { - Class cls = ClassLoaderUtils.loadClass(cname, this.getClass()); + Class cls = ClassLoaderUtil.loadClass(cname, this.getClass()); ConfigurationProvider prov = (ConfigurationProvider)cls.newInstance(); configurationManager.addContainerProvider(prov); } catch (InstantiationException e) { diff --git a/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java index f2c3c0709..cbf9bd9b9 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java @@ -21,10 +21,21 @@ package org.apache.struts2.dispatcher; -import java.io.IOException; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Map; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.config.Configuration; +import com.opensymphony.xwork2.config.ConfigurationProvider; +import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.ClassLoaderUtil; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.util.ValueStackFactory; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import com.opensymphony.xwork2.util.profiling.UtilTimerStack; +import org.apache.struts2.RequestUtils; +import org.apache.struts2.StrutsStatics; +import org.apache.struts2.dispatcher.mapper.ActionMapper; +import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.apache.struts2.dispatcher.ng.filter.FilterHostConfig; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -35,23 +46,10 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - -import org.apache.struts2.RequestUtils; -import org.apache.struts2.StrutsStatics; -import org.apache.struts2.dispatcher.mapper.ActionMapper; -import org.apache.struts2.dispatcher.mapper.ActionMapping; -import org.apache.struts2.dispatcher.ng.filter.FilterHostConfig; -import org.apache.struts2.util.ClassLoaderUtils; - -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.config.Configuration; -import com.opensymphony.xwork2.config.ConfigurationProvider; -import com.opensymphony.xwork2.inject.Inject; -import com.opensymphony.xwork2.util.ValueStack; -import com.opensymphony.xwork2.util.ValueStackFactory; -import com.opensymphony.xwork2.util.logging.Logger; -import com.opensymphony.xwork2.util.logging.LoggerFactory; -import com.opensymphony.xwork2.util.profiling.UtilTimerStack; +import java.io.IOException; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; /** * Master filter for Struts that handles four distinct @@ -205,7 +203,7 @@ public class FilterDispatcher implements StrutsStatics, Filter { String factoryName = filterConfig.getInitParameter("loggerFactory"); if (factoryName != null) { try { - Class cls = ClassLoaderUtils.loadClass(factoryName, this.getClass()); + Class cls = ClassLoaderUtil.loadClass(factoryName, this.getClass()); LoggerFactory fac = (LoggerFactory) cls.newInstance(); LoggerFactory.setLoggerFactory(fac); } catch (InstantiationException e) { diff --git a/core/src/main/java/org/apache/struts2/dispatcher/ng/InitOperations.java b/core/src/main/java/org/apache/struts2/dispatcher/ng/InitOperations.java index f0d76161f..6bce2b603 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/ng/InitOperations.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/ng/InitOperations.java @@ -20,14 +20,19 @@ */ package org.apache.struts2.dispatcher.ng; -import com.opensymphony.xwork2.util.logging.LoggerFactory; import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.util.ClassLoaderUtil; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.StrutsConstants; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.StaticContentLoader; -import org.apache.struts2.util.ClassLoaderUtils; -import org.apache.struts2.StrutsConstants; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.regex.Pattern; /** @@ -45,7 +50,7 @@ public class InitOperations { String factoryName = filterConfig.getInitParameter("loggerFactory"); if (factoryName != null) { try { - Class cls = ClassLoaderUtils.loadClass(factoryName, this.getClass()); + Class cls = ClassLoaderUtil.loadClass(factoryName, this.getClass()); LoggerFactory fac = (LoggerFactory) cls.newInstance(); LoggerFactory.setLoggerFactory(fac); } catch ( InstantiationException e ) { diff --git a/core/src/main/java/org/apache/struts2/util/ClassLoaderUtils.java b/core/src/main/java/org/apache/struts2/util/ClassLoaderUtils.java deleted file mode 100644 index 93c6e1d7b..000000000 --- a/core/src/main/java/org/apache/struts2/util/ClassLoaderUtils.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * $Id$ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.struts2.util; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; - - -/** - * This class is extremely useful for loading resources and classes in a fault tolerant manner - * that works across different applications servers. - *

- * It has come out of many months of frustrating use of multiple application servers at Atlassian, - * please don't change things unless you're sure they're not going to break in one server or another! - * - */ -public class ClassLoaderUtils { - - /** - * Load a given resource. - *

- * This method will try to load the resource using the following methods (in order): - *

- * - * @param resourceName The name of the resource to load - * @param callingClass The Class object of the calling object - */ - public static URL getResource(String resourceName, Class callingClass) { - URL url = null; - - url = Thread.currentThread().getContextClassLoader().getResource(resourceName); - - if (url == null) { - url = ClassLoaderUtils.class.getClassLoader().getResource(resourceName); - } - - if (url == null) { - url = callingClass.getClassLoader().getResource(resourceName); - } - - return url; - } - - /** - * This is a convenience method to load a resource as a stream. - *

- * The algorithm used to find the resource is given in getResource() - * - * @param resourceName The name of the resource to load - * @param callingClass The Class object of the calling object - */ - public static InputStream getResourceAsStream(String resourceName, Class callingClass) { - URL url = getResource(resourceName, callingClass); - - try { - return (url != null) ? url.openStream() : null; - } catch (IOException e) { - return null; - } - } - - /** - * Load a class with a given name. - *

- * It will try to load the class in the following order: - *

- * - * @param className The name of the class to load - * @param callingClass The Class object of the calling object - * @throws ClassNotFoundException If the class cannot be found anywhere. - */ - public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { - try { - return Thread.currentThread().getContextClassLoader().loadClass(className); - } catch (ClassNotFoundException e) { - try { - return Class.forName(className); - } catch (ClassNotFoundException ex) { - try { - return ClassLoaderUtils.class.getClassLoader().loadClass(className); - } catch (ClassNotFoundException exc) { - return callingClass.getClassLoader().loadClass(className); - } - } - } - } - - /** - * Prints the current classloader hierarchy - useful for debugging. - */ - public static void printClassLoader() { - System.out.println("ClassLoaderUtils.printClassLoader"); - printClassLoader(Thread.currentThread().getContextClassLoader()); - } - - /** - * Prints the classloader hierarchy from a given classloader - useful for debugging. - */ - public static void printClassLoader(ClassLoader cl) { - System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")"); - - if (cl != null) { - printClassLoader(cl.getParent()); - } - } -} diff --git a/core/src/main/java/org/apache/struts2/util/StrutsUtil.java b/core/src/main/java/org/apache/struts2/util/StrutsUtil.java index 418d633b6..c5f25e88e 100644 --- a/core/src/main/java/org/apache/struts2/util/StrutsUtil.java +++ b/core/src/main/java/org/apache/struts2/util/StrutsUtil.java @@ -24,6 +24,7 @@ package org.apache.struts2.util; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ObjectFactory; import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; @@ -78,7 +79,7 @@ public class StrutsUtil { Class c = (Class) classes.get(name); if (c == null) { - c = ClassLoaderUtils.loadClass(name, StrutsUtil.class); + c = ClassLoaderUtil.loadClass(name, StrutsUtil.class); classes.put(name, c); } diff --git a/core/src/main/java/org/apache/struts2/views/velocity/StrutsResourceLoader.java b/core/src/main/java/org/apache/struts2/views/velocity/StrutsResourceLoader.java index 91d7be90e..de27c2c48 100644 --- a/core/src/main/java/org/apache/struts2/views/velocity/StrutsResourceLoader.java +++ b/core/src/main/java/org/apache/struts2/views/velocity/StrutsResourceLoader.java @@ -21,12 +21,12 @@ package org.apache.struts2.views.velocity; -import java.io.InputStream; - -import org.apache.struts2.util.ClassLoaderUtils; +import com.opensymphony.xwork2.util.ClassLoaderUtil; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; +import java.io.InputStream; + /** * Loads resource from the Thread's context ClassLoader. @@ -44,7 +44,7 @@ public class StrutsResourceLoader extends ClasspathResourceLoader { } try { - return ClassLoaderUtils.getResourceAsStream(name, StrutsResourceLoader.class); + return ClassLoaderUtil.getResourceAsStream(name, StrutsResourceLoader.class); } catch (Exception e) { throw new ResourceNotFoundException(e.getMessage()); } diff --git a/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java b/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java index 14f68dfad..9cb69b41d 100644 --- a/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java +++ b/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java @@ -21,26 +21,23 @@ package org.apache.struts2.views.xslt; -import java.util.ArrayList; -import java.util.List; +import com.opensymphony.xwork2.Action; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.mock.MockActionInvocation; +import com.opensymphony.xwork2.util.ClassLoaderUtil; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.StrutsTestCase; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockServletContext; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.URIResolver; import javax.xml.transform.stream.StreamSource; - -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.StrutsTestCase; -import org.apache.struts2.util.ClassLoaderUtils; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.mock.web.MockServletContext; - -import com.opensymphony.xwork2.Action; -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.mock.MockActionInvocation; -import com.opensymphony.xwork2.util.ValueStack; -import com.opensymphony.xwork2.util.ValueStackFactory; +import java.util.ArrayList; +import java.util.List; /** * Unit test for {@link XSLTResult}. @@ -138,7 +135,7 @@ public class XSLTResultTest extends StrutsTestCase { protected URIResolver getURIResolver() { return new URIResolver() { public Source resolve(String href, String base) throws TransformerException { - return new StreamSource(ClassLoaderUtils.getResourceAsStream(href, this.getClass())); + return new StreamSource(ClassLoaderUtil.getResourceAsStream(href, this.getClass())); } }; @@ -159,7 +156,7 @@ public class XSLTResultTest extends StrutsTestCase { protected URIResolver getURIResolver() { return new URIResolver() { public Source resolve(String href, String base) throws TransformerException { - return new StreamSource(ClassLoaderUtils.getResourceAsStream(href, this.getClass())); + return new StreamSource(ClassLoaderUtil.getResourceAsStream(href, this.getClass())); } }; diff --git a/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java b/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java index a51986858..430ec6557 100644 --- a/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java +++ b/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java @@ -21,15 +21,24 @@ package org.apache.struts2.codebehind; -import com.opensymphony.xwork2.*; +import com.opensymphony.xwork2.Action; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ObjectFactory; +import com.opensymphony.xwork2.Result; +import com.opensymphony.xwork2.UnknownHandler; +import com.opensymphony.xwork2.XWorkException; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationException; -import com.opensymphony.xwork2.config.entities.*; +import com.opensymphony.xwork2.config.entities.ActionConfig; +import com.opensymphony.xwork2.config.entities.InterceptorLocator; +import com.opensymphony.xwork2.config.entities.PackageConfig; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.config.entities.ResultTypeConfig; import com.opensymphony.xwork2.config.providers.InterceptorBuilder; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; -import org.apache.struts2.util.ClassLoaderUtils; import javax.servlet.ServletContext; import java.net.MalformedURLException; @@ -253,7 +262,7 @@ public class CodebehindUnknownHandler implements UnknownHandler { LOG.debug("Loaded template '" + path + "' from servlet context."); } } else { - template = ClassLoaderUtils.getResource(stripLeadingSlash(path), getClass()); + template = ClassLoaderUtil.getResource(stripLeadingSlash(path), getClass()); if (template != null && LOG.isDebugEnabled()) { LOG.debug("Loaded template '" + stripLeadingSlash(path) + "' from class path."); } diff --git a/plugins/config-browser/src/main/java/org/apache/struts2/config_browser/ListValidatorsAction.java b/plugins/config-browser/src/main/java/org/apache/struts2/config_browser/ListValidatorsAction.java index 10e9913f9..75d8989b0 100644 --- a/plugins/config-browser/src/main/java/org/apache/struts2/config_browser/ListValidatorsAction.java +++ b/plugins/config-browser/src/main/java/org/apache/struts2/config_browser/ListValidatorsAction.java @@ -24,9 +24,9 @@ package org.apache.struts2.config_browser; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.validator.ActionValidatorManager; import com.opensymphony.xwork2.validator.Validator; -import org.apache.struts2.util.ClassLoaderUtils; import java.util.Collections; import java.util.List; @@ -92,7 +92,7 @@ public class ListValidatorsAction extends ActionSupport { private Class getClassInstance() { try { - return ClassLoaderUtils.loadClass(clazz, ActionContext.getContext().getClass()); + return ClassLoaderUtil.loadClass(clazz, ActionContext.getContext().getClass()); } catch (Exception e) { LOG.error("Class '" + clazz + "' not found...",e); } diff --git a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java index bd47ee0e6..b4ff36ad7 100644 --- a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java +++ b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionUnknownHandler.java @@ -20,15 +20,6 @@ */ package org.apache.struts2.convention; -import java.net.MalformedURLException; -import java.util.*; - -import javax.servlet.ServletContext; - -import com.opensymphony.xwork2.config.entities.*; -import com.opensymphony.xwork2.config.providers.InterceptorBuilder; -import org.apache.struts2.util.ClassLoaderUtils; - import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; @@ -38,11 +29,26 @@ import com.opensymphony.xwork2.UnknownHandler; import com.opensymphony.xwork2.XWorkException; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationException; -import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.config.entities.ActionConfig; +import com.opensymphony.xwork2.config.entities.InterceptorMapping; +import com.opensymphony.xwork2.config.entities.PackageConfig; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.config.entities.ResultTypeConfig; +import com.opensymphony.xwork2.config.providers.InterceptorBuilder; import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; +import javax.servlet.ServletContext; +import java.net.MalformedURLException; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + /** *

* This class is the default unknown handler for all of the Convention @@ -328,7 +334,7 @@ public class ConventionUnknownHandler implements UnknownHandler { LOG.trace("Checking ClasLoader for #0", path); String classLoaderPath = path.startsWith("/") ? path.substring(1, path.length()) : path; - if (ClassLoaderUtils.getResource(classLoaderPath, getClass()) != null) { + if (ClassLoaderUtil.getResource(classLoaderPath, getClass()) != null) { if (traceEnabled) LOG.trace("Found"); return buildResult(path, resultCode, resultsByExtension.get(ext), actionContext); diff --git a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionsServiceImpl.java b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionsServiceImpl.java index 17ec6c1b3..0993313fc 100644 --- a/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionsServiceImpl.java +++ b/plugins/convention/src/main/java/org/apache/struts2/convention/ConventionsServiceImpl.java @@ -20,19 +20,18 @@ */ package org.apache.struts2.convention; -import java.util.HashMap; -import java.util.Map; -import java.util.ResourceBundle; - -import org.apache.struts2.convention.annotation.AnnotationTools; -import org.apache.struts2.convention.annotation.ResultPath; -import org.apache.struts2.util.ClassLoaderUtils; - import com.opensymphony.xwork2.config.ConfigurationException; import com.opensymphony.xwork2.config.entities.ActionConfig; import com.opensymphony.xwork2.config.entities.PackageConfig; import com.opensymphony.xwork2.config.entities.ResultTypeConfig; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.ClassLoaderUtil; +import org.apache.struts2.convention.annotation.AnnotationTools; +import org.apache.struts2.convention.annotation.ResultPath; + +import java.util.HashMap; +import java.util.Map; +import java.util.ResourceBundle; /** *

@@ -95,7 +94,7 @@ public class ConventionsServiceImpl implements ConventionsService { } try { - return determineResultPath(ClassLoaderUtils.loadClass(actionConfig.getClassName(), this.getClass())); + return determineResultPath(ClassLoaderUtil.loadClass(actionConfig.getClassName(), this.getClass())); } catch (ClassNotFoundException e) { throw new RuntimeException("Invalid action class configuration that references an unknown " + "class named [" + actionConfig.getClassName() + "]", e); diff --git a/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesSetupInterceptor.java b/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesSetupInterceptor.java index 18ac0df2b..1f67e0e36 100644 --- a/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesSetupInterceptor.java +++ b/plugins/jsf/src/main/java/org/apache/struts2/jsf/FacesSetupInterceptor.java @@ -21,12 +21,14 @@ package org.apache.struts2.jsf; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; +import com.opensymphony.xwork2.Action; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.config.entities.ActionConfig; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.interceptor.Interceptor; +import com.opensymphony.xwork2.util.ClassLoaderUtil; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.StrutsException; import javax.faces.FactoryFinder; import javax.faces.application.Application; @@ -41,16 +43,12 @@ import javax.faces.el.VariableResolver; import javax.faces.event.ActionListener; import javax.faces.lifecycle.Lifecycle; import javax.faces.lifecycle.LifecycleFactory; - -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.StrutsException; -import org.apache.struts2.util.ClassLoaderUtils; - -import com.opensymphony.xwork2.Action; -import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.config.entities.ActionConfig; -import com.opensymphony.xwork2.config.entities.ResultConfig; -import com.opensymphony.xwork2.interceptor.Interceptor; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; /** * * Initializes the JSF context for this request. @@ -301,7 +299,7 @@ public class FacesSetupInterceptor extends FacesSupport implements Interceptor { Class implClass = null; try { - implClass = ClassLoaderUtils.loadClass(implClassName, this + implClass = ClassLoaderUtil.loadClass(implClassName, this .getClass()); } catch (ClassNotFoundException e1) { throw new IllegalArgumentException("Class " + implClassName diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/util/ClassLoaderUtil.java b/xwork-core/src/main/java/com/opensymphony/xwork2/util/ClassLoaderUtil.java index 54f113e83..5ecf2ac4c 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/util/ClassLoaderUtil.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/util/ClassLoaderUtil.java @@ -24,17 +24,12 @@ import java.util.*; /** * This class is extremely useful for loading resources and classes in a fault tolerant manner * that works across different applications servers. - * + *

* It has come out of many months of frustrating use of multiple application servers at Atlassian, * please don't change things unless you're sure they're not going to break in one server or another! - * - * It was brought in from oscore trunk revision 147. * - * @author $Author$ - * @version $Revision$ */ public class ClassLoaderUtil { - //~ Methods //////////////////////////////////////////////////////////////// /** * Load all resources with a given name, potentially aggregating all results @@ -77,18 +72,18 @@ public class ClassLoaderUtil { } /** - * Load a given resource. - * - * This method will try to load the resource using the following methods (in order): - *

- * - * @param resourceName The name IllegalStateException("Unable to call ")of the resource to load - * @param callingClass The Class object of the calling object - */ + * Load a given resource. + *

+ * This method will try to load the resource using the following methods (in order): + *

+ * + * @param resourceName The name of the resource to load + * @param callingClass The Class object of the calling object + */ public static URL getResource(String resourceName, Class callingClass) { URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); @@ -130,20 +125,20 @@ public class ClassLoaderUtil { } /** - * Load a class with a given name. - * - * It will try to load the class in the following order: - * - * - * @param className The name of the class to load - * @param callingClass The Class object of the calling object - * @throws ClassNotFoundException If the class cannot be found anywhere. - */ + * Load a class with a given name. + *

+ * It will try to load the class in the following order: + *

+ * + * @param className The name of the class to load + * @param callingClass The Class object of the calling object + * @throws ClassNotFoundException If the class cannot be found anywhere. + */ public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { try { return Thread.currentThread().getContextClassLoader().loadClass(className); @@ -160,6 +155,25 @@ public class ClassLoaderUtil { } } + /** + * Prints the current classloader hierarchy - useful for debugging. + */ + public static void printClassLoader() { + System.out.println("ClassLoaderUtils.printClassLoader"); + printClassLoader(Thread.currentThread().getContextClassLoader()); + } + + /** + * Prints the classloader hierarchy from a given classloader - useful for debugging. + */ + public static void printClassLoader(ClassLoader cl) { + System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")"); + + if (cl != null) { + printClassLoader(cl.getParent()); + } + } + /** * Aggregates Enumeration instances into one iterator and filters out duplicates. Always keeps one * ahead of the enumerator to protect against returning duplicates.