You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/06/26 11:33:49 UTC
svn commit: r1139747 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/CamelContext.java
main/java/org/apache/camel/impl/DefaultCamelContext.java
test/java/org/apache/camel/impl/DefaultCamelContextTest.java
Author: davsclaus
Date: Sun Jun 26 09:33:49 2011
New Revision: 1139747
URL: http://svn.apache.org/viewvc?rev=1139747&view=rev
Log:
CAMEL-4155: addService will inject CamelContext if service is context aware
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=1139747&r1=1139746&r2=1139747&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Sun Jun 26 09:33:49 2011
@@ -140,9 +140,12 @@ public interface CamelContext extends Su
//-----------------------------------------------------------------------
/**
- * Adds a service, starting it so that it will be stopped with this context
+ * Adds a service to this context, which allows this context to control the lifecycle, ensuring
+ * the service is stopped when the context stops.
* <p/>
- * The added service will also be enlisted in JMX for management (if JMX is enabled)
+ * The service will also have {@link CamelContext} injected if its {@link CamelContextAware}.
+ * The service will also be enlisted in JMX for management (if JMX is enabled).
+ * The service will be started, if its not already started.
*
* @param object the service
* @throws Exception can be thrown when starting the service
@@ -150,7 +153,7 @@ public interface CamelContext extends Su
void addService(Object object) throws Exception;
/**
- * Has the given service already been added?
+ * Has the given service already been added to this context?
*
* @param object the service
* @return <tt>true</tt> if already added, <tt>false</tt> if not.
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1139747&r1=1139746&r2=1139747&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Sun Jun 26 09:33:49 2011
@@ -865,6 +865,13 @@ public class DefaultCamelContext extends
}
public void addService(Object object) throws Exception {
+
+ // inject CamelContext
+ if (object instanceof CamelContextAware) {
+ CamelContextAware aware = (CamelContextAware) object;
+ aware.setCamelContext(this);
+ }
+
if (object instanceof Service) {
Service service = (Service) object;
@@ -885,9 +892,14 @@ public class DefaultCamelContext extends
}
// do not add endpoints as they have their own list
if (singleton && !(service instanceof Endpoint)) {
- servicesToClose.add(service);
+ // only add to list of services to close if its not already there
+ if (!hasService(service)) {
+ servicesToClose.add(service);
+ }
}
}
+
+ // and then ensure service is started (as stated in the javadoc)
startServices(object);
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java?rev=1139747&r1=1139746&r2=1139747&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java Sun Jun 26 09:33:49 2011
@@ -21,6 +21,8 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
import org.apache.camel.Component;
import org.apache.camel.Endpoint;
import org.apache.camel.NoSuchEndpointException;
@@ -324,4 +326,41 @@ public class DefaultCamelContextTest ext
assertEquals(false, ctx.isStarted());
assertEquals(false, ctx.isSuspended());
}
+
+ public void testAddServiceInjectCamelContext() throws Exception {
+ MyService my = new MyService();
+
+ DefaultCamelContext ctx = new DefaultCamelContext();
+ ctx.addService(my);
+
+ assertEquals(ctx, my.getCamelContext());
+ assertEquals("Started", my.getStatus().name());
+
+ ctx.stop();
+ assertEquals("Stopped", my.getStatus().name());
+ }
+
+ private class MyService extends ServiceSupport implements CamelContextAware {
+
+ private CamelContext camelContext;
+
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ @Override
+ protected void doStart() throws Exception {
+ // noop
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ // noop
+ }
+ }
+
}