Merge branch 'master' into jetty-8
This commit is contained in:
commit
2b6fec713c
|
@ -1377,7 +1377,7 @@ public class SslContextFactory extends AbstractLifeCycle
|
|||
/** Set the trust store resource.
|
||||
* @param resource the trust store resource to set
|
||||
*/
|
||||
public void setTrustStore(Resource resource)
|
||||
public void setTrustStoreResource(Resource resource)
|
||||
{
|
||||
checkNotStarted();
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.io.FileInputStream;
|
|||
import java.security.KeyStore;
|
||||
|
||||
import org.eclipse.jetty.http.ssl.SslContextFactory;
|
||||
import org.eclipse.jetty.util.resource.Resource;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
|
@ -72,4 +73,70 @@ public class SslContextFactoryTest
|
|||
cf.start();
|
||||
assertTrue(cf.getSslContext()!=null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoTsResourceKs() throws Exception
|
||||
{
|
||||
Resource keystoreResource = Resource.newSystemResource("keystore");
|
||||
|
||||
SslContextFactory cf = new SslContextFactory();
|
||||
cf.setKeyStoreResource(keystoreResource);
|
||||
cf.setKeyStorePassword("storepwd");
|
||||
cf.setKeyManagerPassword("keypwd");
|
||||
|
||||
cf.start();
|
||||
|
||||
assertTrue(cf.getSslContext()!=null);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourceTsResourceKs() throws Exception
|
||||
{
|
||||
Resource keystoreResource = Resource.newSystemResource("keystore");
|
||||
Resource truststoreResource = Resource.newSystemResource("keystore");
|
||||
|
||||
SslContextFactory cf = new SslContextFactory();
|
||||
cf.setKeyStoreResource(keystoreResource);
|
||||
cf.setTrustStoreResource(truststoreResource);
|
||||
cf.setKeyStorePassword("storepwd");
|
||||
cf.setKeyManagerPassword("keypwd");
|
||||
cf.setTrustStorePassword("storepwd");
|
||||
|
||||
cf.start();
|
||||
|
||||
assertTrue(cf.getSslContext()!=null);
|
||||
}
|
||||
|
||||
@Test(expected = java.security.UnrecoverableKeyException.class)
|
||||
public void testResourceTsResourceKsWrongPW() throws Exception
|
||||
{
|
||||
Resource keystoreResource = Resource.newSystemResource("keystore");
|
||||
Resource truststoreResource = Resource.newSystemResource("keystore");
|
||||
|
||||
SslContextFactory cf = new SslContextFactory();
|
||||
cf.setKeyStoreResource(keystoreResource);
|
||||
cf.setTrustStoreResource(truststoreResource);
|
||||
cf.setKeyStorePassword("storepwd");
|
||||
cf.setKeyManagerPassword("wrong_keypwd");
|
||||
cf.setTrustStorePassword("storepwd");
|
||||
|
||||
cf.start();
|
||||
}
|
||||
|
||||
@Test(expected = java.io.IOException.class)
|
||||
public void testResourceTsWrongPWResourceKs() throws Exception
|
||||
{
|
||||
Resource keystoreResource = Resource.newSystemResource("keystore");
|
||||
Resource truststoreResource = Resource.newSystemResource("keystore");
|
||||
|
||||
SslContextFactory cf = new SslContextFactory();
|
||||
cf.setKeyStoreResource(keystoreResource);
|
||||
cf.setTrustStoreResource(truststoreResource);
|
||||
cf.setKeyStorePassword("storepwd");
|
||||
cf.setKeyManagerPassword("keypwd");
|
||||
cf.setTrustStorePassword("wrong_storepwd");
|
||||
|
||||
cf.start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
|
|||
private long _lowResourcesConnections;
|
||||
private SelectSet[] _selectSet;
|
||||
private int _selectSets=1;
|
||||
private volatile int _set;
|
||||
private volatile int _set=0;
|
||||
private boolean _deferringInterestedOps0=true;
|
||||
private int _selectorPriorityDelta=0;
|
||||
|
||||
|
@ -128,6 +128,8 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
|
|||
// be distributed over the available sets.
|
||||
|
||||
int s=_set++;
|
||||
if (s<0)
|
||||
s=-s;
|
||||
s=s%_selectSets;
|
||||
SelectSet[] sets=_selectSet;
|
||||
if (sets!=null)
|
||||
|
@ -150,6 +152,8 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
|
|||
// be distributed over the available sets.
|
||||
|
||||
int s=_set++;
|
||||
if (s<0)
|
||||
s=-s;
|
||||
s=s%_selectSets;
|
||||
SelectSet[] sets=_selectSet;
|
||||
if (sets!=null)
|
||||
|
@ -167,6 +171,8 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
|
|||
public void register(ServerSocketChannel acceptChannel)
|
||||
{
|
||||
int s=_set++;
|
||||
if (s<0)
|
||||
s=-s;
|
||||
s=s%_selectSets;
|
||||
SelectSet set=_selectSet[s];
|
||||
set.addChange(acceptChannel);
|
||||
|
|
|
@ -484,4 +484,14 @@ public class ProxyRule extends PatternRule
|
|||
_connectorType = connectorType;
|
||||
}
|
||||
|
||||
public String getHostHeader()
|
||||
{
|
||||
return _hostHeader;
|
||||
}
|
||||
|
||||
public void setHostHeader(String hostHeader)
|
||||
{
|
||||
_hostHeader = hostHeader;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -87,7 +87,18 @@ public class DigestAuthenticator extends LoginAuthenticator
|
|||
|
||||
String mna=configuration.getInitParameter("maxNonceAge");
|
||||
if (mna!=null)
|
||||
_maxNonceAgeMs=Long.valueOf(mna);
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
_maxNonceAgeMs=Long.valueOf(mna);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public synchronized void setMaxNonceAge(long maxNonceAgeInMillis)
|
||||
{
|
||||
_maxNonceAgeMs = maxNonceAgeInMillis;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -235,7 +246,11 @@ public class DigestAuthenticator extends LoginAuthenticator
|
|||
private int checkNonce(Digest digest, Request request)
|
||||
{
|
||||
// firstly let's expire old nonces
|
||||
long expired = request.getTimeStamp()-_maxNonceAgeMs;
|
||||
long expired;
|
||||
synchronized (this)
|
||||
{
|
||||
expired = request.getTimeStamp()-_maxNonceAgeMs;
|
||||
}
|
||||
|
||||
Nonce nonce=_nonceQueue.peek();
|
||||
while (nonce!=null && nonce._ts<expired)
|
||||
|
|
|
@ -272,6 +272,85 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
|
|||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Either set virtual hosts or add to an existing set of virtual hosts.
|
||||
*
|
||||
* @param virtualHosts
|
||||
* Array of virtual hosts that this context responds to. A null host name or null/empty array means any hostname is acceptable. Host names may be
|
||||
* String representation of IP addresses. Host names may start with '*.' to wildcard one level of names.
|
||||
*/
|
||||
public void addVirtualHosts(String[] virtualHosts)
|
||||
{
|
||||
if (virtualHosts == null) // since this is add, we don't null the old ones
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
List<String> currentVirtualHosts = null;
|
||||
if (_vhosts != null)
|
||||
{
|
||||
currentVirtualHosts = new ArrayList<String>(Arrays.asList(_vhosts));
|
||||
}
|
||||
else
|
||||
{
|
||||
currentVirtualHosts = new ArrayList<String>();
|
||||
}
|
||||
|
||||
for (int i = 0; i < virtualHosts.length; i++)
|
||||
{
|
||||
String normVhost = normalizeHostname(virtualHosts[i]);
|
||||
if (!currentVirtualHosts.contains(normVhost))
|
||||
{
|
||||
currentVirtualHosts.add(normVhost);
|
||||
}
|
||||
}
|
||||
_vhosts = currentVirtualHosts.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Removes an array of virtual host entries, if this removes all entries the _vhosts will be set to null
|
||||
*
|
||||
* @param virtualHosts
|
||||
* Array of virtual hosts that this context responds to. A null host name or null/empty array means any hostname is acceptable. Host names may be
|
||||
* String representation of IP addresses. Host names may start with '*.' to wildcard one level of names.
|
||||
*/
|
||||
public void removeVirtualHosts(String[] virtualHosts)
|
||||
{
|
||||
if (virtualHosts == null)
|
||||
{
|
||||
return; // do nothing
|
||||
}
|
||||
else if ( _vhosts == null || _vhosts.length == 0)
|
||||
{
|
||||
return; // do nothing
|
||||
}
|
||||
else
|
||||
{
|
||||
List<String> existingVirtualHosts = new ArrayList<String>(Arrays.asList(_vhosts));
|
||||
|
||||
for (int i = 0; i < virtualHosts.length; i++)
|
||||
{
|
||||
String toRemoveVirtualHost = normalizeHostname(virtualHosts[i]);
|
||||
if (existingVirtualHosts.contains(toRemoveVirtualHost))
|
||||
{
|
||||
existingVirtualHosts.remove(toRemoveVirtualHost);
|
||||
}
|
||||
}
|
||||
|
||||
if (existingVirtualHosts.isEmpty())
|
||||
{
|
||||
_vhosts = null; // if we ended up removing them all, just null out _vhosts
|
||||
}
|
||||
else
|
||||
{
|
||||
_vhosts = existingVirtualHosts.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Get the virtual hosts for the context. Only requests that have a matching host header or fully qualified URL will be passed to that context with a
|
||||
|
|
|
@ -92,7 +92,7 @@ public class SelectChannelConnector extends AbstractNIOConnector
|
|||
public void accept(int acceptorID) throws IOException
|
||||
{
|
||||
ServerSocketChannel server = _acceptChannel;
|
||||
if (server!=null && server.isOpen())
|
||||
if (server!=null && server.isOpen() && _manager.isStarted())
|
||||
{
|
||||
SocketChannel channel = _acceptChannel.accept();
|
||||
channel.configureBlocking(false);
|
||||
|
|
|
@ -232,6 +232,37 @@ public class ContextHandlerTest
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVirtualHostManagement() throws Exception
|
||||
{
|
||||
ContextHandler context = new ContextHandler("/");
|
||||
|
||||
// test singular
|
||||
context.setVirtualHosts(new String[] { "www.example.com"} );
|
||||
Assert.assertEquals(1,context.getVirtualHosts().length);
|
||||
|
||||
// test adding two more
|
||||
context.addVirtualHosts(new String[] { "www.example2.com", "www.example3.com"});
|
||||
Assert.assertEquals(3,context.getVirtualHosts().length);
|
||||
|
||||
// test adding existing context
|
||||
context.addVirtualHosts(new String[] { "www.example.com" });
|
||||
Assert.assertEquals(3,context.getVirtualHosts().length);
|
||||
|
||||
// test removing existing
|
||||
context.removeVirtualHosts(new String[] { "www.example3.com" });
|
||||
Assert.assertEquals(2,context.getVirtualHosts().length);
|
||||
|
||||
// test removing non-existent
|
||||
context.removeVirtualHosts(new String[] { "www.example3.com" });
|
||||
Assert.assertEquals(2,context.getVirtualHosts().length);
|
||||
|
||||
// test removing all remaining and resets to null
|
||||
context.removeVirtualHosts(new String[] { "www.example.com", "www.example2.com" });
|
||||
Assert.assertEquals(null,context.getVirtualHosts());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAttributes() throws Exception
|
||||
{
|
||||
|
|
|
@ -70,9 +70,9 @@
|
|||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||
<artifactId>jetty-test-helper</artifactId>
|
||||
<scope>test</scope>
|
||||
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||
<artifactId>jetty-test-helper</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -90,5 +90,16 @@
|
|||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!--
|
||||
This dependency is used to test Slf4jLog.
|
||||
Due to the introduction of src/test/resource/jetty-logging.properties (and the Log.static{} block)
|
||||
the default Log implementation is still StdErrLog during testing.
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-jdk14</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -24,17 +24,13 @@ import java.util.logging.Level;
|
|||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Honors the standard jetty system property <code>"org.eclipse.jetty.util.log.DEBUG"</code> to set logger into debug
|
||||
* mode (defaults to false, set to "true" to enable)
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* You can also set the logger level using <a href="http://java.sun.com/j2se/1.5.0/docs/guide/logging/overview.html">
|
||||
* standard java.util.logging configuration</a> against the name <code>"org.eclipse.jetty.util.log"</code>.
|
||||
* standard java.util.logging configuration</a>.
|
||||
* </p>
|
||||
*/
|
||||
public class JavaUtilLog implements Logger
|
||||
{
|
||||
private Level configuredLevel;
|
||||
private java.util.logging.Logger _logger;
|
||||
|
||||
public JavaUtilLog()
|
||||
|
@ -45,8 +41,11 @@ public class JavaUtilLog implements Logger
|
|||
public JavaUtilLog(String name)
|
||||
{
|
||||
_logger = java.util.logging.Logger.getLogger(name);
|
||||
if (Boolean.getBoolean("org.eclipse.jetty.util.log.DEBUG"))
|
||||
if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.DEBUG", "false")))
|
||||
{
|
||||
_logger.setLevel(Level.FINE);
|
||||
}
|
||||
configuredLevel = _logger.getLevel();
|
||||
}
|
||||
|
||||
public String getName()
|
||||
|
@ -91,7 +90,15 @@ public class JavaUtilLog implements Logger
|
|||
|
||||
public void setDebugEnabled(boolean enabled)
|
||||
{
|
||||
_logger.setLevel(Level.FINE);
|
||||
if (enabled)
|
||||
{
|
||||
configuredLevel = _logger.getLevel();
|
||||
_logger.setLevel(Level.FINE);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.setLevel(configuredLevel);
|
||||
}
|
||||
}
|
||||
|
||||
public void debug(String msg, Object... args)
|
||||
|
|
|
@ -13,10 +13,16 @@
|
|||
|
||||
package org.eclipse.jetty.util.log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.eclipse.jetty.util.IO;
|
||||
import org.eclipse.jetty.util.Loader;
|
||||
|
||||
/**
|
||||
|
@ -40,17 +46,69 @@ public class Log
|
|||
public final static String EXCEPTION= "EXCEPTION ";
|
||||
public final static String IGNORED= "IGNORED ";
|
||||
|
||||
/**
|
||||
* Logging Configuration Properties
|
||||
*/
|
||||
protected static Properties __props;
|
||||
/**
|
||||
* The {@link Logger} implementation class name
|
||||
*/
|
||||
public static String __logClass;
|
||||
/**
|
||||
* Legacy flag indicating if {@link Log#ignore(Throwable)} methods produce any output in the {@link Logger}s
|
||||
*/
|
||||
public static boolean __ignored;
|
||||
|
||||
static
|
||||
{
|
||||
/* Instantiate a default configuration properties (empty)
|
||||
*/
|
||||
__props = new Properties();
|
||||
|
||||
AccessController.doPrivileged(new PrivilegedAction<Object>()
|
||||
{
|
||||
public Object run()
|
||||
{
|
||||
__logClass = System.getProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.Slf4jLog");
|
||||
__ignored = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.IGNORED", "false"));
|
||||
/* 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.
|
||||
*/
|
||||
URL testProps = Log.class.getClassLoader().getResource("jetty-logging.properties");
|
||||
if (testProps != null)
|
||||
{
|
||||
InputStream in = null;
|
||||
try
|
||||
{
|
||||
in = testProps.openStream();
|
||||
__props.load(in);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.err.println("Unable to load " + testProps);
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IO.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
/* 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();
|
||||
__props.setProperty(key,System.getProperty(key));
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
});
|
||||
|
@ -62,12 +120,16 @@ public class Log
|
|||
public static boolean initialized()
|
||||
{
|
||||
if (LOG != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
synchronized (Log.class)
|
||||
{
|
||||
if (__initialized)
|
||||
{
|
||||
return LOG != null;
|
||||
}
|
||||
__initialized = true;
|
||||
}
|
||||
|
||||
|
@ -80,11 +142,14 @@ public class Log
|
|||
LOG.debug("Logging to {} via {}", LOG, log_class.getName());
|
||||
}
|
||||
}
|
||||
catch(ThreadDeath e)
|
||||
{
|
||||
// Let ThreadDeath pass through
|
||||
throw e;
|
||||
}
|
||||
catch(Throwable e)
|
||||
{
|
||||
if (e instanceof ThreadDeath)
|
||||
throw (ThreadDeath)e;
|
||||
|
||||
// Unable to load specified Logger implementation, default to standard logging.
|
||||
initStandardLogging(e);
|
||||
}
|
||||
|
||||
|
@ -95,7 +160,10 @@ public class Log
|
|||
{
|
||||
Class<?> log_class;
|
||||
if(e != null && __ignored)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (LOG == null)
|
||||
{
|
||||
log_class = StdErrLog.class;
|
||||
|
|
|
@ -25,7 +25,6 @@ public class Slf4jLog implements Logger
|
|||
public Slf4jLog() throws Exception
|
||||
{
|
||||
this("org.eclipse.jetty.util.log");
|
||||
|
||||
}
|
||||
|
||||
public Slf4jLog(String name)
|
||||
|
@ -42,6 +41,7 @@ public class Slf4jLog implements Logger
|
|||
}
|
||||
|
||||
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger( name );
|
||||
|
||||
// Fix LocationAwareLogger use to indicate FQCN of this class -
|
||||
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=276670
|
||||
if (logger instanceof org.slf4j.spi.LocationAwareLogger)
|
||||
|
|
|
@ -38,10 +38,11 @@ import org.eclipse.jetty.util.DateCache;
|
|||
public class StdErrLog implements Logger
|
||||
{
|
||||
private static DateCache _dateCache;
|
||||
private static Properties __props = Log.__props;
|
||||
|
||||
private final static boolean __source = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.SOURCE",
|
||||
System.getProperty("org.eclipse.jetty.util.log.stderr.SOURCE","false")));
|
||||
private final static boolean __long = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.stderr.LONG","false"));
|
||||
private final static boolean __source = Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.SOURCE",
|
||||
Log.__props.getProperty("org.eclipse.jetty.util.log.stderr.SOURCE","false")));
|
||||
private final static boolean __long = Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.stderr.LONG","false"));
|
||||
|
||||
/**
|
||||
* Tracking for child loggers only.
|
||||
|
@ -78,6 +79,8 @@ public class StdErrLog implements Logger
|
|||
public static final int LEVEL_WARN = 3;
|
||||
|
||||
private int _level = LEVEL_INFO;
|
||||
// Level that this Logger was configured as (remembered in special case of .setDebugEnabled())
|
||||
private int _configuredLevel;
|
||||
private PrintStream _stderr = System.err;
|
||||
private boolean _source = __source;
|
||||
// Print the long form names, otherwise use abbreviated
|
||||
|
@ -95,14 +98,16 @@ public class StdErrLog implements Logger
|
|||
|
||||
public StdErrLog(String name)
|
||||
{
|
||||
this(name,System.getProperties());
|
||||
this(name,__props);
|
||||
}
|
||||
|
||||
public StdErrLog(String name, Properties props)
|
||||
{
|
||||
__props = props;
|
||||
this._name = name == null?"":name;
|
||||
this._abbrevname = condensePackageString(this._name);
|
||||
this._level = getLoggingLevel(props,this._name);
|
||||
this._configuredLevel = this._level;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -319,18 +324,36 @@ public class StdErrLog implements Logger
|
|||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #setLevel(int)} instead.
|
||||
* Legacy interface where a programmatic configuration of the logger level
|
||||
* is done as a wholesale approach.
|
||||
*/
|
||||
@Deprecated
|
||||
public void setDebugEnabled(boolean enabled)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
_level = LEVEL_DEBUG;
|
||||
synchronized (__loggers)
|
||||
{
|
||||
this._level = LEVEL_DEBUG;
|
||||
|
||||
// Boot stomp all cached log levels to DEBUG
|
||||
for(StdErrLog log: __loggers.values())
|
||||
{
|
||||
log._level = LEVEL_DEBUG;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_level = LEVEL_INFO;
|
||||
synchronized (__loggers)
|
||||
{
|
||||
this._level = this._configuredLevel;
|
||||
|
||||
// restore all cached log configured levels
|
||||
for(StdErrLog log: __loggers.values())
|
||||
{
|
||||
log._level = log._configuredLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -600,6 +623,7 @@ public class StdErrLog implements Logger
|
|||
sel.setPrintLongNames(_printLongNames);
|
||||
// Let Level come from configured Properties instead - sel.setLevel(_level);
|
||||
sel.setSource(_source);
|
||||
sel._stderr = this._stderr;
|
||||
logger = __loggers.putIfAbsent(fullname,sel);
|
||||
if (logger == null)
|
||||
{
|
||||
|
@ -638,6 +662,11 @@ public class StdErrLog implements Logger
|
|||
return s.toString();
|
||||
}
|
||||
|
||||
public static void setProperties(Properties props)
|
||||
{
|
||||
__props = props;
|
||||
}
|
||||
|
||||
public void ignore(Throwable ignored)
|
||||
{
|
||||
if (_level <= LEVEL_ALL)
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package org.eclipse.jetty.util.log;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import org.eclipse.jetty.util.IO;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class CapturingJULHandler extends Handler
|
||||
{
|
||||
private static final String LN = System.getProperty("line.separator");
|
||||
private StringBuilder output = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void publish(LogRecord record)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(record.getLevel().getName()).append("|");
|
||||
buf.append(record.getLoggerName()).append("|");
|
||||
buf.append(record.getMessage());
|
||||
|
||||
output.append(buf);
|
||||
if (record.getMessage().length() > 0)
|
||||
{
|
||||
output.append(LN);
|
||||
}
|
||||
|
||||
if (record.getThrown() != null)
|
||||
{
|
||||
StringWriter sw = new StringWriter(128);
|
||||
PrintWriter capture = new PrintWriter(sw);
|
||||
record.getThrown().printStackTrace(capture);
|
||||
capture.flush();
|
||||
output.append(sw.toString());
|
||||
IO.close(capture);
|
||||
}
|
||||
}
|
||||
|
||||
public void clear()
|
||||
{
|
||||
output.setLength(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush()
|
||||
{
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException
|
||||
{
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
public void dump()
|
||||
{
|
||||
System.out.println(output);
|
||||
}
|
||||
|
||||
public void assertContainsLine(String line)
|
||||
{
|
||||
Assert.assertThat(output.toString(),containsString(line));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,224 @@
|
|||
package org.eclipse.jetty.util.log;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogManager;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaUtilLogTest
|
||||
{
|
||||
private static Handler[] originalHandlers;
|
||||
private static CapturingJULHandler jul;
|
||||
|
||||
@BeforeClass
|
||||
public static void setJUL()
|
||||
{
|
||||
LogManager lmgr = LogManager.getLogManager();
|
||||
java.util.logging.Logger root = lmgr.getLogger("");
|
||||
// Remember original handlers
|
||||
originalHandlers = root.getHandlers();
|
||||
// Remove original handlers
|
||||
for (Handler existing : originalHandlers)
|
||||
{
|
||||
root.removeHandler(existing);
|
||||
}
|
||||
// Set test/capturing handler
|
||||
jul = new CapturingJULHandler();
|
||||
root.addHandler(jul);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void restoreJUL()
|
||||
{
|
||||
LogManager lmgr = LogManager.getLogManager();
|
||||
java.util.logging.Logger root = lmgr.getLogger("");
|
||||
// Remove test handlers
|
||||
for (Handler existing : root.getHandlers())
|
||||
{
|
||||
root.removeHandler(existing);
|
||||
}
|
||||
// Restore original handlers
|
||||
for (Handler original : originalHandlers)
|
||||
{
|
||||
root.addHandler(original);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedLogger()
|
||||
{
|
||||
jul.clear();
|
||||
JavaUtilLog log = new JavaUtilLog("test");
|
||||
log.info("Info test");
|
||||
|
||||
jul.assertContainsLine("INFO|test|Info test");
|
||||
|
||||
JavaUtilLog loglong = new JavaUtilLog("test.a.long.name");
|
||||
loglong.info("Long test");
|
||||
|
||||
jul.assertContainsLine("INFO|test.a.long.name|Long test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDebugOutput()
|
||||
{
|
||||
jul.clear();
|
||||
|
||||
// Common Throwable (for test)
|
||||
Throwable th = new Throwable("Message");
|
||||
|
||||
// Capture raw string form
|
||||
StringWriter tout = new StringWriter();
|
||||
th.printStackTrace(new PrintWriter(tout));
|
||||
String ths = tout.toString();
|
||||
|
||||
// Tests
|
||||
JavaUtilLog log = new JavaUtilLog("test.de.bug");
|
||||
setJulLevel("test.de.bug",Level.FINE);
|
||||
|
||||
log.debug("Simple debug");
|
||||
log.debug("Debug with {} parameter",1);
|
||||
log.debug("Debug with {} {} parameters", 2, "spiffy");
|
||||
log.debug("Debug with throwable", th);
|
||||
log.debug(th);
|
||||
|
||||
// jul.dump();
|
||||
|
||||
jul.assertContainsLine("FINE|test.de.bug|Simple debug");
|
||||
jul.assertContainsLine("FINE|test.de.bug|Debug with 1 parameter");
|
||||
jul.assertContainsLine("FINE|test.de.bug|Debug with 2 spiffy parameters");
|
||||
jul.assertContainsLine("FINE|test.de.bug|Debug with throwable");
|
||||
jul.assertContainsLine(ths);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInfoOutput()
|
||||
{
|
||||
jul.clear();
|
||||
|
||||
// Common Throwable (for test)
|
||||
Throwable th = new Throwable("Message");
|
||||
|
||||
// Capture raw string form
|
||||
StringWriter tout = new StringWriter();
|
||||
th.printStackTrace(new PrintWriter(tout));
|
||||
String ths = tout.toString();
|
||||
|
||||
// Tests
|
||||
JavaUtilLog log = new JavaUtilLog("test.in.fo");
|
||||
setJulLevel("test.in.fo",Level.INFO);
|
||||
|
||||
log.info("Simple info");
|
||||
log.info("Info with {} parameter",1);
|
||||
log.info("Info with {} {} parameters", 2, "spiffy");
|
||||
log.info("Info with throwable", th);
|
||||
log.info(th);
|
||||
|
||||
// jul.dump();
|
||||
|
||||
jul.assertContainsLine("INFO|test.in.fo|Simple info");
|
||||
jul.assertContainsLine("INFO|test.in.fo|Info with 1 parameter");
|
||||
jul.assertContainsLine("INFO|test.in.fo|Info with 2 spiffy parameters");
|
||||
jul.assertContainsLine("INFO|test.in.fo|Info with throwable");
|
||||
jul.assertContainsLine(ths);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWarnOutput()
|
||||
{
|
||||
jul.clear();
|
||||
|
||||
// Common Throwable (for test)
|
||||
Throwable th = new Throwable("Message");
|
||||
|
||||
// Capture raw string form
|
||||
StringWriter tout = new StringWriter();
|
||||
th.printStackTrace(new PrintWriter(tout));
|
||||
String ths = tout.toString();
|
||||
|
||||
// Tests
|
||||
JavaUtilLog log = new JavaUtilLog("test.wa.rn");
|
||||
setJulLevel("test.wa.rn",Level.WARNING);
|
||||
|
||||
log.warn("Simple warn");
|
||||
log.warn("Warn with {} parameter",1);
|
||||
log.warn("Warn with {} {} parameters", 2, "spiffy");
|
||||
log.warn("Warn with throwable", th);
|
||||
log.warn(th);
|
||||
|
||||
// jul.dump();
|
||||
|
||||
jul.assertContainsLine("WARNING|test.wa.rn|Simple warn");
|
||||
jul.assertContainsLine("WARNING|test.wa.rn|Warn with 1 parameter");
|
||||
jul.assertContainsLine("WARNING|test.wa.rn|Warn with 2 spiffy parameters");
|
||||
jul.assertContainsLine("WARNING|test.wa.rn|Warn with throwable");
|
||||
jul.assertContainsLine(ths);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormattingWithNulls()
|
||||
{
|
||||
jul.clear();
|
||||
|
||||
JavaUtilLog log = new JavaUtilLog("test.nu.ll");
|
||||
setJulLevel("test.nu.ll",Level.INFO);
|
||||
|
||||
log.info("Testing info(msg,null,null) - {} {}","arg0","arg1");
|
||||
log.info("Testing info(msg,null,null) - {}/{}",null,null);
|
||||
log.info("Testing info(msg,null,null) > {}",null,null);
|
||||
log.info("Testing info(msg,null,null)",null,null);
|
||||
log.info(null,"Testing","info(null,arg0,arg1)");
|
||||
log.info(null,null,null);
|
||||
|
||||
jul.dump();
|
||||
|
||||
jul.assertContainsLine("INFO|test.nu.ll|Testing info(msg,null,null) - null/null");
|
||||
jul.assertContainsLine("INFO|test.nu.ll|Testing info(msg,null,null) > null null");
|
||||
jul.assertContainsLine("INFO|test.nu.ll|Testing info(msg,null,null) null null");
|
||||
jul.assertContainsLine("INFO|test.nu.ll|null Testing info(null,arg0,arg1)");
|
||||
jul.assertContainsLine("INFO|test.nu.ll|null null null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDebugEnabled() {
|
||||
JavaUtilLog log = new JavaUtilLog("test.legacy");
|
||||
|
||||
setJulLevel("test.legacy",Level.ALL);
|
||||
Assert.assertThat("log.level(all).isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||
|
||||
setJulLevel("test.legacy",Level.FINEST);
|
||||
Assert.assertThat("log.level(finest).isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||
|
||||
setJulLevel("test.legacy",Level.FINER);
|
||||
Assert.assertThat("log.level(finer).isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||
|
||||
setJulLevel("test.legacy",Level.FINE);
|
||||
Assert.assertThat("log.level(fine).isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||
|
||||
setJulLevel("test.legacy",Level.INFO);
|
||||
Assert.assertThat("log.level(info).isDebugEnabled", log.isDebugEnabled(), is(false));
|
||||
|
||||
setJulLevel("test.legacy",Level.WARNING);
|
||||
Assert.assertThat("log.level(warn).isDebugEnabled", log.isDebugEnabled(), is(false));
|
||||
|
||||
log.setDebugEnabled(true);
|
||||
Assert.assertThat("log.isDebugEnabled", log.isDebugEnabled(), is(true));
|
||||
|
||||
log.setDebugEnabled(false);
|
||||
Assert.assertThat("log.isDebugEnabled", log.isDebugEnabled(), is(false));
|
||||
}
|
||||
|
||||
private void setJulLevel(String name, Level lvl)
|
||||
{
|
||||
java.util.logging.Logger log = java.util.logging.Logger.getLogger(name);
|
||||
log.setLevel(lvl);
|
||||
}
|
||||
}
|
|
@ -13,165 +13,71 @@
|
|||
|
||||
package org.eclipse.jetty.util.log;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class LogTest
|
||||
{
|
||||
static PrintStream _orig= System.err;
|
||||
static ByteArrayOutputStream _out = new ByteArrayOutputStream();
|
||||
static PrintStream _pout = new PrintStream(_out);
|
||||
|
||||
private static Logger originalLogger;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@BeforeClass
|
||||
public static void setUp()
|
||||
public static void rememberOriginalLogger()
|
||||
{
|
||||
System.setErr(_pout);
|
||||
originalLogger = Log.getLog();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown()
|
||||
public static void restoreOriginalLogger()
|
||||
{
|
||||
System.setErr(_orig);
|
||||
}
|
||||
|
||||
private void logNotContains(String text)
|
||||
{
|
||||
_pout.flush();
|
||||
String err = _out.toString();
|
||||
_out.reset();
|
||||
|
||||
if (err.indexOf(text)<0)
|
||||
return;
|
||||
|
||||
_orig.println("FAIL '"+text+"' in '"+err+"'");
|
||||
|
||||
assertTrue(false);
|
||||
}
|
||||
|
||||
private void logContains(String text)
|
||||
{
|
||||
_pout.flush();
|
||||
String err = _out.toString();
|
||||
_out.reset();
|
||||
|
||||
err = err.replaceAll("\r\n","\n");
|
||||
text = text.replaceAll("\r\n","\n");
|
||||
|
||||
if (err.indexOf(text)>=0)
|
||||
return;
|
||||
|
||||
_orig.println("FAIL '"+text+"' not in '"+err+"'");
|
||||
assertTrue(false);
|
||||
Log.setLog(originalLogger);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStdErrLogFormat()
|
||||
public void testDefaultLogging()
|
||||
{
|
||||
StdErrLog log = new StdErrLog(LogTest.class.getName());
|
||||
Logger log = Log.getLogger(LogTest.class);
|
||||
log.info("Test default logging");
|
||||
}
|
||||
|
||||
log.info("testing:{},{}","test","format");
|
||||
logContains("INFO:oejul.LogTest:testing:test,format");
|
||||
// @Test
|
||||
public void testNamedLogNamed_StdErrLog()
|
||||
{
|
||||
Log.setLog(new StdErrLog());
|
||||
|
||||
log.info("testing:{}","test","format");
|
||||
logContains("INFO:oejul.LogTest:testing:test format");
|
||||
assertNamedLogging(Red.class);
|
||||
assertNamedLogging(Blue.class);
|
||||
assertNamedLogging(Green.class);
|
||||
}
|
||||
|
||||
log.info("testing","test","format");
|
||||
logContains("INFO:oejul.LogTest:testing test format");
|
||||
@Test
|
||||
public void testNamedLogNamed_JUL()
|
||||
{
|
||||
Log.setLog(new JavaUtilLog());
|
||||
|
||||
log.info("testing:{},{}","test",null);
|
||||
logContains("INFO:oejul.LogTest:testing:test,null");
|
||||
assertNamedLogging(Red.class);
|
||||
assertNamedLogging(Blue.class);
|
||||
assertNamedLogging(Green.class);
|
||||
}
|
||||
|
||||
log.info("testing {} {}",null,null);
|
||||
logContains("INFO:oejul.LogTest:testing null null");
|
||||
@Test
|
||||
public void testNamedLogNamed_Slf4J() throws Exception
|
||||
{
|
||||
Log.setLog(new Slf4jLog());
|
||||
|
||||
log.info("testing:{}",null,null);
|
||||
logContains("INFO:oejul.LogTest:testing:null");
|
||||
|
||||
log.info("testing",null,null);
|
||||
logContains("INFO:oejul.LogTest:testing");
|
||||
assertNamedLogging(Red.class);
|
||||
assertNamedLogging(Blue.class);
|
||||
assertNamedLogging(Green.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void testStdErrLogDebug()
|
||||
private void assertNamedLogging(Class<?> clazz)
|
||||
{
|
||||
StdErrLog log = new StdErrLog("xxx");
|
||||
|
||||
log.setLevel(StdErrLog.LEVEL_DEBUG);
|
||||
log.debug("testing {} {}","test","debug");
|
||||
logContains("DBUG:xxx:testing test debug");
|
||||
|
||||
log.info("testing {} {}","test","info");
|
||||
logContains("INFO:xxx:testing test info");
|
||||
|
||||
log.warn("testing {} {}","test","warn");
|
||||
logContains("WARN:xxx:testing test warn");
|
||||
|
||||
log.setLevel(StdErrLog.LEVEL_INFO);
|
||||
log.debug("YOU SHOULD NOT SEE THIS!",null,null);
|
||||
logNotContains("YOU SHOULD NOT SEE THIS!");
|
||||
|
||||
// Test for backward compat with old (now deprecated) method
|
||||
log.setDebugEnabled(true);
|
||||
log.debug("testing {} {}","test","debug-deprecated");
|
||||
logContains("DBUG:xxx:testing test debug-deprecated");
|
||||
|
||||
log.setDebugEnabled(false);
|
||||
log.debug("testing {} {}","test","debug-deprecated-false");
|
||||
logNotContains("DBUG:xxx:testing test debug-depdeprecated-false");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStdErrLogName()
|
||||
{
|
||||
StdErrLog log = new StdErrLog("test");
|
||||
log.setPrintLongNames(true);
|
||||
Assert.assertEquals("test",log.getName());
|
||||
|
||||
Logger next=log.getLogger("next");
|
||||
|
||||
Assert.assertEquals("test.next",next.getName());
|
||||
|
||||
next.info("testing {} {}","next","info");
|
||||
logContains(":test.next:testing next info");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStdErrThrowable()
|
||||
{
|
||||
Throwable th = new Throwable("Message");
|
||||
|
||||
th.printStackTrace();
|
||||
_pout.flush();
|
||||
String ths = _out.toString();
|
||||
_out.reset();
|
||||
|
||||
|
||||
StdErrLog log = new StdErrLog("test");
|
||||
log.warn("ex",th);
|
||||
|
||||
logContains(ths);
|
||||
|
||||
th = new Throwable("Message with \033 escape");
|
||||
|
||||
log.warn("ex",th);
|
||||
logNotContains("Message with \033 escape");
|
||||
log.info(th.toString());
|
||||
logNotContains("Message with \033 escape");
|
||||
|
||||
log.warn("ex",th);
|
||||
logContains("Message with ? escape");
|
||||
log.info(th.toString());
|
||||
logContains("Message with ? escape");
|
||||
Logger lc = Log.getLogger(clazz);
|
||||
Assert.assertThat("Named logging (impl=" + Log.getLog().getClass().getName() + ")",lc.getName(),is(clazz.getName()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,43 +1,24 @@
|
|||
package org.eclipse.jetty.util.log;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NamedLogTest
|
||||
{
|
||||
private PrintStream orig;
|
||||
private ByteArrayOutputStream logstream;
|
||||
private PrintStream perr;
|
||||
private Logger origLogger;
|
||||
private static Logger originalLogger;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
@SuppressWarnings("deprecation")
|
||||
@BeforeClass
|
||||
public static void rememberOriginalLogger()
|
||||
{
|
||||
origLogger = Log.getRootLogger();
|
||||
|
||||
orig = System.err;
|
||||
logstream = new ByteArrayOutputStream();
|
||||
perr = new PrintStream(logstream);
|
||||
System.setErr(perr);
|
||||
|
||||
StdErrLog logger = new StdErrLog();
|
||||
Log.setLog(logger);
|
||||
originalLogger = Log.getLog();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown()
|
||||
@AfterClass
|
||||
public static void restoreOriginalLogger()
|
||||
{
|
||||
System.out.println(logstream.toString());
|
||||
System.setErr(orig);
|
||||
|
||||
Log.setLog(origLogger);
|
||||
Log.setLog(originalLogger);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -47,28 +28,31 @@ public class NamedLogTest
|
|||
Green green = new Green();
|
||||
Blue blue = new Blue();
|
||||
|
||||
setLoggerOptions(Red.class);
|
||||
setLoggerOptions(Green.class);
|
||||
setLoggerOptions(Blue.class);
|
||||
StdErrCapture output = new StdErrCapture();
|
||||
|
||||
setLoggerOptions(Red.class,output);
|
||||
setLoggerOptions(Green.class,output);
|
||||
setLoggerOptions(Blue.class,output);
|
||||
|
||||
red.generateLogs();
|
||||
green.generateLogs();
|
||||
blue.generateLogs();
|
||||
|
||||
String rawlog = logstream.toString();
|
||||
|
||||
Assert.assertThat(rawlog,containsString(Red.class.getName()));
|
||||
Assert.assertThat(rawlog,containsString(Green.class.getName()));
|
||||
Assert.assertThat(rawlog,containsString(Blue.class.getName()));
|
||||
output.assertContains(Red.class.getName());
|
||||
output.assertContains(Green.class.getName());
|
||||
output.assertContains(Blue.class.getName());
|
||||
}
|
||||
|
||||
private void setLoggerOptions(Class<?> clazz)
|
||||
private void setLoggerOptions(Class<?> clazz, StdErrCapture output)
|
||||
{
|
||||
Logger logger = Log.getLogger(clazz);
|
||||
logger.setDebugEnabled(true);
|
||||
|
||||
if(logger instanceof StdErrLog) {
|
||||
((StdErrLog)logger).setPrintLongNames(true);
|
||||
if (logger instanceof StdErrLog)
|
||||
{
|
||||
StdErrLog sel = (StdErrLog)logger;
|
||||
sel.setPrintLongNames(true);
|
||||
output.capture(sel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package org.eclipse.jetty.util.log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
import org.junit.Assume;
|
||||
|
||||
public final class Slf4jHelper
|
||||
{
|
||||
public static ClassLoader createTestClassLoader(ClassLoader parentClassLoader) throws MalformedURLException
|
||||
{
|
||||
File testJarDir = MavenTestingUtils.getTargetFile("test-jars");
|
||||
Assume.assumeTrue(testJarDir.exists()); // trigger @Ignore if dir not there
|
||||
|
||||
File jarfiles[] = testJarDir.listFiles(new FileFilter()
|
||||
{
|
||||
public boolean accept(File path)
|
||||
{
|
||||
if (!path.isFile())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return path.getName().endsWith(".jar");
|
||||
}
|
||||
});
|
||||
|
||||
Assume.assumeTrue(jarfiles.length > 0); // trigger @Ignore if no jar files.
|
||||
|
||||
URL urls[] = new URL[jarfiles.length];
|
||||
for (int i = 0; i < jarfiles.length; i++)
|
||||
{
|
||||
urls[i] = jarfiles[i].toURI().toURL();
|
||||
// System.out.println("Adding test-jar => " + urls[i]);
|
||||
}
|
||||
|
||||
return new URLClassLoader(urls,parentClassLoader);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package org.eclipse.jetty.util.log;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class StdErrCapture
|
||||
{
|
||||
private ByteArrayOutputStream test;
|
||||
private PrintStream err;
|
||||
|
||||
public StdErrCapture(StdErrLog log)
|
||||
{
|
||||
this();
|
||||
log.setStdErrStream(err);
|
||||
}
|
||||
|
||||
public StdErrCapture()
|
||||
{
|
||||
test = new ByteArrayOutputStream();
|
||||
err = new PrintStream(test);
|
||||
}
|
||||
|
||||
public void capture(StdErrLog log)
|
||||
{
|
||||
log.setStdErrStream(err);
|
||||
}
|
||||
|
||||
public void assertContains(String expectedString)
|
||||
{
|
||||
err.flush();
|
||||
String output = new String(test.toByteArray());
|
||||
Assert.assertThat(output,containsString(expectedString));
|
||||
}
|
||||
|
||||
public void assertNotContains(String unexpectedString)
|
||||
{
|
||||
err.flush();
|
||||
String output = new String(test.toByteArray());
|
||||
Assert.assertThat(output,not(containsString(unexpectedString)));
|
||||
}
|
||||
}
|
|
@ -17,6 +17,8 @@ import static org.hamcrest.Matchers.*;
|
|||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -28,6 +30,104 @@ import org.junit.Test;
|
|||
*/
|
||||
public class StdErrLogTest
|
||||
{
|
||||
@Test
|
||||
public void testStdErrLogFormat() throws UnsupportedEncodingException
|
||||
{
|
||||
StdErrLog log = new StdErrLog(LogTest.class.getName());
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
log.info("testing:{},{}","test","format1");
|
||||
log.info("testing:{}","test","format2");
|
||||
log.info("testing","test","format3");
|
||||
log.info("testing:{},{}","test",null);
|
||||
log.info("testing {} {}",null,null);
|
||||
log.info("testing:{}",null,null);
|
||||
log.info("testing",null,null);
|
||||
|
||||
output.assertContains("INFO:oejul.LogTest:testing:test,format1");
|
||||
output.assertContains("INFO:oejul.LogTest:testing:test,format1");
|
||||
output.assertContains("INFO:oejul.LogTest:testing:test format2");
|
||||
output.assertContains("INFO:oejul.LogTest:testing test format3");
|
||||
output.assertContains("INFO:oejul.LogTest:testing:test,null");
|
||||
output.assertContains("INFO:oejul.LogTest:testing null null");
|
||||
output.assertContains("INFO:oejul.LogTest:testing:null");
|
||||
output.assertContains("INFO:oejul.LogTest:testing");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStdErrLogDebug()
|
||||
{
|
||||
StdErrLog log = new StdErrLog("xxx");
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
log.setLevel(StdErrLog.LEVEL_DEBUG);
|
||||
log.debug("testing {} {}","test","debug");
|
||||
log.info("testing {} {}","test","info");
|
||||
log.warn("testing {} {}","test","warn");
|
||||
log.setLevel(StdErrLog.LEVEL_INFO);
|
||||
log.debug("YOU SHOULD NOT SEE THIS!",null,null);
|
||||
|
||||
// Test for backward compat with old (now deprecated) method
|
||||
log.setDebugEnabled(true);
|
||||
log.debug("testing {} {}","test","debug-deprecated");
|
||||
|
||||
log.setDebugEnabled(false);
|
||||
log.debug("testing {} {}","test","debug-deprecated-false");
|
||||
|
||||
output.assertContains("DBUG:xxx:testing test debug");
|
||||
output.assertContains("INFO:xxx:testing test info");
|
||||
output.assertContains("WARN:xxx:testing test warn");
|
||||
output.assertNotContains("YOU SHOULD NOT SEE THIS!");
|
||||
output.assertContains("DBUG:xxx:testing test debug-deprecated");
|
||||
output.assertNotContains("DBUG:xxx:testing test debug-depdeprecated-false");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStdErrLogName()
|
||||
{
|
||||
StdErrLog log = new StdErrLog("test");
|
||||
log.setPrintLongNames(true);
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
Assert.assertThat("Log.name", log.getName(), is("test"));
|
||||
Logger next=log.getLogger("next");
|
||||
Assert.assertThat("Log.name(child)", next.getName(), is("test.next"));
|
||||
next.info("testing {} {}","next","info");
|
||||
|
||||
output.assertContains(":test.next:testing next info");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStdErrThrowable()
|
||||
{
|
||||
// Common Throwable (for test)
|
||||
Throwable th = new Throwable("Message");
|
||||
|
||||
// Capture raw string form
|
||||
StringWriter tout = new StringWriter();
|
||||
th.printStackTrace(new PrintWriter(tout));
|
||||
String ths = tout.toString();
|
||||
|
||||
// Start test
|
||||
StdErrLog log = new StdErrLog("test");
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
log.warn("ex",th);
|
||||
output.assertContains(ths);
|
||||
|
||||
th = new Throwable("Message with \033 escape");
|
||||
|
||||
log.warn("ex",th);
|
||||
output.assertNotContains("Message with \033 escape");
|
||||
log.info(th.toString());
|
||||
output.assertNotContains("Message with \033 escape");
|
||||
|
||||
log.warn("ex",th);
|
||||
output.assertContains("Message with ? escape");
|
||||
log.info(th.toString());
|
||||
output.assertContains("Message with ? escape");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure that using a Null parameter on parameterized messages does not result in a NPE
|
||||
*/
|
||||
|
@ -183,9 +283,7 @@ public class StdErrLogTest
|
|||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
|
||||
log.setHideStacks(false);
|
||||
|
||||
ByteArrayOutputStream test = new ByteArrayOutputStream();
|
||||
PrintStream err = new PrintStream(test);
|
||||
log.setStdErrStream(err);
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
// Start with default level
|
||||
log.warn("See Me");
|
||||
|
@ -202,16 +300,15 @@ public class StdErrLogTest
|
|||
log.warn(new Throwable("scene lost"));
|
||||
|
||||
// Validate Output
|
||||
String output = new String(test.toByteArray(),"UTF-8");
|
||||
// System.err.print(output);
|
||||
Assert.assertThat(output,containsString("See Me"));
|
||||
Assert.assertThat(output,containsString("Hear Me"));
|
||||
Assert.assertThat(output,containsString("Cheer Me"));
|
||||
output.assertContains("See Me");
|
||||
output.assertContains("Hear Me");
|
||||
output.assertContains("Cheer Me");
|
||||
|
||||
// Validate Stack Traces
|
||||
Assert.assertThat(output,containsString(".StdErrLogTest:<zoom>"));
|
||||
Assert.assertThat(output,containsString("java.lang.Throwable: out of focus"));
|
||||
Assert.assertThat(output,containsString("java.lang.Throwable: scene lost"));
|
||||
output.assertContains(".StdErrLogTest:<zoom>");
|
||||
output.assertContains("java.lang.Throwable: out of focus");
|
||||
output.assertContains("java.lang.Throwable: scene lost");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -225,9 +322,7 @@ public class StdErrLogTest
|
|||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
|
||||
log.setHideStacks(false);
|
||||
|
||||
ByteArrayOutputStream test = new ByteArrayOutputStream();
|
||||
PrintStream err = new PrintStream(test);
|
||||
log.setStdErrStream(err);
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
// Normal/Default behavior
|
||||
log.info("I will not buy");
|
||||
|
@ -249,20 +344,18 @@ public class StdErrLogTest
|
|||
log.info("<spoken line>", new Throwable("on editing room floor"));
|
||||
|
||||
// Validate Output
|
||||
String output = new String(test.toByteArray(),"UTF-8");
|
||||
// System.err.print(output);
|
||||
Assert.assertThat(output,containsString("I will not buy"));
|
||||
Assert.assertThat(output,containsString("this record"));
|
||||
Assert.assertThat(output,containsString("it is scratched."));
|
||||
Assert.assertThat(output,not(containsString("sorry?")));
|
||||
output.assertContains("I will not buy");
|
||||
output.assertContains("this record");
|
||||
output.assertContains("it is scratched.");
|
||||
output.assertNotContains("sorry?");
|
||||
|
||||
// Validate Stack Traces
|
||||
Assert.assertThat(output,not(containsString("<spoken line>")));
|
||||
Assert.assertThat(output,not(containsString("on editing room floor")));
|
||||
output.assertNotContains("<spoken line>");
|
||||
output.assertNotContains("on editing room floor");
|
||||
|
||||
Assert.assertThat(output,containsString(".StdErrLogTest:<zoom>"));
|
||||
Assert.assertThat(output,containsString("java.lang.Throwable: out of focus"));
|
||||
Assert.assertThat(output,containsString("java.lang.Throwable: scene lost"));
|
||||
output.assertContains(".StdErrLogTest:<zoom>");
|
||||
output.assertContains("java.lang.Throwable: out of focus");
|
||||
output.assertContains("java.lang.Throwable: scene lost");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -276,9 +369,7 @@ public class StdErrLogTest
|
|||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
|
||||
log.setHideStacks(true);
|
||||
|
||||
ByteArrayOutputStream test = new ByteArrayOutputStream();
|
||||
PrintStream err = new PrintStream(test);
|
||||
log.setStdErrStream(err);
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
// Normal/Default behavior
|
||||
log.debug("Tobacconist");
|
||||
|
@ -300,20 +391,19 @@ public class StdErrLogTest
|
|||
log.debug("what?");
|
||||
|
||||
// Validate Output
|
||||
String output = new String(test.toByteArray(),"UTF-8");
|
||||
// System.err.print(output);
|
||||
Assert.assertThat(output,not(containsString("Tobacconist")));
|
||||
Assert.assertThat(output,containsString("my hovercraft is"));
|
||||
Assert.assertThat(output,containsString("full of eels."));
|
||||
Assert.assertThat(output,not(containsString("what?")));
|
||||
output.assertNotContains("Tobacconist");
|
||||
output.assertContains("my hovercraft is");
|
||||
output.assertContains("full of eels.");
|
||||
output.assertNotContains("what?");
|
||||
|
||||
// Validate Stack Traces
|
||||
Assert.assertThat(output,not(containsString("<spoken line>")));
|
||||
Assert.assertThat(output,not(containsString("on editing room floor")));
|
||||
output.assertNotContains("<spoken line>");
|
||||
output.assertNotContains("on editing room floor");
|
||||
|
||||
Assert.assertThat(output,containsString(".StdErrLogTest:<zoom>"));
|
||||
Assert.assertThat(output,containsString("java.lang.Throwable: out of focus"));
|
||||
Assert.assertThat(output,containsString("java.lang.Throwable: scene lost"));
|
||||
output.assertContains(".StdErrLogTest:<zoom>");
|
||||
output.assertContains("java.lang.Throwable: out of focus");
|
||||
output.assertContains("java.lang.Throwable: scene lost");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -327,9 +417,7 @@ public class StdErrLogTest
|
|||
StdErrLog log = new StdErrLog(StdErrLogTest.class.getName());
|
||||
log.setHideStacks(true);
|
||||
|
||||
ByteArrayOutputStream test = new ByteArrayOutputStream();
|
||||
PrintStream err = new PrintStream(test);
|
||||
log.setStdErrStream(err);
|
||||
StdErrCapture output = new StdErrCapture(log);
|
||||
|
||||
// Normal/Default behavior
|
||||
log.ignore(new Throwable("IGNORE ME"));
|
||||
|
@ -343,11 +431,10 @@ public class StdErrLogTest
|
|||
log.ignore(new Throwable("Debug me"));
|
||||
|
||||
// Validate Output
|
||||
String output = new String(test.toByteArray(),"UTF-8");
|
||||
// System.err.print(output);
|
||||
Assert.assertThat(output,not(containsString("IGNORE ME")));
|
||||
Assert.assertThat(output,containsString("Don't ignore me"));
|
||||
Assert.assertThat(output,not(containsString("Debug me")));
|
||||
output.assertNotContains("IGNORE ME");
|
||||
output.assertContains("Don't ignore me");
|
||||
output.assertNotContains("Debug me");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -510,4 +597,50 @@ public class StdErrLogTest
|
|||
Assert.assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfiguredAndSetDebugEnabled()
|
||||
{
|
||||
Properties props = new Properties();
|
||||
props.setProperty("org.eclipse.jetty.util.LEVEL","WARN");
|
||||
props.setProperty("org.eclipse.jetty.io.LEVEL", "WARN");
|
||||
|
||||
StdErrLog root = new StdErrLog("", props);
|
||||
assertLevel(root,StdErrLog.LEVEL_INFO); // default
|
||||
|
||||
StdErrLog log = (StdErrLog)root.getLogger(StdErrLogTest.class.getName());
|
||||
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
|
||||
assertLevel(log,StdErrLog.LEVEL_WARN); // as configured
|
||||
|
||||
// Boot stomp it all to debug
|
||||
root.setDebugEnabled(true);
|
||||
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(true));
|
||||
assertLevel(log,StdErrLog.LEVEL_DEBUG); // as stomped
|
||||
|
||||
// Restore configured
|
||||
root.setDebugEnabled(false);
|
||||
Assert.assertThat("Log.isDebugEnabled()", log.isDebugEnabled(), is(false));
|
||||
assertLevel(log,StdErrLog.LEVEL_WARN); // as configured
|
||||
}
|
||||
|
||||
private void assertLevel(StdErrLog log, int expectedLevel)
|
||||
{
|
||||
Assert.assertThat("Log[" + log.getName() + "].level",levelToString(log.getLevel()),is(levelToString(expectedLevel)));
|
||||
}
|
||||
|
||||
private String levelToString(int level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case StdErrLog.LEVEL_ALL:
|
||||
return "ALL";
|
||||
case StdErrLog.LEVEL_DEBUG:
|
||||
return "DEBUG";
|
||||
case StdErrLog.LEVEL_INFO:
|
||||
return "INFO";
|
||||
case StdErrLog.LEVEL_WARN:
|
||||
return "WARN";
|
||||
default:
|
||||
return Integer.toString(level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
# Setup default logging implementation for during testing
|
||||
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
|
|
@ -316,7 +316,14 @@ public class WebSocketClientFactory extends AggregateLifeCycle
|
|||
|
||||
String path=_future.getURI().getPath();
|
||||
if (path==null || path.length()==0)
|
||||
{
|
||||
path="/";
|
||||
}
|
||||
|
||||
if(_future.getURI().getRawQuery() != null)
|
||||
{
|
||||
path += "?" + _future.getURI().getRawQuery();
|
||||
}
|
||||
|
||||
String origin = future.getOrigin();
|
||||
|
||||
|
|
|
@ -37,14 +37,6 @@
|
|||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- DO NOT DEPLOY (or Release) -->
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<modules>
|
||||
|
|
|
@ -26,15 +26,23 @@
|
|||
<artifactId>test-hash-sessions</artifactId>
|
||||
<name>Jetty Tests :: Sessions :: Hash</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- DO NOT DEPLOY (or Release) -->
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -26,15 +26,23 @@
|
|||
<artifactId>test-jdbc-sessions</artifactId>
|
||||
<name>Jetty Tests :: Sessions :: JDBC</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- DO NOT DEPLOY (or Release) -->
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -27,6 +27,16 @@
|
|||
<name>Jetty Tests :: WebApps :: Parent</name>
|
||||
<packaging>pom</packaging>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- DO NOT DEPLOY (or Release) -->
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<modules>
|
||||
<module>test-webapp-rfc2616</module>
|
||||
|
|
|
@ -27,6 +27,16 @@
|
|||
<name>Jetty Tests :: WebApp :: RFC2616</name>
|
||||
<packaging>war</packaging>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- DO NOT DEPLOY (or Release) -->
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
Loading…
Reference in New Issue