From bb0a3201024263b065c6422eecffbcaf95298b89 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 10 Jan 2012 17:41:32 +0100 Subject: [PATCH] Improved toString(). --- .../org/eclipse/jetty/io/AbstractBuffers.java | 11 ++- .../org/eclipse/jetty/io/PooledBuffers.java | 22 +++--- .../jetty/server/AbstractConnector.java | 53 ++++++------- .../jetty/util/ssl/SslContextFactory.java | 75 ++++++++++--------- 4 files changed, 78 insertions(+), 83 deletions(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractBuffers.java b/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractBuffers.java index bc24d078ae9..b94710af5ca 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractBuffers.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractBuffers.java @@ -2,7 +2,6 @@ package org.eclipse.jetty.io; import org.eclipse.jetty.io.nio.DirectNIOBuffer; import org.eclipse.jetty.io.nio.IndirectNIOBuffer; -import org.omg.stub.java.rmi._Remote_Stub; public abstract class AbstractBuffers implements Buffers { @@ -59,7 +58,7 @@ public abstract class AbstractBuffers implements Buffers } throw new IllegalStateException(); } - + /* ------------------------------------------------------------ */ /** * Create a new content Buffer @@ -78,7 +77,7 @@ public abstract class AbstractBuffers implements Buffers } throw new IllegalStateException(); } - + /* ------------------------------------------------------------ */ /** * Create a new content Buffer @@ -142,10 +141,10 @@ public abstract class AbstractBuffers implements Buffers } return false; } - + /* ------------------------------------------------------------ */ public String toString() { - return this.getClass().getSimpleName()+"["+_headerSize+","+_bufferSize+"]"; + return String.format("%s [%d,%d]", getClass().getSimpleName(), _headerSize, _bufferSize); } -} \ No newline at end of file +} diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/PooledBuffers.java b/jetty-io/src/main/java/org/eclipse/jetty/io/PooledBuffers.java index 9c476312544..194eb9bf71b 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/PooledBuffers.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/PooledBuffers.java @@ -25,7 +25,7 @@ public class PooledBuffers extends AbstractBuffers _otherBuffers=bufferType==otherType; _maxSize=maxSize; } - + /* ------------------------------------------------------------ */ public Buffer getHeader() { @@ -55,17 +55,17 @@ public class PooledBuffers extends AbstractBuffers return getHeader(); if (_otherBuffers && size==getBufferSize()) return getBuffer(); - + // Look for an other buffer Buffer buffer = _others.poll(); - + // consume all other buffers until one of the right size is found while (buffer!=null && buffer.capacity()!=size) { _size.decrementAndGet(); buffer = _others.poll(); } - + if (buffer==null) buffer=newBuffer(size); else @@ -89,16 +89,16 @@ public class PooledBuffers extends AbstractBuffers else if (isBuffer(buffer)) _buffers.add(buffer); else - _others.add(buffer); + _others.add(buffer); } } - + public String toString() { - return this.getClass().getSimpleName()+ - "["+ - _headers.size()+"/"+_maxSize+"@"+_headerSize+","+ - _buffers.size()+"/"+_maxSize+"@"+_bufferSize+","+ - _others.size()+"/"+_maxSize+"@-]"; + return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]", + getClass().getSimpleName(), + _headers.size(),_maxSize,_headerSize, + _buffers.size(),_maxSize,_bufferSize, + _others.size(),_maxSize); } } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java index 37ae728d121..7a95f4d5fd2 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java @@ -18,7 +18,6 @@ import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.concurrent.atomic.AtomicLong; - import javax.servlet.ServletRequest; import org.eclipse.jetty.http.HttpBuffers; @@ -34,7 +33,6 @@ import org.eclipse.jetty.io.EofException; import org.eclipse.jetty.util.component.AbstractLifeCycle; import org.eclipse.jetty.util.component.AggregateLifeCycle; import org.eclipse.jetty.util.component.Dumpable; -import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.statistic.CounterStatistic; @@ -87,7 +85,7 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht protected int _lowResourceMaxIdleTime = -1; protected int _soLingerTime = -1; - private transient Thread[] _acceptorThread; + private transient Thread[] _acceptorThreads; private final AtomicLong _statsStartedAt = new AtomicLong(-1L); @@ -99,7 +97,7 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht private final SampleStatistic _connectionDurationStats = new SampleStatistic(); protected final HttpBuffersImpl _buffers = new HttpBuffersImpl(); - + /* ------------------------------------------------------------ */ /** */ @@ -312,15 +310,15 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht _threadPool = _server.getThreadPool(); addBean(_threadPool,false); } - + super.doStart(); // Start selector thread synchronized (this) { - _acceptorThread = new Thread[getAcceptors()]; + _acceptorThreads = new Thread[getAcceptors()]; - for (int i = 0; i < _acceptorThread.length; i++) + for (int i = 0; i < _acceptorThreads.length; i++) if (!_threadPool.dispatch(new Acceptor(i))) throw new IllegalStateException("!accepting"); if (_threadPool.isLowOnThreads()) @@ -345,17 +343,16 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht super.doStop(); - Thread[] acceptors = null; + Thread[] acceptors; synchronized (this) { - acceptors = _acceptorThread; - _acceptorThread = null; + acceptors = _acceptorThreads; + _acceptorThreads = null; } if (acceptors != null) { - for (int i = 0; i < acceptors.length; i++) + for (Thread thread : acceptors) { - Thread thread = acceptors[i]; if (thread != null) thread.interrupt(); } @@ -368,12 +365,12 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht Thread[] threads; synchronized(this) { - threads= _acceptorThread; + threads=_acceptorThreads; } if (threads != null) - for (int i = 0; i < threads.length; i++) - if (threads[i] != null) - threads[i].join(); + for (Thread thread : threads) + if (thread != null) + thread.join(); } /* ------------------------------------------------------------ */ @@ -792,8 +789,6 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht { _forwardedSslSessionIdHeader = forwardedSslSessionId; } - - public int getRequestBufferSize() { @@ -889,13 +884,11 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht @Override public String toString() { - String name = this.getClass().getName(); - int dot = name.lastIndexOf('.'); - if (dot > 0) - name = name.substring(dot + 1); - - return name + "@" + (getHost() == null?"0.0.0.0":getHost()) + ":" + (getLocalPort() <= 0?getPort():getLocalPort()) + " " - + AbstractLifeCycle.getState(this); + return String.format("%s@%s:%d %s", + getClass().getSimpleName(), + getHost()==null?"0.0.0.0":getHost(), + getLocalPort()<=0?getPort():getLocalPort(), + AbstractLifeCycle.getState(this)); } /* ------------------------------------------------------------ */ @@ -917,11 +910,11 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht String name; synchronized (AbstractConnector.this) { - if (_acceptorThread == null) + if (_acceptorThreads == null) return; - _acceptorThread[_acceptor] = current; - name = _acceptorThread[_acceptor].getName(); + _acceptorThreads[_acceptor] = current; + name = _acceptorThreads[_acceptor].getName(); current.setName(name + " Acceptor" + _acceptor + " " + AbstractConnector.this); } int old_priority = current.getPriority(); @@ -961,8 +954,8 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Ht synchronized (AbstractConnector.this) { - if (_acceptorThread != null) - _acceptorThread[_acceptor] = null; + if (_acceptorThreads != null) + _acceptorThreads[_acceptor] = null; } } } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java b/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java index d1b8d9bbfac..956c294b885 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java @@ -38,7 +38,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; - import javax.net.ssl.CertPathTrustManagerParameters; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; @@ -73,7 +72,7 @@ import org.eclipse.jetty.util.security.Password; public class SslContextFactory extends AbstractLifeCycle { private static final Logger LOG = Log.getLogger(SslContextFactory.class); - + public static final String DEFAULT_KEYMANAGERFACTORY_ALGORITHM = (Security.getProperty("ssl.KeyManagerFactory.algorithm") == null ? "SunX509" : Security.getProperty("ssl.KeyManagerFactory.algorithm")); @@ -96,7 +95,7 @@ public class SslContextFactory extends AbstractLifeCycle // private final Set _excludeProtocols = new HashSet(Collections.singleton("SSLv2Hello")); /** Included protocols. */ private Set _includeProtocols = null; - + /** Excluded cipher suites. */ private final Set _excludeCipherSuites = new HashSet(); /** Included cipher suites. */ @@ -178,7 +177,7 @@ public class SslContextFactory extends AbstractLifeCycle /** SSL context */ private SSLContext _context; - + private boolean _trustAll; /* ------------------------------------------------------------ */ @@ -225,7 +224,7 @@ public class SslContextFactory extends AbstractLifeCycle _trustStore==null && _trustStoreInputStream == null && _trustStorePath == null ) { TrustManager[] trust_managers=null; - + if (_trustAll) { LOG.debug("No keystore or trust store configured. ACCEPTING UNTRUSTED CERTIFICATES!!!!!"); @@ -247,7 +246,7 @@ public class SslContextFactory extends AbstractLifeCycle }; trust_managers = new TrustManager[] { trustAllCerts }; } - + SecureRandom secureRandom = (_secureRandomAlgorithm == null)?null:SecureRandom.getInstance(_secureRandomAlgorithm); _context = SSLContext.getInstance(_sslProtocol); _context.init(null, trust_managers, secureRandom); @@ -255,7 +254,7 @@ public class SslContextFactory extends AbstractLifeCycle else { // verify that keystore and truststore - // parameters are set up correctly + // parameters are set up correctly checkKeyStore(); KeyStore keyStore = loadKeyStore(); @@ -293,7 +292,7 @@ public class SslContextFactory extends AbstractLifeCycle _context.init(keyManagers,trustManagers,secureRandom); SSLEngine engine=newSslEngine(); - + LOG.info("Enabled Protocols {} of {}",Arrays.asList(engine.getEnabledProtocols()),Arrays.asList(engine.getSupportedProtocols())); if (LOG.isDebugEnabled()) LOG.debug("Enabled Ciphers {} of {}",Arrays.asList(engine.getEnabledCipherSuites()),Arrays.asList(engine.getSupportedCipherSuites())); @@ -334,7 +333,7 @@ public class SslContextFactory extends AbstractLifeCycle checkNotStarted(); _excludeProtocols.addAll(Arrays.asList(protocol)); } - + /* ------------------------------------------------------------ */ /** * @return The array of protocol names to include in @@ -380,7 +379,7 @@ public class SslContextFactory extends AbstractLifeCycle _excludeCipherSuites.clear(); _excludeCipherSuites.addAll(Arrays.asList(cipherSuites)); } - + /* ------------------------------------------------------------ */ /** * @param cipher Cipher names to add to {@link SSLEngine#setEnabledCipherSuites(String[])} @@ -429,7 +428,7 @@ public class SslContextFactory extends AbstractLifeCycle { return _keyStorePath; } - + /* ------------------------------------------------------------ */ /** * @param keyStorePath @@ -878,7 +877,7 @@ public class SslContextFactory extends AbstractLifeCycle { return (_keyManagerFactoryAlgorithm); } - + /* ------------------------------------------------------------ */ /** * @param algorithm @@ -1094,7 +1093,7 @@ public class SslContextFactory extends AbstractLifeCycle /* ------------------------------------------------------------ */ protected TrustManager[] getTrustManagers(KeyStore trustStore, Collection crls) throws Exception - { + { TrustManager[] managers = null; if (trustStore != null) { @@ -1156,15 +1155,15 @@ public class SslContextFactory extends AbstractLifeCycle * used as truststore. * @throws IllegalStateException if SslContextFactory configuration can't be used. */ - public void checkKeyStore() + public void checkKeyStore() { if (_context != null) return; //nothing to check if using preconfigured context - - + + if (_keyStore == null && _keyStoreInputStream == null && _keyStorePath == null) throw new IllegalStateException("SSL doesn't have a valid keystore"); - + // if the keystore has been configured but there is no // truststore configured, use the keystore as the truststore if (_trustStore == null && _trustStoreInputStream == null && _trustStorePath == null) @@ -1209,7 +1208,7 @@ public class SslContextFactory extends AbstractLifeCycle public String[] selectProtocols(String[] enabledProtocols, String[] supportedProtocols) { Set selected_protocols = new HashSet(); - + // Set the starting protocols - either from the included or enabled list if (_includeProtocols!=null) { @@ -1220,15 +1219,15 @@ public class SslContextFactory extends AbstractLifeCycle } else selected_protocols.addAll(Arrays.asList(enabledProtocols)); - - + + // Remove any excluded protocols if (_excludeProtocols != null) selected_protocols.removeAll(_excludeProtocols); - + return selected_protocols.toArray(new String[selected_protocols.size()]); } - + /* ------------------------------------------------------------ */ /** * Select cipher suites to be used by the connector @@ -1241,7 +1240,7 @@ public class SslContextFactory extends AbstractLifeCycle public String[] selectCipherSuites(String[] enabledCipherSuites, String[] supportedCipherSuites) { Set selected_ciphers = new HashSet(); - + // Set the starting ciphers - either from the included or enabled list if (_includeCipherSuites!=null) { @@ -1252,8 +1251,8 @@ public class SslContextFactory extends AbstractLifeCycle } else selected_ciphers.addAll(Arrays.asList(enabledCipherSuites)); - - + + // Remove any excluded ciphers if (_excludeCipherSuites != null) selected_ciphers.removeAll(_excludeCipherSuites); @@ -1450,7 +1449,7 @@ public class SslContextFactory extends AbstractLifeCycle { SSLServerSocketFactory factory = _context.getServerSocketFactory(); - SSLServerSocket socket = + SSLServerSocket socket = (SSLServerSocket) (host==null ? factory.createServerSocket(port,backlog): factory.createServerSocket(port,backlog,InetAddress.getByName(host))); @@ -1467,14 +1466,14 @@ public class SslContextFactory extends AbstractLifeCycle return socket; } - + /* ------------------------------------------------------------ */ public SSLSocket newSslSocket() throws IOException { SSLSocketFactory factory = _context.getSocketFactory(); - + SSLSocket socket = (SSLSocket)factory.createSocket(); - + if (getWantClientAuth()) socket.setWantClientAuth(getWantClientAuth()); if (getNeedClientAuth()) @@ -1482,23 +1481,23 @@ public class SslContextFactory extends AbstractLifeCycle socket.setEnabledCipherSuites(selectCipherSuites( socket.getEnabledCipherSuites(), - socket.getSupportedCipherSuites())); + socket.getSupportedCipherSuites())); socket.setEnabledProtocols(selectProtocols(socket.getEnabledProtocols(),socket.getSupportedProtocols())); return socket; } - + /* ------------------------------------------------------------ */ public SSLEngine newSslEngine(String host,int port) { SSLEngine sslEngine=isSessionCachingEnabled() ?_context.createSSLEngine(host, port) :_context.createSSLEngine(); - + customize(sslEngine); return sslEngine; } - + /* ------------------------------------------------------------ */ public SSLEngine newSslEngine() { @@ -1518,14 +1517,18 @@ public class SslContextFactory extends AbstractLifeCycle sslEngine.setEnabledCipherSuites(selectCipherSuites( sslEngine.getEnabledCipherSuites(), sslEngine.getSupportedCipherSuites())); - + sslEngine.setEnabledProtocols(selectProtocols(sslEngine.getEnabledProtocols(),sslEngine.getSupportedProtocols())); } /* ------------------------------------------------------------ */ public String toString() { - return this.getClass().getSimpleName()+"@"+Integer.toHexString(hashCode())+ - "("+_keyStorePath+","+_trustStorePath+")#"+getState(); + return String.format("%s@%x(%s,%s)#%s", + getClass().getSimpleName(), + hashCode(), + _keyStorePath, + _trustStorePath, + getState()); } }