jetty-9 combined the Container and AggregateLifeCycle into ContainerLifeCycle

This commit is contained in:
Greg Wilkins 2012-09-21 11:45:51 +10:00
parent 15804e76e2
commit 68ee346b8a
55 changed files with 1237 additions and 1580 deletions

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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;
* });
* </pre>
*/
public class HttpClient extends AggregateLifeCycle
public class HttpClient extends ContainerLifeCycle
{
private static final Logger LOG = Log.getLogger(HttpClient.class);

View File

@ -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<String> 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

View File

@ -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;
* <img src="doc-files/DeploymentManager.png">
*/
@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<AppProvider> providers)
@ -175,11 +175,7 @@ public class DeploymentManager extends AggregateLifeCycle
{
if (isRunning())
throw new IllegalStateException();
List<AppProvider> old = new ArrayList<AppProvider>(_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();

View File

@ -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);

View File

@ -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)

View File

@ -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

View File

@ -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);
}
}

View File

@ -1,13 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!-- ============================================================================ -->
<!-- To correctly start Jetty with JMX module enabled, this configuration -->
<!-- file must appear first in the list of the configuration files. -->
<!-- The simplest way to achieve this is to add etc/jetty-jmx.xml as the -->
<!-- first file in configuration file list at the end of start.ini file. -->
<!-- ============================================================================ -->
<Configure id="Container" class="org.eclipse.jetty.util.component.Container">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Set the java.rmi.server.hostname property in case you've -->
@ -27,26 +21,24 @@
name="getPlatformMBeanServer" />
<!-- =========================================================== -->
<!-- Initialize the Jetty MBean container -->
<!-- Initialize the Jetty MBean container -->
<!-- =========================================================== -->
<New id="MBeanContainer" class="org.eclipse.jetty.jmx.MBeanContainer">
<Arg><Ref id="MBeanServer" /></Arg>
<Call name="start"/>
</New>
<!-- Add to the Server to listen for object events -->
<Call name="addEventListener">
<Arg><Ref id="MBeanContainer" /></Arg>
<Call name="addBean">
<Arg>
<New id="MBeanContainer" class="org.eclipse.jetty.jmx.MBeanContainer">
<Arg>
<Ref id="MBeanServer" />
</Arg>
</New>
</Arg>
</Call>
<!-- Add the static log -->
<Ref id="MBeanContainer">
<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.util.log.Log"/>
</Arg>
</Call>
</Ref>
<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.util.log.Log" />
</Arg>
</Call>
<!-- In order to connect to the JMX server remotely from a different
process, possibly running on a different host, Jetty JMX module

View File

