diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java index deb6f2c4090..30c6d432132 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java @@ -58,10 +58,7 @@ public class LikeJettyXml // Setup JMX MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); - mbContainer.start(); - server.getContainer().addEventListener(mbContainer); - server.addBean(mbContainer,true); - mbContainer.addBean(new Log()); + server.addBean(mbContainer); // Setup Connectors HttpConnectionFactory http = new HttpConnectionFactory(); diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyServletContexts.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyServletContexts.java index bfcb693f5d1..d2a3306b8db 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyServletContexts.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyServletContexts.java @@ -36,8 +36,6 @@ public class ManyServletContexts // Setup JMX MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); - mbContainer.start(); - server.getContainer().addEventListener(mbContainer); server.addBean(mbContainer,true); ContextHandlerCollection contexts = new ContextHandlerCollection(); @@ -51,9 +49,9 @@ public class ManyServletContexts ServletContextHandler other = new ServletContextHandler(contexts,"/other",ServletContextHandler.SESSIONS); other.addServlet(DefaultServlet.class.getCanonicalName(),"/"); other.addServlet(new ServletHolder(new HelloServlet("YO!")),"*.yo"); - + server.start(); - System.err.println(server.dump()); + server.dumpStdErr(); server.join(); } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index 964df790f15..52908bb1464 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -54,7 +54,7 @@ import org.eclipse.jetty.io.ssl.SslConnection; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.Jetty; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.ssl.SslContextFactory; @@ -95,7 +95,7 @@ import org.eclipse.jetty.util.thread.TimerScheduler; * }); * */ -public class HttpClient extends AggregateLifeCycle +public class HttpClient extends ContainerLifeCycle { private static final Logger LOG = Log.getLogger(HttpClient.class); diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java index 9490c53cef2..51110da0171 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java @@ -36,7 +36,7 @@ import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -312,19 +312,19 @@ public class HttpDestination implements Destination, AutoCloseable, Dumpable @Override public String dump() { - return AggregateLifeCycle.dump(this); + return ContainerLifeCycle.dump(this); } @Override public void dump(Appendable out, String indent) throws IOException { - AggregateLifeCycle.dumpObject(out, this + " - requests queued: " + requests.size()); + ContainerLifeCycle.dumpObject(out, this + " - requests queued: " + requests.size()); List connections = new ArrayList<>(); for (Connection connection : idleConnections) connections.add(connection + " - IDLE"); for (Connection connection : activeConnections) connections.add(connection + " - ACTIVE"); - AggregateLifeCycle.dump(out, indent, connections); + ContainerLifeCycle.dump(out, indent, connections); } @Override diff --git a/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/DeploymentManager.java b/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/DeploymentManager.java index 9cb3f86e49e..4484cd37fe8 100644 --- a/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/DeploymentManager.java +++ b/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/DeploymentManager.java @@ -42,7 +42,7 @@ import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.ManagedOperation; import org.eclipse.jetty.util.annotation.Name; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -61,7 +61,7 @@ import org.eclipse.jetty.util.log.Logger; * */ @ManagedObject("Deployment Manager") -public class DeploymentManager extends AggregateLifeCycle +public class DeploymentManager extends ContainerLifeCycle { private static final Logger LOG = Log.getLogger(DeploymentManager.class); @@ -150,7 +150,7 @@ public class DeploymentManager extends AggregateLifeCycle /* ------------------------------------------------------------ */ /** Set the AppProviders. * The providers passed are added via {@link #addBean(Object)} so that - * their lifecycles may be managed as a {@link AggregateLifeCycle}. + * their lifecycles may be managed as a {@link ContainerLifeCycle}. * @param providers */ public void setAppProviders(Collection providers) @@ -175,11 +175,7 @@ public class DeploymentManager extends AggregateLifeCycle { if (isRunning()) throw new IllegalStateException(); - - List old = new ArrayList(_providers); - if (_providers.add(provider) && getServer()!=null) - getServer().getContainer().update(this, null, provider, "provider"); - + _providers.add(provider); addBean(provider); } @@ -417,11 +413,8 @@ public class DeploymentManager extends AggregateLifeCycle public void removeAppProvider(AppProvider provider) { if(_providers.remove(provider)) - { removeBean(provider); - if (getServer()!=null) - getServer().getContainer().update(this, provider,null, "provider"); - } + try { provider.stop(); diff --git a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/DeploymentManagerLifeCyclePathTest.java b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/DeploymentManagerLifeCyclePathTest.java index a5e6161839a..c4eb14a5d5e 100644 --- a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/DeploymentManagerLifeCyclePathTest.java +++ b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/DeploymentManagerLifeCyclePathTest.java @@ -98,8 +98,7 @@ public class DeploymentManagerLifeCyclePathTest // Setup JMX MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); - mbContainer.start(); - mbContainer.addBean(depman); + depman.addBean(mbContainer); depman.addLifeCycleBinding(pathtracker); depman.addAppProvider(mockProvider); diff --git a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ScanningAppProviderRuntimeUpdatesTest.java b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ScanningAppProviderRuntimeUpdatesTest.java index 982907a7c7c..3f314aa94f9 100644 --- a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ScanningAppProviderRuntimeUpdatesTest.java +++ b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/providers/ScanningAppProviderRuntimeUpdatesTest.java @@ -63,7 +63,7 @@ public class ScanningAppProviderRuntimeUpdatesTest jetty.start(); // monitor tick - DeploymentManager dm = jetty.getServer().getBeans(DeploymentManager.class).get(0); + DeploymentManager dm = jetty.getServer().getBean(DeploymentManager.class); for (AppProvider provider : dm.getAppProviders()) { if (provider instanceof ScanningAppProvider) diff --git a/jetty-distribution/src/main/resources/start.ini b/jetty-distribution/src/main/resources/start.ini index e9dfa9abd28..d156b039c47 100644 --- a/jetty-distribution/src/main/resources/start.ini +++ b/jetty-distribution/src/main/resources/start.ini @@ -61,6 +61,7 @@ OPTIONS=Server,jsp,resources,websocket,ext # java -jar start.jar --help #----------------------------------------------------------- etc/jetty.xml +start.d/ # etc/jetty-ssl.xml # etc/jetty-requestlog.xml etc/jetty-deploy.xml diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java b/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java index 494792f90d6..7eae2be3a09 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java @@ -41,7 +41,7 @@ import org.eclipse.jetty.util.ForkInvoker; import org.eclipse.jetty.util.TypeUtil; import org.eclipse.jetty.util.annotation.Name; import org.eclipse.jetty.util.component.AbstractLifeCycle; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -249,14 +249,14 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa @Override public String dump() { - return AggregateLifeCycle.dump(this); + return ContainerLifeCycle.dump(this); } @Override public void dump(Appendable out, String indent) throws IOException { - AggregateLifeCycle.dumpObject(out, this); - AggregateLifeCycle.dump(out, indent, TypeUtil.asList(_selectors)); + ContainerLifeCycle.dumpObject(out, this); + ContainerLifeCycle.dump(out, indent, TypeUtil.asList(_selectors)); } /** @@ -504,7 +504,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa @Override public String dump() { - return AggregateLifeCycle.dump(this); + return ContainerLifeCycle.dump(this); } @Override @@ -536,7 +536,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa submit(dumpKeys); dumpKeys.await(5, TimeUnit.SECONDS); - AggregateLifeCycle.dump(out, indent, dump); + ContainerLifeCycle.dump(out, indent, dump); } } diff --git a/jetty-jmx/src/main/config/etc/jetty-jmx.xml b/jetty-jmx/src/main/config/etc/jetty-jmx.xml index 3b3e75bb99b..8bf523c11bc 100644 --- a/jetty-jmx/src/main/config/etc/jetty-jmx.xml +++ b/jetty-jmx/src/main/config/etc/jetty-jmx.xml @@ -1,13 +1,7 @@ - - - - - - - + @@ -27,26 +21,24 @@ name="getPlatformMBeanServer" /> - + - - - - - - - - + + + + + + + + - - - - - - - + + + + + diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnectionFactory.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnectionFactory.java index 33b712e07f4..d7bef86d4d3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnectionFactory.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnectionFactory.java @@ -22,10 +22,10 @@ import org.eclipse.jetty.io.AbstractConnection; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.ArrayUtil; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.ssl.SslContextFactory; -public abstract class AbstractConnectionFactory extends AggregateLifeCycle implements ConnectionFactory +public abstract class AbstractConnectionFactory extends ContainerLifeCycle implements ConnectionFactory { private final String _protocol; private int _inputbufferSize = 8192; @@ -55,9 +55,9 @@ public abstract class AbstractConnectionFactory extends AggregateLifeCycle imple { connection.setInputBufferSize(getInputBufferSize()); - if (connector instanceof AggregateLifeCycle) + if (connector instanceof ContainerLifeCycle) { - AggregateLifeCycle aggregate = (AggregateLifeCycle)connector; + ContainerLifeCycle aggregate = (ContainerLifeCycle)connector; for (Connection.Listener listener : aggregate.getBeans(Connection.Listener.class)) connection.addListener(listener); } 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 a1ee0c2e96c..5478f14599e 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 @@ -38,7 +38,7 @@ import org.eclipse.jetty.io.ssl.SslConnection; import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -64,7 +64,7 @@ import org.eclipse.jetty.util.thread.TimerScheduler; * {@link ByteBuffer} instances from a pool. The default is to use a new {@link ArrayByteBufferPool} instance. * * - * These services are managed as aggregate beans by the {@link AggregateLifeCycle} super class and + * These services are managed as aggregate beans by the {@link ContainerLifeCycle} super class and * may either be managed or unmanaged beans. * *

Connection Factories

