diff src/org/eclipse/jetty/util/component/AggregateLifeCycle.java @ 912:1d0c304e12b5

simplify AggregateLifeCycle
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 08 Oct 2016 22:05:14 -0600
parents 9b65e8064f90
children
line wrap: on
line diff
--- a/src/org/eclipse/jetty/util/component/AggregateLifeCycle.java	Sat Oct 08 21:37:13 2016 -0600
+++ b/src/org/eclipse/jetty/util/component/AggregateLifeCycle.java	Sat Oct 08 22:05:14 2016 -0600
@@ -51,12 +51,13 @@
 
 	private class Bean
 	{
-		Bean(Object b) 
+		Bean(LifeCycle b,boolean managed) 
 		{
-			_bean=b;
+			_bean = b;
+			_managed = managed;
 		}
-		final Object _bean;
-		volatile boolean _managed=true;
+		final LifeCycle _bean;
+		final boolean _managed;
 		
 		public String toString()
 		{
@@ -74,15 +75,15 @@
 	{
 		for (Bean b:_beans)
 		{
-			if (b._managed && b._bean instanceof LifeCycle)
+			if (b._managed)
 			{
-				LifeCycle l=(LifeCycle)b._bean;
+				LifeCycle l = b._bean;
 				if (!l.isRunning())
 					l.start();
 			}
 		}
 		// indicate that we are started, so that addBean will start other beans added.
-		_started=true;
+		_started = true;
 		super.doStart();
 	}
 	
@@ -94,15 +95,15 @@
 	@Override
 	protected void doStop() throws Exception
 	{
-		_started=false;
+		_started = false;
 		super.doStop();
 		List<Bean> reverse = new ArrayList<Bean>(_beans);
 		Collections.reverse(reverse);
 		for (Bean b:reverse)
 		{
-			if (b._managed && b._bean instanceof LifeCycle)
+			if (b._managed)
 			{
-				LifeCycle l=(LifeCycle)b._bean;
+				LifeCycle l = b._bean;
 				if (l.isRunning())
 					l.stop();
 			}
@@ -123,7 +124,7 @@
 		{
 			if (b._bean instanceof Destroyable && b._managed)
 			{
-				Destroyable d=(Destroyable)b._bean;
+				Destroyable d = (Destroyable)b._bean;
 				d.destroy();
 			}
 		}
@@ -136,7 +137,7 @@
 	 * @param bean
 	 * @return True if the aggregate contains the bean
 	 */
-	private boolean contains(Object bean)
+	private boolean contains(LifeCycle bean)
 	{
 		for (Bean b:_beans)
 			if (b._bean==bean)
@@ -154,10 +155,10 @@
 	 * @param o the bean object to add
 	 * @return true if the bean was added or false if it has already been added.
 	 */
-	public boolean addBean(Object o)
+	public boolean addBean(LifeCycle o)
 	{
 		// beans are joined unless they are started lifecycles
-		return addBean(o,!((o instanceof LifeCycle)&&((LifeCycle)o).isStarted()));
+		return addBean(o,!o.isStarted());
 	}
 	
 	/* ------------------------------------------------------------ */
@@ -166,30 +167,24 @@
 	 * @param managed True if the LifeCycle is to be joined, otherwise it will be disjoint.
 	 * @return true if bean was added, false if already present.
 	 */
-	public boolean addBean(Object o, boolean managed)
+	public boolean addBean(LifeCycle o, boolean managed)
 	{
 		if (contains(o))
 			return false;
 		
-		Bean b = new Bean(o);
-		b._managed=managed;
+		Bean b = new Bean(o,managed);
 		_beans.add(b);
 		
-		if (o instanceof LifeCycle)
+		// Start the bean if we are started
+		if (managed && _started)
 		{
-			LifeCycle l=(LifeCycle)o;
-
-			// Start the bean if we are started
-			if (managed && _started)
+			try
 			{
-				try
-				{
-					l.start();
-				}
-				catch(Exception e)
-				{
-					throw new RuntimeException (e);
-				}
+				o.start();
+			}
+			catch(Exception e)
+			{
+				throw new RuntimeException(e);
 			}
 		}
 		return true;
@@ -201,35 +196,20 @@
 	 */
 	public Collection<Object> getBeans()
 	{
-		List<Object> beans = new ArrayList<Object>();
-		beans.addAll(_beans);
+//		return new ArrayList<Object>(_beans);
+		ArrayList<Object> beans = new ArrayList<Object>();
+		for (Bean b:_beans) {
+			beans.add(b._bean);
+		}
 		return beans;
 	}
 
 	
 	/* ------------------------------------------------------------ */
-	/** Get dependent beans of a specific class.
-	 * If more than one bean of the type exist, the first is returned.
-	 * @see #addBean(Object)
-	 * @param clazz
-	 * @return bean or null
-	 */
-	public <T> T getBean(Class<T> clazz)
-	{
-		for (Bean b:_beans)
-		{
-			if (clazz.isInstance(b._bean))
-				return (T)b._bean;
-		}
-		
-		return null;
-	}
-	
-	/* ------------------------------------------------------------ */
 	/**
 	 * Remove an associated bean.
 	 */
-	public boolean removeBean (Object o)
+	public boolean removeBean (LifeCycle o)
 	{
 		Iterator<Bean> i = _beans.iterator();
 		while(i.hasNext())
@@ -244,26 +224,11 @@
 		return false;
 	}
 
-	/* ------------------------------------------------------------ */
-	public void dumpStdErr()
-	{
-		try
-		{
-			dump(System.err,"");
-		}
-		catch (IOException e)
-		{
-			LOG.warn("",e);
-		}
-	}
-	
-	/* ------------------------------------------------------------ */
 	public String dump()
 	{
 		return dump(this);
 	}    
 	
-	/* ------------------------------------------------------------ */
 	public static String dump(Dumpable dumpable)
 	{
 		StringBuilder b = new StringBuilder();
@@ -278,19 +243,16 @@
 		return b.toString();
 	}    
 
-	/* ------------------------------------------------------------ */
-	public void dump(Appendable out) throws IOException
+	private void dump(Appendable out) throws IOException
 	{
 		dump(out,"");
 	}
 
-	/* ------------------------------------------------------------ */
-	protected void dumpThis(Appendable out) throws IOException
+	private void dumpThis(Appendable out) throws IOException
 	{
 		out.append(String.valueOf(this)).append(" - ").append(getState()).append("\n");
 	}
 
-	/* ------------------------------------------------------------ */
 	public static void dumpObject(Appendable out,Object o) throws IOException
 	{
 		try
@@ -306,7 +268,6 @@
 		}
 	}
 	
-	/* ------------------------------------------------------------ */
 	public void dump(Appendable out,String indent) throws IOException
 	{
 		dumpThis(out);
@@ -334,7 +295,6 @@
 			out.append(indent).append(" |\n");
 	}
 	
-	/* ------------------------------------------------------------ */
 	public static void dump(Appendable out,String indent,Collection<?>... collections) throws IOException
 	{
 		if (collections.length==0)