@ -19,38 +19,33 @@
package org.eclipse.jetty.jmx;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.AggregateLifeCycle;
import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.component.Container.Relationship;
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;
import org.eclipse.jetty.util.thread.ShutdownThread;
/**
* Container class for the MBean instances
*/
public class MBeanContainer extends AbstractLifeCycle implements Container.Listener, Dumpable
public class MBeanContainer implements Container.InheritedListener, Dumpable
{
private final static Logger LOG = Log.getLogger(MBeanContainer.class.getName());
private final MBeanServer _server;
private final MBeanServer _mbeanServer;
private final WeakHashMap<Object, ObjectName> _beans = new WeakHashMap<Object, ObjectName>();
private final HashMap<String, Integer> _unique = new HashMap<String, Integer>();
private final WeakHashMap<ObjectName,List<Container.Relationship>> _relations = new WeakHashMap<ObjectName,List<Container.Relationship>>();
private String _domain = null;
/**
@ -61,11 +56,7 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
*/
public synchronized ObjectName findMBean(Object object)
{
ObjectName bean = _beans.get(object);
LOG.debug("findMBean {} {}", object, bean );
return bean == null ? null : bean;
}
@ -77,8 +68,6 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
*/
public synchronized Object findBean(ObjectName oname)
{
LOG.debug("findBean {}", oname );
for (Map.Entry<Object, ObjectName> entry : _beans.entrySet())
{
ObjectName bean = entry.getValue();
@ -95,7 +84,7 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
*/
public MBeanContainer(MBeanServer server)
{
_server = server;
_mbeanServer = server;
}
/**
@ -105,7 +94,7 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
*/
public MBeanServer getMBeanServer()
{
return _server;
return _mbeanServer;
}
/**
@ -128,125 +117,37 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
return _domain;
}
/**
* Implementation of Container.Listener interface
*
* @see org.eclipse.jetty.util.component.Container.Listener#add(org.eclipse.jetty.util.component.Container.Relationship)
*/
public synchronized void add(Relationship relationship)
@Override
public void beanAdded(Container parent, Object obj)
{
LOG.debug("add {}",relationship);
ObjectName parent = _beans.get(relationship.getParent());
if (parent == null)
LOG.debug("beanAdded {}->{}",parent,obj);
// Is their an object name for the parent
ObjectName pname=null;
if (parent!=null)
{
addBean(relationship.getParent());
parent = _beans.get(relationship.getParent());
}
ObjectName child = _beans.get(relationship.getChild());
if (child == null)
{
addBean(relationship.getChild());
child = _beans.get(relationship.getChild());
}
if (parent != null && child != null)
{
List<Container.Relationship> rels = _relations.get(parent);
if (rels==null)
pname=_beans.get(parent);
if (pname==null)
{
rels=new ArrayList<Container.Relationship>();
_relations.put(parent,rels);
}
rels.add(relationship);
}
}
/**
* Implementation of Container.Listener interface
*
* @see org.eclipse.jetty.util.component.Container.Listener#remove(org.eclipse.jetty.util.component.Container.Relationship)
*/
public synchronized void remove(Relationship relationship)
{
LOG.debug("remove {}",relationship);
ObjectName parent = _beans.get(relationship.getParent());
ObjectName child = _beans.get(relationship.getChild());
if (parent != null && child != null)
{
List<Container.Relationship> rels = _relations.get(parent);
if (rels!=null)
{
for (Iterator<Container.Relationship> i=rels.iterator();i.hasNext();)
{
Container.Relationship r = i.next();
if (relationship.equals(r) || r.getChild()==null)
i.remove();
}
// create the parent bean
beanAdded(null,parent);
pname=_beans.get(parent);
}
}
}
/**
* Implementation of Container.Listener interface
*
* @see org.eclipse.jetty.util.component.Container.Listener#removeBean(java.lang.Object)
*/
public synchronized void removeBean(Object obj)
{
LOG.debug("removeBean {}",obj);
ObjectName bean = _beans.remove(obj);
if (bean != null)
{
List<Container.Relationship> beanRelations= _relations.remove(bean);
if (beanRelations != null)
{
LOG.debug("Unregister {}", beanRelations);
List<?> removeList = new ArrayList<Object>(beanRelations);
for (Object r : removeList)
{
Container.Relationship relation = (Relationship)r;
relation.getContainer().update(relation.getParent(), relation.getChild(), null, relation.getRelationship(), true);
}
}
try
{
_server.unregisterMBean(bean);
LOG.debug("Unregistered {}", bean);
}
catch (javax.management.InstanceNotFoundException e)
{
LOG.ignore(e);
}
catch (Exception e)
{
LOG.warn(e);
}
}
}
/**
* Implementation of Container.Listener interface
*
* @see org.eclipse.jetty.util.component.Container.Listener#addBean(java.lang.Object)
*
* TODO improve the id property to include better information
*/
public synchronized void addBean(Object obj)
{
LOG.debug("addBean {}",obj);
// Does an mbean already exist?
if (obj == null || _beans.containsKey(obj))
return;
try
{
if (obj == null || _beans.containsKey(obj))
return;
// Create an MBean for the object
Object mbean = ObjectMBean.mbeanFor(obj);
if (mbean == null)
return;
ObjectName oname = null;
if (mbean instanceof ObjectMBean)
{
@ -256,25 +157,34 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
//no override mbean object name, so make a generic one
if (oname == null)
{
{
//if no explicit domain, create one
String domain = _domain;
if (domain == null)
domain = obj.getClass().getPackage().getName();
String type = obj.getClass().getName().toLowerCase();
int dot = type.lastIndexOf('.');
if (dot >= 0)
type = type.substring(dot + 1);
String context = null;
if (mbean instanceof ObjectMBean)
{
context = makeName(((ObjectMBean)mbean).getObjectContextBasis());
}
String name = null;
if (mbean instanceof ObjectMBean)
{
name = makeName(((ObjectMBean)mbean).getObjectNameBasis());
}
String context = (mbean instanceof ObjectMBean)?makeName(((ObjectMBean)mbean).getObjectContextBasis()):null;
String name = (mbean instanceof ObjectMBean)?makeName(((ObjectMBean)mbean).getObjectNameBasis()):null;
StringBuffer buf = new StringBuffer();
if (pname!=null)
{
buf.append("parent=")
.append(pname.getKeyProperty("type"))
.append("-");
if (pname.getKeyProperty("context")!=null)
buf.append(pname.getKeyProperty("context")).append("-");
buf.append(pname.getKeyProperty("id"))
.append(",");
}
buf.append("type=").append(type);
if (context != null && context.length()>1)
{
@ -292,15 +202,10 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
count = count == null ? 0 : 1 + count;
_unique.put(basis, count);
//if no explicit domain, create one
String domain = _domain;
if (domain == null)
domain = obj.getClass().getPackage().getName();
oname = ObjectName.getInstance(domain + ":" + basis + ",id=" + count);
}
ObjectInstance oinstance = _server.registerMBean(mbean, oname);
ObjectInstance oinstance = _mbeanServer.registerMBean(mbean, oname);
LOG.debug("Registered {}", oinstance.getObjectName());
_beans.put(obj, oinstance.getObjectName());
@ -311,6 +216,30 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
}
}
@Override
public void beanRemoved(Container parent, Object obj)
{
LOG.debug("beanRemoved {}",obj);
ObjectName bean = _beans.remove(obj);
if (bean != null)
{
try
{
_mbeanServer.unregisterMBean(bean);
LOG.debug("Unregistered {}", bean);
}
catch (javax.management.InstanceNotFoundException e)
{
LOG.ignore(e);
}
catch (Exception e)
{
LOG.warn(e);
}
}
}
/**
* @param basis name to strip of special characters.
* @return normalized name
@ -322,38 +251,32 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
return basis.replace(':', '_').replace('*', '_').replace('?', '_').replace('=', '_').replace(',', '_').replace(' ', '_');
}
/**
* Perform actions needed to start lifecycle
*
* @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
*/
public void doStart()
{
ShutdownThread.register(this);
}
/**
* Perform actions needed to stop lifecycle
*
* @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop()
*/
public void doStop()
{
Set<Object> removeSet = new HashSet<Object>(_beans.keySet());
for (Object removeObj : removeSet)
{
removeBean(removeObj);
}
}
@Override
public void dump(Appendable out, String indent) throws IOException
{
AggregateLifeCycle.dumpObject(out,this);
AggregateLifeCycle.dump(out, indent, _beans.entrySet());
ContainerLifeCycle.dumpObject(out,this);
ContainerLifeCycle.dump(out, indent, _beans.entrySet());
}
@Override
public String dump()
{
return AggregateLifeCycle.dump(this);
return ContainerLifeCycle.dump(this);
}
public void destroy()
{
for (ObjectName oname : _beans.values())
if (oname!=null)
{
try
{
_mbeanServer.unregisterMBean(oname);
}
catch (MBeanRegistrationException | InstanceNotFoundException e)
{
LOG.warn(e);
}
}
}
}

View File

@ -34,7 +34,6 @@ import org.eclipse.jetty.util.log.Log;
@ManagedObject("Jetty Logging")
public class LogMBean extends ObjectMBean
{
public LogMBean(Object managedObject)
{
super(managedObject);

View File

@ -42,16 +42,15 @@ public class ObjectMBeanTest
private static MBeanContainer container;
@Before
public void beforeClass() throws Exception
public void before() throws Exception
{
container = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
container.start();
}
@After
public void afterClass() throws Exception
public void after() throws Exception
{
container.stop();
container.destroy();
container = null;
}
@ -61,7 +60,6 @@ public class ObjectMBeanTest
@Test
public void testDerivedAttributes() throws Exception
{
Derived derived = new Derived();
ObjectMBean mbean = (ObjectMBean)ObjectMBean.mbeanFor(derived);
@ -69,8 +67,8 @@ public class ObjectMBeanTest
mbean.setMBeanContainer(container);
managed.setMBeanContainer(container);
container.addBean(derived);
container.addBean(derived.getManagedInstance());
container.beanAdded(null,derived);
container.beanAdded(null,derived.getManagedInstance());
MBeanInfo toss = managed.getMBeanInfo();
@ -110,7 +108,7 @@ public class ObjectMBeanTest
mbean.setMBeanContainer(container);
container.addBean(derived);
container.beanAdded(null,derived);
MBeanInfo info = mbean.getMBeanInfo();
@ -169,10 +167,10 @@ public class ObjectMBeanTest
Assert.assertNotNull(mbean.getMBeanInfo());
container.addBean(derived);
container.addBean(derived.getManagedInstance());
container.addBean(mbean);
container.addBean(managed);
container.beanAdded(null,derived);
container.beanAdded(null,derived.getManagedInstance());
container.beanAdded(null,mbean);
container.beanAdded(null,managed);
//Managed managedInstance = (Managed)mbean.getAttribute("managedInstance");
//Assert.assertNotNull(managedInstance);
@ -200,11 +198,11 @@ public class ObjectMBeanTest
bqtp.getMBeanInfo();
container.addBean(derived);
container.addBean(derived.getManagedInstance());
container.addBean(mbean);
container.addBean(managed);
container.addBean(qtp);
container.beanAdded(null,derived);
container.beanAdded(null,derived.getManagedInstance());
container.beanAdded(null,mbean);
container.beanAdded(null,managed);
container.beanAdded(null,qtp);
Thread.sleep(10000000);

View File

@ -22,7 +22,7 @@ import java.lang.reflect.Method;
import java.sql.Statement;
import javax.sql.DataSource;
import org.eclipse.jetty.util.component.AggregateLifeCycle;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.Destroyable;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -30,7 +30,7 @@ import org.eclipse.jetty.util.log.Logger;
/**
* Close a DataSource.
* Some {@link DataSource}'s need to be close (eg. Atomikos). This bean is a {@link Destroyable} and
* may be added to any {@link AggregateLifeCycle} so that {@link #destroy()}
* may be added to any {@link ContainerLifeCycle} so that {@link #destroy()}
* will be called. The {@link #destroy()} method calls any no-arg method called "close" on the passed DataSource.
*
*/

View File

@ -427,14 +427,12 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr
@Override
public void dump(Appendable out,String indent) throws IOException
{
dumpThis(out);
dump(out,indent,
// TODO these should all be beans
dumpBeans(out,indent,
Collections.singleton(getLoginService()),
Collections.singleton(getIdentityService()),
Collections.singleton(getAuthenticator()),
Collections.singleton(_roles),
_constraintMap.entrySet(),
getBeans(),
TypeUtil.asList(getHandlers()));
_constraintMap.entrySet());
}
}

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.security;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
@ -255,7 +256,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
/* ------------------------------------------------------------ */
protected LoginService findLoginService()
{
List<LoginService> list = getServer().getBeans(LoginService.class);
Collection<LoginService> list = getServer().getBeans(LoginService.class);
String realm=getRealmName();
if (realm!=null)
@ -265,7 +266,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti
return service;
}
else if (list.size()==1)
return list.get(0);
return list.iterator().next();
return null;
}

View File

@ -27,8 +27,6 @@
<Set name="detailedDump">false</Set>
</New>
</Arg>
<Arg name="container"><Ref id="Container"/></Arg>
<!-- =========================================================== -->
<!-- HttpChannel Configuration -->

View File

@ -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);
}

View File

@ -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.
* </li>
* </ul>
* 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.
*
* <h2>Connection Factories</h2>
@ -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

View File

@ -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));
}
}
}
}

