From a5df0ab4561095bdfff46b2deeae3c5f5f56109e Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 20 Nov 2012 17:33:34 +0100 Subject: [PATCH] Updated jetty-spring module. --- jetty-spring/README.TXT | 5 - jetty-spring/pom.xml | 19 ++-- .../src/main/config/etc/jetty-spring.xml | 59 +++++------- .../java/org/eclipse/jetty/spring/Main.java | 12 +-- .../java/org/eclipse/jetty/spring/Server.java | 61 ------------ .../spring/SpringConfigurationProcessor.java | 95 ++++++++++--------- .../SpringConfigurationProcessorFactory.java | 7 +- .../spring/SpringXmlConfigurationTest.java | 56 ++++++----- .../jetty/spring/TestConfiguration.java | 36 +++++-- .../org/eclipse/jetty/spring/configure.xml | 26 ++--- .../org/eclipse/jetty/spring/jetty-deploy.xml | 36 ------- .../org/eclipse/jetty/spring/jetty.xml | 24 +++-- .../util/component/ContainerLifeCycle.java | 67 +++++++------ 13 files changed, 203 insertions(+), 300 deletions(-) delete mode 100644 jetty-spring/README.TXT delete mode 100644 jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java delete mode 100644 jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty-deploy.xml diff --git a/jetty-spring/README.TXT b/jetty-spring/README.TXT deleted file mode 100644 index 40f631c8f30..00000000000 --- a/jetty-spring/README.TXT +++ /dev/null @@ -1,5 +0,0 @@ -This jetty module is an example of how spring can be used to -configure and run Jetty. - - - diff --git a/jetty-spring/pom.xml b/jetty-spring/pom.xml index b2d2988cbb3..b4941b55db4 100644 --- a/jetty-spring/pom.xml +++ b/jetty-spring/pom.xml @@ -9,7 +9,7 @@ Example :: Jetty Spring - 2.5.6 + 3.1.3.RELEASE target/dependencies @@ -38,24 +38,25 @@ org.eclipse.jetty - jetty-webapp + jetty-xml ${project.version} org.eclipse.jetty - jetty-plus - ${project.version} - - - org.eclipse.jetty - jetty-deploy + jetty-util ${project.version} org.springframework - spring + spring-beans ${spring-version} + + org.eclipse.jetty + jetty-server + ${project.version} + test + org.eclipse.jetty.toolchain jetty-test-helper diff --git a/jetty-spring/src/main/config/etc/jetty-spring.xml b/jetty-spring/src/main/config/etc/jetty-spring.xml index 2a3995db763..637450d51a7 100644 --- a/jetty-spring/src/main/config/etc/jetty-spring.xml +++ b/jetty-spring/src/main/config/etc/jetty-spring.xml @@ -5,72 +5,57 @@ - - - - - - - - - - - - + + + - - + + - + + - + - - + + - + - + - - + + + + + + + + + - - - - - - - - - - - - - - - + diff --git a/jetty-spring/src/main/java/org/eclipse/jetty/spring/Main.java b/jetty-spring/src/main/java/org/eclipse/jetty/spring/Main.java index f2dd4611b2b..0b9455c2b5a 100644 --- a/jetty-spring/src/main/java/org/eclipse/jetty/spring/Main.java +++ b/jetty-spring/src/main/java/org/eclipse/jetty/spring/Main.java @@ -16,23 +16,19 @@ // ======================================================================== // - package org.eclipse.jetty.spring; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.xml.XmlConfiguration; - -/* ------------------------------------------------------------ */ -/** Run Jetty from Spring configuration. - * @see jetty-spring.xml +/** + * Runs Jetty from a Spring configuration file passed as argument. */ public class Main { public static void main(String[] args) throws Exception { - Resource config = Resource.newResource(args.length == 1?args[0]:"src/main/config/etc/jetty-spring.xml"); - XmlConfiguration.main(new String[]{config.getFile().getAbsolutePath()}); - + Resource config = Resource.newResource(args.length == 1 ? args[0] : "etc/jetty-spring.xml"); + XmlConfiguration.main(config.getFile().getAbsolutePath()); } } diff --git a/jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java b/jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java deleted file mode 100644 index dc783551107..00000000000 --- a/jetty-spring/src/main/java/org/eclipse/jetty/spring/Server.java +++ /dev/null @@ -1,61 +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.spring; - -import java.net.InetSocketAddress; -import java.util.Collection; - -import org.eclipse.jetty.util.thread.ThreadPool; - - -/* ------------------------------------------------------------ */ -/** - * Convenience class for jetty with Spring. - * This class provides a setBeans method as an alternate - * access to the {@link #addBean(Object)} API. - */ -public class Server extends org.eclipse.jetty.server.Server -{ - public Server() - { - super(); - } - - public Server(InetSocketAddress addr) - { - super(addr); - } - - public Server(int port) - { - super(port); - } - - public Server(ThreadPool pool) - { - super(pool); - } - - public void setBeans(Collection beans) - { - for (Object o:beans) - addBean(o); - } -} diff --git a/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java b/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java index 4158c0b680d..110e42050e4 100644 --- a/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java +++ b/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java @@ -22,7 +22,9 @@ package org.eclipse.jetty.spring; import java.net.URL; import java.util.Arrays; import java.util.Map; +import java.util.ServiceLoader; +import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.xml.ConfigurationProcessor; import org.eclipse.jetty.xml.ConfigurationProcessorFactory; @@ -35,61 +37,64 @@ import org.springframework.core.io.UrlResource; /** * Spring ConfigurationProcessor - *

