Merge branch `jetty-9.4.x` into `jetty-10.0.x`

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>

# Conflicts:
#	jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java
This commit is contained in:
Joakim Erdfelt 2019-08-07 05:31:02 -05:00
commit 075040e88e
5 changed files with 136 additions and 99 deletions

View File

@ -1,15 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<!--
<mirrors>
<mirror>
<id>local.mirror</id>
<url>file://@localRepo@</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
-->
<profiles>
<profile>
<id>it-repo</id>

View File

@ -23,9 +23,6 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* Fast String Utilities.
*
@ -36,8 +33,6 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class StringUtil
{
private static final Logger LOG = Log.getLogger(StringUtil.class);
private static final Trie<String> CHARSETS = new ArrayTrie<>(256);
public static final String ALL_INTERFACES = "0.0.0.0";
@ -481,7 +476,6 @@ public class StringUtil
}
catch (UnsupportedEncodingException e)
{
LOG.warn(e);
throw new IllegalArgumentException(e);
}
}
@ -679,7 +673,6 @@ public class StringUtil
}
catch (Exception e)
{
LOG.warn(e);
return s.getBytes();
}
}

View File

@ -106,18 +106,20 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
switch (b._managed)
{
case MANAGED:
if (!l.isRunning())
if (l.isStopped() || l.isFailed())
start(l);
break;
case AUTO:
if (l.isRunning())
unmanage(b);
else
if (l.isStopped())
{
manage(b);
start(l);
}
else
{
unmanage(b);
}
break;
default:
@ -142,7 +144,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container,
{
try
{
l.stop();
stop(l);
}
catch (Throwable th2)
{

View File

@ -33,7 +33,6 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.Uptime;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
@ -57,11 +56,6 @@ public class Log
{
public static final String EXCEPTION = "EXCEPTION ";
public static final String IGNORED = "IGNORED EXCEPTION ";
/**
* Logging Configuration Properties
*/
protected static final Properties __props;
/**
* The {@link Logger} implementation class name
*/
@ -70,60 +64,51 @@ public class Log
* Legacy flag indicating if {@link Logger#ignore(Throwable)} methods produce any output in the {@link Logger}s
*/
public static boolean __ignored;
/**
* Hold loggers only.
* Logging Configuration Properties
*/
protected static final Properties __props = new Properties();
private static final ConcurrentMap<String, Logger> __loggers = new ConcurrentHashMap<>();
private static boolean __initialized;
private static Logger LOG;
static
{
/* Instantiate a default configuration properties (empty)
*/
__props = new Properties();
AccessController.doPrivileged(new PrivilegedAction<Object>()
{
@Override
public Object run()
{
/* First see if the jetty-logging.properties object exists in the classpath.
* This is an optional feature used by embedded mode use, and test cases to allow for early
* configuration of the Log class in situations where access to the System.properties are
* either too late or just impossible.
*/
// First see if the jetty-logging.properties object exists in the classpath.
// * This is an optional feature used by embedded mode use, and test cases to allow for early
// * configuration of the Log class in situations where access to the System.properties are
// * either too late or just impossible.
loadProperties("jetty-logging.properties", __props);
/*
* Next see if an OS specific jetty-logging.properties object exists in the classpath.
* This really for setting up test specific logging behavior based on OS.
*/
// Next see if an OS specific jetty-logging.properties object exists in the classpath.
// This really for setting up test specific logging behavior based on OS.
String osName = System.getProperty("os.name");
// NOTE: cannot use jetty-util's StringUtil as that initializes logging itself.
if (osName != null && osName.length() > 0)
{
osName = StringUtil.replace(osName.toLowerCase(Locale.ENGLISH), ' ', '-');
// NOTE: cannot use jetty-util's StringUtil.replace() as it may initialize logging itself.
osName = osName.toLowerCase(Locale.ENGLISH).replace(' ', '-');
loadProperties("jetty-logging-" + osName + ".properties", __props);
}
/* Now load the System.properties as-is into the __props, these values will override
* any key conflicts in __props.
*/
// Now load the System.properties as-is into the __props,
// these values will override any key conflicts in __props.
@SuppressWarnings("unchecked")
Enumeration<String> systemKeyEnum = (Enumeration<String>)System.getProperties().propertyNames();
while (systemKeyEnum.hasMoreElements())
{
String key = systemKeyEnum.nextElement();
String val = System.getProperty(key);
// protect against application code insertion of non-String values (returned as null)
// Protect against application code insertion of non-String values (returned as null).
if (val != null)
{
__props.setProperty(key, val);
}
}
/* Now use the configuration properties to configure the Log statics
*/
// Now use the configuration properties to configure the Log statics.
__logClass = __props.getProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.Slf4jLog");
__ignored = Boolean.parseBoolean(__props.getProperty("org.eclipse.jetty.util.log.IGNORED", "false"));
return null;
@ -131,7 +116,7 @@ public class Log
});
}
static void loadProperties(String resourceName, Properties props)
private static void loadProperties(String resourceName, Properties props)
{
URL testProps = Loader.getResource(resourceName);
if (testProps != null)
@ -144,22 +129,17 @@ public class Log
{
Object value = p.get(key);
if (value != null)
{
props.put(key, value);
}
}
}
catch (IOException e)
{
System.err.println("[WARN] Error loading logging config: " + testProps);
e.printStackTrace(System.err);
e.printStackTrace();
}
}
}
private static Logger LOG;
private static boolean __initialized = false;
public static void initialized()
{
synchronized (Log.class)
@ -168,20 +148,17 @@ public class Log
return;
__initialized = true;
Boolean announce = Boolean.parseBoolean(__props.getProperty("org.eclipse.jetty.util.log.announce", "true"));
boolean announce = Boolean.parseBoolean(__props.getProperty("org.eclipse.jetty.util.log.announce", "true"));
try
{
Class<?> logClass = __logClass == null ? null : Loader.loadClass(Log.class, __logClass);
if (LOG == null || (logClass != null && !LOG.getClass().equals(logClass)))
Class<?> logClass = Loader.loadClass(Log.class, __logClass);
if (LOG == null || !LOG.getClass().equals(logClass))
{
LOG = (Logger)logClass.getDeclaredConstructor().newInstance();
if (announce)
{
LOG.debug("Logging to {} via {}", LOG, logClass.getName());
}
}
}
catch (Throwable e)
{
// Unable to load specified Logger implementation, default to standard logging.
@ -189,31 +166,17 @@ public class Log
}
if (announce && LOG != null)
{
LOG.info(String.format("Logging initialized @%dms to %s", Uptime.getUptime(), LOG.getClass().getName()));
}
}
}
private static void initStandardLogging(Throwable e)
{
Class<?> logClass;
if (e != null && __ignored)
{
e.printStackTrace(System.err);
}
if (__ignored)
e.printStackTrace();
if (LOG == null)
{
logClass = StdErrLog.class;
LOG = new StdErrLog();
boolean announce = Boolean.parseBoolean(__props.getProperty("org.eclipse.jetty.util.log.announce", "true"));
if (announce)
{
LOG.debug("Logging to {} via {}", LOG, logClass.getName());
}
}
}
public static Logger getLog()

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.StringReader;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.util.TypeUtil;
@ -129,10 +130,7 @@ public class ContainerLifeCycleTest
container.stop();
container.destroy();
assertThrows(IllegalStateException.class, () ->
{
container.start();
});
assertThrows(IllegalStateException.class, container::start);
}
@Test
@ -210,13 +208,13 @@ public class ContainerLifeCycleTest
{
ContainerLifeCycle a0 = new ContainerLifeCycle();
String dump = trim(a0.dump());
dump = check(dump, "ContainerLifeCycl");
check(dump, "ContainerLifeCycl");
ContainerLifeCycle aa0 = new ContainerLifeCycle();
a0.addBean(aa0);
dump = trim(a0.dump());
dump = check(dump, "ContainerLifeCycl");
dump = check(dump, "+? ContainerLife");
check(dump, "+? ContainerLife");
ContainerLifeCycle aa1 = new ContainerLifeCycle();
a0.addBean(aa1);
@ -224,7 +222,7 @@ public class ContainerLifeCycleTest
dump = check(dump, "ContainerLifeCycl");
dump = check(dump, "+? ContainerLife");
dump = check(dump, "+? ContainerLife");
dump = check(dump, "");
check(dump, "");
ContainerLifeCycle aa2 = new ContainerLifeCycle();
a0.addBean(aa2, false);
@ -233,7 +231,7 @@ public class ContainerLifeCycleTest
dump = check(dump, "+? ContainerLife");
dump = check(dump, "+? ContainerLife");
dump = check(dump, "+~ ContainerLife");
dump = check(dump, "");
check(dump, "");
aa1.start();
a0.start();
@ -242,7 +240,7 @@ public class ContainerLifeCycleTest
dump = check(dump, "+= ContainerLife");
dump = check(dump, "+~ ContainerLife");
dump = check(dump, "+~ ContainerLife");
dump = check(dump, "");
check(dump, "");
a0.manage(aa1);
a0.removeBean(aa2);
@ -250,7 +248,7 @@ public class ContainerLifeCycleTest
dump = check(dump, "ContainerLifeCycl");
dump = check(dump, "+= ContainerLife");
dump = check(dump, "+= ContainerLife");
dump = check(dump, "");
check(dump, "");
ContainerLifeCycle aaa0 = new ContainerLifeCycle();
aa0.addBean(aaa0);
@ -259,7 +257,7 @@ public class ContainerLifeCycleTest
dump = check(dump, "+= ContainerLife");
dump = check(dump, "| +~ Container");
dump = check(dump, "+= ContainerLife");
dump = check(dump, "");
check(dump, "");
ContainerLifeCycle aa10 = new ContainerLifeCycle();
aa1.addBean(aa10, true);
@ -269,7 +267,7 @@ public class ContainerLifeCycleTest
dump = check(dump, "| +~ Container");
dump = check(dump, "+= ContainerLife");
dump = check(dump, " += Container");
dump = check(dump, "");
check(dump, "");
final ContainerLifeCycle a1 = new ContainerLifeCycle();
final ContainerLifeCycle a2 = new ContainerLifeCycle();
@ -301,7 +299,7 @@ public class ContainerLifeCycleTest
dump = check(dump, " +> java.util.Arrays$ArrayList");
dump = check(dump, " +: ContainerLifeCycle");
dump = check(dump, " +: ContainerLifeCycle");
dump = check(dump, "");
check(dump, "");
a2.addBean(aa0, true);
dump = trim(a0.dump());
@ -319,7 +317,7 @@ public class ContainerLifeCycleTest
dump = check(dump, " +> java.util.Arrays$ArrayList");
dump = check(dump, " +: ContainerLifeCycle");
dump = check(dump, " +: ContainerLifeCycle");
dump = check(dump, "");
check(dump, "");
a2.unmanage(aa0);
dump = trim(a0.dump());
@ -336,7 +334,7 @@ public class ContainerLifeCycleTest
dump = check(dump, " +> java.util.Arrays$ArrayList");
dump = check(dump, " +: ContainerLifeCycle");
dump = check(dump, " +: ContainerLifeCycle");
dump = check(dump, "");
check(dump, "");
a0.unmanage(aa);
dump = trim(a0.dump());
@ -346,7 +344,7 @@ public class ContainerLifeCycleTest
dump = check(dump, "+= ContainerLife");
dump = check(dump, "| += Container");
dump = check(dump, "+~ ContainerLife");
dump = check(dump, "");
check(dump, "");
}
@Test
@ -504,7 +502,7 @@ public class ContainerLifeCycleTest
assertEquals(c00, child.poll());
}
private final class InheritedListenerLifeCycle extends AbstractLifeCycle implements Container.InheritedListener
private static final class InheritedListenerLifeCycle extends AbstractLifeCycle implements Container.InheritedListener
{
@Override
public void beanRemoved(Container p, Object c)
@ -627,7 +625,7 @@ public class ContainerLifeCycleTest
}
@Test
public void testGetBeans() throws Exception
public void testGetBeans()
{
TestContainerLifeCycle root = new TestContainerLifeCycle();
TestContainerLifeCycle left = new TestContainerLifeCycle();
@ -652,4 +650,94 @@ public class ContainerLifeCycleTest
assertThat(root.getContainedBeans(Integer.class), containsInAnyOrder(0, 1, 2, 3, 4));
assertThat(root.getContainedBeans(String.class), containsInAnyOrder("leaf"));
}
@Test
public void testBeanStoppingAddedToStartingBean() throws Exception
{
ContainerLifeCycle longLived = new ContainerLifeCycle()
{
@Override
protected void doStop() throws Exception
{
super.doStop();
ContainerLifeCycle shortLived = new ContainerLifeCycle();
shortLived.addBean(this);
shortLived.start();
assertTrue(shortLived.isStarted());
assertTrue(isStopping());
assertFalse(shortLived.isManaged(this));
}
};
longLived.start();
longLived.stop();
}
@Test
public void testFailedManagedBeanCanBeRestarted() throws Exception
{
AtomicBoolean fail = new AtomicBoolean();
ContainerLifeCycle container = new ContainerLifeCycle();
ContainerLifeCycle bean1 = new ContainerLifeCycle();
ContainerLifeCycle bean2 = new ContainerLifeCycle()
{
@Override
protected void doStart() throws Exception
{
super.doStart();
// Fail only the first time.
if (fail.compareAndSet(false, true))
throw new RuntimeException();
}
};
ContainerLifeCycle bean3 = new ContainerLifeCycle();
container.addBean(bean1);
container.addBean(bean2);
container.addBean(bean3);
// Start the first time, it should fail.
assertThrows(RuntimeException.class, container::start);
assertTrue(container.isFailed());
assertTrue(bean1.isStopped());
assertTrue(bean2.isFailed());
assertTrue(bean3.isStopped());
// Re-start, it should succeed.
container.start();
assertTrue(container.isStarted());
assertTrue(bean1.isStarted());
assertTrue(bean2.isStarted());
assertTrue(bean3.isStarted());
}
@Test
public void testFailedAutoBeanIsNotRestarted() throws Exception
{
AtomicBoolean fail = new AtomicBoolean();
ContainerLifeCycle bean = new ContainerLifeCycle()
{
@Override
protected void doStart() throws Exception
{
super.doStart();
// Fail only the first time.
if (fail.compareAndSet(false, true))
throw new RuntimeException();
}
};
// The bean is started externally and fails.
assertThrows(RuntimeException.class, bean::start);
// The same bean now becomes part of a container.
ContainerLifeCycle container = new ContainerLifeCycle();
container.addBean(bean);
assertTrue(container.isAuto(bean));
// Start the container, the bean must not be managed.
container.start();
assertTrue(container.isStarted());
assertTrue(bean.isFailed());
assertTrue(container.isUnmanaged(bean));
}
}