Repository: camel
Updated Branches:
  refs/heads/master 003d4d05c -> aca74067c


CAMEL-8263: Added adapt(T) to CamelContext to use instead of ugly type cast for 
adapting to another type such as CdiCamelContext or ModelCamelContext etc. 
Removed the @deprecatation on CamelContext as they are essential api and 
intended to be used from a plain CamelContext as-is.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/aca74067
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/aca74067
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/aca74067

Branch: refs/heads/master
Commit: aca74067c337ac1a0e6c0fe9d18ba5fd79581213
Parents: 003d4d0
Author: Claus Ibsen <davscl...@apache.org>
Authored: Fri Jan 23 10:16:46 2015 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Jan 23 10:16:46 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/camel/CamelContext.java     | 41 +++++++------------
 .../apache/camel/impl/DefaultCamelContext.java  |  4 ++
 .../camel/impl/ModelCamelContextTest.java       | 42 ++++++++++++++++++++
 3 files changed, 60 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/aca74067/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 afa77a5..e66d132 100644
--- a/camel-core/src/main/java/org/apache/camel/CamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java
@@ -94,6 +94,17 @@ import org.apache.camel.util.LoadPropertiesException;
 public interface CamelContext extends SuspendableService, RuntimeConfiguration 
{
 
     /**
+     * Adapts this {@link org.apache.camel.CamelContext} to the specialized 
type.
+     * <p/>
+     * For example to adapt to {@link 
org.apache.camel.model.ModelCamelContext},
+     * or <tt>SpringCamelContext</tt>, or <tt>CdiCamelContext</tt>, etc.
+     *
+     * @param type the type to adapt to
+     * @return this {@link org.apache.camel.CamelContext} adapted to the given 
type
+     */
+    <T extends CamelContext> T adapt(Class<T> type);
+
+    /**
      * Starts the {@link CamelContext} (<b>important:</b> the start method is 
not blocked, see more details
      *     <a 
href="http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html";>here</a>)</li>.
      * <p/>
@@ -424,9 +435,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      * Returns a list of the current route definitions
      *
      * @return list of the current route definitions
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#getRouteDefinitions()}
      */
-    @Deprecated
     List<RouteDefinition> getRouteDefinitions();
 
     /**
@@ -434,27 +443,21 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param id id of the route
      * @return the route definition or <tt>null</tt> if not found
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#getRouteDefinition(String)}
      */
-    @Deprecated
     RouteDefinition getRouteDefinition(String id);
 
     /**
      * Returns a list of the current REST definitions
      *
      * @return list of the current REST definitions
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#getRestDefinitions()}
      */
-    @Deprecated
     List<RestDefinition> getRestDefinitions();
 
     /**
      * Adds a collection of rest definitions to the context
      *
      * @param restDefinitions the rest(s) definition to add
-     * @throws Exception if the rest definitions could not be created for 
whatever reason
      */
-    @Deprecated
     void addRestDefinitions(Collection<RestDefinition> restDefinitions) throws 
Exception;
 
     /**
@@ -519,9 +522,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      * @param is input stream with the route(s) definition to add
      * @throws Exception if the route definitions could not be loaded for 
whatever reason
      * @return the route definitions
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#loadRoutesDefinition(java.io.InputStream)}
      */
-    @Deprecated
     RoutesDefinition loadRoutesDefinition(InputStream is) throws Exception;
 
     /**
@@ -529,9 +530,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param routeDefinitions the route(s) definition to add
      * @throws Exception if the route definitions could not be created for 
whatever reason
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#addRouteDefinitions(java.util.Collection)}
      */
-    @Deprecated
     void addRouteDefinitions(Collection<RouteDefinition> routeDefinitions) 
throws Exception;
 
     /**
@@ -539,9 +538,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param routeDefinition the route definition to add
      * @throws Exception if the route definition could not be created for 
whatever reason
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#addRouteDefinition(org.apache.camel.model.RouteDefinition)}
      */
-    @Deprecated
     void addRouteDefinition(RouteDefinition routeDefinition) throws Exception;
 
