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/09/15 20:24:14 UTC

svn commit: r1171210 - in /camel/trunk/platforms/karaf/commands/src/main: java/org/apache/camel/karaf/commands/ java/org/apache/camel/karaf/commands/internal/ resources/OSGI-INF/blueprint/

Author: davsclaus
Date: Thu Sep 15 18:24:14 2011
New Revision: 1171210

URL: http://svn.apache.org/viewvc?rev=1171210&view=rev
Log:
CAMEL-4450: Improved Camel Karaf commands. Thanks to Ioannis for the patch.

Added:
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ResumeRouteCommand.java   (with props)
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/SuspendRouteCommand.java   (with props)
Modified:
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ListRoutesCommand.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/StartRouteCommand.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java
    camel/trunk/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java?rev=1171210&r1=1171209&r2=1171210&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/CamelController.java Thu Sep 15 18:24:14 2011
@@ -51,6 +51,14 @@ public interface CamelController {
     List<Route> getRoutes(String camelContextName);
 
     /**
+     * Get all route definitions. If Camel context name is null, all route definitions from all contexts are listed.
+     *
+     * @param camelContextName the Camel context name. If null, all contexts are considered.
+     * @return the list of the Camel route definitions.
+     */
+    List<RouteDefinition> getRouteDefinitions(String camelContextName);
+
+    /**
      * Return the route with the given route ID.
      *
      * @param routeId the route ID.

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ListRoutesCommand.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ListRoutesCommand.java?rev=1171210&r1=1171209&r2=1171210&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ListRoutesCommand.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ListRoutesCommand.java Thu Sep 15 18:24:14 2011
@@ -16,9 +16,11 @@
  */
 package org.apache.camel.karaf.commands;
 
+import java.util.LinkedList;
 import java.util.List;
 
-import org.apache.camel.Route;
+import org.apache.camel.CamelContext;
+import org.apache.camel.model.RouteDefinition;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -29,7 +31,12 @@ import org.apache.karaf.shell.console.Os
 @Command(scope = "camel", name = "list-routes", description = "List all Camel routes.")
 public class ListRoutesCommand extends OsgiCommandSupport {
 
-    protected static final String OUTPUT_FORMAT = "[%-20s]";
+    protected static final String HEADER_FORMAT = "%-20s %-20s %-20s";
+    protected static final String OUTPUT_FORMAT = "[%-18s] [%-18s] [%-18s]";
+    protected static final String UNKNOWN = "Unknown";
+    protected static final String ROUTE_ID = "Route Id";
+    protected static final String CONTEXT_ID = "Context Name";
+    protected static final String STATUS = "Status";
 
     @Argument(index = 0, name = "name", description = "The Camel context name where to look for the route", required = false, multiValued = false)
     String name;
@@ -41,10 +48,26 @@ public class ListRoutesCommand extends O
     }
 
     protected Object doExecute() throws Exception {
-        List<Route> routes = camelController.getRoutes(name);
-        for (Route route : routes) {
-            System.out.println(String.format(OUTPUT_FORMAT, route.getId()));
+        System.out.println(String.format(HEADER_FORMAT, ROUTE_ID, CONTEXT_ID, STATUS));
+
+        List<CamelContext> camelContexts = new LinkedList<CamelContext>();
+        if (name != null && camelController.getCamelContext(name) != null) {
+            camelContexts.add(camelController.getCamelContext(name));
+        } else {
+            camelContexts = camelController.getCamelContexts();
+        }
+
+        for (CamelContext camelContext : camelContexts) {
+            List<RouteDefinition> routeDefinitions = camelController.getRouteDefinitions(camelContext.getName());
+            if (routeDefinitions != null && !routeDefinitions.isEmpty()) {
+                for (RouteDefinition routeDefinition : routeDefinitions) {
+                    String contextName = camelContext.getName();
+                    String status = camelContext.getRouteStatus(routeDefinition.getId()).name();
+                    System.out.println(String.format(OUTPUT_FORMAT, routeDefinition.getId(), contextName, status));
+                }
+            }
         }
+
         return null;
     }
 

Added: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ResumeRouteCommand.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ResumeRouteCommand.java?rev=1171210&view=auto
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ResumeRouteCommand.java (added)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ResumeRouteCommand.java Thu Sep 15 18:24:14 2011
@@ -0,0 +1,54 @@
+/**
+ * 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.karaf.commands;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Route;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.karaf.shell.console.OsgiCommandSupport;
+
+/**
+ * Command to resume a route.
+ */
+@Command(scope = "camel", name = "resume-route", description = "Resume a Camel route.")
+public class ResumeRouteCommand extends OsgiCommandSupport {
+
+    @Argument(index = 0, name = "route", description = "The Camel route ID.", required = true, multiValued = false)
+    String route;
+
+    @Argument(index = 1, name = "context", description = "The Camel context name.", required = false, multiValued = false)
+    String context;
+
+    private CamelController camelController;
+
+    public void setCamelController(CamelController camelController) {
+        this.camelController = camelController;
+    }
+
+    public Object doExecute() throws Exception {
+        Route camelRoute = camelController.getRoute(route, context);
+        if (camelRoute == null) {
+            System.err.println("Camel route " + route + " not found.");
+            return null;
+        }
+        CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
+        camelContext.resumeRoute(route);
+        return null;
+    }
+
+}

Propchange: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ResumeRouteCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ResumeRouteCommand.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/StartRouteCommand.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/StartRouteCommand.java?rev=1171210&r1=1171209&r2=1171210&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/StartRouteCommand.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/StartRouteCommand.java Thu Sep 15 18:24:14 2011
@@ -16,8 +16,11 @@
  */
 package org.apache.camel.karaf.commands;
 
+import java.util.List;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.Route;
+import org.apache.camel.model.RouteDefinition;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -43,11 +46,20 @@ public class StartRouteCommand extends O
     public Object doExecute() throws Exception {
         Route camelRoute = camelController.getRoute(route, context);
         if (camelRoute == null) {
+            List<CamelContext> camelContexts = camelController.getCamelContexts();
+            for (CamelContext camelContext : camelContexts) {
+                RouteDefinition routeDefinition = camelContext.getRouteDefinition(route);
+                if (routeDefinition != null) {
+                    camelContext.startRoute(routeDefinition.getId());
+                    return null;
+                }
+            }
             System.err.println("Camel route " + route + " not found.");
             return null;
+        } else {
+            CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
+            camelContext.startRoute(route);
         }
-        CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
-        camelContext.startRoute(route);
         return null;
     }
 

Added: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/SuspendRouteCommand.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/SuspendRouteCommand.java?rev=1171210&view=auto
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/SuspendRouteCommand.java (added)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/SuspendRouteCommand.java Thu Sep 15 18:24:14 2011
@@ -0,0 +1,54 @@
+/**
+ * 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.karaf.commands;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Route;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.karaf.shell.console.OsgiCommandSupport;
+
+/**
+ * Command to suspend a route.
+ */
+@Command(scope = "camel", name = " suspend-route", description = "Suspend a Camel route.")
+public class SuspendRouteCommand extends OsgiCommandSupport {
+
+    @Argument(index = 0, name = "route", description = "The Camel route ID.", required = true, multiValued = false)
+    String route;
+
+    @Argument(index = 1, name = "context", description = "The Camel context name.", required = false, multiValued = false)
+    String context;
+
+    private CamelController camelController;
+
+    public void setCamelController(CamelController camelController) {
+        this.camelController = camelController;
+    }
+
+    public Object doExecute() throws Exception {
+        Route camelRoute = camelController.getRoute(route, context);
+        if (camelRoute == null) {
+            System.err.println("Camel route " + route + " not found.");
+            return null;
+        }
+        CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
+        camelContext.suspendRoute(route);
+        return null;
+    }
+
+}

Propchange: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/SuspendRouteCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/SuspendRouteCommand.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java?rev=1171210&r1=1171209&r2=1171210&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java Thu Sep 15 18:24:14 2011
@@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory;
 
 
 /**
- * Implementation of <code>CamelConrtoller</code>.
+ * Implementation of <code>CamelController</code>.
  */
 public class CamelControllerImpl implements CamelController {
 
@@ -90,6 +90,26 @@ public class CamelControllerImpl impleme
         return null;
     }
 
+
+    public List<RouteDefinition> getRouteDefinitions(String camelContextName) {
+        if (camelContextName != null) {
+            CamelContext context = this.getCamelContext(camelContextName);
+            if (context != null) {
+                return context.getRouteDefinitions();
+            }
+        } else {
+            ArrayList<RouteDefinition> routeDefinitions = new ArrayList<RouteDefinition>();
+            List<CamelContext> camelContexts = this.getCamelContexts();
+            for (CamelContext camelContext : camelContexts) {
+                for (RouteDefinition routeDefinition : camelContext.getRouteDefinitions()) {
+                    routeDefinitions.add(routeDefinition);
+                }
+            }
+            return routeDefinitions;
+        }
+        return null;
+    }
+
     public Route getRoute(String routeId, String camelContextName) {
         List<Route> routes = this.getRoutes(camelContextName);
         for (Route route : routes) {

Modified: camel/trunk/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml?rev=1171210&r1=1171209&r2=1171210&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml (original)
+++ camel/trunk/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml Thu Sep 15 18:24:14 2011
@@ -95,6 +95,24 @@
                 <null/>
             </completers>
         </command>
+        <command name="camel/resume-route">
+            <action class="org.apache.camel.karaf.commands.ResumeRouteCommand">
+                <property name="camelController" ref="camelController"/>
+            </action>
+            <completers>
+                <ref component-id="routeCompleter"/>
+                <null/>
+            </completers>
+        </command>
+        <command name="camel/suspend-route">
+            <action class="org.apache.camel.karaf.commands.SuspendRouteCommand">
+                <property name="camelController" ref="camelController"/>
+            </action>
+            <completers>
+                <ref component-id="routeCompleter"/>
+                <null/>
+            </completers>
+        </command>
     </command-bundle>
 
     <bean id="camelContextCompleter" class="org.apache.camel.karaf.commands.completers.CamelContextCompleter">