Mercurial Hosting > luan
changeset 864:e21ca9878a10
simplify ThreadPool use
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 02 Oct 2016 16:17:38 -0600 |
parents | 88d3c8ff242a |
children | 6b210bb66c63 |
files | src/org/eclipse/jetty/server/AbstractConnector.java src/org/eclipse/jetty/server/Server.java src/org/eclipse/jetty/server/nio/SelectChannelConnector.java src/org/eclipse/jetty/util/component/AggregateLifeCycle.java |
diffstat | 4 files changed, 1435 insertions(+), 1602 deletions(-) [+] |
line wrap: on
line diff
diff -r 88d3c8ff242a -r e21ca9878a10 src/org/eclipse/jetty/server/AbstractConnector.java --- a/src/org/eclipse/jetty/server/AbstractConnector.java Sun Oct 02 05:22:55 2016 -0600 +++ b/src/org/eclipse/jetty/server/AbstractConnector.java Sun Oct 02 16:17:38 2016 -0600 @@ -55,998 +55,977 @@ */ public abstract class AbstractConnector extends AggregateLifeCycle implements HttpBuffers, Connector, Dumpable { - private static final Logger LOG = LoggerFactory.getLogger(AbstractConnector.class); + private static final Logger LOG = LoggerFactory.getLogger(AbstractConnector.class); - private String _name; + private String _name; - private Server _server; - private ThreadPool _threadPool; - private String _host; - private int _port = 0; - private String _integralScheme = HttpSchemes.HTTPS; - private int _integralPort = 0; - private String _confidentialScheme = HttpSchemes.HTTPS; - private int _confidentialPort = 0; - private int _acceptQueueSize = 0; - private int _acceptors = 1; - private int _acceptorPriorityOffset = 0; - private boolean _useDNS; - private boolean _forwarded; - private String _hostHeader; + private Server _server; + private String _host; + private int _port = 0; + private String _integralScheme = HttpSchemes.HTTPS; + private int _integralPort = 0; + private String _confidentialScheme = HttpSchemes.HTTPS; + private int _confidentialPort = 0; + private int _acceptQueueSize = 0; + private int _acceptors = 1; + private int _acceptorPriorityOffset = 0; + private boolean _useDNS; + private boolean _forwarded; + private String _hostHeader; - private String _forwardedHostHeader = HttpHeaders.X_FORWARDED_HOST; - private String _forwardedServerHeader = HttpHeaders.X_FORWARDED_SERVER; - private String _forwardedForHeader = HttpHeaders.X_FORWARDED_FOR; - private String _forwardedProtoHeader = HttpHeaders.X_FORWARDED_PROTO; - private String _forwardedCipherSuiteHeader; - private String _forwardedSslSessionIdHeader; - private boolean _reuseAddress = true; + private String _forwardedHostHeader = HttpHeaders.X_FORWARDED_HOST; + private String _forwardedServerHeader = HttpHeaders.X_FORWARDED_SERVER; + private String _forwardedForHeader = HttpHeaders.X_FORWARDED_FOR; + private String _forwardedProtoHeader = HttpHeaders.X_FORWARDED_PROTO; + private String _forwardedCipherSuiteHeader; + private String _forwardedSslSessionIdHeader; + private boolean _reuseAddress = true; - protected int _maxIdleTime = 200000; - protected int _lowResourceMaxIdleTime = -1; - protected int _soLingerTime = -1; - - private transient Thread[] _acceptorThreads; - - protected final HttpBuffersImpl _buffers = new HttpBuffersImpl(); + protected int _maxIdleTime = 200000; + protected int _lowResourceMaxIdleTime = -1; + protected int _soLingerTime = -1; - /* ------------------------------------------------------------ */ - /** - */ - public AbstractConnector() - { - addBean(_buffers); - } + private transient Thread[] _acceptorThreads; + + protected final HttpBuffersImpl _buffers = new HttpBuffersImpl(); - /* ------------------------------------------------------------ */ - /* - */ - public Server getServer() - { - return _server; - } + /* ------------------------------------------------------------ */ + /** + */ + public AbstractConnector() + { + addBean(_buffers); + } - /* ------------------------------------------------------------ */ - public void setServer(Server server) - { - _server = server; - } + /* ------------------------------------------------------------ */ + /* + */ + public Server getServer() + { + return _server; + } - /* ------------------------------------------------------------ */ - public ThreadPool getThreadPool() - { - return _threadPool; - } + /* ------------------------------------------------------------ */ + public void setServer(Server server) + { + _server = server; + } - /* ------------------------------------------------------------ */ - /** Set the ThreadPool. - * The threadpool passed is added via {@link #addBean(Object)} so that - * it's lifecycle may be managed as a {@link AggregateLifeCycle}. - * @param pool the threadPool to set - */ - public void setThreadPool(ThreadPool pool) - { - removeBean(_threadPool); - _threadPool = pool; - addBean(_threadPool); - } + /* ------------------------------------------------------------ */ + public ThreadPool getThreadPool() + { + return _server.getThreadPool(); + } + + /* ------------------------------------------------------------ */ + /** + */ + public void setHost(String host) + { + _host = host; + } - /* ------------------------------------------------------------ */ - /** - */ - public void setHost(String host) - { - _host = host; - } + /* ------------------------------------------------------------ */ + /* + */ + public String getHost() + { + return _host; + } - /* ------------------------------------------------------------ */ - /* - */ - public String getHost() - { - return _host; - } + /* ------------------------------------------------------------ */ + public void setPort(int port) + { + _port = port; + } - /* ------------------------------------------------------------ */ - public void setPort(int port) - { - _port = port; - } + /* ------------------------------------------------------------ */ + public int getPort() + { + return _port; + } - /* ------------------------------------------------------------ */ - public int getPort() - { - return _port; - } - - /* ------------------------------------------------------------ */ - /** - * @return Returns the maxIdleTime. - */ - public int getMaxIdleTime() - { - return _maxIdleTime; - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the maxIdleTime. + */ + public int getMaxIdleTime() + { + return _maxIdleTime; + } - /* ------------------------------------------------------------ */ - /** - * Set the maximum Idle time for a connection, which roughly translates to the {@link Socket#setSoTimeout(int)} call, although with NIO implementations - * other mechanisms may be used to implement the timeout. The max idle time is applied: - * <ul> - * <li>When waiting for a new request to be received on a connection</li> - * <li>When reading the headers and content of a request</li> - * <li>When writing the headers and content of a response</li> - * </ul> - * Jetty interprets this value as the maximum time between some progress being made on the connection. So if a single byte is read or written, then the - * timeout (if implemented by jetty) is reset. However, in many instances, the reading/writing is delegated to the JVM, and the semantic is more strictly - * enforced as the maximum time a single read/write operation can take. Note, that as Jetty supports writes of memory mapped file buffers, then a write may - * take many 10s of seconds for large content written to a slow device. - * <p> - * Previously, Jetty supported separate idle timeouts and IO operation timeouts, however the expense of changing the value of soTimeout was significant, so - * these timeouts were merged. With the advent of NIO, it may be possible to again differentiate these values (if there is demand). - * - * @param maxIdleTime - * The maxIdleTime to set. - */ - public void setMaxIdleTime(int maxIdleTime) - { - _maxIdleTime = maxIdleTime; - } + /* ------------------------------------------------------------ */ + /** + * Set the maximum Idle time for a connection, which roughly translates to the {@link Socket#setSoTimeout(int)} call, although with NIO implementations + * other mechanisms may be used to implement the timeout. The max idle time is applied: + * <ul> + * <li>When waiting for a new request to be received on a connection</li> + * <li>When reading the headers and content of a request</li> + * <li>When writing the headers and content of a response</li> + * </ul> + * Jetty interprets this value as the maximum time between some progress being made on the connection. So if a single byte is read or written, then the + * timeout (if implemented by jetty) is reset. However, in many instances, the reading/writing is delegated to the JVM, and the semantic is more strictly + * enforced as the maximum time a single read/write operation can take. Note, that as Jetty supports writes of memory mapped file buffers, then a write may + * take many 10s of seconds for large content written to a slow device. + * <p> + * Previously, Jetty supported separate idle timeouts and IO operation timeouts, however the expense of changing the value of soTimeout was significant, so + * these timeouts were merged. With the advent of NIO, it may be possible to again differentiate these values (if there is demand). + * + * @param maxIdleTime + * The maxIdleTime to set. + */ + public void setMaxIdleTime(int maxIdleTime) + { + _maxIdleTime = maxIdleTime; + } - /* ------------------------------------------------------------ */ - /** - * @return Returns the maxIdleTime when resources are low. - */ - public int getLowResourcesMaxIdleTime() - { - return _lowResourceMaxIdleTime; - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the maxIdleTime when resources are low. + */ + public int getLowResourcesMaxIdleTime() + { + return _lowResourceMaxIdleTime; + } + + /* ------------------------------------------------------------ */ + /** + * @param maxIdleTime + * The maxIdleTime to set when resources are low. + */ + public void setLowResourcesMaxIdleTime(int maxIdleTime) + { + _lowResourceMaxIdleTime = maxIdleTime; + } - /* ------------------------------------------------------------ */ - /** - * @param maxIdleTime - * The maxIdleTime to set when resources are low. - */ - public void setLowResourcesMaxIdleTime(int maxIdleTime) - { - _lowResourceMaxIdleTime = maxIdleTime; - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the maxIdleTime when resources are low. + * @deprecated + */ + @Deprecated + public final int getLowResourceMaxIdleTime() + { + return getLowResourcesMaxIdleTime(); + } - /* ------------------------------------------------------------ */ - /** - * @return Returns the maxIdleTime when resources are low. - * @deprecated - */ - @Deprecated - public final int getLowResourceMaxIdleTime() - { - return getLowResourcesMaxIdleTime(); - } + /* ------------------------------------------------------------ */ + /** + * @param maxIdleTime + * The maxIdleTime to set when resources are low. + * @deprecated + */ + @Deprecated + public final void setLowResourceMaxIdleTime(int maxIdleTime) + { + setLowResourcesMaxIdleTime(maxIdleTime); + } - /* ------------------------------------------------------------ */ - /** - * @param maxIdleTime - * The maxIdleTime to set when resources are low. - * @deprecated - */ - @Deprecated - public final void setLowResourceMaxIdleTime(int maxIdleTime) - { - setLowResourcesMaxIdleTime(maxIdleTime); - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the soLingerTime. + */ + public int getSoLingerTime() + { + return _soLingerTime; + } - /* ------------------------------------------------------------ */ - /** - * @return Returns the soLingerTime. - */ - public int getSoLingerTime() - { - return _soLingerTime; - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the acceptQueueSize. + */ + public int getAcceptQueueSize() + { + return _acceptQueueSize; + } - /* ------------------------------------------------------------ */ - /** - * @return Returns the acceptQueueSize. - */ - public int getAcceptQueueSize() - { - return _acceptQueueSize; - } + /* ------------------------------------------------------------ */ + /** + * @param acceptQueueSize + * The acceptQueueSize to set. + */ + public void setAcceptQueueSize(int acceptQueueSize) + { + _acceptQueueSize = acceptQueueSize; + } - /* ------------------------------------------------------------ */ - /** - * @param acceptQueueSize - * The acceptQueueSize to set. - */ - public void setAcceptQueueSize(int acceptQueueSize) - { - _acceptQueueSize = acceptQueueSize; - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the number of acceptor threads. + */ + public int getAcceptors() + { + return _acceptors; + } - /* ------------------------------------------------------------ */ - /** - * @return Returns the number of acceptor threads. - */ - public int getAcceptors() - { - return _acceptors; - } + /* ------------------------------------------------------------ */ + /** + * @param acceptors + * The number of acceptor threads to set. + */ + public void setAcceptors(int acceptors) + { + if (acceptors > 2 * Runtime.getRuntime().availableProcessors()) + LOG.warn("Acceptors should be <=2*availableProcessors: " + this); + _acceptors = acceptors; + } - /* ------------------------------------------------------------ */ - /** - * @param acceptors - * The number of acceptor threads to set. - */ - public void setAcceptors(int acceptors) - { - if (acceptors > 2 * Runtime.getRuntime().availableProcessors()) - LOG.warn("Acceptors should be <=2*availableProcessors: " + this); - _acceptors = acceptors; - } + /* ------------------------------------------------------------ */ + /** + * @param soLingerTime + * The soLingerTime to set or -1 to disable. + */ + public void setSoLingerTime(int soLingerTime) + { + _soLingerTime = soLingerTime; + } - /* ------------------------------------------------------------ */ - /** - * @param soLingerTime - * The soLingerTime to set or -1 to disable. - */ - public void setSoLingerTime(int soLingerTime) - { - _soLingerTime = soLingerTime; - } + /* ------------------------------------------------------------ */ + @Override + protected void doStart() throws Exception + { + if (_server == null) + throw new IllegalStateException("No server"); + + // open listener port + open(); + + super.doStart(); - /* ------------------------------------------------------------ */ - @Override - protected void doStart() throws Exception - { - if (_server == null) - throw new IllegalStateException("No server"); - - // open listener port - open(); + // Start selector thread + synchronized (this) + { + _acceptorThreads = new Thread[getAcceptors()]; - if (_threadPool == null) - { - _threadPool = _server.getThreadPool(); - addBean(_threadPool,false); - } + ThreadPool _threadPool = getThreadPool(); + for (int i = 0; i < _acceptorThreads.length; i++) + if (!_threadPool.dispatch(new Acceptor(i))) + throw new IllegalStateException("!accepting"); + if (_threadPool.isLowOnThreads()) + LOG.warn("insufficient threads configured for {}",this); + } - super.doStart(); - - // Start selector thread - synchronized (this) - { - _acceptorThreads = new Thread[getAcceptors()]; + LOG.info("Started {}",this); + } - for (int i = 0; i < _acceptorThreads.length; i++) - if (!_threadPool.dispatch(new Acceptor(i))) - throw new IllegalStateException("!accepting"); - if (_threadPool.isLowOnThreads()) - LOG.warn("insufficient threads configured for {}",this); - } + /* ------------------------------------------------------------ */ + @Override + protected void doStop() throws Exception + { + try + { + close(); + } + catch (IOException e) + { + LOG.warn("",e); + } - LOG.info("Started {}",this); - } + super.doStop(); - /* ------------------------------------------------------------ */ - @Override - protected void doStop() throws Exception - { - try - { - close(); - } - catch (IOException e) - { - LOG.warn("",e); - } - - super.doStop(); + Thread[] acceptors; + synchronized (this) + { + acceptors = _acceptorThreads; + _acceptorThreads = null; + } + if (acceptors != null) + { + for (Thread thread : acceptors) + { + if (thread != null) + thread.interrupt(); + } + } + } - Thread[] acceptors; - synchronized (this) - { - acceptors = _acceptorThreads; - _acceptorThreads = null; - } - if (acceptors != null) - { - for (Thread thread : acceptors) - { - if (thread != null) - thread.interrupt(); - } - } - } + /* ------------------------------------------------------------ */ + public void join() throws InterruptedException + { + Thread[] threads; + synchronized(this) + { + threads=_acceptorThreads; + } + if (threads != null) + for (Thread thread : threads) + if (thread != null) + thread.join(); + } - /* ------------------------------------------------------------ */ - public void join() throws InterruptedException - { - Thread[] threads; - synchronized(this) - { - threads=_acceptorThreads; - } - if (threads != null) - for (Thread thread : threads) - if (thread != null) - thread.join(); - } + /* ------------------------------------------------------------ */ + protected void configure(Socket socket) throws IOException + { + try + { + socket.setTcpNoDelay(true); + if (_soLingerTime >= 0) + socket.setSoLinger(true,_soLingerTime / 1000); + else + socket.setSoLinger(false,0); + } + catch (Exception e) + { + LOG.trace("",e); + } + } - /* ------------------------------------------------------------ */ - protected void configure(Socket socket) throws IOException - { - try - { - socket.setTcpNoDelay(true); - if (_soLingerTime >= 0) - socket.setSoLinger(true,_soLingerTime / 1000); - else - socket.setSoLinger(false,0); - } - catch (Exception e) - { - LOG.trace("",e); - } - } + /* ------------------------------------------------------------ */ + public void customize(EndPoint endpoint, Request request) throws IOException + { + if (isForwarded()) + checkForwardedHeaders(endpoint,request); + } + + /* ------------------------------------------------------------ */ + protected void checkForwardedHeaders(EndPoint endpoint, Request request) throws IOException + { + HttpFields httpFields = request.getConnection().getRequestFields(); - /* ------------------------------------------------------------ */ - public void customize(EndPoint endpoint, Request request) throws IOException - { - if (isForwarded()) - checkForwardedHeaders(endpoint,request); - } - - /* ------------------------------------------------------------ */ - protected void checkForwardedHeaders(EndPoint endpoint, Request request) throws IOException - { - HttpFields httpFields = request.getConnection().getRequestFields(); + // Do SSL first + if (getForwardedCipherSuiteHeader()!=null) + { + String cipher_suite=httpFields.getStringField(getForwardedCipherSuiteHeader()); + if (cipher_suite!=null) + request.setAttribute("javax.servlet.request.cipher_suite",cipher_suite); + } + if (getForwardedSslSessionIdHeader()!=null) + { + String ssl_session_id=httpFields.getStringField(getForwardedSslSessionIdHeader()); + if(ssl_session_id!=null) + { + request.setAttribute("javax.servlet.request.ssl_session_id", ssl_session_id); + request.setScheme(HttpSchemes.HTTPS); + } + } - // Do SSL first - if (getForwardedCipherSuiteHeader()!=null) - { - String cipher_suite=httpFields.getStringField(getForwardedCipherSuiteHeader()); - if (cipher_suite!=null) - request.setAttribute("javax.servlet.request.cipher_suite",cipher_suite); - } - if (getForwardedSslSessionIdHeader()!=null) - { - String ssl_session_id=httpFields.getStringField(getForwardedSslSessionIdHeader()); - if(ssl_session_id!=null) - { - request.setAttribute("javax.servlet.request.ssl_session_id", ssl_session_id); - request.setScheme(HttpSchemes.HTTPS); - } - } + // Retrieving headers from the request + String forwardedHost = getLeftMostFieldValue(httpFields,getForwardedHostHeader()); + String forwardedServer = getLeftMostFieldValue(httpFields,getForwardedServerHeader()); + String forwardedFor = getLeftMostFieldValue(httpFields,getForwardedForHeader()); + String forwardedProto = getLeftMostFieldValue(httpFields,getForwardedProtoHeader()); - // Retrieving headers from the request - String forwardedHost = getLeftMostFieldValue(httpFields,getForwardedHostHeader()); - String forwardedServer = getLeftMostFieldValue(httpFields,getForwardedServerHeader()); - String forwardedFor = getLeftMostFieldValue(httpFields,getForwardedForHeader()); - String forwardedProto = getLeftMostFieldValue(httpFields,getForwardedProtoHeader()); + if (_hostHeader != null) + { + // Update host header + httpFields.put(HttpHeaders.HOST_BUFFER,_hostHeader); + request.setServerName(null); + request.setServerPort(-1); + request.getServerName(); + } + else if (forwardedHost != null) + { + // Update host header + httpFields.put(HttpHeaders.HOST_BUFFER,forwardedHost); + request.setServerName(null); + request.setServerPort(-1); + request.getServerName(); + } + else if (forwardedServer != null) + { + // Use provided server name + request.setServerName(forwardedServer); + } - if (_hostHeader != null) - { - // Update host header - httpFields.put(HttpHeaders.HOST_BUFFER,_hostHeader); - request.setServerName(null); - request.setServerPort(-1); - request.getServerName(); - } - else if (forwardedHost != null) - { - // Update host header - httpFields.put(HttpHeaders.HOST_BUFFER,forwardedHost); - request.setServerName(null); - request.setServerPort(-1); - request.getServerName(); - } - else if (forwardedServer != null) - { - // Use provided server name - request.setServerName(forwardedServer); - } + if (forwardedFor != null) + { + request.setRemoteAddr(forwardedFor); + InetAddress inetAddress = null; - if (forwardedFor != null) - { - request.setRemoteAddr(forwardedFor); - InetAddress inetAddress = null; + if (_useDNS) + { + try + { + inetAddress = InetAddress.getByName(forwardedFor); + } + catch (UnknownHostException e) + { + LOG.trace("",e); + } + } - if (_useDNS) - { - try - { - inetAddress = InetAddress.getByName(forwardedFor); - } - catch (UnknownHostException e) - { - LOG.trace("",e); - } - } + request.setRemoteHost(inetAddress == null?forwardedFor:inetAddress.getHostName()); + } - request.setRemoteHost(inetAddress == null?forwardedFor:inetAddress.getHostName()); - } + if (forwardedProto != null) + { + request.setScheme(forwardedProto); + } + } - if (forwardedProto != null) - { - request.setScheme(forwardedProto); - } - } + /* ------------------------------------------------------------ */ + protected String getLeftMostFieldValue(HttpFields fields, String header) + { + if (header == null) + return null; - /* ------------------------------------------------------------ */ - protected String getLeftMostFieldValue(HttpFields fields, String header) - { - if (header == null) - return null; + String headerValue = fields.getStringField(header); - String headerValue = fields.getStringField(header); + if (headerValue == null) + return null; + + int commaIndex = headerValue.indexOf(','); - if (headerValue == null) - return null; - - int commaIndex = headerValue.indexOf(','); + if (commaIndex == -1) + { + // Single value + return headerValue; + } - if (commaIndex == -1) - { - // Single value - return headerValue; - } + // The left-most value is the farthest downstream client + return headerValue.substring(0,commaIndex); + } + + /* ------------------------------------------------------------ */ + public void persist(EndPoint endpoint) throws IOException + { + } - // The left-most value is the farthest downstream client - return headerValue.substring(0,commaIndex); - } - - /* ------------------------------------------------------------ */ - public void persist(EndPoint endpoint) throws IOException - { - } + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.Connector#getConfidentialPort() + */ + public int getConfidentialPort() + { + return _confidentialPort; + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.Connector#getConfidentialPort() - */ - public int getConfidentialPort() - { - return _confidentialPort; - } + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.Connector#getConfidentialScheme() + */ + public String getConfidentialScheme() + { + return _confidentialScheme; + } - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.Connector#getConfidentialScheme() - */ - public String getConfidentialScheme() - { - return _confidentialScheme; - } + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.Connector#isConfidential(org.eclipse.jetty.server .Request) + */ + public boolean isIntegral(Request request) + { + return false; + } + + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.Connector#getConfidentialPort() + */ + public int getIntegralPort() + { + return _integralPort; + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.Connector#isConfidential(org.eclipse.jetty.server .Request) - */ - public boolean isIntegral(Request request) - { - return false; - } + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.Connector#getIntegralScheme() + */ + public String getIntegralScheme() + { + return _integralScheme; + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.Connector#getConfidentialPort() - */ - public int getIntegralPort() - { - return _integralPort; - } - - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.Connector#getIntegralScheme() - */ - public String getIntegralScheme() - { - return _integralScheme; - } + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.Connector#isConfidential(org.eclipse.jetty.server.Request) + */ + public boolean isConfidential(Request request) + { + return _forwarded && request.getScheme().equalsIgnoreCase(HttpSchemes.HTTPS); + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.Connector#isConfidential(org.eclipse.jetty.server.Request) - */ - public boolean isConfidential(Request request) - { - return _forwarded && request.getScheme().equalsIgnoreCase(HttpSchemes.HTTPS); - } + /* ------------------------------------------------------------ */ + /** + * @param confidentialPort + * The confidentialPort to set. + */ + public void setConfidentialPort(int confidentialPort) + { + _confidentialPort = confidentialPort; + } - /* ------------------------------------------------------------ */ - /** - * @param confidentialPort - * The confidentialPort to set. - */ - public void setConfidentialPort(int confidentialPort) - { - _confidentialPort = confidentialPort; - } + /* ------------------------------------------------------------ */ + /** + * @param confidentialScheme + * The confidentialScheme to set. + */ + public void setConfidentialScheme(String confidentialScheme) + { + _confidentialScheme = confidentialScheme; + } - /* ------------------------------------------------------------ */ - /** - * @param confidentialScheme - * The confidentialScheme to set. - */ - public void setConfidentialScheme(String confidentialScheme) - { - _confidentialScheme = confidentialScheme; - } + /* ------------------------------------------------------------ */ + /** + * @param integralPort + * The integralPort to set. + */ + public void setIntegralPort(int integralPort) + { + _integralPort = integralPort; + } - /* ------------------------------------------------------------ */ - /** - * @param integralPort - * The integralPort to set. - */ - public void setIntegralPort(int integralPort) - { - _integralPort = integralPort; - } + /* ------------------------------------------------------------ */ + /** + * @param integralScheme + * The integralScheme to set. + */ + public void setIntegralScheme(String integralScheme) + { + _integralScheme = integralScheme; + } - /* ------------------------------------------------------------ */ - /** - * @param integralScheme - * The integralScheme to set. - */ - public void setIntegralScheme(String integralScheme) - { - _integralScheme = integralScheme; - } + /* ------------------------------------------------------------ */ + protected abstract void accept(int acceptorID) throws IOException, InterruptedException; - /* ------------------------------------------------------------ */ - protected abstract void accept(int acceptorID) throws IOException, InterruptedException; + /* ------------------------------------------------------------ */ + public void stopAccept(int acceptorID) throws Exception + { + } - /* ------------------------------------------------------------ */ - public void stopAccept(int acceptorID) throws Exception - { - } + /* ------------------------------------------------------------ */ + public boolean getResolveNames() + { + return _useDNS; + } - /* ------------------------------------------------------------ */ - public boolean getResolveNames() - { - return _useDNS; - } + /* ------------------------------------------------------------ */ + public void setResolveNames(boolean resolve) + { + _useDNS = resolve; + } - /* ------------------------------------------------------------ */ - public void setResolveNames(boolean resolve) - { - _useDNS = resolve; - } - - /* ------------------------------------------------------------ */ - /** - * Is reverse proxy handling on? - * - * @return true if this connector is checking the x-forwarded-for/host/server headers - */ - public boolean isForwarded() - { - return _forwarded; - } + /* ------------------------------------------------------------ */ + /** + * Is reverse proxy handling on? + * + * @return true if this connector is checking the x-forwarded-for/host/server headers + */ + public boolean isForwarded() + { + return _forwarded; + } - /* ------------------------------------------------------------ */ - /** - * Set reverse proxy handling. If set to true, then the X-Forwarded headers (or the headers set in their place) are looked for to set the request protocol, - * host, server and client ip. - * - * @param check - * true if this connector is checking the x-forwarded-for/host/server headers - * @see #setForwardedForHeader(String) - * @see #setForwardedHostHeader(String) - * @see #setForwardedProtoHeader(String) - * @see #setForwardedServerHeader(String) - */ - public void setForwarded(boolean check) - { - if (check) - LOG.debug("{} is forwarded",this); - _forwarded = check; - } + /* ------------------------------------------------------------ */ + /** + * Set reverse proxy handling. If set to true, then the X-Forwarded headers (or the headers set in their place) are looked for to set the request protocol, + * host, server and client ip. + * + * @param check + * true if this connector is checking the x-forwarded-for/host/server headers + * @see #setForwardedForHeader(String) + * @see #setForwardedHostHeader(String) + * @see #setForwardedProtoHeader(String) + * @see #setForwardedServerHeader(String) + */ + public void setForwarded(boolean check) + { + if (check) + LOG.debug("{} is forwarded",this); + _forwarded = check; + } - /* ------------------------------------------------------------ */ - public String getHostHeader() - { - return _hostHeader; - } + /* ------------------------------------------------------------ */ + public String getHostHeader() + { + return _hostHeader; + } - /* ------------------------------------------------------------ */ - /** - * Set a forced valued for the host header to control what is returned by {@link ServletRequest#getServerName()} and {@link ServletRequest#getServerPort()}. - * This value is only used if {@link #isForwarded()} is true. - * - * @param hostHeader - * The value of the host header to force. - */ - public void setHostHeader(String hostHeader) - { - _hostHeader = hostHeader; - } + /* ------------------------------------------------------------ */ + /** + * Set a forced valued for the host header to control what is returned by {@link ServletRequest#getServerName()} and {@link ServletRequest#getServerPort()}. + * This value is only used if {@link #isForwarded()} is true. + * + * @param hostHeader + * The value of the host header to force. + */ + public void setHostHeader(String hostHeader) + { + _hostHeader = hostHeader; + } + + /* ------------------------------------------------------------ */ + /* + * + * @see #setForwarded(boolean) + */ + public String getForwardedHostHeader() + { + return _forwardedHostHeader; + } - /* ------------------------------------------------------------ */ - /* - * - * @see #setForwarded(boolean) - */ - public String getForwardedHostHeader() - { - return _forwardedHostHeader; - } + /* ------------------------------------------------------------ */ + /** + * @param forwardedHostHeader + * The header name for forwarded hosts (default x-forwarded-host) + * @see #setForwarded(boolean) + */ + public void setForwardedHostHeader(String forwardedHostHeader) + { + _forwardedHostHeader = forwardedHostHeader; + } - /* ------------------------------------------------------------ */ - /** - * @param forwardedHostHeader - * The header name for forwarded hosts (default x-forwarded-host) - * @see #setForwarded(boolean) - */ - public void setForwardedHostHeader(String forwardedHostHeader) - { - _forwardedHostHeader = forwardedHostHeader; - } - - /* ------------------------------------------------------------ */ - /** - * @return the header name for forwarded server. - * @see #setForwarded(boolean) - */ - public String getForwardedServerHeader() - { - return _forwardedServerHeader; - } + /* ------------------------------------------------------------ */ + /** + * @return the header name for forwarded server. + * @see #setForwarded(boolean) + */ + public String getForwardedServerHeader() + { + return _forwardedServerHeader; + } - /* ------------------------------------------------------------ */ - /** - * @param forwardedServerHeader - * The header name for forwarded server (default x-forwarded-server) - * @see #setForwarded(boolean) - */ - public void setForwardedServerHeader(String forwardedServerHeader) - { - _forwardedServerHeader = forwardedServerHeader; - } + /* ------------------------------------------------------------ */ + /** + * @param forwardedServerHeader + * The header name for forwarded server (default x-forwarded-server) + * @see #setForwarded(boolean) + */ + public void setForwardedServerHeader(String forwardedServerHeader) + { + _forwardedServerHeader = forwardedServerHeader; + } - /* ------------------------------------------------------------ */ - /** - * @see #setForwarded(boolean) - */ - public String getForwardedForHeader() - { - return _forwardedForHeader; - } + /* ------------------------------------------------------------ */ + /** + * @see #setForwarded(boolean) + */ + public String getForwardedForHeader() + { + return _forwardedForHeader; + } - /* ------------------------------------------------------------ */ - /** - * @param forwardedRemoteAddressHeader - * The header name for forwarded for (default x-forwarded-for) - * @see #setForwarded(boolean) - */ - public void setForwardedForHeader(String forwardedRemoteAddressHeader) - { - _forwardedForHeader = forwardedRemoteAddressHeader; - } + /* ------------------------------------------------------------ */ + /** + * @param forwardedRemoteAddressHeader + * The header name for forwarded for (default x-forwarded-for) + * @see #setForwarded(boolean) + */ + public void setForwardedForHeader(String forwardedRemoteAddressHeader) + { + _forwardedForHeader = forwardedRemoteAddressHeader; + } - /* ------------------------------------------------------------ */ - /** - * Get the forwardedProtoHeader. - * - * @return the forwardedProtoHeader (default X-Forwarded-For) - * @see #setForwarded(boolean) - */ - public String getForwardedProtoHeader() - { - return _forwardedProtoHeader; - } + /* ------------------------------------------------------------ */ + /** + * Get the forwardedProtoHeader. + * + * @return the forwardedProtoHeader (default X-Forwarded-For) + * @see #setForwarded(boolean) + */ + public String getForwardedProtoHeader() + { + return _forwardedProtoHeader; + } - /* ------------------------------------------------------------ */ - /** - * Set the forwardedProtoHeader. - * - * @param forwardedProtoHeader - * the forwardedProtoHeader to set (default X-Forwarded-For) - * @see #setForwarded(boolean) - */ - public void setForwardedProtoHeader(String forwardedProtoHeader) - { - _forwardedProtoHeader = forwardedProtoHeader; - } + /* ------------------------------------------------------------ */ + /** + * Set the forwardedProtoHeader. + * + * @param forwardedProtoHeader + * the forwardedProtoHeader to set (default X-Forwarded-For) + * @see #setForwarded(boolean) + */ + public void setForwardedProtoHeader(String forwardedProtoHeader) + { + _forwardedProtoHeader = forwardedProtoHeader; + } - /* ------------------------------------------------------------ */ - /** - * @return The header name holding a forwarded cipher suite (default null) - */ - public String getForwardedCipherSuiteHeader() - { - return _forwardedCipherSuiteHeader; - } + /* ------------------------------------------------------------ */ + /** + * @return The header name holding a forwarded cipher suite (default null) + */ + public String getForwardedCipherSuiteHeader() + { + return _forwardedCipherSuiteHeader; + } - /* ------------------------------------------------------------ */ - /** - * @param forwardedCipherSuite - * The header name holding a forwarded cipher suite (default null) - */ - public void setForwardedCipherSuiteHeader(String forwardedCipherSuite) - { - _forwardedCipherSuiteHeader = forwardedCipherSuite; - } + /* ------------------------------------------------------------ */ + /** + * @param forwardedCipherSuite + * The header name holding a forwarded cipher suite (default null) + */ + public void setForwardedCipherSuiteHeader(String forwardedCipherSuite) + { + _forwardedCipherSuiteHeader = forwardedCipherSuite; + } + + /* ------------------------------------------------------------ */ + /** + * @return The header name holding a forwarded SSL Session ID (default null) + */ + public String getForwardedSslSessionIdHeader() + { + return _forwardedSslSessionIdHeader; + } - /* ------------------------------------------------------------ */ - /** - * @return The header name holding a forwarded SSL Session ID (default null) - */ - public String getForwardedSslSessionIdHeader() - { - return _forwardedSslSessionIdHeader; - } + /* ------------------------------------------------------------ */ + /** + * @param forwardedSslSessionId + * The header name holding a forwarded SSL Session ID (default null) + */ + public void setForwardedSslSessionIdHeader(String forwardedSslSessionId) + { + _forwardedSslSessionIdHeader = forwardedSslSessionId; + } - /* ------------------------------------------------------------ */ - /** - * @param forwardedSslSessionId - * The header name holding a forwarded SSL Session ID (default null) - */ - public void setForwardedSslSessionIdHeader(String forwardedSslSessionId) - { - _forwardedSslSessionIdHeader = forwardedSslSessionId; - } + public int getRequestBufferSize() + { + return _buffers.getRequestBufferSize(); + } - public int getRequestBufferSize() - { - return _buffers.getRequestBufferSize(); - } + public void setRequestBufferSize(int requestBufferSize) + { + _buffers.setRequestBufferSize(requestBufferSize); + } - public void setRequestBufferSize(int requestBufferSize) - { - _buffers.setRequestBufferSize(requestBufferSize); - } + public int getRequestHeaderSize() + { + return _buffers.getRequestHeaderSize(); + } - public int getRequestHeaderSize() - { - return _buffers.getRequestHeaderSize(); - } - - public void setRequestHeaderSize(int requestHeaderSize) - { - _buffers.setRequestHeaderSize(requestHeaderSize); - } + public void setRequestHeaderSize(int requestHeaderSize) + { + _buffers.setRequestHeaderSize(requestHeaderSize); + } - public int getResponseBufferSize() - { - return _buffers.getResponseBufferSize(); - } + public int getResponseBufferSize() + { + return _buffers.getResponseBufferSize(); + } - public void setResponseBufferSize(int responseBufferSize) - { - _buffers.setResponseBufferSize(responseBufferSize); - } + public void setResponseBufferSize(int responseBufferSize) + { + _buffers.setResponseBufferSize(responseBufferSize); + } - public int getResponseHeaderSize() - { - return _buffers.getResponseHeaderSize(); - } + public int getResponseHeaderSize() + { + return _buffers.getResponseHeaderSize(); + } - public void setResponseHeaderSize(int responseHeaderSize) - { - _buffers.setResponseHeaderSize(responseHeaderSize); - } + public void setResponseHeaderSize(int responseHeaderSize) + { + _buffers.setResponseHeaderSize(responseHeaderSize); + } - public Type getRequestBufferType() - { - return _buffers.getRequestBufferType(); - } + public Type getRequestBufferType() + { + return _buffers.getRequestBufferType(); + } - public Type getRequestHeaderType() - { - return _buffers.getRequestHeaderType(); - } + public Type getRequestHeaderType() + { + return _buffers.getRequestHeaderType(); + } - public Type getResponseBufferType() - { - return _buffers.getResponseBufferType(); - } + public Type getResponseBufferType() + { + return _buffers.getResponseBufferType(); + } - public Type getResponseHeaderType() - { - return _buffers.getResponseHeaderType(); - } + public Type getResponseHeaderType() + { + return _buffers.getResponseHeaderType(); + } - public void setRequestBuffers(Buffers requestBuffers) - { - _buffers.setRequestBuffers(requestBuffers); - } + public void setRequestBuffers(Buffers requestBuffers) + { + _buffers.setRequestBuffers(requestBuffers); + } - public void setResponseBuffers(Buffers responseBuffers) - { - _buffers.setResponseBuffers(responseBuffers); - } + public void setResponseBuffers(Buffers responseBuffers) + { + _buffers.setResponseBuffers(responseBuffers); + } - public Buffers getRequestBuffers() - { - return _buffers.getRequestBuffers(); - } + public Buffers getRequestBuffers() + { + return _buffers.getRequestBuffers(); + } - public Buffers getResponseBuffers() - { - return _buffers.getResponseBuffers(); - } + public Buffers getResponseBuffers() + { + return _buffers.getResponseBuffers(); + } - public void setMaxBuffers(int maxBuffers) - { - _buffers.setMaxBuffers(maxBuffers); - } + public void setMaxBuffers(int maxBuffers) + { + _buffers.setMaxBuffers(maxBuffers); + } - public int getMaxBuffers() - { - return _buffers.getMaxBuffers(); - } + public int getMaxBuffers() + { + return _buffers.getMaxBuffers(); + } - /* ------------------------------------------------------------ */ - @Override - public String toString() - { - return String.format("%s@%s:%d", - getClass().getSimpleName(), - getHost()==null?"0.0.0.0":getHost(), - getLocalPort()<=0?getPort():getLocalPort()); - } + /* ------------------------------------------------------------ */ + @Override + public String toString() + { + return String.format("%s@%s:%d", + getClass().getSimpleName(), + getHost()==null?"0.0.0.0":getHost(), + getLocalPort()<=0?getPort():getLocalPort()); + } - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - private class Acceptor implements Runnable - { - int _acceptor = 0; + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + private class Acceptor implements Runnable + { + int _acceptor = 0; - Acceptor(int id) - { - _acceptor = id; - } + Acceptor(int id) + { + _acceptor = id; + } - /* ------------------------------------------------------------ */ - public void run() - { - Thread current = Thread.currentThread(); - String name; - synchronized (AbstractConnector.this) - { - if (_acceptorThreads == null) - return; + /* ------------------------------------------------------------ */ + public void run() + { + Thread current = Thread.currentThread(); + String name; + synchronized (AbstractConnector.this) + { + if (_acceptorThreads == null) + return; - _acceptorThreads[_acceptor] = current; - name = _acceptorThreads[_acceptor].getName(); - current.setName(name + " Acceptor" + _acceptor + " " + AbstractConnector.this); - } - int old_priority = current.getPriority(); + _acceptorThreads[_acceptor] = current; + name = _acceptorThreads[_acceptor].getName(); + current.setName(name + " Acceptor" + _acceptor + " " + AbstractConnector.this); + } + int old_priority = current.getPriority(); - try - { - current.setPriority(old_priority - _acceptorPriorityOffset); - while (isRunning() && getConnection() != null) - { - try - { - accept(_acceptor); - } - catch (EofException e) - { - LOG.trace("",e); - } - catch (IOException e) - { - LOG.trace("",e); - } - catch (InterruptedException x) - { - // Connector has been stopped - LOG.trace("",x); - } - catch (Throwable e) - { - LOG.warn("",e); - } - } - } - finally - { - current.setPriority(old_priority); - current.setName(name); + try + { + current.setPriority(old_priority - _acceptorPriorityOffset); + while (isRunning() && getConnection() != null) + { + try + { + accept(_acceptor); + } + catch (EofException e) + { + LOG.trace("",e); + } + catch (IOException e) + { + LOG.trace("",e); + } + catch (InterruptedException x) + { + // Connector has been stopped + LOG.trace("",x); + } + catch (Throwable e) + { + LOG.warn("",e); + } + } + } + finally + { + current.setPriority(old_priority); + current.setName(name); - synchronized (AbstractConnector.this) - { - if (_acceptorThreads != null) - _acceptorThreads[_acceptor] = null; - } - } - } - } + synchronized (AbstractConnector.this) + { + if (_acceptorThreads != null) + _acceptorThreads[_acceptor] = null; + } + } + } + } - /* ------------------------------------------------------------ */ - public String getName() - { - if (_name == null) - _name = (getHost() == null?"0.0.0.0":getHost()) + ":" + (getLocalPort() <= 0?getPort():getLocalPort()); - return _name; - } + /* ------------------------------------------------------------ */ + public String getName() + { + if (_name == null) + _name = (getHost() == null?"0.0.0.0":getHost()) + ":" + (getLocalPort() <= 0?getPort():getLocalPort()); + return _name; + } - /* ------------------------------------------------------------ */ - public void setName(String name) - { - _name = name; - } + /* ------------------------------------------------------------ */ + public void setName(String name) + { + _name = name; + } - /* ------------------------------------------------------------ */ - protected void connectionOpened(Connection connection) - { - } + /* ------------------------------------------------------------ */ + protected void connectionOpened(Connection connection) + { + } - /* ------------------------------------------------------------ */ - protected void connectionUpgraded(Connection oldConnection, Connection newConnection) - { - } + /* ------------------------------------------------------------ */ + protected void connectionUpgraded(Connection oldConnection, Connection newConnection) + { + } - /* ------------------------------------------------------------ */ - protected void connectionClosed(Connection connection) - { - connection.onClose(); - } + /* ------------------------------------------------------------ */ + protected void connectionClosed(Connection connection) + { + connection.onClose(); + } - /* ------------------------------------------------------------ */ - /** - * @return the acceptorPriority - */ - public int getAcceptorPriorityOffset() - { - return _acceptorPriorityOffset; - } + /* ------------------------------------------------------------ */ + /** + * @return the acceptorPriority + */ + public int getAcceptorPriorityOffset() + { + return _acceptorPriorityOffset; + } - /* ------------------------------------------------------------ */ - /** - * Set the priority offset of the acceptor threads. The priority is adjusted by this amount (default 0) to either favour the acceptance of new threads and - * newly active connections or to favour the handling of already dispatched connections. - * - * @param offset - * the amount to alter the priority of the acceptor threads. - */ - public void setAcceptorPriorityOffset(int offset) - { - _acceptorPriorityOffset = offset; - } + /* ------------------------------------------------------------ */ + /** + * Set the priority offset of the acceptor threads. The priority is adjusted by this amount (default 0) to either favour the acceptance of new threads and + * newly active connections or to favour the handling of already dispatched connections. + * + * @param offset + * the amount to alter the priority of the acceptor threads. + */ + public void setAcceptorPriorityOffset(int offset) + { + _acceptorPriorityOffset = offset; + } - /* ------------------------------------------------------------ */ - /** - * @return True if the the server socket will be opened in SO_REUSEADDR mode. - */ - public boolean getReuseAddress() - { - return _reuseAddress; - } + /* ------------------------------------------------------------ */ + /** + * @return True if the the server socket will be opened in SO_REUSEADDR mode. + */ + public boolean getReuseAddress() + { + return _reuseAddress; + } - /* ------------------------------------------------------------ */ - /** - * @param reuseAddress - * True if the the server socket will be opened in SO_REUSEADDR mode. - */ - public void setReuseAddress(boolean reuseAddress) - { - _reuseAddress = reuseAddress; - } + /* ------------------------------------------------------------ */ + /** + * @param reuseAddress + * True if the the server socket will be opened in SO_REUSEADDR mode. + */ + public void setReuseAddress(boolean reuseAddress) + { + _reuseAddress = reuseAddress; + } - /* ------------------------------------------------------------ */ - public boolean isLowResources() - { - if (_threadPool != null) - return _threadPool.isLowOnThreads(); - return _server.getThreadPool().isLowOnThreads(); - } + /* ------------------------------------------------------------ */ + public boolean isLowResources() + { + return getThreadPool().isLowOnThreads(); + } - /* ------------------------------------------------------------ */ - private void updateNotEqual(AtomicLong valueHolder, long compare, long value) - { - long oldValue = valueHolder.get(); - while (compare != oldValue) - { - if (valueHolder.compareAndSet(oldValue,value)) - break; - oldValue = valueHolder.get(); - } - } + /* ------------------------------------------------------------ */ + private void updateNotEqual(AtomicLong valueHolder, long compare, long value) + { + long oldValue = valueHolder.get(); + while (compare != oldValue) + { + if (valueHolder.compareAndSet(oldValue,value)) + break; + oldValue = valueHolder.get(); + } + } }
diff -r 88d3c8ff242a -r e21ca9878a10 src/org/eclipse/jetty/server/Server.java --- a/src/org/eclipse/jetty/server/Server.java Sun Oct 02 05:22:55 2016 -0600 +++ b/src/org/eclipse/jetty/server/Server.java Sun Oct 02 16:17:38 2016 -0600 @@ -57,19 +57,10 @@ { private static final Logger LOG = LoggerFactory.getLogger(Server.class); - private static final String __version; - static - { - if (Server.class.getPackage()!=null && - "Eclipse.org - Jetty".equals(Server.class.getPackage().getImplementationVendor()) && - Server.class.getPackage().getImplementationVersion()!=null) - __version=Server.class.getPackage().getImplementationVersion(); - else - __version=System.getProperty("jetty.version","8.y.z-SNAPSHOT"); - } + private static final String __version = "8"; private final AttributesMap _attributes = new AttributesMap(); - private ThreadPool _threadPool; + private final ThreadPool _threadPool; private Connector[] _connectors; private boolean _sendServerVersion = true; //send Server: header private boolean _sendDateHeader = false; //send Date: header @@ -81,12 +72,6 @@ /* ------------------------------------------------------------ */ - public Server() - { - setServer(this); - } - - /* ------------------------------------------------------------ */ /** Convenience constructor * Creates server and a {@link SelectChannelConnector} at the passed port. */ @@ -97,20 +82,9 @@ Connector connector=new SelectChannelConnector(); connector.setPort(port); setConnectors(new Connector[]{connector}); - } - /* ------------------------------------------------------------ */ - /** Convenience constructor - * Creates server and a {@link SelectChannelConnector} at the passed address. - */ - public Server(InetSocketAddress addr) - { - setServer(this); - - Connector connector=new SelectChannelConnector(); - connector.setHost(addr.getHostName()); - connector.setPort(addr.getPort()); - setConnectors(new Connector[]{connector}); + _threadPool = new QueuedThreadPool(); + addBean(_threadPool); } @@ -196,19 +170,6 @@ return _threadPool; } - /* ------------------------------------------------------------ */ - /** - * @param threadPool The threadPool to set. - */ - public void setThreadPool(ThreadPool threadPool) - { - if (_threadPool!=null) - removeBean(_threadPool); - _threadPool = threadPool; - if (_threadPool!=null) - addBean(_threadPool); - } - /** * @return true if {@link #dumpStdErr()} is called after starting */ @@ -259,9 +220,6 @@ MultiException mex=new MultiException(); - if (_threadPool==null) - setThreadPool(new QueuedThreadPool()); - try { super.doStart(); @@ -450,63 +408,6 @@ } /* ------------------------------------------------------------ */ - /** - * Add a LifeCycle object to be started/stopped - * along with the Server. - * @deprecated Use {@link #addBean(Object)} - * @param c - */ - @Deprecated - public void addLifeCycle (LifeCycle c) - { - addBean(c); - } - - /* ------------------------------------------------------------ */ - /** - * Add an associated bean. - * The bean will be added to the servers {@link Container} - * and if it is a {@link LifeCycle} instance, it will be - * started/stopped along with the Server. Any beans that are also - * {@link Destroyable}, will be destroyed with the server. - * @param o the bean object to add - */ - @Override - public boolean addBean(Object o) - { - if (super.addBean(o)) - { - return true; - } - return false; - } - - /** - * Remove a LifeCycle object to be started/stopped - * along with the Server - * @deprecated Use {@link #removeBean(Object)} - */ - @Deprecated - public void removeLifeCycle (LifeCycle c) - { - removeBean(c); - } - - /* ------------------------------------------------------------ */ - /** - * Remove an associated bean. - */ - @Override - public boolean removeBean (Object o) - { - if (super.removeBean(o)) - { - return true; - } - return false; - } - - /* ------------------------------------------------------------ */ /* * @see org.eclipse.util.AttributesMap#clearAttributes() */
diff -r 88d3c8ff242a -r e21ca9878a10 src/org/eclipse/jetty/server/nio/SelectChannelConnector.java --- a/src/org/eclipse/jetty/server/nio/SelectChannelConnector.java Sun Oct 02 05:22:55 2016 -0600 +++ b/src/org/eclipse/jetty/server/nio/SelectChannelConnector.java Sun Oct 02 16:17:38 2016 -0600 @@ -66,269 +66,260 @@ */ public class SelectChannelConnector extends AbstractNIOConnector { - protected ServerSocketChannel _acceptChannel; - private int _lowResourcesConnections; - private int _lowResourcesMaxIdleTime; - private int _localPort=-1; + protected ServerSocketChannel _acceptChannel; + private int _lowResourcesConnections; + private int _lowResourcesMaxIdleTime; + private int _localPort=-1; - private final SelectorManager _manager = new ConnectorSelectorManager(); + private final SelectorManager _manager = new ConnectorSelectorManager(); - /* ------------------------------------------------------------------------------- */ - /** - * Constructor. - * - */ - public SelectChannelConnector() - { - _manager.setMaxIdleTime(getMaxIdleTime()); - addBean(_manager,true); - setAcceptors(Math.max(1,(Runtime.getRuntime().availableProcessors()+3)/4)); - } - - @Override - public void setThreadPool(ThreadPool pool) - { - super.setThreadPool(pool); - // preserve start order - removeBean(_manager); - addBean(_manager,true); - } - - /* ------------------------------------------------------------ */ - @Override - public void accept(int acceptorID) throws IOException - { - ServerSocketChannel server; - synchronized(this) - { - server = _acceptChannel; - } + /* ------------------------------------------------------------------------------- */ + /** + * Constructor. + * + */ + public SelectChannelConnector() + { + _manager.setMaxIdleTime(getMaxIdleTime()); + addBean(_manager,true); + setAcceptors(Math.max(1,(Runtime.getRuntime().availableProcessors()+3)/4)); + } + + /* ------------------------------------------------------------ */ + @Override + public void accept(int acceptorID) throws IOException + { + ServerSocketChannel server; + synchronized(this) + { + server = _acceptChannel; + } + + if (server!=null && server.isOpen() && _manager.isStarted()) + { + SocketChannel channel = server.accept(); + channel.configureBlocking(false); + Socket socket = channel.socket(); + configure(socket); + _manager.register(channel); + } + } - if (server!=null && server.isOpen() && _manager.isStarted()) - { - SocketChannel channel = server.accept(); - channel.configureBlocking(false); - Socket socket = channel.socket(); - configure(socket); - _manager.register(channel); - } - } + /* ------------------------------------------------------------ */ + public void close() throws IOException + { + synchronized(this) + { + if (_acceptChannel != null) + { + removeBean(_acceptChannel); + if (_acceptChannel.isOpen()) + _acceptChannel.close(); + } + _acceptChannel = null; + _localPort=-2; + } + } - /* ------------------------------------------------------------ */ - public void close() throws IOException - { - synchronized(this) - { - if (_acceptChannel != null) - { - removeBean(_acceptChannel); - if (_acceptChannel.isOpen()) - _acceptChannel.close(); - } - _acceptChannel = null; - _localPort=-2; - } - } + /* ------------------------------------------------------------------------------- */ + @Override + public void customize(EndPoint endpoint, Request request) throws IOException + { + request.setTimeStamp(System.currentTimeMillis()); + endpoint.setMaxIdleTime(_maxIdleTime); + super.customize(endpoint, request); + } - /* ------------------------------------------------------------------------------- */ - @Override - public void customize(EndPoint endpoint, Request request) throws IOException - { - request.setTimeStamp(System.currentTimeMillis()); - endpoint.setMaxIdleTime(_maxIdleTime); - super.customize(endpoint, request); - } + /* ------------------------------------------------------------------------------- */ + @Override + public void persist(EndPoint endpoint) throws IOException + { + AsyncEndPoint aEndp = ((AsyncEndPoint)endpoint); + aEndp.setCheckForIdle(true); + super.persist(endpoint); + } - /* ------------------------------------------------------------------------------- */ - @Override - public void persist(EndPoint endpoint) throws IOException - { - AsyncEndPoint aEndp = ((AsyncEndPoint)endpoint); - aEndp.setCheckForIdle(true); - super.persist(endpoint); - } + /* ------------------------------------------------------------ */ + public SelectorManager getSelectorManager() + { + return _manager; + } - /* ------------------------------------------------------------ */ - public SelectorManager getSelectorManager() - { - return _manager; - } + /* ------------------------------------------------------------ */ + public synchronized Object getConnection() + { + return _acceptChannel; + } - /* ------------------------------------------------------------ */ - public synchronized Object getConnection() - { - return _acceptChannel; - } + /* ------------------------------------------------------------------------------- */ + public int getLocalPort() + { + synchronized(this) + { + return _localPort; + } + } - /* ------------------------------------------------------------------------------- */ - public int getLocalPort() - { - synchronized(this) - { - return _localPort; - } - } + /* ------------------------------------------------------------ */ + public void open() throws IOException + { + synchronized(this) + { + if (_acceptChannel == null) + { + // Create a new server socket + _acceptChannel = ServerSocketChannel.open(); + // Set to blocking mode + _acceptChannel.configureBlocking(true); - /* ------------------------------------------------------------ */ - public void open() throws IOException - { - synchronized(this) - { - if (_acceptChannel == null) - { - // Create a new server socket - _acceptChannel = ServerSocketChannel.open(); - // Set to blocking mode - _acceptChannel.configureBlocking(true); + // Bind the server socket to the local host and port + _acceptChannel.socket().setReuseAddress(getReuseAddress()); + InetSocketAddress addr = getHost()==null?new InetSocketAddress(getPort()):new InetSocketAddress(getHost(),getPort()); + _acceptChannel.socket().bind(addr,getAcceptQueueSize()); + + _localPort=_acceptChannel.socket().getLocalPort(); + if (_localPort<=0) + throw new IOException("Server channel not bound"); - // Bind the server socket to the local host and port - _acceptChannel.socket().setReuseAddress(getReuseAddress()); - InetSocketAddress addr = getHost()==null?new InetSocketAddress(getPort()):new InetSocketAddress(getHost(),getPort()); - _acceptChannel.socket().bind(addr,getAcceptQueueSize()); + addBean(_acceptChannel); + } + } + } - _localPort=_acceptChannel.socket().getLocalPort(); - if (_localPort<=0) - throw new IOException("Server channel not bound"); - - addBean(_acceptChannel); - } - } - } + /* ------------------------------------------------------------ */ + @Override + public void setMaxIdleTime(int maxIdleTime) + { + _manager.setMaxIdleTime(maxIdleTime); + super.setMaxIdleTime(maxIdleTime); + } - /* ------------------------------------------------------------ */ - @Override - public void setMaxIdleTime(int maxIdleTime) - { - _manager.setMaxIdleTime(maxIdleTime); - super.setMaxIdleTime(maxIdleTime); - } + /* ------------------------------------------------------------ */ + /** + * @return the lowResourcesConnections + */ + public int getLowResourcesConnections() + { + return _lowResourcesConnections; + } - /* ------------------------------------------------------------ */ - /** - * @return the lowResourcesConnections - */ - public int getLowResourcesConnections() - { - return _lowResourcesConnections; - } + /* ------------------------------------------------------------ */ + /** + * Set the number of connections, which if exceeded places this manager in low resources state. + * This is not an exact measure as the connection count is averaged over the select sets. + * @param lowResourcesConnections the number of connections + * @see #setLowResourcesMaxIdleTime(int) + */ + public void setLowResourcesConnections(int lowResourcesConnections) + { + _lowResourcesConnections=lowResourcesConnections; + } - /* ------------------------------------------------------------ */ - /** - * Set the number of connections, which if exceeded places this manager in low resources state. - * This is not an exact measure as the connection count is averaged over the select sets. - * @param lowResourcesConnections the number of connections - * @see #setLowResourcesMaxIdleTime(int) - */ - public void setLowResourcesConnections(int lowResourcesConnections) - { - _lowResourcesConnections=lowResourcesConnections; - } + /* ------------------------------------------------------------ */ + /** + * @return the lowResourcesMaxIdleTime + */ + @Override + public int getLowResourcesMaxIdleTime() + { + return _lowResourcesMaxIdleTime; + } - /* ------------------------------------------------------------ */ - /** - * @return the lowResourcesMaxIdleTime - */ - @Override - public int getLowResourcesMaxIdleTime() - { - return _lowResourcesMaxIdleTime; - } - - /* ------------------------------------------------------------ */ - /** - * Set the period in ms that a connection is allowed to be idle when this there are more - * than {@link #getLowResourcesConnections()} connections. This allows the server to rapidly close idle connections - * in order to gracefully handle high load situations. - * @param lowResourcesMaxIdleTime the period in ms that a connection is allowed to be idle when resources are low. - * @see #setMaxIdleTime(int) - */ - @Override - public void setLowResourcesMaxIdleTime(int lowResourcesMaxIdleTime) - { - _lowResourcesMaxIdleTime=lowResourcesMaxIdleTime; - super.setLowResourcesMaxIdleTime(lowResourcesMaxIdleTime); - } + /* ------------------------------------------------------------ */ + /** + * Set the period in ms that a connection is allowed to be idle when this there are more + * than {@link #getLowResourcesConnections()} connections. This allows the server to rapidly close idle connections + * in order to gracefully handle high load situations. + * @param lowResourcesMaxIdleTime the period in ms that a connection is allowed to be idle when resources are low. + * @see #setMaxIdleTime(int) + */ + @Override + public void setLowResourcesMaxIdleTime(int lowResourcesMaxIdleTime) + { + _lowResourcesMaxIdleTime=lowResourcesMaxIdleTime; + super.setLowResourcesMaxIdleTime(lowResourcesMaxIdleTime); + } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.server.AbstractConnector#doStart() - */ - @Override - protected void doStart() throws Exception - { - _manager.setSelectSets(getAcceptors()); - _manager.setMaxIdleTime(getMaxIdleTime()); - _manager.setLowResourcesConnections(getLowResourcesConnections()); - _manager.setLowResourcesMaxIdleTime(getLowResourcesMaxIdleTime()); + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.server.AbstractConnector#doStart() + */ + @Override + protected void doStart() throws Exception + { + _manager.setSelectSets(getAcceptors()); + _manager.setMaxIdleTime(getMaxIdleTime()); + _manager.setLowResourcesConnections(getLowResourcesConnections()); + _manager.setLowResourcesMaxIdleTime(getLowResourcesMaxIdleTime()); - super.doStart(); - } + super.doStart(); + } - /* ------------------------------------------------------------ */ - protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException - { - SelectChannelEndPoint endp= new SelectChannelEndPoint(channel,selectSet,key, SelectChannelConnector.this._maxIdleTime); - endp.setConnection(selectSet.getManager().newConnection(channel,endp, key.attachment())); - return endp; - } + /* ------------------------------------------------------------ */ + protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException + { + SelectChannelEndPoint endp= new SelectChannelEndPoint(channel,selectSet,key, SelectChannelConnector.this._maxIdleTime); + endp.setConnection(selectSet.getManager().newConnection(channel,endp, key.attachment())); + return endp; + } - /* ------------------------------------------------------------------------------- */ - protected void endPointClosed(SelectChannelEndPoint endpoint) - { - connectionClosed(endpoint.getConnection()); - } + /* ------------------------------------------------------------------------------- */ + protected void endPointClosed(SelectChannelEndPoint endpoint) + { + connectionClosed(endpoint.getConnection()); + } - /* ------------------------------------------------------------------------------- */ - protected AsyncConnection newConnection(SocketChannel channel,final AsyncEndPoint endpoint) - { - return new AsyncHttpConnection(SelectChannelConnector.this,endpoint,getServer()); - } + /* ------------------------------------------------------------------------------- */ + protected AsyncConnection newConnection(SocketChannel channel,final AsyncEndPoint endpoint) + { + return new AsyncHttpConnection(SelectChannelConnector.this,endpoint,getServer()); + } - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - private final class ConnectorSelectorManager extends SelectorManager - { - @Override - public boolean dispatch(Runnable task) - { - ThreadPool pool=getThreadPool(); - if (pool==null) - pool=getServer().getThreadPool(); - return pool.dispatch(task); - } + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + private final class ConnectorSelectorManager extends SelectorManager + { + @Override + public boolean dispatch(Runnable task) + { + ThreadPool pool=getThreadPool(); + if (pool==null) + pool=getServer().getThreadPool(); + return pool.dispatch(task); + } - @Override - protected void endPointClosed(final SelectChannelEndPoint endpoint) - { - SelectChannelConnector.this.endPointClosed(endpoint); - } + @Override + protected void endPointClosed(final SelectChannelEndPoint endpoint) + { + SelectChannelConnector.this.endPointClosed(endpoint); + } - @Override - protected void endPointOpened(SelectChannelEndPoint endpoint) - { - // TODO handle max connections and low resources - connectionOpened(endpoint.getConnection()); - } + @Override + protected void endPointOpened(SelectChannelEndPoint endpoint) + { + // TODO handle max connections and low resources + connectionOpened(endpoint.getConnection()); + } - @Override - protected void endPointUpgraded(ConnectedEndPoint endpoint, Connection oldConnection) - { - connectionUpgraded(oldConnection,endpoint.getConnection()); - } + @Override + protected void endPointUpgraded(ConnectedEndPoint endpoint, Connection oldConnection) + { + connectionUpgraded(oldConnection,endpoint.getConnection()); + } - @Override - public AsyncConnection newConnection(SocketChannel channel,AsyncEndPoint endpoint, Object attachment) - { - return SelectChannelConnector.this.newConnection(channel,endpoint); - } + @Override + public AsyncConnection newConnection(SocketChannel channel,AsyncEndPoint endpoint, Object attachment) + { + return SelectChannelConnector.this.newConnection(channel,endpoint); + } - @Override - protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey sKey) throws IOException - { - return SelectChannelConnector.this.newEndPoint(channel,selectSet,sKey); - } - } + @Override + protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey sKey) throws IOException + { + return SelectChannelConnector.this.newEndPoint(channel,selectSet,sKey); + } + } }
diff -r 88d3c8ff242a -r e21ca9878a10 src/org/eclipse/jetty/util/component/AggregateLifeCycle.java --- a/src/org/eclipse/jetty/util/component/AggregateLifeCycle.java Sun Oct 02 05:22:55 2016 -0600 +++ b/src/org/eclipse/jetty/util/component/AggregateLifeCycle.java Sun Oct 02 16:17:38 2016 -0600 @@ -45,397 +45,359 @@ */ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable, Dumpable { - private static final Logger LOG = LoggerFactory.getLogger(AggregateLifeCycle.class); - private final List<Bean> _beans=new CopyOnWriteArrayList<Bean>(); - private boolean _started=false; + private static final Logger LOG = LoggerFactory.getLogger(AggregateLifeCycle.class); + private final List<Bean> _beans=new CopyOnWriteArrayList<Bean>(); + private boolean _started=false; - private class Bean - { - Bean(Object b) - { - _bean=b; - } - final Object _bean; - volatile boolean _managed=true; - - public String toString() - { - return "{"+_bean+","+_managed+"}"; - } - } + private class Bean + { + Bean(Object b) + { + _bean=b; + } + final Object _bean; + volatile boolean _managed=true; + + public String toString() + { + return "{"+_bean+","+_managed+"}"; + } + } - /* ------------------------------------------------------------ */ - /** - * Start the managed lifecycle beans in the order they were added. - * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() - */ - @Override - protected void doStart() throws Exception - { - for (Bean b:_beans) - { - if (b._managed && b._bean instanceof LifeCycle) - { - LifeCycle l=(LifeCycle)b._bean; - if (!l.isRunning()) - l.start(); - } - } - // indicate that we are started, so that addBean will start other beans added. - _started=true; - super.doStart(); - } - - /* ------------------------------------------------------------ */ - /** - * Stop the joined lifecycle beans in the reverse order they were added. - * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() - */ - @Override - protected void doStop() throws Exception - { - _started=false; - super.doStop(); - List<Bean> reverse = new ArrayList<Bean>(_beans); - Collections.reverse(reverse); - for (Bean b:reverse) - { - if (b._managed && b._bean instanceof LifeCycle) - { - LifeCycle l=(LifeCycle)b._bean; - if (l.isRunning()) - l.stop(); - } - } - } + /* ------------------------------------------------------------ */ + /** + * Start the managed lifecycle beans in the order they were added. + * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() + */ + @Override + protected void doStart() throws Exception + { + for (Bean b:_beans) + { + if (b._managed && b._bean instanceof LifeCycle) + { + LifeCycle l=(LifeCycle)b._bean; + if (!l.isRunning()) + l.start(); + } + } + // indicate that we are started, so that addBean will start other beans added. + _started=true; + super.doStart(); + } + + /* ------------------------------------------------------------ */ + /** + * Stop the joined lifecycle beans in the reverse order they were added. + * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() + */ + @Override + protected void doStop() throws Exception + { + _started=false; + super.doStop(); + List<Bean> reverse = new ArrayList<Bean>(_beans); + Collections.reverse(reverse); + for (Bean b:reverse) + { + if (b._managed && b._bean instanceof LifeCycle) + { + LifeCycle l=(LifeCycle)b._bean; + if (l.isRunning()) + l.stop(); + } + } + } - /* ------------------------------------------------------------ */ - /** - * Destroy the joined Destroyable beans in the reverse order they were added. - * @see org.eclipse.jetty.util.component.Destroyable#destroy() - */ - public void destroy() - { - List<Bean> reverse = new ArrayList<Bean>(_beans); - Collections.reverse(reverse); - for (Bean b:reverse) - { - if (b._bean instanceof Destroyable && b._managed) - { - Destroyable d=(Destroyable)b._bean; - d.destroy(); - } - } - _beans.clear(); - } + /* ------------------------------------------------------------ */ + /** + * Destroy the joined Destroyable beans in the reverse order they were added. + * @see org.eclipse.jetty.util.component.Destroyable#destroy() + */ + public void destroy() + { + List<Bean> reverse = new ArrayList<Bean>(_beans); + Collections.reverse(reverse); + for (Bean b:reverse) + { + if (b._bean instanceof Destroyable && b._managed) + { + Destroyable d=(Destroyable)b._bean; + d.destroy(); + } + } + _beans.clear(); + } - /* ------------------------------------------------------------ */ - /** Is the bean contained in the aggregate. - * @param bean - * @return True if the aggregate contains the bean - */ - public boolean contains(Object bean) - { - for (Bean b:_beans) - if (b._bean==bean) - return true; - return false; - } - - /* ------------------------------------------------------------ */ - /** Is the bean joined to the aggregate. - * @param bean - * @return True if the aggregate contains the bean and it is joined - */ - public boolean isManaged(Object bean) - { - for (Bean b:_beans) - if (b._bean==bean) - return b._managed; - return false; - } - - /* ------------------------------------------------------------ */ - /** - * Add an associated bean. - * If the bean is a {@link LifeCycle}, then it will be managed if it is not - * already started and umanaged if it is already started. The {@link #addBean(Object, boolean)} - * method should be used if this is not correct, or the {@link #manage(Object)} and {@link #unmanage(Object)} - * methods may be used after an add to change the status. - * @param o the bean object to add - * @return true if the bean was added or false if it has already been added. - */ - public boolean addBean(Object o) - { - // beans are joined unless they are started lifecycles - return addBean(o,!((o instanceof LifeCycle)&&((LifeCycle)o).isStarted())); - } - - /* ------------------------------------------------------------ */ - /** Add an associated lifecycle. - * @param o The lifecycle to add - * @param managed True if the LifeCycle is to be joined, otherwise it will be disjoint. - * @return true if bean was added, false if already present. - */ - public boolean addBean(Object o, boolean managed) - { - if (contains(o)) - return false; - - Bean b = new Bean(o); - b._managed=managed; - _beans.add(b); - - if (o instanceof LifeCycle) - { - LifeCycle l=(LifeCycle)o; + /* ------------------------------------------------------------ */ + /** Is the bean contained in the aggregate. + * @param bean + * @return True if the aggregate contains the bean + */ + public boolean contains(Object bean) + { + for (Bean b:_beans) + if (b._bean==bean) + return true; + return false; + } + + /* ------------------------------------------------------------ */ + /** Is the bean joined to the aggregate. + * @param bean + * @return True if the aggregate contains the bean and it is joined + */ + public boolean isManaged(Object bean) + { + for (Bean b:_beans) + if (b._bean==bean) + return b._managed; + return false; + } + + /* ------------------------------------------------------------ */ + /** + * Add an associated bean. + * If the bean is a {@link LifeCycle}, then it will be managed if it is not + * already started and umanaged if it is already started. The {@link #addBean(Object, boolean)} + * method should be used if this is not correct, or the {@link #manage(Object)} and {@link #unmanage(Object)} + * methods may be used after an add to change the status. + * @param o the bean object to add + * @return true if the bean was added or false if it has already been added. + */ + public boolean addBean(Object o) + { + // beans are joined unless they are started lifecycles + return addBean(o,!((o instanceof LifeCycle)&&((LifeCycle)o).isStarted())); + } + + /* ------------------------------------------------------------ */ + /** Add an associated lifecycle. + * @param o The lifecycle to add + * @param managed True if the LifeCycle is to be joined, otherwise it will be disjoint. + * @return true if bean was added, false if already present. + */ + public boolean addBean(Object o, boolean managed) + { + if (contains(o)) + return false; + + Bean b = new Bean(o); + b._managed=managed; + _beans.add(b); + + if (o instanceof LifeCycle) + { + LifeCycle l=(LifeCycle)o; - // Start the bean if we are started - if (managed && _started) - { - try - { - l.start(); - } - catch(Exception e) - { - throw new RuntimeException (e); - } - } - } - return true; - } - - /* ------------------------------------------------------------ */ - /** - * Manage a bean by this aggregate, so that it is started/stopped/destroyed with the - * aggregate lifecycle. - * @param bean The bean to manage (must already have been added). - */ - public void manage(Object bean) - { - for (Bean b :_beans) - { - if (b._bean==bean) - { - b._managed=true; - return; - } - } - throw new IllegalArgumentException(); - } + // Start the bean if we are started + if (managed && _started) + { + try + { + l.start(); + } + catch(Exception e) + { + throw new RuntimeException (e); + } + } + } + return true; + } + + /* ------------------------------------------------------------ */ + /** Get dependent beans + * @return List of beans. + */ + public Collection<Object> getBeans() + { + return getBeans(Object.class); + } + + /* ------------------------------------------------------------ */ + /** Get dependent beans of a specific class + * @see #addBean(Object) + * @param clazz + * @return List of beans. + */ + public <T> List<T> getBeans(Class<T> clazz) + { + ArrayList<T> beans = new ArrayList<T>(); + for (Bean b:_beans) + { + if (clazz.isInstance(b._bean)) + beans.add((T)(b._bean)); + } + return beans; + } - /* ------------------------------------------------------------ */ - /** - * Unmanage a bean by this aggregate, so that it is not started/stopped/destroyed with the - * aggregate lifecycle. - * @param bean The bean to manage (must already have been added). - */ - public void unmanage(Object bean) - { - for (Bean b :_beans) - { - if (b._bean==bean) - { - b._managed=false; - return; - } - } - throw new IllegalArgumentException(); - } - - /* ------------------------------------------------------------ */ - /** Get dependent beans - * @return List of beans. - */ - public Collection<Object> getBeans() - { - return getBeans(Object.class); - } - - /* ------------------------------------------------------------ */ - /** Get dependent beans of a specific class - * @see #addBean(Object) - * @param clazz - * @return List of beans. - */ - public <T> List<T> getBeans(Class<T> clazz) - { - ArrayList<T> beans = new ArrayList<T>(); - for (Bean b:_beans) - { - if (clazz.isInstance(b._bean)) - beans.add((T)(b._bean)); - } - return beans; - } + + /* ------------------------------------------------------------ */ + /** Get dependent beans of a specific class. + * If more than one bean of the type exist, the first is returned. + * @see #addBean(Object) + * @param clazz + * @return bean or null + */ + public <T> T getBean(Class<T> clazz) + { + for (Bean b:_beans) + { + if (clazz.isInstance(b._bean)) + return (T)b._bean; + } + + return null; + } + + /* ------------------------------------------------------------ */ + /** + * Remove all associated bean. + */ + public void removeBeans () + { + _beans.clear(); + } - - /* ------------------------------------------------------------ */ - /** Get dependent beans of a specific class. - * If more than one bean of the type exist, the first is returned. - * @see #addBean(Object) - * @param clazz - * @return bean or null - */ - public <T> T getBean(Class<T> clazz) - { - for (Bean b:_beans) - { - if (clazz.isInstance(b._bean)) - return (T)b._bean; - } - - return null; - } - - /* ------------------------------------------------------------ */ - /** - * Remove all associated bean. - */ - public void removeBeans () - { - _beans.clear(); - } + /* ------------------------------------------------------------ */ + /** + * Remove an associated bean. + */ + public boolean removeBean (Object o) + { + Iterator<Bean> i = _beans.iterator(); + while(i.hasNext()) + { + Bean b=i.next(); + if (b._bean==o) + { + _beans.remove(b); + return true; + } + } + return false; + } - /* ------------------------------------------------------------ */ - /** - * Remove an associated bean. - */ - public boolean removeBean (Object o) - { - Iterator<Bean> i = _beans.iterator(); - while(i.hasNext()) - { - Bean b=i.next(); - if (b._bean==o) - { - _beans.remove(b); - return true; - } - } - return false; - } + /* ------------------------------------------------------------ */ + public void dumpStdErr() + { + try + { + dump(System.err,""); + } + catch (IOException e) + { + LOG.warn("",e); + } + } + + /* ------------------------------------------------------------ */ + public String dump() + { + return dump(this); + } + + /* ------------------------------------------------------------ */ + public static String dump(Dumpable dumpable) + { + StringBuilder b = new StringBuilder(); + try + { + dumpable.dump(b,""); + } + catch (IOException e) + { + LOG.warn("",e); + } + return b.toString(); + } - /* ------------------------------------------------------------ */ - public void dumpStdErr() - { - try - { - dump(System.err,""); - } - catch (IOException e) - { - LOG.warn("",e); - } - } - - /* ------------------------------------------------------------ */ - public String dump() - { - return dump(this); - } - - /* ------------------------------------------------------------ */ - public static String dump(Dumpable dumpable) - { - StringBuilder b = new StringBuilder(); - try - { - dumpable.dump(b,""); - } - catch (IOException e) - { - LOG.warn("",e); - } - return b.toString(); - } + /* ------------------------------------------------------------ */ + public void dump(Appendable out) throws IOException + { + dump(out,""); + } + + /* ------------------------------------------------------------ */ + protected void dumpThis(Appendable out) throws IOException + { + out.append(String.valueOf(this)).append(" - ").append(getState()).append("\n"); + } - /* ------------------------------------------------------------ */ - public void dump(Appendable out) throws IOException - { - dump(out,""); - } - - /* ------------------------------------------------------------ */ - protected void dumpThis(Appendable out) throws IOException - { - out.append(String.valueOf(this)).append(" - ").append(getState()).append("\n"); - } - - /* ------------------------------------------------------------ */ - public static void dumpObject(Appendable out,Object o) throws IOException - { - try - { - if (o instanceof LifeCycle) - out.append(String.valueOf(o)).append(" - ").append((AbstractLifeCycle.getState((LifeCycle)o))).append("\n"); - else - out.append(String.valueOf(o)).append("\n"); - } - catch(Throwable th) - { - out.append(" => ").append(th.toString()).append('\n'); - } - } - - /* ------------------------------------------------------------ */ - public void dump(Appendable out,String indent) throws IOException - { - dumpThis(out); - int size=_beans.size(); - if (size==0) - return; - int i=0; - for (Bean b : _beans) - { - i++; + /* ------------------------------------------------------------ */ + public static void dumpObject(Appendable out,Object o) throws IOException + { + try + { + if (o instanceof LifeCycle) + out.append(String.valueOf(o)).append(" - ").append((AbstractLifeCycle.getState((LifeCycle)o))).append("\n"); + else + out.append(String.valueOf(o)).append("\n"); + } + catch(Throwable th) + { + out.append(" => ").append(th.toString()).append('\n'); + } + } + + /* ------------------------------------------------------------ */ + public void dump(Appendable out,String indent) throws IOException + { + dumpThis(out); + int size=_beans.size(); + if (size==0) + return; + int i=0; + for (Bean b : _beans) + { + i++; - out.append(indent).append(" +- "); - if (b._managed) - { - if (b._bean instanceof Dumpable) - ((Dumpable)b._bean).dump(out,indent+(i==size?" ":" | ")); - else - dumpObject(out,b._bean); - } - else - dumpObject(out,b._bean); - } + out.append(indent).append(" +- "); + if (b._managed) + { + if (b._bean instanceof Dumpable) + ((Dumpable)b._bean).dump(out,indent+(i==size?" ":" | ")); + else + dumpObject(out,b._bean); + } + else + dumpObject(out,b._bean); + } - if (i!=size) - out.append(indent).append(" |\n"); - } - - /* ------------------------------------------------------------ */ - public static void dump(Appendable out,String indent,Collection<?>... collections) throws IOException - { - if (collections.length==0) - return; - int size=0; - for (Collection<?> c : collections) - size+=c.size(); - if (size==0) - return; + if (i!=size) + out.append(indent).append(" |\n"); + } + + /* ------------------------------------------------------------ */ + public static void dump(Appendable out,String indent,Collection<?>... collections) throws IOException + { + if (collections.length==0) + return; + int size=0; + for (Collection<?> c : collections) + size+=c.size(); + if (size==0) + return; - int i=0; - for (Collection<?> c : collections) - { - for (Object o : c) - { - i++; - out.append(indent).append(" +- "); + int i=0; + for (Collection<?> c : collections) + { + for (Object o : c) + { + i++; + out.append(indent).append(" +- "); - if (o instanceof Dumpable) - ((Dumpable)o).dump(out,indent+(i==size?" ":" | ")); - else - dumpObject(out,o); - } - - if (i!=size) - out.append(indent).append(" |\n"); - } - } + if (o instanceof Dumpable) + ((Dumpable)o).dump(out,indent+(i==size?" ":" | ")); + else + dumpObject(out,o); + } + + if (i!=size) + out.append(indent).append(" |\n"); + } + } }