Skip to content

Commit

Permalink
480904 - jetty-util Loader simplification
Browse files Browse the repository at this point in the history
The Loader has been simplified to now just be a switch between loading from the context loader,
the same loader as another class or the system loader.    Multiple loaders will never be tried.

A new runWithServerClassAccess(PriviledgedAction) method has been added to WebAppClassLoader, that
is now used during configuration for actions that need access to both the WEB-INF/lib classes and
the server classes (eg jetty-web.xml and env.xml).

The JMX MBean mechanism has also been modified to look for an MBean class in the same loader that
object came from before attempting the context loader (only if different).
  • Loading branch information
gregw committed Nov 19, 2015
1 parent 65d33f8 commit a311c8b
Show file tree
Hide file tree
Showing 35 changed files with 158 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public static void main( String[] args ) throws Exception
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
File warFile = new File(
"../../jetty-distribution/target/distribution/test/webapps/test/");
"../../tests/test-jmx/jmx-webapp/target/jmx-webapp");
webapp.setWar(warFile.getAbsolutePath());
webapp.addAliasCheck(new AllowSymLinkAliasChecker());

// A WebAppContext is a ContextHandler as well so it needs to be set to
// the server so it is aware of where to send the appropriate requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public void parse (Set<? extends Handler> handlers, String className, ClassNameR
if (!isParsed(className) || resolver.shouldOverride(className))
{
className = className.replace('.', '/')+".class";
URL resource = Loader.getResource(this.getClass(), className);
URL resource = Loader.getResource(className);
if (resource!= null)
{
Resource r = Resource.newResource(resource);
Expand Down Expand Up @@ -593,7 +593,7 @@ public void parse (Set<? extends Handler> handlers, Class<?> clazz, ClassNameRes
if (!isParsed(cz.getName()) || resolver.shouldOverride(cz.getName()))
{
String nameAsResource = cz.getName().replace('.', '/')+".class";
URL resource = Loader.getResource(this.getClass(), nameAsResource);
URL resource = Loader.getResource(nameAsResource);
if (resource!= null)
{
Resource r = Resource.newResource(resource);
Expand Down Expand Up @@ -652,7 +652,7 @@ public void parse (Set<? extends Handler> handlers, List<String> classNames, Cla
if ((resolver == null) || (!resolver.isExcluded(s) && (!isParsed(s) || resolver.shouldOverride(s))))
{
s = s.replace('.', '/')+".class";
URL resource = Loader.getResource(this.getClass(), s);
URL resource = Loader.getResource(s);
if (resource!= null)
{
Resource r = Resource.newResource(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static Class convertType (Type t)
}
case Type.OBJECT:
{
return (Loader.loadClass(null, t.getClassName()));
return (Loader.loadClass(t.getClassName()));
}
case Type.SHORT:
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ else if (callback instanceof RequestParameterCallback)
}
else
{
Class<?> clazz = Loader.loadClass(getClass(), _callbackHandlerClass);
Class<?> clazz = Loader.loadClass(_callbackHandlerClass);
callbackHandler = (CallbackHandler)clazz.newInstance();
}
//set up the login context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void initialize(Subject subject,
dbPassword = "";

if (dbDriver != null)
Loader.loadClass(this.getClass(), dbDriver).newInstance();
Loader.loadClass(dbDriver).newInstance();
}
catch (ClassNotFoundException e)
{
Expand Down
17 changes: 15 additions & 2 deletions jetty-jmx/src/main/java/org/eclipse/jetty/jmx/ObjectMBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,21 @@ public static Object mbeanFor(Object o)
String mName = pName + ".jmx." + cName + "MBean";

try
{
Class<?> mClass = (Object.class.equals(oClass))?oClass=ObjectMBean.class:Loader.loadClass(oClass,mName);
{
Class<?> mClass;
try
{
// Look for an MBean class from the same loader that loaded the original class
mClass = (Object.class.equals(oClass))?oClass=ObjectMBean.class:Loader.loadClass(oClass,mName);
}
catch (ClassNotFoundException e)
{
// Not found, so if not the same as the thread context loader, try that.
if (Thread.currentThread().getContextClassLoader()==oClass.getClassLoader())
throw e;
LOG.ignore(e);
mClass=Loader.loadClass(oClass,mName);
}

if (LOG.isDebugEnabled())
LOG.debug("ObjectMbean: mbeanFor {} mClass={}", o, mClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void callStartup(WebAppContext context)
try
{
for (String s : _applicableTypeNames)
classes.add(Loader.loadClass(context.getClass(), s));
classes.add(Loader.loadClass(s));

context.getServletContext().setExtendedListenerTypes(true);
if (LOG.isDebugEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void callback (Object instance)
if (_target == null)
{
if (_targetClass == null)
_targetClass = Loader.loadClass(null, _className);
_targetClass = Loader.loadClass(_className);
_target = _targetClass.getDeclaredMethod(_methodName, TypeUtil.NO_ARGS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.AbstractConfiguration;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;

Expand Down Expand Up @@ -113,7 +114,7 @@ public Binding bind(NamingContext ctx, Binding binding)
{
localContextRoot.getRoot().addListener(listener);
XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
configuration.configure(context);
WebAppClassLoader.runWithServerClassAccess(()->{configuration.configure(context);return null;});
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected void doStart() throws Exception
+ " = u."
+ _userRoleTableRoleKey;

Loader.loadClass(this.getClass(), _jdbcDriver).newInstance();
Loader.loadClass(_jdbcDriver).newInstance();
super.doStart();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ public synchronized Class<?> loadClass(String className) throws ClassNotFoundExc
return null;

if (_classLoader == null)
return Loader.loadClass(this.getClass(),className);
return Loader.loadClass(className);

return _classLoader.loadClass(className);
}
Expand Down Expand Up @@ -2317,7 +2317,7 @@ public void addListener(String className)
try
{
@SuppressWarnings({ "unchecked", "rawtypes" })
Class<? extends EventListener> clazz = _classLoader==null?Loader.loadClass(ContextHandler.class,className):(Class)_classLoader.loadClass(className);
Class<? extends EventListener> clazz = _classLoader==null?Loader.loadClass(className):(Class)_classLoader.loadClass(className);
addListener(clazz);
}
catch (ClassNotFoundException e)
Expand Down Expand Up @@ -2410,7 +2410,7 @@ public ClassLoader getClassLoader()
//classloader, or a parent of it
try
{
Class<?> reflect = Loader.loadClass(getClass(), "sun.reflect.Reflection");
Class<?> reflect = Loader.loadClass("sun.reflect.Reflection");
Method getCallerClass = reflect.getMethod("getCallerClass", Integer.TYPE);
Class<?> caller = (Class<?>)getCallerClass.invoke(null, 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void doStart()
{
try
{
_class=Loader.loadClass(Holder.class, _className);
_class=Loader.loadClass(_className);
if(LOG.isDebugEnabled())
LOG.debug("Holding {} from {}",_class,_class.getClassLoader());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public ServletHolder addServlet(String className,String pathSpec)
*/
public ServletHolder addServlet(Class<? extends Servlet> servlet,String pathSpec)
{
return getServletHandler().addServletWithMapping(servlet.getName(), pathSpec);
return getServletHandler().addServletWithMapping(servlet,pathSpec);
}

/* ------------------------------------------------------------ */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ private void detectJspContainer ()
try
{
//check for apache
Loader.loadClass(Holder.class, APACHE_SENTINEL_CLASS);
Loader.loadClass(APACHE_SENTINEL_CLASS);
if (LOG.isDebugEnabled())LOG.debug("Apache jasper detected");
_jspContainer = JspContainer.APACHE;
}
Expand All @@ -897,7 +897,7 @@ private String getNameOfJspClass (String jsp)
jsp = jsp.substring(i);
try
{
Class<?> jspUtil = Loader.loadClass(Holder.class, "org.apache.jasper.compiler.JspUtil");
Class<?> jspUtil = Loader.loadClass("org.apache.jasper.compiler.JspUtil");
Method makeJavaIdentifier = jspUtil.getMethod("makeJavaIdentifier", String.class);
return (String)makeJavaIdentifier.invoke(null, jsp);
}
Expand All @@ -923,7 +923,7 @@ private String getPackageOfJspClass (String jsp)
return "";
try
{
Class<?> jspUtil = Loader.loadClass(Holder.class, "org.apache.jasper.compiler.JspUtil");
Class<?> jspUtil = Loader.loadClass("org.apache.jasper.compiler.JspUtil");
Method makeJavaPackage = jspUtil.getMethod("makeJavaPackage", String.class);
return (String)makeJavaPackage.invoke(null, jsp.substring(0,i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void contextDestroyed(ServletContextEvent sce)
try
{
//Check that the BeanELResolver class is on the classpath
Class<?> beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");
Class<?> beanELResolver = Loader.loadClass("javax.el.BeanELResolver");

//Get a reference via reflection to the properties field which is holding class references
Field field = getField(beanELResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ protected Object parseObject(Source source)
{
try
{
Class c = Loader.loadClass(JSON.class,classname);
Class c = Loader.loadClass(classname);
return convertTo(c,map);
}
catch (ClassNotFoundException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Object fromJSON(Map object)
{
try
{
Collection result = (Collection)Loader.loadClass(getClass(), (String)object.get("class")).newInstance();
Collection result = (Collection)Loader.loadClass((String)object.get("class")).newInstance();
Collections.addAll(result, (Object[])object.get("list"));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class JSONEnumConvertor implements JSON.Convertor
{
try
{
Class<?> e = Loader.loadClass(getClass(),"java.lang.Enum");
Class<?> e = Loader.loadClass("java.lang.Enum");
_valueOf=e.getMethod("valueOf",Class.class,String.class);
}
catch(Exception e)
Expand All @@ -68,7 +68,7 @@ public Object fromJSON(Map map)
throw new UnsupportedOperationException();
try
{
Class c=Loader.loadClass(getClass(),(String)map.get("class"));
Class c=Loader.loadClass((String)map.get("class"));
return _valueOf.invoke(null,c,map.get("value"));
}
catch(Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void toJSON(Object obj, Output out)
{
try
{
Class cls=Loader.loadClass(JSON.class,clsName);
Class cls=Loader.loadClass(clsName);
convertor=new JSONPojoConvertor(cls,_fromJson);
_json.addConvertorFor(clsName, convertor);
}
Expand All @@ -91,7 +91,7 @@ public Object fromJSON(Map object)
{
try
{
Class cls=Loader.loadClass(JSON.class,clsName);
Class cls=Loader.loadClass(clsName);
convertor=new JSONPojoConvertor(cls,_fromJson);
_json.addConvertorFor(clsName, convertor);
}
Expand Down
Loading

0 comments on commit a311c8b

Please sign in to comment.