View File

@ -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}));
}
}

View File

@ -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<Customizer> _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;

View File

@ -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<Connector> _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
{

View File

@ -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');

View File

@ -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()));
}
}

View File

@ -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));
}
}
}
}
}

View File

@ -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 && i<handlers.length;i++)
{
if (handlers[i].getServer()!=server)
handlers[i].setServer(server);
}
if (getServer()!=null)
getServer().getContainer().update(this, old_handlers, handlers, "handler");
// stop old handlers
for (int i=0;old_handlers!=null && i<old_handlers.length;i++)
{
if (old_handlers[i]!=null)
{
try
{
if (old_handlers[i].isStarted())
old_handlers[i].stop();
}
catch (Throwable e)
{
mex.add(e);
}
}
}
mex.ifExceptionThrowRuntime();
}
/* ------------------------------------------------------------ */
/** Get the parrallelStart.
* @return true if the contained handlers are started in parallel.
*/
public boolean isParallelStart()
{
return _parallelStart;
}
/* ------------------------------------------------------------ */
/** Set the parallelStart.
* @param parallelStart If true, contained handlers are started in parallel.
*/
public void setParallelStart(boolean parallelStart)
{
this._parallelStart = parallelStart;
}
/* ------------------------------------------------------------ */
/**
* @see Handler#handle(String, Request, HttpServletRequest, HttpServletResponse)
*/
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
@ -184,97 +140,14 @@ public class HandlerCollection extends AbstractHandlerContainer
}
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStart()
*/
@Override
protected void doStart() throws Exception
{
final MultiException mex=new MultiException();
if (_handlers!=null)
{
if (_parallelStart)
{
final CountDownLatch latch = new CountDownLatch(_handlers.length);
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
for (int i=0;i<_handlers.length;i++)
{
final int h=i;
getServer().getThreadPool().dispatch(
new Runnable()
{
public void run()
{
ClassLoader orig = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(loader);
_handlers[h].start();
}
catch(Throwable e)
{
mex.add(e);
}
finally
{
Thread.currentThread().setContextClassLoader(orig);
latch.countDown();
}
}
}
);
}
latch.await();
}
else
{
for (int i=0;i<_handlers.length;i++)
{
try{_handlers[i].start();}
catch(Throwable e){mex.add(e);}
}
}
}
super.doStart();
mex.ifExceptionThrow();
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.handler.AbstractHandler#doStop()
*/
@Override
protected void doStop() throws Exception
{
MultiException mex=new MultiException();
try { super.doStop(); } catch(Throwable e){mex.add(e);}
if (_handlers!=null)
{
for (int i=_handlers.length;i-->0;)
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<h.length;i++)
h[i].setServer(server);
if (server!=null && server!=old_server)
server.getContainer().update(this, null,_handlers, "handler");
}
/* ------------------------------------------------------------ */

