CAMEL-7695: CamelContext - Allow to check if a service by its type has been added
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fa7e2254 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fa7e2254 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fa7e2254 Branch: refs/heads/master Commit: fa7e2254f065f3e3e7bf28d73ca218c914676487 Parents: 76c39dc Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Aug 14 08:52:51 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Aug 14 08:55:22 2014 +0200 ---------------------------------------------------------------------- .../src/main/java/org/apache/camel/CamelContext.java | 8 ++++++++ .../org/apache/camel/impl/DefaultCamelContext.java | 13 ++++++++++++- .../org/apache/camel/impl/DefaultCamelContextTest.java | 13 +++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fa7e2254/camel-core/src/main/java/org/apache/camel/CamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java b/camel-core/src/main/java/org/apache/camel/CamelContext.java index 5b175a3..8a3c31b 100644 --- a/camel-core/src/main/java/org/apache/camel/CamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java @@ -230,6 +230,14 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration { boolean hasService(Object object); /** + * Has the given service type already been added to this context? + * + * @param type the class type + * @return the service instance or <tt>null</tt> if not already added. + */ + public <T> T hasService(Class<T> type); + + /** * Adds the given listener to be invoked when {@link CamelContext} have just been started. * <p/> * This allows listeners to do any custom work after the routes and other services have been started and are running. http://git-wip-us.apache.org/repos/asf/camel/blob/fa7e2254/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index dab1eaa..72defeb 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -66,6 +66,7 @@ import org.apache.camel.ShutdownRunningTask; import org.apache.camel.StartupListener; import org.apache.camel.StatefulService; import org.apache.camel.SuspendableService; +import org.apache.camel.TypeConversionException; import org.apache.camel.TypeConverter; import org.apache.camel.VetoCamelContextStartException; import org.apache.camel.builder.ErrorHandlerBuilder; @@ -161,7 +162,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon private final List<EndpointStrategy> endpointStrategies = new ArrayList<EndpointStrategy>(); private final Map<String, Component> components = new HashMap<String, Component>(); private final Set<Route> routes = new LinkedHashSet<Route>(); - private final List<Service> servicesToClose = new ArrayList<Service>(); + private final List<Service> servicesToClose = new CopyOnWriteArrayList<Service>(); private final Set<StartupListener> startupListeners = new LinkedHashSet<StartupListener>(); private TypeConverter typeConverter; private TypeConverterRegistry typeConverterRegistry; @@ -1031,6 +1032,16 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon return false; } + @Override + public <T> T hasService(Class<T> type) { + for (Service service : servicesToClose) { + if (type.isInstance(service)) { + return type.cast(service); + } + } + return null; + } + public void addStartupListener(StartupListener listener) throws Exception { // either add to listener so we can invoke then later when CamelContext has been started // or invoke the callback right now http://git-wip-us.apache.org/repos/asf/camel/blob/fa7e2254/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java b/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java index b7f51ae..3403ae4 100644 --- a/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java +++ b/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java @@ -335,6 +335,19 @@ public class DefaultCamelContextTest extends TestSupport { assertEquals("Stopped", my.getStatus().name()); } + public void testAddServiceType() throws Exception { + MyService my = new MyService(); + + DefaultCamelContext ctx = new DefaultCamelContext(); + assertNull(ctx.hasService(MyService.class)); + + ctx.addService(my); + assertSame(my, ctx.hasService(MyService.class)); + + ctx.stop(); + assertNull(ctx.hasService(MyService.class)); + } + private static class MyService extends ServiceSupport implements CamelContextAware { private CamelContext camelContext;