Using new Decorator

This commit is contained in:
Joakim Erdfelt 2015-03-11 16:02:18 -07:00
parent 0301de099d
commit e92bcccc84
9 changed files with 334 additions and 8 deletions

View File

@ -424,7 +424,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
@Override
public void configure(WebAppContext context) throws Exception
{
context.addDecorator(new AnnotationDecorator(context));
context.getObjectFactory().addDecorator(new AnnotationDecorator(context));
//Even if metadata is complete, we still need to scan for ServletContainerInitializers - if there are any
@ -646,7 +646,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
@Override
public void cloneConfigure(WebAppContext template, WebAppContext context) throws Exception
{
context.addDecorator(new AnnotationDecorator(context));
context.getObjectFactory().addDecorator(new AnnotationDecorator(context));
}

View File

@ -18,7 +18,7 @@
package org.eclipse.jetty.annotations;
import org.eclipse.jetty.servlet.ServletContextHandler.Decorator;
import org.eclipse.jetty.util.Decorator;
import org.eclipse.jetty.webapp.WebAppContext;
/**

View File

@ -48,13 +48,13 @@ public class PlusConfiguration extends AbstractConfiguration
public void preConfigure (WebAppContext context)
throws Exception
{
context.addDecorator(new PlusDecorator(context));
context.getObjectFactory().addDecorator(new PlusDecorator(context));
}
@Override
public void cloneConfigure(WebAppContext template, WebAppContext context) throws Exception
{
context.addDecorator(new PlusDecorator(context));
context.getObjectFactory().addDecorator(new PlusDecorator(context));
}
@Override

View File

@ -21,7 +21,7 @@ package org.eclipse.jetty.plus.webapp;
import org.eclipse.jetty.plus.annotation.InjectionCollection;
import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
import org.eclipse.jetty.plus.annotation.RunAsCollection;
import org.eclipse.jetty.servlet.ServletContextHandler.Decorator;
import org.eclipse.jetty.util.Decorator;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;

View File

@ -138,7 +138,7 @@ public class QuickStartConfiguration extends WebInfConfiguration
context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());
//add a decorator that will find introspectable annotations
context.addDecorator(new AnnotationDecorator(context)); //this must be the last Decorator because they are run in reverse order!
context.getObjectFactory().addDecorator(new AnnotationDecorator(context)); //this must be the last Decorator because they are run in reverse order!
//add a context bean that will run ServletContainerInitializers as the context starts
ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);

View File

@ -696,7 +696,7 @@ public class ServletContextHandler extends ContextHandler
{
_objFactory.addDecorator(decorator);
}
/* ------------------------------------------------------------ */
void destroyServlet(Servlet servlet)
{
@ -1462,7 +1462,9 @@ public class ServletContextHandler extends ContextHandler
* Legacy Interface to decorate loaded classes.
* <p>
* Left for backwards compatibility with Weld / CDI
* @deprecated use new {@link org.eclipse.jetty.util.Decorator}
*/
@Deprecated
public interface Decorator extends org.eclipse.jetty.util.Decorator
{
}

View File

@ -0,0 +1,200 @@
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.log;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.regex.Pattern;
import org.eclipse.jetty.util.log.Log;
/**
* Redirect java.util.logging events to Jetty Log
*/
public class JettyLogHandler extends java.util.logging.Handler
{
public static void config()
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource("logging.properties");
if (url != null)
{
System.err.printf("Initializing java.util.logging from %s%n",url);
try (InputStream in = url.openStream())
{
LogManager.getLogManager().readConfiguration(in);
}
catch (IOException e)
{
e.printStackTrace(System.err);
}
}
else
{
System.err.printf("WARNING: java.util.logging failed to initialize: logging.properties not found%n");
}
System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.Jdk14Logger");
}
public JettyLogHandler()
{
if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.DEBUG","false")))
{
setLevel(Level.FINEST);
}
if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.IGNORED","false")))
{
setLevel(Level.ALL);
}
System.err.printf("%s Initialized at level [%s]%n",this.getClass().getName(),getLevel().getName());
}
private synchronized String formatMessage(LogRecord record)
{
String msg = getMessage(record);
try
{
Object params[] = record.getParameters();
if ((params == null) || (params.length == 0))
{
return msg;
}
if (Pattern.compile("\\{\\d+\\}").matcher(msg).find())
{
return MessageFormat.format(msg,params);
}
return msg;
}
catch (Exception ex)
{
return msg;
}
}
private String getMessage(LogRecord record)
{
ResourceBundle bundle = record.getResourceBundle();
if (bundle != null)
{
try
{
return bundle.getString(record.getMessage());
}
catch (java.util.MissingResourceException ex)
{
}
}
return record.getMessage();
}
@Override
public void publish(LogRecord record)
{
org.eclipse.jetty.util.log.Logger JLOG = getJettyLogger(record.getLoggerName());
int level = record.getLevel().intValue();
if (level >= Level.OFF.intValue())
{
// nothing to log, skip it.
return;
}
Throwable cause = record.getThrown();
String msg = formatMessage(record);
if (level >= Level.WARNING.intValue())
{
// log at warn
if (cause != null)
{
JLOG.warn(msg,cause);
}
else
{
JLOG.warn(msg);
}
return;
}
if (level >= Level.INFO.intValue())
{
// log at info
if (cause != null)
{
JLOG.info(msg,cause);
}
else
{
JLOG.info(msg);
}
return;
}
if (level >= Level.FINEST.intValue())
{
// log at debug
if (cause != null)
{
JLOG.debug(msg,cause);
}
else
{
JLOG.debug(msg);
}
return;
}
if (level >= Level.ALL.intValue())
{
// only corresponds with ignore (in jetty speak)
JLOG.ignore(cause);
return;
}
}
private Logger getJettyLogger(String loggerName)
{
return org.eclipse.jetty.util.log.Log.getLogger(loggerName);
}
@Override
public void flush()
{
// ignore
}
@Override
public void close() throws SecurityException
{
// ignore
}
}

View File

@ -0,0 +1,45 @@
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.thread;
import java.io.Closeable;
public class ThreadClassLoaderScope implements Closeable
{
private final ClassLoader old;
private final ClassLoader scopedClassLoader;
public ThreadClassLoaderScope(ClassLoader cl)
{
old = Thread.currentThread().getContextClassLoader();
scopedClassLoader = cl;
Thread.currentThread().setContextClassLoader(scopedClassLoader);
}
@Override
public void close()
{
Thread.currentThread().setContextClassLoader(old);
}
public ClassLoader getScopedClassLoader()
{
return scopedClassLoader;
}
}

View File

@ -0,0 +1,79 @@
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.thread;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.net.URL;
import java.net.URLClassLoader;
import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.junit.Test;
public class ThreadClassLoaderScopeTest
{
private static class ClassLoaderFoo extends URLClassLoader
{
public ClassLoaderFoo()
{
super(new URL[0]);
}
}
private static class ClassLoaderBar extends URLClassLoader
{
public ClassLoaderBar()
{
super(new URL[0]);
}
}
@Test
public void testNormal()
{
try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(new ClassLoaderFoo()))
{
assertThat("ClassLoader in scope",Thread.currentThread().getContextClassLoader(),instanceOf(ClassLoaderFoo.class));
assertThat("Scoped ClassLoader",scope.getScopedClassLoader(),instanceOf(ClassLoaderFoo.class));
}
assertThat("ClassLoader after scope",Thread.currentThread().getContextClassLoader(),not(instanceOf(ClassLoaderFoo.class)));
}
@Test
public void testWithException()
{
try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(new ClassLoaderBar()))
{
assertThat("ClassLoader in 'scope'",Thread.currentThread().getContextClassLoader(),instanceOf(ClassLoaderBar.class));
assertThat("Scoped ClassLoader",scope.getScopedClassLoader(),instanceOf(ClassLoaderBar.class));
try (ThreadClassLoaderScope inner = new ThreadClassLoaderScope(new ClassLoaderFoo()))
{
assertThat("ClassLoader in 'inner'",Thread.currentThread().getContextClassLoader(),instanceOf(ClassLoaderFoo.class));
assertThat("Scoped ClassLoader",scope.getScopedClassLoader(),instanceOf(ClassLoaderFoo.class));
throw new RuntimeException("Intention exception");
}
}
catch (Throwable ignore)
{
/* ignore */
}
assertThat("ClassLoader after 'scope'",Thread.currentThread().getContextClassLoader(),not(instanceOf(ClassLoaderBar.class)));
}
}