View File

@ -81,37 +81,11 @@ public class HandlerWrapper extends AbstractHandlerContainer
if (isStarted())
throw new IllegalStateException(STARTED);
Handler old_handler = _handler;
_handler = handler;
if (handler!=null)
handler.setServer(getServer());
if (getServer()!=null)
getServer().getContainer().update(this, old_handler, handler, "handler");
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.thread.AbstractLifeCycle#doStart()
*/
@Override
protected void doStart() throws Exception
{
if (_handler!=null)
_handler.start();
super.doStart();
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.thread.AbstractLifeCycle#doStop()
*/
@Override
protected void doStop() throws Exception
{
if (_handler!=null)
_handler.stop();
super.doStop();
updateBean(_handler,handler);
_handler=handler;
}
/* ------------------------------------------------------------ */
@ -141,9 +115,6 @@ public class HandlerWrapper extends AbstractHandlerContainer
Handler h=getHandler();
if (h!=null)
h.setServer(server);
if (server!=null && server!=old_server)
server.getContainer().update(this, null,_handler, "handler");
}

View File

@ -77,20 +77,10 @@ public class HotSwapHandler extends AbstractHandlerContainer
throw new IllegalArgumentException("Parameter handler is null.");
try
{
Handler old_handler = _handler;
_handler = handler;
updateBean(_handler,handler);
_handler=handler;
Server server = getServer();
handler.setServer(server);
addBean(handler);
if (server != null)
server.getContainer().update(this,old_handler,handler,"handler");
// if there is an old handler and it was started, stop it
if (old_handler != null)
{
removeBean(old_handler);
}
}
catch (Exception e)
@ -148,9 +138,6 @@ public class HotSwapHandler extends AbstractHandlerContainer
Handler h = getHandler();
if (h != null)
h.setServer(server);
if (server != null && server != old_server)
server.getContainer().update(this,null,_handler,"handler");
}
/* ------------------------------------------------------------ */