+ *

* A {@link ConfigurationProcessor} that uses a spring XML file to emulate the {@link XmlConfiguration} format. - *

- * {@link XmlConfiguration} expects a primary object that is either passed in to a call to {@link #configure(Object)} - * or that is constructed by a call to {@link #configure()}. This processor looks for a bean definition - * with an id, name or alias of "Main" as uses that as the primary bean. - *

+ *

+ * {@link XmlConfiguration} expects a primary object that is either passed in to a call to {@link #configure(Object)} + * or that is constructed by a call to {@link #configure()}. This processor looks for a bean definition + * with an id, name or alias of "Main" as uses that as the primary bean. + *

* The objects mapped by {@link XmlConfiguration#getIdMap()} are set as singletons before any configuration calls - * and if the spring configuration file contains a definition for the singleton id, the the singleton is updated + * and if the spring configuration file contains a definition for the singleton id, the the singleton is updated * with a call to {@link XmlBeanFactory#configureBean(Object, String)}. - *

- * The property map obtained via {@link XmlConfiguration#getProperties()} is set as a singleton called "properties" - * and values can be accessed by somewhat verbose + *

+ * The property map obtained via {@link XmlConfiguration#getProperties()} is set as a singleton called "properties" + * and values can be accessed by somewhat verbose * usage of {@link org.springframework.beans.factory.config.MethodInvokingFactoryBean}. - *

- * This processor is returned by the {@link SpringConfigurationProcessorFactory} for any XML document whos first + *

+ * This processor is returned by the {@link SpringConfigurationProcessorFactory} for any XML document whos first * element is "beans". The factory is discovered by a {@link ServiceLoader} for {@link ConfigurationProcessorFactory}. */ public class SpringConfigurationProcessor implements ConfigurationProcessor { - static final Logger __log = org.eclipse.jetty.util.log.Log.getLogger(SpringConfigurationProcessor.class.getName()); - - Map _idMap; - Map _propertyMap; - XmlBeanFactory _beanFactory; - String _main; + private static final Logger LOG = Log.getLogger(SpringConfigurationProcessor.class); + + private Map _idMap; + private Map _propertyMap; + private XmlBeanFactory _beanFactory; + private String _main; public void init(URL url, XmlParser.Node config, Map idMap, Map properties) { try { - _idMap=idMap; - _propertyMap=properties; + _idMap = idMap; + _propertyMap = properties; - Resource resource = (url!=null) - ?new UrlResource(url) - :new ByteArrayResource((""+config).getBytes("UTF-8")); - - _beanFactory=new XmlBeanFactory(resource); - + Resource resource = url != null + ? new UrlResource(url) + : new ByteArrayResource(("" + + "" + + "" + + config).getBytes("UTF-8")); + + _beanFactory = new XmlBeanFactory(resource); } - catch(Exception e) + catch (Exception e) { throw new RuntimeException(e); } } - + public Object configure(Object obj) throws Exception { doConfigure(); - return _beanFactory.configureBean(obj,_main); + return _beanFactory.configureBean(obj, _main); } - /** + /** * Return a configured bean. If a bean has the id or alias of "Main", then it is returned, otherwise the first bean in the file is returned. + * * @see org.eclipse.jetty.xml.ConfigurationProcessor#configure() */ public Object configure() throws Exception @@ -100,28 +105,28 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor private void doConfigure() { - _beanFactory.registerSingleton("properties",_propertyMap); - + _beanFactory.registerSingleton("properties", _propertyMap); + // Look for the main bean; for (String bean : _beanFactory.getBeanDefinitionNames()) { - __log.debug("{} - {}",bean,Arrays.asList(_beanFactory.getAliases(bean))); + LOG.debug("{} - {}", bean, Arrays.asList(_beanFactory.getAliases(bean))); String[] aliases = _beanFactory.getAliases(bean); - if ("Main".equals(bean) || aliases!=null && Arrays.asList(aliases).contains("Main")) + if ("Main".equals(bean) || aliases != null && Arrays.asList(aliases).contains("Main")) { - _main=bean; + _main = bean; break; } } - if (_main==null) - _main=_beanFactory.getBeanDefinitionNames()[0]; - + if (_main == null) + _main = _beanFactory.getBeanDefinitionNames()[0]; + // Register id beans as singletons - __log.debug("idMap {}",_idMap); + LOG.debug("idMap {}", _idMap); for (String id : _idMap.keySet()) { - __log.debug("register {}",id); - _beanFactory.registerSingleton(id,_idMap.get(id)); + LOG.debug("register {}", id); + _beanFactory.registerSingleton(id, _idMap.get(id)); } // Apply configuration to existing singletons @@ -129,13 +134,13 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor { if (_beanFactory.containsBeanDefinition(id)) { - __log.debug("reconfigure {}",id); - _beanFactory.configureBean(_idMap.get(id),id); + LOG.debug("reconfigure {}", id); + _beanFactory.configureBean(_idMap.get(id), id); } } - + // Extract id's for next time. for (String id : _beanFactory.getSingletonNames()) - _idMap.put(id,_beanFactory.getBean(id)); + _idMap.put(id, _beanFactory.getBean(id)); } } diff --git a/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessorFactory.java b/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessorFactory.java index 44a35e957e4..94169a0a575 100644 --- a/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessorFactory.java +++ b/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessorFactory.java @@ -16,7 +16,6 @@ // ======================================================================== // - package org.eclipse.jetty.spring; import org.eclipse.jetty.xml.ConfigurationProcessor; @@ -25,12 +24,12 @@ import org.eclipse.jetty.xml.XmlConfiguration; /** * Spring ConfigurationProcessor Factory - *

+ *

* Create a {@link SpringConfigurationProcessor} for XML documents with a "beans" element. - * The factory is discovered by a {@link ServiceLoader} for {@link ConfigurationProcessorFactory}. + * The factory is discovered by a {@link java.util.ServiceLoader} for {@link ConfigurationProcessorFactory}. + * * @see SpringConfigurationProcessor * @see XmlConfiguration - * */ public class SpringConfigurationProcessorFactory implements ConfigurationProcessorFactory { diff --git a/jetty-spring/src/test/java/org/eclipse/jetty/spring/SpringXmlConfigurationTest.java b/jetty-spring/src/test/java/org/eclipse/jetty/spring/SpringXmlConfigurationTest.java index 47ae2d4d833..2251360ef64 100644 --- a/jetty-spring/src/test/java/org/eclipse/jetty/spring/SpringXmlConfigurationTest.java +++ b/jetty-spring/src/test/java/org/eclipse/jetty/spring/SpringXmlConfigurationTest.java @@ -24,13 +24,13 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.xml.XmlConfiguration; +import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.assertEquals; - public class SpringXmlConfigurationTest { protected String _configure="org/eclipse/jetty/spring/configure.xml"; @@ -60,7 +60,7 @@ public class SpringXmlConfigurationTest URL url = SpringXmlConfigurationTest.class.getClassLoader().getResource(_configure); XmlConfiguration configuration = new XmlConfiguration(url); - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put("test", "xxx"); TestConfiguration nested = new TestConfiguration(); @@ -74,20 +74,20 @@ public class SpringXmlConfigurationTest tc=(TestConfiguration)configuration.configure(tc); - assertEquals("preconfig",tc.getTestString0()); - assertEquals(42,tc.getTestInt0()); - assertEquals("SetValue",tc.getTestString1()); - assertEquals(1,tc.getTestInt1()); + Assert.assertEquals("preconfig", tc.getTestString0()); + Assert.assertEquals(42, tc.getTestInt0()); + Assert.assertEquals("SetValue", tc.getTestString1()); + Assert.assertEquals(1, tc.getTestInt1()); - assertEquals("nested",tc.getNested().getTestString0()); - assertEquals("nested",tc.getNested().getTestString1()); - assertEquals("default",tc.getNested().getNested().getTestString0()); - assertEquals("deep",tc.getNested().getNested().getTestString1()); + Assert.assertEquals("nested", tc.getNested().getTestString0()); + Assert.assertEquals("nested", tc.getNested().getTestString1()); + Assert.assertEquals("default", tc.getNested().getNested().getTestString0()); + Assert.assertEquals("deep", tc.getNested().getNested().getTestString1()); - assertEquals("deep",((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1()); - assertEquals(2,((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2()); + Assert.assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1()); + Assert.assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2()); - assertEquals("xxx",tc.getTestString2()); + Assert.assertEquals("xxx", tc.getTestString2()); } @Test @@ -98,7 +98,7 @@ public class SpringXmlConfigurationTest URL url = SpringXmlConfigurationTest.class.getClassLoader().getResource(_configure); XmlConfiguration configuration = new XmlConfiguration(url); - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put("test", "xxx"); TestConfiguration nested = new TestConfiguration(); @@ -108,20 +108,20 @@ public class SpringXmlConfigurationTest configuration.getProperties().putAll(properties); TestConfiguration tc = (TestConfiguration)configuration.configure(); - assertEquals("default",tc.getTestString0()); - assertEquals(-1,tc.getTestInt0()); - assertEquals("SetValue",tc.getTestString1()); - assertEquals(1,tc.getTestInt1()); + Assert.assertEquals("default", tc.getTestString0()); + Assert.assertEquals(-1, tc.getTestInt0()); + Assert.assertEquals("SetValue", tc.getTestString1()); + Assert.assertEquals(1, tc.getTestInt1()); - assertEquals("nested",tc.getNested().getTestString0()); - assertEquals("nested",tc.getNested().getTestString1()); - assertEquals("default",tc.getNested().getNested().getTestString0()); - assertEquals("deep",tc.getNested().getNested().getTestString1()); + Assert.assertEquals("nested", tc.getNested().getTestString0()); + Assert.assertEquals("nested", tc.getNested().getTestString1()); + Assert.assertEquals("default", tc.getNested().getNested().getTestString0()); + Assert.assertEquals("deep", tc.getNested().getNested().getTestString1()); - assertEquals("deep",((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1()); - assertEquals(2,((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2()); + Assert.assertEquals("deep", ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestString1()); + Assert.assertEquals(2, ((TestConfiguration)configuration.getIdMap().get("nestedDeep")).getTestInt2()); - assertEquals("xxx",tc.getTestString2()); + Assert.assertEquals("xxx", tc.getTestString2()); } @Test @@ -133,13 +133,11 @@ public class SpringXmlConfigurationTest Server server = (Server)configuration.configure(); server.dumpStdErr(); - } @Test public void XmlConfigurationMain() throws Exception { - XmlConfiguration.main(new String[]{"src/test/resources/org/eclipse/jetty/spring/jetty.xml"}); - + XmlConfiguration.main("src/test/resources/org/eclipse/jetty/spring/jetty.xml"); } } diff --git a/jetty-spring/src/test/java/org/eclipse/jetty/spring/TestConfiguration.java b/jetty-spring/src/test/java/org/eclipse/jetty/spring/TestConfiguration.java index e3300b6096d..4a46cdb8123 100644 --- a/jetty-spring/src/test/java/org/eclipse/jetty/spring/TestConfiguration.java +++ b/jetty-spring/src/test/java/org/eclipse/jetty/spring/TestConfiguration.java @@ -25,113 +25,131 @@ import org.junit.Ignore; @Ignore public class TestConfiguration { - public static int VALUE=77; + public static int VALUE = 77; public TestConfiguration nested; - public String testString0="default"; + public String testString0 = "default"; public String testString1; public String testString2; - public int testInt0=-1; + public int testInt0 = -1; public int testInt1; public int testInt2; public URL url; public Object[] objArray; public int[] intArray; - - + + public static int getVALUE() { return VALUE; } + public static void setVALUE(int vALUE) { VALUE = vALUE; } - + public TestConfiguration() { } - + public TestConfiguration getNested() { return nested; } + public void setNested(TestConfiguration nested) { this.nested = nested; } + public String getTestString0() { return testString0; } + public void setTestString0(String testString0) { this.testString0 = testString0; } + public String getTestString1() { return testString1; } + public void setTestString1(String testString1) { this.testString1 = testString1; } + public String getTestString2() { return testString2; } + public void setTestString2(String testString2) { this.testString2 = testString2; } + public int getTestInt0() { return testInt0; } + public void setTestInt0(int testInt0) { this.testInt0 = testInt0; } + public int getTestInt1() { return testInt1; } + public void setTestInt1(int testInt1) { this.testInt1 = testInt1; } + public int getTestInt2() { return testInt2; } + public void setTestInt2(int testInt2) { this.testInt2 = testInt2; } + public URL getUrl() { return url; } + public void setUrl(URL url) { this.url = url; } + public Object[] getObjArray() { return objArray; } + public void setObjArray(Object[] objArray) { this.objArray = objArray; } + public int[] getIntArray() { return intArray; } + public void setIntArray(int[] intArray) { this.intArray = intArray; } - - } diff --git a/jetty-spring/src/test/resources/org/eclipse/jetty/spring/configure.xml b/jetty-spring/src/test/resources/org/eclipse/jetty/spring/configure.xml index 8263c469f45..740e85b81d9 100644 --- a/jetty-spring/src/test/resources/org/eclipse/jetty/spring/configure.xml +++ b/jetty-spring/src/test/resources/org/eclipse/jetty/spring/configure.xml @@ -4,30 +4,30 @@ - + - get + test - + - SetValue - 1 - + + + - + - 2 - nested - + + + - + - deep - 2 + + diff --git a/jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty-deploy.xml b/jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty-deploy.xml deleted file mode 100644 index 290873d779b..00000000000 --- a/jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty-deploy.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - diff --git a/jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty.xml b/jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty.xml index 0aded0fd6fe..34c7c46340d 100644 --- a/jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty.xml +++ b/jetty-spring/src/test/resources/org/eclipse/jetty/spring/jetty.xml @@ -2,44 +2,42 @@ - - + + - + - - - + - + - + - + diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java index 304d8185d0c..aa561773c61 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java @@ -37,8 +37,8 @@ import org.eclipse.jetty.util.log.Logger; * An unmanaged bean is associated with the aggregate for the purposes of {@link #dump()}, but it's lifecycle must be managed externally. *

* When a {@link LifeCycle} bean is added with out a managed state being specified, if it is already started, then it is assumed to be an unmanaged bean. - * If it is not started then it is added in and auto managed state, which means that when this bean is itself started, it if must also start the added bean, then it - * is switched from Auto to be a managed bean. Otherwise it becomes an unmanaged bean. Simply put an Auto bean will be stopped by this aggregate only if it + * If it is not started then it is added in and auto managed state, which means that when this bean is itself started, it if must also start the added bean, then it + * is switched from Auto to be a managed bean. Otherwise it becomes an unmanaged bean. Simply put an Auto bean will be stopped by this aggregate only if it * is started by this aggregate. *

* The methods {@link #addBean(Object, boolean)}, {@link #manage(Object)} and {@link #unmanage(Object)} can be used to @@ -55,11 +55,11 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, private final List _listeners = new CopyOnWriteArrayList<>(); private boolean _started = false; - + public ContainerLifeCycle() { } - + /** * Starts the managed lifecycle beans in the order they were added. */ @@ -99,7 +99,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, /** * Starts the given lifecycle. - * + * * @param l * @throws Exception */ @@ -107,7 +107,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, { l.start(); } - + /** * Stops the managed lifecycle beans in the reverse order they were added. */ @@ -192,7 +192,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, LifeCycle l = (LifeCycle)o; return addBean(o,l.isRunning()?Managed.UNMANAGED:Managed.AUTO); } - + return addBean(o,Managed.POJO); } @@ -227,7 +227,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, for (Bean b:_beans) { listener.beanAdded(this,b._bean); - + // handle inheritance if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container) ((Container)b._bean).addBean(listener); @@ -236,7 +236,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, // Add the bean _beans.add(new_bean); - + // Tell existing listeners about the new bean for (Container.Listener l:_listeners) l.beanAdded(this,o); @@ -254,7 +254,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, if (_started) { - LifeCycle l = (LifeCycle)o; + LifeCycle l = (LifeCycle)o; if (!l.isRunning()) l.start(); } @@ -263,7 +263,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, case AUTO: if (o instanceof LifeCycle) { - LifeCycle l = (LifeCycle)o; + LifeCycle l = (LifeCycle)o; if (_started) { if (l.isRunning()) @@ -280,7 +280,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, else new_bean._managed=Managed.POJO; break; - + case POJO: new_bean._managed=Managed.POJO; } @@ -293,9 +293,9 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, { throw new RuntimeException(e); } - + LOG.debug("{} added {}",this,new_bean); - + return true; } @@ -324,7 +324,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, if (bean._managed!=Managed.MANAGED) { bean._managed=Managed.MANAGED; - + if (bean._bean instanceof Container) { for (Container.Listener l:_listeners) @@ -338,14 +338,14 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, } } } - + if (bean._bean instanceof AbstractLifeCycle) { ((AbstractLifeCycle)bean._bean).setStopTimeout(getStopTimeout()); } } } - + /** * Unmanages a bean already contained by this aggregate, so that it is not started/stopped/destroyed with this * aggregate. @@ -380,14 +380,19 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, bean._managed=Managed.UNMANAGED; } } - - + @Override public Collection getBeans() { return getBeans(Object.class); } + public void setBeans(Collection beans) + { + for (Object bean : beans) + addBean(bean); + } + @Override public Collection getBeans(Class clazz) { @@ -420,7 +425,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, for (Bean b : beans) remove(b); } - + private Bean getBean(Object o) { for (Bean b : _beans) @@ -437,13 +442,13 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, 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); @@ -467,7 +472,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, return false; } - + @Override public void setStopTimeout(long stopTimeout) @@ -548,7 +553,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, { dumpBeans(out,indent); } - + protected void dumpBeans(Appendable out, String indent, Collection... collections) throws IOException { dumpThis(out); @@ -564,14 +569,14 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, switch(b._managed) { - case POJO: + 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) @@ -598,7 +603,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, if (i c : collections) { for (Object o : c) @@ -639,7 +644,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, } } } - + enum Managed { POJO, MANAGED, UNMANAGED, AUTO }; @@ -657,7 +662,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, { return _managed==Managed.MANAGED; } - + @Override public String toString() { @@ -675,7 +680,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, addBean(newBean); } } - + public void updateBeans(Object[] oldBeans, final Object[] newBeans) { // remove oldChildren not in newChildren @@ -692,7 +697,7 @@ public class ContainerLifeCycle extends AbstractLifeCycle implements Container, removeBean(o); } } - + // add new beans not in old if (newBeans!=null) {