Author: dkulp
Date: Sat Jul  9 02:21:59 2011
New Revision: 1144575

URL: http://svn.apache.org/viewvc?rev=1144575&view=rev
Log:
Merged revisions 1139747 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r1139747 | davsclaus | 2011-06-26 05:33:49 -0400 (Sun, 26 Jun 2011) | 1 line
  
  CAMEL-4155: addService will inject CamelContext if service is context aware
........

Modified:
    camel/branches/camel-2.7.x/   (props changed)
    
camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/CamelContext.java
    
camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    
camel/branches/camel-2.7.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java

Propchange: camel/branches/camel-2.7.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sat Jul  9 02:21:59 2011
@@ -1 +1 @@
-/camel/trunk:1083696,1083723-1083724,1084150,1085277,1085543,1085549,1085905,1085909,1086165,1086231,1087005,1087276,1087612,1087620,1087856,1088583,1088916-1088917,1089275,1089348,1090166,1090204,1090564,1090960-1090969,1091082,1091518,1091771,1091799,1092034,1092068,1092577,1092667,1093978,1093980,1093999,1094123,1094147,1094156,1095405,1095469,1095471,1095475-1095476,1096346,1096736,1097761,1097909,1097912,1097978,1098032,1098628,1098630,1099228,1099417,1100711,1100975,1102162,1102177,1102181,1104076,1124497,1127744,1127988,1128315,1128970,1131411,1132961,1134252,1134260,1134404,1134501,1134626,1134681,1134714-1134911,1135223,1135364,1136065,1136290,1137696,1138285,1139163,1139749,1140096-1140102,1141783,1142500,1142654,1142721,1143332,1143925,1144248,1144324
+/camel/trunk:1083696,1083723-1083724,1084150,1085277,1085543,1085549,1085905,1085909,1086165,1086231,1087005,1087276,1087612,1087620,1087856,1088583,1088916-1088917,1089275,1089348,1090166,1090204,1090564,1090960-1090969,1091082,1091518,1091771,1091799,1092034,1092068,1092577,1092667,1093978,1093980,1093999,1094123,1094147,1094156,1095405,1095469,1095471,1095475-1095476,1096346,1096736,1097761,1097909,1097912,1097978,1098032,1098628,1098630,1099228,1099417,1100711,1100975,1102162,1102177,1102181,1104076,1124497,1127744,1127988,1128315,1128970,1131411,1132961,1134252,1134260,1134404,1134501,1134626,1134681,1134714-1134911,1135223,1135364,1136065,1136290,1137696,1138285,1139163,1139747,1139749,1140096-1140102,1141783,1142500,1142654,1142721,1143332,1143925,1144248,1144324

Propchange: camel/branches/camel-2.7.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=1144575&r1=1144574&r2=1144575&view=diff
==============================================================================
--- 
camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/CamelContext.java
 (original)
+++ 
camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/CamelContext.java
 Sat Jul  9 02:21:59 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/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1144575&r1=1144574&r2=1144575&view=diff
==============================================================================
--- 
camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 (original)
+++ 
camel/branches/camel-2.7.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 Sat Jul  9 02:21:59 2011
@@ -873,6 +873,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;
 
@@ -893,9 +900,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/branches/camel-2.7.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java?rev=1144575&r1=1144574&r2=1144575&view=diff
==============================================================================
--- 
camel/branches/camel-2.7.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java
 (original)
+++ 
camel/branches/camel-2.7.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java
 Sat Jul  9 02:21:59 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
+        }
+    }
+
 }


Reply via email to