View File

@ -72,58 +72,14 @@ public class RequestLogHandler extends HandlerWrapper
{
_requestLog.log(baseRequest, (Response)response);
}
}
}
/* ------------------------------------------------------------ */
public void setRequestLog(RequestLog requestLog)
{
//are we changing the request log impl?
try
{
if (_requestLog != null)
_requestLog.stop();
}
catch (Exception e)
{
LOG.warn (e);
}
if (getServer()!=null)
getServer().getContainer().update(this, _requestLog, requestLog, "logimpl",true);
_requestLog = requestLog;
//if we're already started, then start our request log
try
{
if (isStarted() && (_requestLog != null))
_requestLog.start();
}
catch (Exception e)
{
throw new RuntimeException (e);
}
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.handler.HandlerWrapper#setServer(org.eclipse.jetty.server.server.Server)
*/
@Override
public void setServer(Server server)
{
if (_requestLog!=null)
{
if (getServer()!=null && getServer()!=server)
getServer().getContainer().update(this, _requestLog, null, "logimpl",true);
super.setServer(server);
if (server!=null && server!=getServer())
server.getContainer().update(this, null,_requestLog, "logimpl",true);
}
else
super.setServer(server);
updateBean(_requestLog,requestLog);
_requestLog=requestLog;
}
/* ------------------------------------------------------------ */
@ -132,28 +88,4 @@ public class RequestLogHandler extends HandlerWrapper
return _requestLog;
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart()
*/
@Override
protected void doStart() throws Exception
{
super.doStart();
if (_requestLog!=null)
_requestLog.start();
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop()
*/
@Override
protected void doStop() throws Exception
{
super.doStop();
if (_requestLog!=null)
_requestLog.stop();
}
}

View File

@ -88,34 +88,12 @@ public class SessionHandler extends ScopedHandler
{
if (isStarted())
throw new IllegalStateException();
SessionManager old_session_manager = _sessionManager;
if (getServer()!=null)
getServer().getContainer().update(this, old_session_manager, sessionManager, "sessionManager",true);
updateBean(_sessionManager,sessionManager);
_sessionManager=sessionManager;
if (sessionManager!=null)
sessionManager.setSessionHandler(this);
_sessionManager = sessionManager;
if (old_session_manager!=null)
old_session_manager.setSessionHandler(null);
}
/* ------------------------------------------------------------ */
@Override
public void setServer(Server server)
{
Server old_server=getServer();
if (old_server!=null && old_server!=server)
old_server.getContainer().update(this, _sessionManager, null, "sessionManager",true);
super.setServer(server);
if (server!=null && server!=old_server)
server.getContainer().update(this, null,_sessionManager, "sessionManager",true);
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.thread.AbstractLifeCycle#doStart()

View File

@ -27,7 +27,7 @@ import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.util.TypeUtil;
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;
@ManagedObject("Filter Mappings")
@ -276,6 +276,6 @@ public class FilterMapping implements Dumpable
/* ------------------------------------------------------------ */
public String dump()
{
return AggregateLifeCycle.dump(this);
return ContainerLifeCycle.dump(this);
}
}

View File

@ -34,7 +34,7 @@ import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
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;
@ -260,6 +260,7 @@ public class Holder<T> extends AbstractLifeCycle implements Dumpable
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
return _name;
@ -277,17 +278,19 @@ public class Holder<T> 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);
}
/* ------------------------------------------------------------ */