     /**
@@ -550,9 +547,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param routeDefinitions route(s) definitions to remove
      * @throws Exception if the route definitions could not be removed for 
whatever reason
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#removeRouteDefinitions(java.util.Collection)}
      */
-    @Deprecated
     void removeRouteDefinitions(Collection<RouteDefinition> routeDefinitions) 
throws Exception;
 
     /**
@@ -561,9 +556,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param routeDefinition route definition to remove
      * @throws Exception if the route definition could not be removed for 
whatever reason
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#removeRouteDefinition(org.apache.camel.model.RouteDefinition)}
      */
-    @Deprecated
     void removeRouteDefinition(RouteDefinition routeDefinition) throws 
Exception;
 
     /**
@@ -571,7 +564,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param route the route to start
      * @throws Exception is thrown if the route could not be started for 
whatever reason
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#startRoute(org.apache.camel.model.RouteDefinition)}
+     * @deprecated favor using {@link CamelContext#startRoute(String)}
      */
     @Deprecated
     void startRoute(RouteDefinition route) throws Exception;
@@ -596,7 +589,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param route the route to stop
      * @throws Exception is thrown if the route could not be stopped for 
whatever reason
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#stopRoute(org.apache.camel.model.RouteDefinition)}
+     * @deprecated favor using {@link CamelContext#stopRoute(String)}
      */
     @Deprecated
     void stopRoute(RouteDefinition route) throws Exception;
@@ -940,9 +933,9 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
 
     /**
      * Gets the default error handler builder which is inherited by the routes
-     * @deprecated The return type will be switched to {@link 
ErrorHandlerFactory} in Camel 3.0
      *
      * @return the builder
+     * @deprecated The return type will be switched to {@link 
ErrorHandlerFactory} in Camel 3.0
      */
     @Deprecated
     ErrorHandlerBuilder getErrorHandlerBuilder();
@@ -964,18 +957,14 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      * Sets the data formats that can be referenced in the routes.
      *
      * @param dataFormats the data formats
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#setDataFormats(java.util.Map)}
      */
-    @Deprecated
     void setDataFormats(Map<String, DataFormatDefinition> dataFormats);
 
     /**
      * Gets the data formats that can be referenced in the routes.
      *
      * @return the data formats available
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#getDataFormats()}
      */
-    @Deprecated
     Map<String, DataFormatDefinition> getDataFormats();
 
     /**
@@ -991,9 +980,7 @@ public interface CamelContext extends SuspendableService, 
RuntimeConfiguration {
      *
      * @param name the data format definition name or a reference to it in the 
{@link Registry}
      * @return the resolved data format definition, or <tt>null</tt> if not 
found
-     * @deprecated use {@link 
org.apache.camel.model.ModelCamelContext#resolveDataFormatDefinition(String)}
      */
-    @Deprecated
     DataFormatDefinition resolveDataFormatDefinition(String name);
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/aca74067/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 9acf65e..98b0ecf 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
@@ -305,6 +305,10 @@ public class DefaultCamelContext extends ServiceSupport 
implements ModelCamelCon
         setRegistry(registry);
     }
 
+    public <T extends CamelContext> T adapt(Class<T> type) {
+        return type.cast(this);
+    }
+
     public String getName() {
         return getNameStrategy().getName();
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/aca74067/camel-core/src/test/java/org/apache/camel/impl/ModelCamelContextTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/ModelCamelContextTest.java 
b/camel-core/src/test/java/org/apache/camel/impl/ModelCamelContextTest.java
new file mode 100644
index 0000000..330eb10
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/ModelCamelContextTest.java
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.ModelCamelContext;
+
+public class ModelCamelContextTest extends ContextTestSupport {
+
+    public void testAdapt() throws Exception {
+        ModelCamelContext mcc = context.adapt(ModelCamelContext.class);
+        assertNotNull(mcc);
+        assertSame(context, mcc);
+
+        assertEquals("foo", mcc.getRouteDefinitions().get(0).getId());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").routeId("foo").to("mock:result");
+            }
+        };
+    }
+}

Reply via email to