@@ -132,7 +132,7 @@ import org.eclipse.jetty.util.thread.TimerScheduler; * sufficient for modern persistent protocols (HTTP/1.1, SPDY etc.) */ @ManagedObject("Abstract implementation of the Connector Interface") -public abstract class AbstractConnector extends AggregateLifeCycle implements Connector, Dumpable +public abstract class AbstractConnector extends ContainerLifeCycle implements Connector, Dumpable { protected final Logger LOG = Log.getLogger(getClass()); // Order is important on server side, so we use a LinkedHashMap diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ClassLoaderDump.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ClassLoaderDump.java new file mode 100644 index 00000000000..ee381897a61 --- /dev/null +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ClassLoaderDump.java @@ -0,0 +1,49 @@ +package org.eclipse.jetty.server; + +import java.io.IOException; +import java.net.URLClassLoader; +import java.util.Collections; + +import org.eclipse.jetty.util.TypeUtil; +import org.eclipse.jetty.util.component.ContainerLifeCycle; +import org.eclipse.jetty.util.component.Dumpable; + +public class ClassLoaderDump implements Dumpable +{ + final ClassLoader _loader; + + public ClassLoaderDump(ClassLoader loader) + { + _loader = loader; + } + + @Override + public String dump() + { + return ContainerLifeCycle.dump(this); + } + + @Override + public void dump(Appendable out, String indent) throws IOException + { + if (_loader==null) + out.append("No ClassLoader\n"); + else + { + out.append(String.valueOf(_loader)).append("\n"); + + Object parent = _loader.getParent(); + if (parent != null) + { + if (!(parent instanceof Dumpable)) + parent = new ClassLoaderDump((ClassLoader)parent); + + if (_loader instanceof URLClassLoader) + ContainerLifeCycle.dump(out,indent,TypeUtil.asList(((URLClassLoader)_loader).getURLs()),Collections.singleton(parent)); + else + ContainerLifeCycle.dump(out,indent,Collections.singleton(parent)); + } + } + } + +} \ No newline at end of file diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectorStatistics.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectorStatistics.java index dfd4e4ecb3b..da88a424a95 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectorStatistics.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectorStatistics.java @@ -26,7 +26,7 @@ import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.ManagedOperation; import org.eclipse.jetty.util.component.AbstractLifeCycle; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.statistic.CounterStatistic; import org.eclipse.jetty.util.statistic.SampleStatistic; @@ -183,13 +183,13 @@ public class ConnectorStatistics extends AbstractLifeCycle implements Dumpable, @ManagedOperation("dump thread state") public String dump() { - return AggregateLifeCycle.dump(this); + return ContainerLifeCycle.dump(this); } @Override public void dump(Appendable out, String indent) throws IOException { - AggregateLifeCycle.dumpObject(out,this); - AggregateLifeCycle.dump(out,indent,Arrays.asList(new String[]{"connections="+_connectionStats,"duration="+_connectionDurationStats,"in="+_messagesIn,"out="+_messagesOut})); + ContainerLifeCycle.dumpObject(out,this); + ContainerLifeCycle.dump(out,indent,Arrays.asList(new String[]{"connections="+_connectionStats,"duration="+_connectionDurationStats,"in="+_messagesIn,"out="+_messagesOut})); } } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelConfig.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelConfig.java index 2ef9016b82c..39bdaf4d5c0 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelConfig.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelConfig.java @@ -22,7 +22,10 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import org.eclipse.jetty.http.HttpScheme; +import org.eclipse.jetty.util.annotation.ManagedAttribute; +import org.eclipse.jetty.util.annotation.ManagedObject; +@ManagedObject("HTTP Channel Configuration") public class HttpChannelConfig { private List _customizers=new CopyOnWriteArrayList<>(); @@ -60,26 +63,31 @@ public class HttpChannelConfig return null; } + @ManagedAttribute("The size in bytes of the output buffer used to aggregate HTTP output") public int getOutputBufferSize() { return _outputBufferSize; } - + + @ManagedAttribute("The maximum allowed size in bytes for a HTTP request header") public int getRequestHeaderSize() { return _requestHeaderSize; } - + + @ManagedAttribute("The maximum allowed size in bytes for a HTTP response header") public int getResponseHeaderSize() { return _responseHeaderSize; } + @ManagedAttribute("The port to which Integral or Confidential security constraints are redirected") public int getSecurePort() { return _securePort; } + @ManagedAttribute("The scheme with which Integral or Confidential security constraints are redirected") public String getSecureScheme() { return _secureScheme; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index 5eefee5bf12..767ea8fa036 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.List; @@ -48,7 +49,6 @@ import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.Name; -import org.eclipse.jetty.util.component.Container; import org.eclipse.jetty.util.component.Destroyable; import org.eclipse.jetty.util.component.Graceful; import org.eclipse.jetty.util.component.LifeCycle; @@ -70,7 +70,6 @@ public class Server extends HandlerWrapper implements Attributes { private static final Logger LOG = Log.getLogger(Server.class); - private final Container _container; private final AttributesMap _attributes = new AttributesMap(); private final ThreadPool _threadPool; private final List _connectors = new CopyOnWriteArrayList<>(); @@ -85,7 +84,7 @@ public class Server extends HandlerWrapper implements Attributes /* ------------------------------------------------------------ */ public Server() { - this((ThreadPool)null,null); + this((ThreadPool)null); } /* ------------------------------------------------------------ */ @@ -94,7 +93,7 @@ public class Server extends HandlerWrapper implements Attributes */ public Server(@Name("port")int port) { - this((ThreadPool)null,null); + this((ThreadPool)null); ServerConnector connector=new ServerConnector(this); connector.setPort(port); setConnectors(new Connector[]{connector}); @@ -106,7 +105,7 @@ public class Server extends HandlerWrapper implements Attributes */ public Server(@Name("address")InetSocketAddress addr) { - this((ThreadPool)null,null); + this((ThreadPool)null); ServerConnector connector=new ServerConnector(this); connector.setHost(addr.getHostName()); connector.setPort(addr.getPort()); @@ -114,17 +113,11 @@ public class Server extends HandlerWrapper implements Attributes } + /* ------------------------------------------------------------ */ public Server(@Name("threadpool") ThreadPool pool) - { - this(pool, null); - } - - /* ------------------------------------------------------------ */ - public Server(@Name("threadpool") ThreadPool pool,@Name("container") Container container) { _threadPool=pool!=null?pool:new QueuedThreadPool(); - _container=container!=null?container:new Container(); addBean(_threadPool); setServer(this); } @@ -137,15 +130,6 @@ public class Server extends HandlerWrapper implements Attributes return Jetty.VERSION; } - /* ------------------------------------------------------------ */ - /** - * @return Returns the container. - */ - public Container getContainer() - { - return _container; - } - /* ------------------------------------------------------------ */ public boolean getStopAtShutdown() { @@ -186,8 +170,11 @@ public class Server extends HandlerWrapper implements Attributes /* ------------------------------------------------------------ */ public void addConnector(Connector connector) { + if (connector.getServer() != this) + throw new IllegalArgumentException("Connector " + connector + + " cannot be shared among server " + connector.getServer() + " and server " + this); if (_connectors.add(connector)) - _container.update(this, null, connector, "connector"); + addBean(connector); } /* ------------------------------------------------------------ */ @@ -199,7 +186,7 @@ public class Server extends HandlerWrapper implements Attributes public void removeConnector(Connector connector) { if (_connectors.remove(connector)) - _container.update(this, connector, null, "connector"); + removeBean(connector); } /* ------------------------------------------------------------ */ @@ -220,7 +207,7 @@ public class Server extends HandlerWrapper implements Attributes } Connector[] oldConnectors = getConnectors(); - _container.update(this, oldConnectors, connectors, "connector"); + updateBeans(oldConnectors, connectors); _connectors.removeAll(Arrays.asList(oldConnectors)); if (connectors != null) _connectors.addAll(Arrays.asList(connectors)); @@ -397,7 +384,7 @@ public class Server extends HandlerWrapper implements Attributes * or after the entire request has been received (for short requests of known length), or * on the dispatch of an async request. */ - public void handle(HttpChannel connection) throws IOException, ServletException + public void handle(HttpChannel connection) throws IOException, ServletException { final String target=connection.getRequest().getPathInfo(); final Request request=connection.getRequest(); @@ -439,7 +426,7 @@ public class Server extends HandlerWrapper implements Attributes * or after the entire request has been received (for short requests of known length), or * on the dispatch of an async request. */ - public void handleAsync(HttpChannel connection) throws IOException, ServletException + public void handleAsync(HttpChannel connection) throws IOException, ServletException { final HttpChannelState async = connection.getRequest().getAsyncContinuation(); final HttpChannelState.AsyncEventState state = async.getAsyncEventState(); @@ -481,7 +468,6 @@ public class Server extends HandlerWrapper implements Attributes getThreadPool().join(); } - /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** * @return Returns the sessionIdManager. @@ -491,19 +477,14 @@ public class Server extends HandlerWrapper implements Attributes return _sessionIdManager; } - /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** * @param sessionIdManager The sessionIdManager to set. */ public void setSessionIdManager(SessionIdManager sessionIdManager) { - if (_sessionIdManager!=null) - removeBean(_sessionIdManager); - _container.update(this, _sessionIdManager, sessionIdManager, "sessionIdManager",false); - _sessionIdManager = sessionIdManager; - if (_sessionIdManager!=null) - addBean(_sessionIdManager); + updateBean(_sessionIdManager,sessionIdManager); + _sessionIdManager=sessionIdManager; } /* ------------------------------------------------------------ */ @@ -532,41 +513,6 @@ public class Server extends HandlerWrapper implements Attributes return _sendDateHeader; } - /* ------------------------------------------------------------ */ - /** - * 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)) - { - _container.addBean(o); - return true; - } - return false; - } - - /* ------------------------------------------------------------ */ - /** - * Remove an associated bean. - */ - @Override - public boolean removeBean (Object o) - { - if (super.removeBean(o)) - { - _container.removeBean(o); - return true; - } - return false; - } - /* ------------------------------------------------------------ */ /* * @see org.eclipse.util.AttributesMap#clearAttributes() @@ -646,14 +592,12 @@ public class Server extends HandlerWrapper implements Attributes return this.getClass().getName()+"@"+Integer.toHexString(hashCode()); } - /* ------------------------------------------------------------ */ @Override public void dump(Appendable out,String indent) throws IOException { - dumpThis(out); - dump(out,indent,TypeUtil.asList(getHandlers()),getBeans(),_connectors); + dumpBeans(out,indent,Collections.singleton(new ClassLoaderDump(this.getClass().getClassLoader()))); } - + /* ------------------------------------------------------------ */ public static void main(String...args) throws Exception { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandler.java index e33a5979160..0ede60ed471 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandler.java @@ -24,18 +24,16 @@ import java.io.IOException; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; /* ------------------------------------------------------------ */ /** AbstractHandler. - * - * */ @ManagedObject("Jetty Handler") -public abstract class AbstractHandler extends AggregateLifeCycle implements Handler +public abstract class AbstractHandler extends ContainerLifeCycle implements Handler { private static final Logger LOG = Log.getLogger(AbstractHandler.class); @@ -72,33 +70,32 @@ public abstract class AbstractHandler extends AggregateLifeCycle implements Hand } /* ------------------------------------------------------------ */ + @Override public void setServer(Server server) { - Server old_server=_server; - if (old_server!=null && old_server!=server) - old_server.getContainer().removeBean(this); + if (isStarted()) + throw new IllegalStateException(STARTED); _server=server; - if (_server!=null && _server!=old_server) - _server.getContainer().addBean(this); } /* ------------------------------------------------------------ */ + @Override public Server getServer() { return _server; } /* ------------------------------------------------------------ */ + @Override public void destroy() { if (!isStopped()) throw new IllegalStateException("!STOPPED"); super.destroy(); - if (_server!=null) - _server.getContainer().removeBean(this); } /* ------------------------------------------------------------ */ + @Override public void dumpThis(Appendable out) throws IOException { out.append(toString()).append(" - ").append(getState()).append('\n'); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java index 39e54a16840..99785c3ea23 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/AbstractHandlerContainer.java @@ -117,12 +117,4 @@ public abstract class AbstractHandlerContainer extends AbstractHandler implement } return null; } - - /* ------------------------------------------------------------ */ - @Override - public void dump(Appendable out,String indent) throws IOException - { - dumpThis(out); - dump(out,indent,getBeans(),TypeUtil.asList(getHandlers())); - } } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index d97874d1ac0..2fe3766f664 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -61,6 +61,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.MimeTypes; +import org.eclipse.jetty.server.ClassLoaderDump; import org.eclipse.jetty.server.Dispatcher; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HandlerContainer; @@ -73,12 +74,9 @@ import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.LazyList; import org.eclipse.jetty.util.Loader; import org.eclipse.jetty.util.StringUtil; -import org.eclipse.jetty.util.TypeUtil; import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; -import org.eclipse.jetty.util.component.AggregateLifeCycle; -import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.Graceful; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -224,9 +222,11 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu @Override public void dump(Appendable out, String indent) throws IOException { - dumpThis(out); - dump(out,indent,Collections.singletonList(new CLDump(getClassLoader())),TypeUtil.asList(getHandlers()),getBeans(),_initParams.entrySet(), - _attributes.getAttributeEntrySet(),_contextAttributes.getAttributeEntrySet()); + dumpBeans(out,indent, + Collections.singletonList(new ClassLoaderDump(getClassLoader())), + _initParams.entrySet(), + _attributes.getAttributeEntrySet(), + _contextAttributes.getAttributeEntrySet()); } /* ------------------------------------------------------------ */ @@ -259,18 +259,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu @Override public void setServer(Server server) { + super.setServer(server); if (_errorHandler != null) - { - Server old_server = getServer(); - if (old_server != null && old_server != server) - old_server.getContainer().update(this,_errorHandler,null,"error",true); - super.setServer(server); - if (server != null && server != old_server) - server.getContainer().update(this,null,_errorHandler,"error",true); _errorHandler.setServer(server); - } - else - super.setServer(server); } /* ------------------------------------------------------------ */ @@ -395,6 +386,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu /* * @see javax.servlet.ServletContext#getAttribute(java.lang.String) */ + @Override public Object getAttribute(String name) { return _attributes.getAttribute(name); @@ -405,6 +397,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu * @see javax.servlet.ServletContext#getAttributeNames() */ @SuppressWarnings("unchecked") + @Override public Enumeration getAttributeNames() { return AttributesMap.getAttributeNamesCopy(_attributes); @@ -1162,6 +1155,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu /* * @see javax.servlet.ServletContext#removeAttribute(java.lang.String) */ + @Override public void removeAttribute(String name) { checkManagedAttribute(name,null); @@ -1175,6 +1169,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu * * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object) */ + @Override public void setAttribute( String name, Object value) { checkManagedAttribute(name,value); @@ -1199,6 +1194,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu } /* ------------------------------------------------------------ */ + @Override public void clearAttributes() { Enumeration e = _attributes.getAttributeNames(); @@ -1223,7 +1219,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu public void setManagedAttribute(String name, Object value) { Object old = _managedAttributes.put(name,value); - getServer().getContainer().update(this,old,value,name,true); + updateBean(old,value); } /* ------------------------------------------------------------ */ @@ -1395,11 +1391,11 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu */ public void setErrorHandler(ErrorHandler errorHandler) { + updateBean(_errorHandler,errorHandler); + _errorHandler = errorHandler; if (errorHandler != null) errorHandler.setServer(getServer()); - if (getServer() != null) - getServer().getContainer().update(this,_errorHandler,errorHandler,"errorHandler",true); - _errorHandler = errorHandler; + } /* ------------------------------------------------------------ */ @@ -2374,40 +2370,4 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu return _enabled; } } - - private static class CLDump implements Dumpable - { - final ClassLoader _loader; - - CLDump(ClassLoader loader) - { - _loader = loader; - } - - public String dump() - { - return AggregateLifeCycle.dump(this); - } - - public void dump(Appendable out, String indent) throws IOException - { - out.append(String.valueOf(_loader)).append("\n"); - - if (_loader != null) - { - Object parent = _loader.getParent(); - if (parent != null) - { - if (!(parent instanceof Dumpable)) - parent = new CLDump((ClassLoader)parent); - - if (_loader instanceof URLClassLoader) - AggregateLifeCycle.dump(out,indent,TypeUtil.asList(((URLClassLoader)_loader).getURLs()),Collections.singleton(parent)); - else - AggregateLifeCycle.dump(out,indent,Collections.singleton(parent)); - } - } - } - - } } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java index 2a2d8e3ba36..c2bfa918aed 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java @@ -49,7 +49,6 @@ public class HandlerCollection extends AbstractHandlerContainer { private final boolean _mutableWhenRunning; private volatile Handler[] _handlers; - private boolean _parallelStart=false; /* ------------------------------------------------------------ */ public HandlerCollection() @@ -84,67 +83,24 @@ public class HandlerCollection extends AbstractHandlerContainer if (!_mutableWhenRunning && isStarted()) throw new IllegalStateException(STARTED); - Handler [] old_handlers = _handlers==null?null:_handlers.clone(); + updateBeans(_handlers, handlers); _handlers = handlers; Server server = getServer(); - MultiException mex = new MultiException(); for (int i=0;handlers!=null && i0;) - try{_handlers[i].stop();}catch(Throwable e){mex.add(e);} - } - mex.ifExceptionThrow(); - } - /* ------------------------------------------------------------ */ @Override public void setServer(Server server) { - if (isStarted()) - throw new IllegalStateException(STARTED); - - Server old_server=getServer(); - super.setServer(server); - Handler[] h=getHandlers(); for (int i=0;h!=null && i extends AbstractLifeCycle implements Dumpable } /* ------------------------------------------------------------ */ + @Override public String toString() { return _name; @@ -277,17 +278,19 @@ public class Holder extends AbstractLifeCycle implements Dumpable } /* ------------------------------------------------------------ */ + @Override public void dump(Appendable out, String indent) throws IOException { out.append(_name).append("==").append(_className) .append(" - ").append(AbstractLifeCycle.getState(this)).append("\n"); - AggregateLifeCycle.dump(out,indent,_initParams.entrySet()); + ContainerLifeCycle.dump(out,indent,_initParams.entrySet()); } /* ------------------------------------------------------------ */ + @Override public String dump() { - return AggregateLifeCycle.dump(this); + return ContainerLifeCycle.dump(this); } /* ------------------------------------------------------------ */ diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java index d3561d3ed4c..95b8c5e670c 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java @@ -122,32 +122,6 @@ public class ServletHandler extends ScopedHandler { } - /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.handler.AbstractHandler#setServer(org.eclipse.jetty.server.Server) - */ - public void setServer(Server server) - { - Server old=getServer(); - if (old!=null && old!=server) - { - getServer().getContainer().update(this, _filters, null, "filter",true); - getServer().getContainer().update(this, _filterMappings, null, "filterMapping",true); - getServer().getContainer().update(this, _servlets, null, "servlet",true); - getServer().getContainer().update(this, _servletMappings, null, "servletMapping",true); - } - - super.setServer(server); - - if (server!=null && old!=server) - { - server.getContainer().update(this, null, _filters, "filter",true); - server.getContainer().update(this, null, _filterMappings, "filterMapping",true); - server.getContainer().update(this, null, _servlets, "servlet",true); - server.getContainer().update(this, null, _servletMappings, "servletMapping",true); - } - } - /* ----------------------------------------------------------------- */ @Override protected synchronized void doStart() @@ -1222,8 +1196,7 @@ public class ServletHandler extends ScopedHandler */ public void setFilterMappings(FilterMapping[] filterMappings) { - if (getServer()!=null) - getServer().getContainer().update(this,_filterMappings,filterMappings,"filterMapping",true); + updateBeans(_filterMappings,filterMappings); _filterMappings = filterMappings; updateMappings(); invalidateChainsCache(); @@ -1232,8 +1205,11 @@ public class ServletHandler extends ScopedHandler /* ------------------------------------------------------------ */ public synchronized void setFilters(FilterHolder[] holders) { - if (getServer()!=null) - getServer().getContainer().update(this,_filters,holders,"filter",true); + if (holders!=null) + for (FilterHolder holder:holders) + holder.setServletHandler(this); + + updateBeans(_filters,holders); _filters=holders; updateNameMappings(); invalidateChainsCache(); @@ -1245,8 +1221,7 @@ public class ServletHandler extends ScopedHandler */ public void setServletMappings(ServletMapping[] servletMappings) { - if (getServer()!=null) - getServer().getContainer().update(this,_servletMappings,servletMappings,"servletMapping",true); + updateBeans(_servletMappings,servletMappings); _servletMappings = servletMappings; updateMappings(); invalidateChainsCache(); @@ -1254,12 +1229,14 @@ public class ServletHandler extends ScopedHandler /* ------------------------------------------------------------ */ /** Set Servlets. - * @param holders Array of servletsto define + * @param holders Array of servlets to define */ public synchronized void setServlets(ServletHolder[] holders) { - if (getServer()!=null) - getServer().getContainer().update(this,_servlets,holders,"servlet",true); + if (holders!=null) + for (ServletHolder holder:holders) + holder.setServletHandler(this); + updateBeans(_servlets,holders); _servlets=holders; updateNameMappings(); invalidateChainsCache(); @@ -1470,18 +1447,4 @@ public class ServletHandler extends ScopedHandler if (_contextHandler!=null) _contextHandler.destroyFilter(filter); } - - /* ------------------------------------------------------------ */ - @Override - public void dump(Appendable out,String indent) throws IOException - { - super.dumpThis(out); - dump(out,indent, - TypeUtil.asList(getHandlers()), - getBeans(), - TypeUtil.asList(getFilterMappings()), - TypeUtil.asList(getFilters()), - TypeUtil.asList(getServletMappings()), - TypeUtil.asList(getServlets())); - } } diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletTester.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletTester.java index f47ef873ced..7deed9333f0 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletTester.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletTester.java @@ -32,10 +32,10 @@ import org.eclipse.jetty.server.LocalConnector; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.Attributes; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.resource.Resource; -public class ServletTester extends AggregateLifeCycle +public class ServletTester extends ContainerLifeCycle { private final Server _server=new Server(); private final LocalConnector _connector=new LocalConnector(_server); diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java index fe83e54ee11..0d53d3b86fa 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DefaultServletTest.java @@ -497,16 +497,19 @@ public class DefaultServletTest public static class OutputFilter implements Filter { + @Override public void init(FilterConfig filterConfig) throws ServletException { } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.getOutputStream().println("Extra Info"); chain.doFilter(request, response); } + @Override public void destroy() { } @@ -514,16 +517,19 @@ public class DefaultServletTest public static class WriterFilter implements Filter { + @Override public void init(FilterConfig filterConfig) throws ServletException { } + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.getWriter().println("Extra Info"); chain.doFilter(request, response); } + @Override public void destroy() { } diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java index 90ea0dc1522..54be3c1966d 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletContextHandlerTest.java @@ -91,7 +91,7 @@ public class ServletContextHandlerTest _server.start(); StringBuffer request = new StringBuffer(); - request.append("GET /test HTTP/1.1\n"); + request.append("GET /test HTTP/1.0\n"); request.append("Host: localhost\n"); request.append("\n"); @@ -101,7 +101,7 @@ public class ServletContextHandlerTest context.addServlet(HelloServlet.class, "/hello"); request = new StringBuffer(); - request.append("GET /hello HTTP/1.1\n"); + request.append("GET /hello HTTP/1.0\n"); request.append("Host: localhost\n"); request.append("\n"); @@ -119,7 +119,7 @@ public class ServletContextHandlerTest _server.start(); StringBuffer request = new StringBuffer(); - request.append("GET /test HTTP/1.1\n"); + request.append("GET /test HTTP/1.0\n"); request.append("Host: localhost\n"); request.append("\n"); @@ -133,7 +133,7 @@ public class ServletContextHandlerTest context.start(); request = new StringBuffer(); - request.append("GET /hello HTTP/1.1\n"); + request.append("GET /hello HTTP/1.0\n"); request.append("Host: localhost\n"); request.append("\n"); @@ -151,7 +151,7 @@ public class ServletContextHandlerTest _server.start(); StringBuffer request = new StringBuffer(); - request.append("GET /test HTTP/1.1\n"); + request.append("GET /test HTTP/1.0\n"); request.append("Host: localhost\n"); request.append("\n"); @@ -166,7 +166,7 @@ public class ServletContextHandlerTest context.addServlet(HelloServlet.class,"/hello"); request = new StringBuffer(); - request.append("GET /hello HTTP/1.1\n"); + request.append("GET /hello HTTP/1.0\n"); request.append("Host: localhost\n"); request.append("\n"); diff --git a/jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/SPDYClient.java b/jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/SPDYClient.java index 8d42bb1d045..a9374437cbe 100644 --- a/jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/SPDYClient.java +++ b/jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/SPDYClient.java @@ -44,7 +44,7 @@ import org.eclipse.jetty.spdy.FlowControlStrategy; import org.eclipse.jetty.spdy.Promise; import org.eclipse.jetty.spdy.api.Session; import org.eclipse.jetty.spdy.api.SessionFrameListener; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.Scheduler; @@ -160,7 +160,7 @@ public class SPDYClient connection.onOpen(); } - public static class Factory extends AggregateLifeCycle + public static class Factory extends ContainerLifeCycle { private final Queue sessions = new ConcurrentLinkedQueue<>(); final ByteBufferPool bufferPool = new MappedByteBufferPool(); diff --git a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/StandardSession.java b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/StandardSession.java index cb3a076215f..adc8376d0ca 100644 --- a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/StandardSession.java +++ b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/StandardSession.java @@ -71,7 +71,7 @@ import org.eclipse.jetty.util.Atomics; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.ForkInvoker; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -1093,14 +1093,14 @@ public class StandardSession implements ISession, Parser.Listener, Callback diff --git a/jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java b/jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java index fd22cd8a2c6..dc783551107 100644 --- a/jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java +++ b/jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java @@ -22,7 +22,6 @@ package org.eclipse.jetty.spring; import java.net.InetSocketAddress; import java.util.Collection; -import org.eclipse.jetty.util.component.Container; import org.eclipse.jetty.util.thread.ThreadPool; @@ -49,11 +48,6 @@ public class Server extends org.eclipse.jetty.server.Server super(port); } - public Server(ThreadPool pool, Container container) - { - super(pool,container); - } - public Server(ThreadPool pool) { super(pool); diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java b/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java index 73d47bc3cb1..53593187fe7 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java @@ -142,35 +142,9 @@ public class Main { List ini_args=new ArrayList(); File start_ini = new File(_jettyHome,"start.ini"); - - boolean start_ini_processed=!start_ini.exists(); - - File start_d = new File(_jettyHome,"start.d"); - if (start_d.isDirectory()) - { - File[] inis = start_d.listFiles(new FilenameFilter() - { - public boolean accept(File dir, String name) - { - return name.toLowerCase().endsWith(".ini"); - } - }); - Arrays.sort(inis); - - for (File i : inis) - { - if (!start_ini_processed && i.getName().charAt(0)>'0') - { - start_ini_processed=true; - ini_args.addAll(loadStartIni(start_ini)); - } - ini_args.addAll(loadStartIni(i)); - } - } - - if (!start_ini_processed ) + if (start_ini.exists()) ini_args.addAll(loadStartIni(start_ini)); - + return ini_args; } @@ -258,12 +232,6 @@ public class Main continue; } - if (arg.startsWith("--pre=")) - { - xmls.add(startup++,arg.substring(6)); - continue; - } - if (arg.startsWith("-D")) { String[] assign = arg.substring(2).split("=",2); @@ -1073,7 +1041,7 @@ public class Main /** * Convert a start.ini format file into an argument list. */ - static List loadStartIni(File ini) + List loadStartIni(File ini) { if (!ini.exists()) { @@ -1099,6 +1067,39 @@ public class Main { continue; } + + if (arg.endsWith("/")) + { + try + { + File start_d = new File(arg); + if (!start_d.exists() || !start_d.isDirectory()) + start_d = new File(_jettyHome,arg); + + if (start_d.isDirectory()) + { + File[] inis = start_d.listFiles(new FilenameFilter() + { + @Override + public boolean accept(File dir, String name) + { + return name.toLowerCase().endsWith(".ini"); + } + }); + Arrays.sort(inis); + + for (File i : inis) + args.addAll(loadStartIni(i)); + + continue; + } + } + catch(Exception e) + { + e.printStackTrace(); + } + } + args.add(arg); } } diff --git a/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt b/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt index 2a899160a28..5848974e706 100644 --- a/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt +++ b/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt @@ -41,9 +41,6 @@ Command Line Options: and additional .ini files in jetty.home/start.d/ will NOT be read. A --ini option with no file indicates that start.ini should not be read. - - --pre= Specify a configuration file that is to be processed - before any configuration files listed in start.ini System Properties: These are set with a command line like "java -Dname=value ..." and are @@ -118,14 +115,12 @@ Available Configurations: Defaults: A start.ini file may be used to specify default arguments to start.jar, which are used if no command line arguments are provided and override - the defaults in the start.config file. If the directory jetty.home/start.d - exists, then multiple *.ini files will be read from that directory in - alphabetical order. The contents of start.ini are processed after any - start.d files starting with a character <= "0". + the defaults in the start.config file. If a line of start.ini contains + a directory (eg start.d/) then that directory is scanned for *.ini files + will be processed in name sorted order. - If --ini options are provided on the command line, then start.ini and - start.d will NOT be read. + If --ini options are provided on the command line, then start.ini will NOT be read. - The current start.ini/start.d arguments are: + The current start.ini arguments are: @STARTINI@ diff --git a/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java b/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java index db7850d6e4b..0b70111d33a 100644 --- a/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java +++ b/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java @@ -55,9 +55,10 @@ public class MainTest { Main main = new Main(); List args = main.parseStartIniFiles(); + assertEquals("Expected 5 uncommented lines in start.ini",9,args.size()); assertEquals("First uncommented line in start.ini doesn't match expected result","OPTIONS=Server,jsp,resources,websocket,ext",args.get(0)); - assertEquals("Last uncommented line in start.ini doesn't match expected result","etc/jetty-testrealm.xml",args.get(8)); + assertEquals("Last uncommented line in start.ini doesn't match expected result","etc/jetty-contexts.xml",args.get(8)); } @Test @@ -65,10 +66,11 @@ public class MainTest { Main main = new Main(); List args = main.expandCommandLine(new String[] {}); + assertEquals("start.ini OPTIONS","OPTIONS=Server,jsp,resources,websocket,ext",args.get(0)); - assertEquals("start.d/jmx OPTIONS","OPTIONS=jmx",args.get(5)); - assertEquals("start.d/jmx XML","--pre=etc/jetty-jmx.xml",args.get(6)); - assertEquals("start.d/websocket OPTIONS","OPTIONS=websocket",args.get(7)); + assertEquals("start.d/jmx OPTIONS","OPTIONS=jmx",args.get(2)); + assertEquals("start.d/jmx XML","etc/jetty-jmx.xml",args.get(3)); + assertEquals("start.d/websocket OPTIONS","OPTIONS=websocket",args.get(4)); } @Test @@ -78,9 +80,12 @@ public class MainTest List args = main.expandCommandLine(new String[] {}); List xmls = main.processCommandLine(args); - assertEquals("jmx --pre","etc/jetty-jmx.xml",xmls.get(0)); - assertEquals("start.ini","etc/jetty.xml",xmls.get(1)); - assertEquals("start.d","etc/jetty-testrealm.xml",xmls.get(5)); + System.err.println(args); + System.err.println(xmls); + assertEquals("etc/jetty.xml",xmls.get(0)); + assertEquals("etc/jetty-jmx.xml",xmls.get(1)); + assertEquals("start.d","etc/jetty-testrealm.xml",xmls.get(2)); + assertEquals("start.d","etc/jetty-contexts.xml",xmls.get(5)); } @Test diff --git a/jetty-start/src/test/resources/jetty.home/start.d/10-jmx.ini b/jetty-start/src/test/resources/jetty.home/start.d/10-jmx.ini index 827e41bf43c..356fccd3906 100644 --- a/jetty-start/src/test/resources/jetty.home/start.d/10-jmx.ini +++ b/jetty-start/src/test/resources/jetty.home/start.d/10-jmx.ini @@ -18,5 +18,5 @@ OPTIONS=jmx # For a full list of available configuration files do # java -jar start.jar --help #----------------------------------------------------------- ---pre=etc/jetty-jmx.xml +etc/jetty-jmx.xml #=========================================================== diff --git a/jetty-start/src/test/resources/jetty.home/start.ini b/jetty-start/src/test/resources/jetty.home/start.ini index a9b724988ef..65c0816fbbd 100644 --- a/jetty-start/src/test/resources/jetty.home/start.ini +++ b/jetty-start/src/test/resources/jetty.home/start.ini @@ -56,6 +56,7 @@ OPTIONS=Server,jsp,resources,websocket,ext # java -jar start.jar --help #----------------------------------------------------------- etc/jetty.xml +start.d/ # etc/jetty-ssl.xml # etc/jetty-requestlog.xml etc/jetty-deploy.xml diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java index 948b0c47233..9c6199e373d 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java @@ -55,7 +55,8 @@ public abstract class AbstractLifeCycle implements LifeCycle protected void doStop() throws Exception { } - + + @Override public final void start() throws Exception { synchronized (_lock) @@ -76,6 +77,7 @@ public abstract class AbstractLifeCycle implements LifeCycle } } + @Override public final void stop() throws Exception { synchronized (_lock) @@ -96,6 +98,7 @@ public abstract class AbstractLifeCycle implements LifeCycle } } + @Override public boolean isRunning() { final int state = _state; @@ -103,36 +106,43 @@ public abstract class AbstractLifeCycle implements LifeCycle return state == __STARTED || state == __STARTING; } + @Override public boolean isStarted() { return _state == __STARTED; } + @Override public boolean isStarting() { return _state == __STARTING; } + @Override public boolean isStopping() { return _state == __STOPPING; } + @Override public boolean isStopped() { return _state == __STOPPED; } + @Override public boolean isFailed() { return _state == __FAILED; } + @Override public void addLifeCycleListener(LifeCycle.Listener listener) { _listeners.add(listener); } + @Override public void removeLifeCycleListener(LifeCycle.Listener listener) { _listeners.remove(listener); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java index 6dcc9789b57..faeea9e4718 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java @@ -17,289 +17,56 @@ // package org.eclipse.jetty.util.component; -import java.lang.ref.WeakReference; -import java.util.EventListener; -import java.util.concurrent.CopyOnWriteArrayList; -import org.eclipse.jetty.util.LazyList; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; +import java.util.Collection; -/* ------------------------------------------------------------ */ -/** Container. - * This class allows a containment events to be generated from update methods. - * - * The style of usage is:
- *   public void setFoo(Foo foo)
- *   {
- *       getContainer().update(this,this.foo,foo,"foo");
- *       this.foo=foo;
- *   }
- *   
- *   public void setBars(Bar[] bars)
- *   {
- *       getContainer().update(this,this.bars,bars,"bar");
- *       this.bars=bars;
- *   }
- * 
- */ -public class Container +public interface Container { - private static final Logger LOG = Log.getLogger(Container.class); - private final CopyOnWriteArrayList _listeners=new CopyOnWriteArrayList(); - - public void addEventListener(Container.Listener listener) - { - _listeners.add(listener); - } - - public void removeEventListener(Container.Listener listener) - { - _listeners.remove(listener); - } - - /* ------------------------------------------------------------ */ - /** Update single parent to child relationship. - * @param parent The parent of the child. - * @param oldChild The previous value of the child. If this is non null and differs from child, then a remove event is generated. - * @param child The current child. If this is non null and differs from oldChild, then an add event is generated. - * @param relationship The name of the relationship - */ - public void update(Object parent, Object oldChild, final Object child, String relationship) - { - if (oldChild!=null && !oldChild.equals(child)) - remove(parent,oldChild,relationship); - if (child!=null && !child.equals(oldChild)) - add(parent,child,relationship); - } - - /* ------------------------------------------------------------ */ - /** Update single parent to child relationship. - * @param parent The parent of the child. - * @param oldChild The previous value of the child. If this is non null and differs from child, then a remove event is generated. - * @param child The current child. If this is non null and differs from oldChild, then an add event is generated. - * @param relationship The name of the relationship - * @param addRemove If true add/remove is called for the new/old children as well as the relationships - */ - public void update(Object parent, Object oldChild, final Object child, String relationship,boolean addRemove) - { - if (oldChild!=null && !oldChild.equals(child)) - { - remove(parent,oldChild,relationship); - if (addRemove) - removeBean(oldChild); - } - - if (child!=null && !child.equals(oldChild)) - { - if (addRemove) - addBean(child); - add(parent,child,relationship); - } - } + public boolean addBean(Object o); - /* ------------------------------------------------------------ */ - /** Update multiple parent to child relationship. - * @param parent The parent of the child. - * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the children array. - * This array is modified and children that remain in the new children array are nulled out of the old children array. - * @param children The current array of children. An add event is generated for any child in this array but not in the oldChildren array. - * @param relationship The name of the relationship + /** + * @return the list of beans known to this aggregate + * @see #getBean(Class) */ - public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship) - { - update(parent,oldChildren,children,relationship,false); - } - - /* ------------------------------------------------------------ */ - /** Update multiple parent to child relationship. - * @param parent The parent of the child. - * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the children array. - * This array is modified and children that remain in the new children array are nulled out of the old children array. - * @param children The current array of children. An add event is generated for any child in this array but not in the oldChildren array. - * @param relationship The name of the relationship - * @param addRemove If true add/remove is called for the new/old children as well as the relationships - */ - public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship, boolean addRemove) - { - Object[] newChildren = null; - if (children!=null) - { - newChildren = new Object[children.length]; - - for (int i=children.length;i-->0;) - { - boolean new_child=true; - if (oldChildren!=null) - { - for (int j=oldChildren.length;j-->0;) - { - if (children[i]!=null && children[i].equals(oldChildren[j])) - { - oldChildren[j]=null; - new_child=false; - } - } - } - if (new_child) - newChildren[i]=children[i]; - } - } - - if (oldChildren!=null) - { - for (int i=oldChildren.length;i-->0;) - { - if (oldChildren[i]!=null) - { - remove(parent,oldChildren[i],relationship); - if (addRemove) - removeBean(oldChildren[i]); - } - } - } - - if (newChildren!=null) - { - for (int i=0;i getBeans(); - /* ------------------------------------------------------------ */ - public void addBean(Object obj) - { - if (_listeners!=null) - { - for (int i=0; i Collection getBeans(Class clazz); - /* ------------------------------------------------------------ */ - public void removeBean(Object obj) - { - if (_listeners!=null) - { - for (int i=0; i T getBean(Class clazz); + + /** + * Removes the given bean. + * @return whether the bean was removed + * @see #removeBeans() */ - private void remove(Object parent, Object child, String relationship) - { - if (LOG.isDebugEnabled()) - LOG.debug("Container "+parent+" - "+child+" as "+relationship); - if (_listeners!=null) - { - Relationship event=new Relationship(this,parent,child,relationship); - for (int i=0; i _parent; - private final WeakReference _child; - private String _relationship; - private Container _container; - - private Relationship(Container container, Object parent,Object child, String relationship) - { - _container=container; - _parent=new WeakReference(parent); - _child=new WeakReference(child); - _relationship=relationship; - } - - public Container getContainer() - { - return _container; - } - - public Object getChild() - { - return _child.get(); - } - - public Object getParent() - { - return _parent.get(); - } - - public String getRelationship() - { - return _relationship; - } - - @Override - public String toString() - { - return _parent+"---"+_relationship+"-->"+_child; - } - - @Override - public int hashCode() - { - return _parent.hashCode()+_child.hashCode()+_relationship.hashCode(); - } - - @Override - public boolean equals(Object o) - { - if (o==null || !(o instanceof Relationship)) - return false; - Relationship r = (Relationship)o; - return r._parent.get()==_parent.get() && r._child.get()==_child.get() && r._relationship.equals(_relationship); - } - } - - /* ------------------------------------------------------------ */ - /** Listener. + /** * A listener for Container events. + * If an added bean implements this interface it will receive the events + * for this container. */ - public interface Listener extends EventListener + public interface Listener + { + void beanAdded(Container parent,Object child); + void beanRemoved(Container parent,Object child); + } + + /** + * Inherited Listener. + * If an added bean implements this interface, then it will + * be added to all contained beans that are themselves Containers + */ + public interface InheritedListener extends Listener { - public void addBean(Object bean); - public void removeBean(Object bean); - public void add(Container.Relationship relationship); - public void remove(Container.Relationship relationship); } } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AggregateLifeCycle.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java similarity index 61% rename from jetty-util/src/main/java/org/eclipse/jetty/util/component/AggregateLifeCycle.java rename to jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java index 8a8e6d734b7..75132da3aa2 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AggregateLifeCycle.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java @@ -42,19 +42,21 @@ import org.eclipse.jetty.util.log.Logger; * The methods {@link #addBean(Object, boolean)}, {@link #manage(Object)} and {@link #unmanage(Object)} can be used to * explicitly control the life cycle relationship. *

- * If adding a bean that is shared between multiple {@link AggregateLifeCycle} instances, then it should be started before being added, so it is unmanaged, or + * If adding a bean that is shared between multiple {@link ContainerLifeCycle} instances, then it should be started before being added, so it is unmanaged, or * the API must be used to explicitly set it as unmanaged. */ -public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable, Dumpable +public class ContainerLifeCycle extends AbstractLifeCycle implements Container, Destroyable, Dumpable { - private static final Logger LOG = Log.getLogger(AggregateLifeCycle.class); + private static final Logger LOG = Log.getLogger(ContainerLifeCycle.class); private final List _beans = new CopyOnWriteArrayList<>(); + private final List _listeners = new CopyOnWriteArrayList<>(); private boolean _started = false; - public AggregateLifeCycle() + + public ContainerLifeCycle() { } - + /** * Starts the managed lifecycle beans in the order they were added. */ @@ -78,10 +80,10 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable break; case AUTO: if (l.isRunning()) - b._managed=Managed.UNMANAGED; + unmanage(b); else { - b._managed=Managed.MANAGED; + manage(b); l.start(); } break; @@ -133,7 +135,6 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable } - /** * @param bean the bean to test * @return whether this aggregate contains the bean @@ -154,7 +155,7 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable { for (Bean b : _beans) if (b._bean == bean) - return b._managed==Managed.MANAGED; + return b.isManaged(); return false; } @@ -169,6 +170,7 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable * @param o the bean object to add * @return true if the bean was added, false if it was already present */ + @Override public boolean addBean(Object o) { if (o instanceof LifeCycle) @@ -176,7 +178,8 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable LifeCycle l = (LifeCycle)o; return addBean(o,l.isRunning()?Managed.UNMANAGED:Managed.AUTO); } - return addBean(o,Managed.MANAGED); + + return addBean(o,Managed.POJO); } /** @@ -188,7 +191,9 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable */ public boolean addBean(Object o, boolean managed) { - return addBean(o,managed?Managed.MANAGED:Managed.UNMANAGED); + if (o instanceof LifeCycle) + return addBean(o,managed?Managed.MANAGED:Managed.UNMANAGED); + return addBean(o,managed?Managed.POJO:Managed.UNMANAGED); } public boolean addBean(Object o, Managed managed) @@ -196,41 +201,87 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable if (contains(o)) return false; - Bean b = new Bean(o); - b._managed = managed; - _beans.add(b); + Bean new_bean = new Bean(o); - if (_started) + // if the bean is a Listener + if (o instanceof Container.Listener) { - try - { - if (o instanceof LifeCycle) - { - LifeCycle l = (LifeCycle)o; + Container.Listener listener = (Container.Listener)o; + _listeners.add(listener); - switch(b._managed) - { - case MANAGED: - if (!l.isRunning()) - l.start(); - break; - case AUTO: - if (l.isRunning()) - b._managed=Managed.UNMANAGED; - else - { - b._managed=Managed.MANAGED; - l.start(); - } - break; - } - } - } - catch (Exception e) + // tell it about existing beans + for (Bean b:_beans) { - throw new RuntimeException(e); + listener.beanAdded(this,b._bean); + + // handle inheritance + if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container) + ((Container)b._bean).addBean(listener); } } + + // Add the bean + _beans.add(new_bean); + + // Tell existing listeners about the new bean + for (Container.Listener l:_listeners) + l.beanAdded(this,o); + + try + { + switch (managed) + { + case UNMANAGED: + unmanage(new_bean); + break; + + case MANAGED: + manage(new_bean); + + if (_started) + { + LifeCycle l = (LifeCycle)o; + if (!l.isRunning()) + l.start(); + } + break; + + case AUTO: + if (o instanceof LifeCycle) + { + LifeCycle l = (LifeCycle)o; + if (_started) + { + if (l.isRunning()) + unmanage(new_bean); + else + { + manage(new_bean); + l.start(); + } + } + else + new_bean._managed=Managed.AUTO; + } + else + new_bean._managed=Managed.POJO; + break; + + case POJO: + new_bean._managed=Managed.POJO; + } + } + catch (RuntimeException | Error e) + { + throw e; + } + catch (Exception e) + { + throw new RuntimeException(e); + } + + LOG.debug("{} added {}",this,new_bean); + return true; } @@ -247,13 +298,35 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable { if (b._bean == bean) { - b._managed = Managed.MANAGED; + manage(b); return; } } throw new IllegalArgumentException("Unknown bean " + bean); } + private void manage(Bean bean) + { + if (bean._managed!=Managed.MANAGED) + { + bean._managed=Managed.MANAGED; + + if (bean._bean instanceof Container) + { + for (Container.Listener l:_listeners) + { + if (l instanceof InheritedListener) + { + if (bean._bean instanceof ContainerLifeCycle) + ((ContainerLifeCycle)bean._bean).addBean(l,false); + else + ((Container)bean._bean).addBean(l); + } + } + } + } + } + /** * Unmanages a bean already contained by this aggregate, so that it is not started/stopped/destroyed with this * aggregate. @@ -266,28 +339,38 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable { if (b._bean == bean) { - b._managed = Managed.UNMANAGED; + unmanage(b); return; } } throw new IllegalArgumentException("Unknown bean " + bean); } - /** - * @return the list of beans known to this aggregate - * @see #getBean(Class) - */ + private void unmanage(Bean bean) + { + if (bean._managed!=Managed.UNMANAGED) + { + if (bean._managed==Managed.MANAGED && bean._bean instanceof Container) + { + for (Container.Listener l:_listeners) + { + if (l instanceof InheritedListener) + ((Container)bean._bean).removeBean(l); + } + } + bean._managed=Managed.UNMANAGED; + } + } + + + @Override public Collection getBeans() { return getBeans(Object.class); } - /** - * @param clazz the class of the beans - * @return the list of beans of the given class (or subclass) - * @see #getBeans() - */ - public List getBeans(Class clazz) + @Override + public Collection getBeans(Class clazz) { ArrayList beans = new ArrayList<>(); for (Bean b : _beans) @@ -298,10 +381,7 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable return beans; } - /** - * @param clazz the class of the bean - * @return the first bean of a specific class (or subclass), or null if no such bean exist - */ + @Override public T getBean(Class clazz) { for (Bean b : _beans) @@ -313,32 +393,63 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable } /** - * Removes all bean, without performing any lifecycle - * @see #destroy() + * Removes all bean */ public void removeBeans() { - _beans.clear(); + ArrayList beans= new ArrayList<>(_beans); + for (Bean b : beans) + remove(b); } - - /** - * Removes the given bean. - * @return whether the bean was removed - * @see #removeBeans() - */ - public boolean removeBean(Object o) + + private Bean getBean(Object o) { for (Bean b : _beans) { if (b._bean == o) + return b; + } + return null; + } + + @Override + public boolean removeBean(Object o) + { + Bean b=getBean(o); + return b!=null && remove(b); + } + + private boolean remove(Bean bean) + { + if (_beans.remove(bean)) + { + unmanage(bean); + + for (Container.Listener l:_listeners) + l.beanRemoved(this,bean._bean); + + if (bean._bean instanceof Container.Listener) { - _beans.remove(b); - return true; + Container.Listener listener = (Container.Listener)bean._bean; + if (_listeners.remove(listener)) + { + // remove existing beans + for (Bean b:_beans) + { + listener.beanRemoved(this,b._bean); + + if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container) + ((Container)b._bean).removeBean(listener); + } + } } + return true; } return false; } + + @Override public void setStopTimeout(long stopTimeout) { @@ -369,6 +480,7 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable } } + @Override public String dump() { return dump(this); @@ -415,9 +527,16 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable @Override public void dump(Appendable out, String indent) throws IOException + { + dumpBeans(out,indent); + } + + protected void dumpBeans(Appendable out, String indent, Collection... collections) throws IOException { dumpThis(out); int size = _beans.size(); + for (Collection c : collections) + size += c.size(); if (size == 0) return; int i = 0; @@ -427,13 +546,21 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable switch(b._managed) { - case MANAGED: + case POJO: out.append(indent).append(" +- "); if (b._bean instanceof Dumpable) ((Dumpable)b._bean).dump(out, indent + (i == size ? " " : " | ")); else dumpObject(out, b._bean); break; + + case MANAGED: + out.append(indent).append(" += "); + if (b._bean instanceof Dumpable) + ((Dumpable)b._bean).dump(out, indent + (i == size ? " " : " | ")); + else + dumpObject(out, b._bean); + break; case UNMANAGED: out.append(indent).append(" +~ "); @@ -441,7 +568,7 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable break; case AUTO: - out.append(indent).append(" += "); + out.append(indent).append(" +? "); if (b._bean instanceof Dumpable) ((Dumpable)b._bean).dump(out, indent + (i == size ? " " : " | ")); else @@ -451,8 +578,22 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable } } - if (i != size) + if (i 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); + } + } } public static void dump(Appendable out, String indent, Collection... collections) throws IOException @@ -482,22 +623,71 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable } - enum Managed { MANAGED, UNMANAGED, AUTO }; + enum Managed { POJO, MANAGED, UNMANAGED, AUTO }; - private class Bean + private static class Bean { private final Object _bean; - private volatile Managed _managed = Managed.AUTO; + private volatile Managed _managed = Managed.POJO; private Bean(Object b) { _bean = b; } + public boolean isManaged() + { + return _managed==Managed.MANAGED; + } + @Override public String toString() { - return String.format("{%s,%b}", _bean, _managed); + return String.format("{%s,%s}", _bean, _managed); + } + } + + public void updateBean(Object oldBean, final Object newBean) + { + if (newBean!=oldBean) + { + if (oldBean!=null) + removeBean(oldBean); + if (newBean!=null) + addBean(newBean); + } + } + + public void updateBeans(Object[] oldBeans, final Object[] newBeans) + { + // remove oldChildren not in newChildren + if (oldBeans!=null) + { + loop: for (Object o:oldBeans) + { + if (newBeans!=null) + { + for (Object n:newBeans) + if (o==n) + continue loop; + } + removeBean(o); + } + } + + // add new beans not in old + if (newBeans!=null) + { + loop: for (Object n:newBeans) + { + if (oldBeans!=null) + { + for (Object o:oldBeans) + if (o==n) + continue loop; + } + addBean(n); + } } } } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java index 7fc7f0ccae1..f4636a53e3d 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java @@ -38,7 +38,7 @@ import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.ManagedOperation; import org.eclipse.jetty.util.annotation.Name; import org.eclipse.jetty.util.component.AbstractLifeCycle; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.log.Log; @@ -442,7 +442,7 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo @ManagedOperation("dump thread state") public String dump() { - return AggregateLifeCycle.dump(this); + return ContainerLifeCycle.dump(this); } @Override @@ -472,7 +472,7 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo { out.append(String.valueOf(thread.getId())).append(' ').append(thread.getName()).append(' ').append(thread.getState().toString()).append(idle?" IDLE":"").append('\n'); if (!idle) - AggregateLifeCycle.dump(out,indent,Arrays.asList(trace)); + ContainerLifeCycle.dump(out,indent,Arrays.asList(trace)); } @Override @@ -488,8 +488,8 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo } } - AggregateLifeCycle.dumpObject(out,this); - AggregateLifeCycle.dump(out,indent,dump); + ContainerLifeCycle.dumpObject(out,this); + ContainerLifeCycle.dump(out,indent,dump); } @Override diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/component/AggregateLifeCycleTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/component/AggregateLifeCycleTest.java deleted file mode 100644 index 750fe4f73b8..00000000000 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/component/AggregateLifeCycleTest.java +++ /dev/null @@ -1,403 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.component; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; -import java.util.concurrent.atomic.AtomicInteger; - -import org.eclipse.jetty.util.TypeUtil; -import org.junit.Assert; -import org.junit.Test; - - -public class AggregateLifeCycleTest -{ - - @Test - public void testStartStopDestroy() throws Exception - { - final AtomicInteger destroyed=new AtomicInteger(); - final AtomicInteger started=new AtomicInteger(); - final AtomicInteger stopped=new AtomicInteger(); - - AggregateLifeCycle a0=new AggregateLifeCycle(); - - AggregateLifeCycle a1=new AggregateLifeCycle() - { - @Override - protected void doStart() throws Exception - { - started.incrementAndGet(); - super.doStart(); - } - - @Override - protected void doStop() throws Exception - { - stopped.incrementAndGet(); - super.doStop(); - } - - @Override - public void destroy() - { - destroyed.incrementAndGet(); - super.destroy(); - } - - }; - - - a0.addBean(a1); - - a0.start(); - Assert.assertEquals(1, started.get()); - Assert.assertEquals(0,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.start(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(0,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.stop(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(1,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.start(); - Assert.assertEquals(2,started.get()); - Assert.assertEquals(1,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.stop(); - Assert.assertEquals(2,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.destroy(); - - Assert.assertEquals(2,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(1,destroyed.get()); - - a0.start(); - Assert.assertEquals(2,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(1,destroyed.get()); - - a0.addBean(a1); - a0.start(); - Assert.assertEquals(3,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(1,destroyed.get()); - - a0.removeBean(a1); - a0.stop(); - a0.destroy(); - Assert.assertEquals(3,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(1,destroyed.get()); - - a1.stop(); - Assert.assertEquals(3,started.get()); - Assert.assertEquals(3,stopped.get()); - Assert.assertEquals(1,destroyed.get()); - - a1.destroy(); - Assert.assertEquals(3,started.get()); - Assert.assertEquals(3,stopped.get()); - Assert.assertEquals(2,destroyed.get()); - - } - - @Test - public void testDisJoint() throws Exception - { - final AtomicInteger destroyed=new AtomicInteger(); - final AtomicInteger started=new AtomicInteger(); - final AtomicInteger stopped=new AtomicInteger(); - - AggregateLifeCycle a0=new AggregateLifeCycle(); - - AggregateLifeCycle a1=new AggregateLifeCycle() - { - @Override - protected void doStart() throws Exception - { - started.incrementAndGet(); - super.doStart(); - } - - @Override - protected void doStop() throws Exception - { - stopped.incrementAndGet(); - super.doStop(); - } - - @Override - public void destroy() - { - destroyed.incrementAndGet(); - super.destroy(); - } - - }; - - // Start the a1 bean before adding, makes it auto disjoint - a1.start(); - - // Now add it - a0.addBean(a1); - Assert.assertFalse(a0.isManaged(a1)); - - a0.start(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(0,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.start(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(0,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.stop(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(0,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a1.stop(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(1,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.start(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(1,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.manage(a1); - Assert.assertTrue(a0.isManaged(a1)); - - a0.stop(); - Assert.assertEquals(1,started.get()); - Assert.assertEquals(1,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - - a0.start(); - Assert.assertEquals(2,started.get()); - Assert.assertEquals(1,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a0.stop(); - Assert.assertEquals(2,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - - a0.unmanage(a1); - Assert.assertFalse(a0.isManaged(a1)); - - a0.destroy(); - Assert.assertEquals(2,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(0,destroyed.get()); - - a1.destroy(); - Assert.assertEquals(2,started.get()); - Assert.assertEquals(2,stopped.get()); - Assert.assertEquals(1,destroyed.get()); - - } - - @Test - public void testDumpable() throws Exception - { - AggregateLifeCycle a0 = new AggregateLifeCycle(); - String dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - - AggregateLifeCycle aa0 = new AggregateLifeCycle(); - a0.addBean(aa0); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," += org.eclipse.jetty.util.component.AggregateLife"); - - AggregateLifeCycle aa1 = new AggregateLifeCycle(); - a0.addBean(aa1); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," += org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," += org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump,""); - - AggregateLifeCycle aa2 = new AggregateLifeCycle(); - a0.addBean(aa2,false); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," += org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," += org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +~ org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump,""); - - aa1.start(); - a0.start(); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +~ org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +~ org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump,""); - - a0.manage(aa1); - a0.removeBean(aa2); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump,""); - - AggregateLifeCycle aaa0 = new AggregateLifeCycle(); - aa0.addBean(aaa0); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump,""); - - AggregateLifeCycle aa10 = new AggregateLifeCycle(); - aa1.addBean(aa10); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump,""); - - final AggregateLifeCycle a1 = new AggregateLifeCycle(); - final AggregateLifeCycle a2 = new AggregateLifeCycle(); - final AggregateLifeCycle a3 = new AggregateLifeCycle(); - final AggregateLifeCycle a4 = new AggregateLifeCycle(); - - - AggregateLifeCycle aa = new AggregateLifeCycle() - { - @Override - public void dump(Appendable out, String indent) throws IOException - { - out.append(this.toString()).append("\n"); - dump(out,indent,TypeUtil.asList(new Object[]{a1,a2}),TypeUtil.asList(new Object[]{a3,a4})); - } - }; - a0.addBean(aa); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump,""); - - a2.addBean(aa0,true); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggre"); - dump=check(dump," | +- org.eclipse.jetty.util.component.A"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump,""); - - a2.unmanage(aa0); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," | +~ org.eclipse.jetty.util.component.Aggre"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump,""); - - a0.unmanage(aa); - dump=trim(a0.dump()); - dump=check(dump,"org.eclipse.jetty.util.component.AggregateLifeCycl"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +- org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump," | +- org.eclipse.jetty.util.component.Aggregate"); - dump=check(dump," +~ org.eclipse.jetty.util.component.AggregateLife"); - dump=check(dump,""); - - } - - String trim(String s) throws IOException - { - StringBuilder b=new StringBuilder(); - BufferedReader reader=new BufferedReader(new StringReader(s)); - - for (String line=reader.readLine();line!=null;line=reader.readLine()) - { - if (line.length()>50) - line=line.substring(0,50); - b.append(line).append('\n'); - } - - return b.toString(); - } - - String check(String s,String x) - { - String r=s; - int nl = s.indexOf('\n'); - if (nl>0) - { - r=s.substring(nl+1); - s=s.substring(0,nl); - } - - Assert.assertEquals(x,s); - - return r; - } - - - -} diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java new file mode 100644 index 00000000000..21b17965faf --- /dev/null +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/component/ContainerLifeCycleTest.java @@ -0,0 +1,538 @@ +// +// ======================================================================== +// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.util.component; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.eclipse.jetty.util.TypeUtil; +import org.junit.Assert; +import org.junit.Test; + + +public class ContainerLifeCycleTest +{ + + @Test + public void testStartStopDestroy() throws Exception + { + final AtomicInteger destroyed=new AtomicInteger(); + final AtomicInteger started=new AtomicInteger(); + final AtomicInteger stopped=new AtomicInteger(); + + ContainerLifeCycle a0=new ContainerLifeCycle(); + + ContainerLifeCycle a1=new ContainerLifeCycle() + { + @Override + protected void doStart() throws Exception + { + started.incrementAndGet(); + super.doStart(); + } + + @Override + protected void doStop() throws Exception + { + stopped.incrementAndGet(); + super.doStop(); + } + + @Override + public void destroy() + { + destroyed.incrementAndGet(); + super.destroy(); + } + + }; + + + a0.addBean(a1); + + a0.start(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(0,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.start(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(0,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.stop(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(1,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.start(); + Assert.assertEquals(2,started.get()); + Assert.assertEquals(1,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.stop(); + Assert.assertEquals(2,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.destroy(); + + Assert.assertEquals(2,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(1,destroyed.get()); + + a0.start(); + Assert.assertEquals(2,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(1,destroyed.get()); + + a0.addBean(a1); + a0.start(); + Assert.assertEquals(3,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(1,destroyed.get()); + + a0.removeBean(a1); + a0.stop(); + a0.destroy(); + Assert.assertEquals(3,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(1,destroyed.get()); + + a1.stop(); + Assert.assertEquals(3,started.get()); + Assert.assertEquals(3,stopped.get()); + Assert.assertEquals(1,destroyed.get()); + + a1.destroy(); + Assert.assertEquals(3,started.get()); + Assert.assertEquals(3,stopped.get()); + Assert.assertEquals(2,destroyed.get()); + + } + + @Test + public void testDisJoint() throws Exception + { + final AtomicInteger destroyed=new AtomicInteger(); + final AtomicInteger started=new AtomicInteger(); + final AtomicInteger stopped=new AtomicInteger(); + + ContainerLifeCycle a0=new ContainerLifeCycle(); + + ContainerLifeCycle a1=new ContainerLifeCycle() + { + @Override + protected void doStart() throws Exception + { + started.incrementAndGet(); + super.doStart(); + } + + @Override + protected void doStop() throws Exception + { + stopped.incrementAndGet(); + super.doStop(); + } + + @Override + public void destroy() + { + destroyed.incrementAndGet(); + super.destroy(); + } + + }; + + // Start the a1 bean before adding, makes it auto disjoint + a1.start(); + + // Now add it + a0.addBean(a1); + Assert.assertFalse(a0.isManaged(a1)); + + a0.start(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(0,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.start(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(0,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.stop(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(0,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a1.stop(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(1,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.start(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(1,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.manage(a1); + Assert.assertTrue(a0.isManaged(a1)); + + a0.stop(); + Assert.assertEquals(1,started.get()); + Assert.assertEquals(1,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + + a0.start(); + Assert.assertEquals(2,started.get()); + Assert.assertEquals(1,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a0.stop(); + Assert.assertEquals(2,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + + a0.unmanage(a1); + Assert.assertFalse(a0.isManaged(a1)); + + a0.destroy(); + Assert.assertEquals(2,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(0,destroyed.get()); + + a1.destroy(); + Assert.assertEquals(2,started.get()); + Assert.assertEquals(2,stopped.get()); + Assert.assertEquals(1,destroyed.get()); + + } + + @Test + public void testDumpable() throws Exception + { + ContainerLifeCycle a0 = new ContainerLifeCycle(); + String dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + + ContainerLifeCycle aa0 = new ContainerLifeCycle(); + a0.addBean(aa0); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," +? org.eclipse.jetty.util.component.ContainerLife"); + + ContainerLifeCycle aa1 = new ContainerLifeCycle(); + a0.addBean(aa1); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," +? org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +? org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump,""); + + ContainerLifeCycle aa2 = new ContainerLifeCycle(); + a0.addBean(aa2,false); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," +? org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +? org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +~ org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump,""); + + aa1.start(); + a0.start(); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +~ org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +~ org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump,""); + + a0.manage(aa1); + a0.removeBean(aa2); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump,""); + + ContainerLifeCycle aaa0 = new ContainerLifeCycle(); + aa0.addBean(aaa0); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump,""); + + ContainerLifeCycle aa10 = new ContainerLifeCycle(); + aa1.addBean(aa10); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," += org.eclipse.jetty.util.component.Container"); + dump=check(dump,""); + + final ContainerLifeCycle a1 = new ContainerLifeCycle(); + final ContainerLifeCycle a2 = new ContainerLifeCycle(); + final ContainerLifeCycle a3 = new ContainerLifeCycle(); + final ContainerLifeCycle a4 = new ContainerLifeCycle(); + + + ContainerLifeCycle aa = new ContainerLifeCycle() + { + @Override + public void dump(Appendable out, String indent) throws IOException + { + out.append(this.toString()).append("\n"); + dump(out,indent,TypeUtil.asList(new Object[]{a1,a2}),TypeUtil.asList(new Object[]{a3,a4})); + } + }; + a0.addBean(aa); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump,""); + + a2.addBean(aa0,true); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," | += org.eclipse.jetty.util.component.Conta"); + dump=check(dump," | += org.eclipse.jetty.util.component.C"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump,""); + + a2.unmanage(aa0); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," | +~ org.eclipse.jetty.util.component.Conta"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump," +- org.eclipse.jetty.util.component.Container"); + dump=check(dump,""); + + a0.unmanage(aa); + dump=trim(a0.dump()); + dump=check(dump,"org.eclipse.jetty.util.component.ContainerLifeCycl"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," += org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump," | += org.eclipse.jetty.util.component.Container"); + dump=check(dump," +~ org.eclipse.jetty.util.component.ContainerLife"); + dump=check(dump,""); + + } + + @Test + public void listenerTest() throws Exception + { + final Queue handled = new ConcurrentLinkedQueue<>(); + final Queue operation = new ConcurrentLinkedQueue<>(); + final Queue parent = new ConcurrentLinkedQueue<>(); + final Queue child = new ConcurrentLinkedQueue<>(); + + Container.Listener listener= new Container.Listener() + { + @Override + public void beanRemoved(Container p, Object c) + { + handled.add(toString()); + operation.add("removed"); + parent.add(p); + child.add(c); + } + + @Override + public void beanAdded(Container p, Object c) + { + handled.add(toString()); + operation.add("added"); + parent.add(p); + child.add(c); + } + + public @Override String toString() {return "listener";} + }; + + + ContainerLifeCycle c0 = new ContainerLifeCycle() { public @Override String toString() {return "c0";}}; + ContainerLifeCycle c00 = new ContainerLifeCycle() { public @Override String toString() {return "c00";}}; + c0.addBean(c00); + String b000="b000"; + c00.addBean(b000); + + c0.addBean(listener); + + Assert.assertEquals("listener",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(c00,child.poll()); + + Assert.assertEquals("listener",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(listener,child.poll()); + + + Container.InheritedListener inherited= new Container.InheritedListener() + { + @Override + public void beanRemoved(Container p, Object c) + { + handled.add(toString()); + operation.add("removed"); + parent.add(p); + child.add(c); + } + + @Override + public void beanAdded(Container p, Object c) + { + handled.add(toString()); + operation.add("added"); + parent.add(p); + child.add(c); + } + + public @Override String toString() {return "inherited";} + }; + + c0.addBean(inherited); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(c00,child.poll()); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(listener,child.poll()); + + Assert.assertEquals("listener",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(inherited,child.poll()); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(inherited,child.poll()); + + c0.start(); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c00,parent.poll()); + Assert.assertEquals(b000,child.poll()); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("added",operation.poll()); + Assert.assertEquals(c00,parent.poll()); + Assert.assertEquals(inherited,child.poll()); + + c0.removeBean(c00); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("removed",operation.poll()); + Assert.assertEquals(c00,parent.poll()); + Assert.assertEquals(inherited,child.poll()); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("removed",operation.poll()); + Assert.assertEquals(c00,parent.poll()); + Assert.assertEquals(b000,child.poll()); + + Assert.assertEquals("listener",handled.poll()); + Assert.assertEquals("removed",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(c00,child.poll()); + + Assert.assertEquals("inherited",handled.poll()); + Assert.assertEquals("removed",operation.poll()); + Assert.assertEquals(c0,parent.poll()); + Assert.assertEquals(c00,child.poll()); + + } + + String trim(String s) throws IOException + { + StringBuilder b=new StringBuilder(); + BufferedReader reader=new BufferedReader(new StringReader(s)); + + for (String line=reader.readLine();line!=null;line=reader.readLine()) + { + if (line.length()>50) + line=line.substring(0,50); + b.append(line).append('\n'); + } + + return b.toString(); + } + + String check(String s,String x) + { + String r=s; + int nl = s.indexOf('\n'); + if (nl>0) + { + r=s.substring(nl+1); + s=s.substring(0,nl); + } + + Assert.assertEquals(x,s); + + return r; + } + + + +} diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java index 81b65ed353d..7ac08011134 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java @@ -27,7 +27,7 @@ import java.util.concurrent.Executor; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.ssl.SslContextFactory; @@ -45,7 +45,7 @@ import org.eclipse.jetty.websocket.core.extensions.WebSocketExtensionRegistry; import org.eclipse.jetty.websocket.core.io.WebSocketSession; import org.eclipse.jetty.websocket.core.protocol.ExtensionConfig; -public class WebSocketClientFactory extends AggregateLifeCycle +public class WebSocketClientFactory extends ContainerLifeCycle { private static final Logger LOG = Log.getLogger(WebSocketClientFactory.class); diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/internal/ConnectionManager.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/internal/ConnectionManager.java index d4708e4fbb8..27ecb98c744 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/internal/ConnectionManager.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/internal/ConnectionManager.java @@ -31,7 +31,7 @@ import java.util.concurrent.Executor; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.util.FutureCallback; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.ssl.SslContextFactory; @@ -45,7 +45,7 @@ import org.eclipse.jetty.websocket.core.api.WebSocketPolicy; * Internal Connection/Client Manager used to track active clients, their physical vs virtual connection information, and provide some means to create new * physical or virtual connections. */ -public class ConnectionManager extends AggregateLifeCycle +public class ConnectionManager extends ContainerLifeCycle { private static final Logger LOG = Log.getLogger(ConnectionManager.class); diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java index 655bc30e7f9..f3045a5fc68 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java @@ -36,7 +36,7 @@ import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.server.HttpConnection; -import org.eclipse.jetty.util.component.AggregateLifeCycle; +import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.thread.Scheduler; @@ -61,7 +61,7 @@ import org.eclipse.jetty.websocket.server.handshake.HandshakeRFC6455; /** * Factory to create WebSocket connections */ -public class WebSocketServerFactory extends AggregateLifeCycle implements WebSocketCreator +public class WebSocketServerFactory extends ContainerLifeCycle implements WebSocketCreator { private static final Logger LOG = Log.getLogger(WebSocketServerFactory.class); diff --git a/test-jetty-webapp/src/test/java/org/eclipse/jetty/TestServer.java b/test-jetty-webapp/src/test/java/org/eclipse/jetty/TestServer.java index e5cf7306bec..313aba80e56 100644 --- a/test-jetty-webapp/src/test/java/org/eclipse/jetty/TestServer.java +++ b/test-jetty-webapp/src/test/java/org/eclipse/jetty/TestServer.java @@ -72,9 +72,8 @@ public class TestServer // Setup JMX MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); - server.getContainer().addEventListener(mbContainer); server.addBean(mbContainer); - mbContainer.addBean(Log.getLog()); + server.addBean(Log.getLog()); // Setup Connectors ServerConnector connector0 = new ServerConnector(server);