View File

@ -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()));
}
}

View File

@ -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);

View File

@ -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()
{
}

View File

@ -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");

View File

@ -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<Session> sessions = new ConcurrentLinkedQueue<>();
final ByteBufferPool bufferPool = new MappedByteBufferPool();

View File

@ -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<Stan
@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,Collections.singletonList(controller),streams.values());
ContainerLifeCycle.dumpObject(out,this);
ContainerLifeCycle.dump(out,indent,Collections.singletonList(controller),streams.values());
}
private class SessionInvoker extends ForkInvoker<Runnable>

View File

@ -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);

View File

@ -142,35 +142,9 @@ public class Main
{
List<String> ini_args=new ArrayList<String>();
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<String> loadStartIni(File ini)
List<String> 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);
}
}

View File

@ -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=<file> 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@

View File

@ -55,9 +55,10 @@ public class MainTest
{
Main main = new Main();
List<String> 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<String> 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<String> args = main.expandCommandLine(new String[] {});
List<String> 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

View File

@ -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
#===========================================================

View File

@ -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

View File

@ -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);

View File

@ -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: <pre>
* 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;
* }
* </pre>
*/
public class Container
public interface Container
{
private static final Logger LOG = Log.getLogger(Container.class);
private final CopyOnWriteArrayList<Container.Listener> _listeners=new CopyOnWriteArrayList<Container.Listener>();
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 <code>child</code>, then a remove event is generated.
* @param child The current child. If this is non null and differs from <code>oldChild</code>, 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 <code>child</code>, then a remove event is generated.
* @param child The current child. If this is non null and differs from <code>oldChild</code>, 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 <code>children</code> 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 <code>oldChildren</code> 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 <code>children</code> 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 <code>oldChildren</code> 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<newChildren.length;i++)
if (newChildren[i]!=null)
{
if (addRemove)
addBean(newChildren[i]);
add(parent,newChildren[i],relationship);
}
}
}
public Collection<Object> getBeans();
/* ------------------------------------------------------------ */
public void addBean(Object obj)
{
if (_listeners!=null)
{
for (int i=0; i<LazyList.size(_listeners); i++)
{
Listener listener=(Listener)LazyList.get(_listeners, i);
listener.addBean(obj);
}
}
}
/**
* @param clazz the class of the beans
* @return the list of beans of the given class (or subclass)
* @see #getBeans()
*/
public <T> Collection<T> getBeans(Class<T> clazz);
/* ------------------------------------------------------------ */
public void removeBean(Object obj)
{
if (_listeners!=null)
{
for (int i=0; i<LazyList.size(_listeners); i++)
((Listener)LazyList.get(_listeners, i)).removeBean(obj);
}
}
/* ------------------------------------------------------------ */
/** Add a parent child relationship
* @param parent
* @param child
* @param relationship
/**
* @param clazz the class of the bean
* @return the first bean of a specific class (or subclass), or null if no such bean exist
*/
private void add(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<LazyList.size(_listeners); i++)
((Listener)LazyList.get(_listeners, i)).add(event);
}
}
/* ------------------------------------------------------------ */
/** remove a parent child relationship
* @param parent
* @param child
* @param relationship
public <T> T getBean(Class<T> 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<LazyList.size(_listeners); i++)
((Listener)LazyList.get(_listeners, i)).remove(event);
}
}
public boolean removeBean(Object o);
/* ------------------------------------------------------------ */
/** A Container event.
* @see Listener
*/
public static class Relationship
{
private final WeakReference<Object> _parent;
private final WeakReference<Object> _child;
private String _relationship;
private Container _container;
private Relationship(Container container, Object parent,Object child, String relationship)
{
_container=container;
_parent=new WeakReference<Object>(parent);
_child=new WeakReference<Object>(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);
}
}

View File

@ -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.
* <p>
* 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<Bean> _beans = new CopyOnWriteArrayList<>();
private final List<Container.Listener> _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<Object> 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 <T> List<T> getBeans(Class<T> clazz)
@Override
public <T> Collection<T> getBeans(Class<T> clazz)
{
ArrayList<T> 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> T getBean(Class<T> 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<Bean> 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<size)
out.append(indent).append(" |\n");
for (Collection<?> c : collections)
{
for (Object o : c)
{
i++;
out.append(indent).append(" +> ");
if (o instanceof Dumpable)
((Dumpable)o).dump(out, indent + (i == size ? " " : " | "));
else
dumpObject(out, o);
}
}
}
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);
}
}
}
}

View File

@ -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

View File

@ -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;
}
}

View File

@ -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<String> handled = new ConcurrentLinkedQueue<>();
final Queue<String> operation = new ConcurrentLinkedQueue<>();
final Queue<Container> parent = new ConcurrentLinkedQueue<>();
final Queue<Object> 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;
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);