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 2012/06/17 14:19:13 UTC

svn commit: r1351094 - in /camel/trunk/platforms/karaf/commands: ./ src/main/java/org/apache/camel/karaf/commands/ src/main/java/org/apache/camel/karaf/commands/internal/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/ja...

Author: davsclaus
Date: Sun Jun 17 12:19:12 2012
New Revision: 1351094

URL: http://svn.apache.org/viewvc?rev=1351094&view=rev
Log:
CAMEL-5369: Added wildcard support to Camel Karaf commands to manage routes. Thanks to Nikolaos Dimos for the patch.

Added:
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/RegexUtil.java
    camel/trunk/platforms/karaf/commands/src/test/
    camel/trunk/platforms/karaf/commands/src/test/java/
    camel/trunk/platforms/karaf/commands/src/test/java/org/
    camel/trunk/platforms/karaf/commands/src/test/java/org/apache/
    camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/
    camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/karaf/
    camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/karaf/commands/
    camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/karaf/commands/RegexUtilTest.java
Modified:
    camel/trunk/platforms/karaf/commands/   (props changed)
    camel/trunk/platforms/karaf/commands/pom.xml
    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/RouteResume.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStart.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStop.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteSuspend.java
    camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java

Propchange: camel/trunk/platforms/karaf/commands/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sun Jun 17 12:19:12 2012
@@ -14,3 +14,4 @@ eclipse-classes
 *.ipr
 *.iml
 *.iws
+*.idea

Modified: camel/trunk/platforms/karaf/commands/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/pom.xml?rev=1351094&r1=1351093&r2=1351094&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/pom.xml (original)
+++ camel/trunk/platforms/karaf/commands/pom.xml Sun Jun 17 12:19:12 2012
@@ -58,6 +58,13 @@
       <artifactId>org.apache.karaf.util</artifactId>
       <version>${karaf-version}</version>
     </dependency>
+    
+    <!-- Test -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+     </dependency>
 
   </dependencies>
 

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=1351094&r1=1351093&r2=1351094&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 Sun Jun 17 12:19:12 2012
@@ -49,6 +49,15 @@ public interface CamelController {
      * @return the list of the Camel routes.
      */
     List<Route> getRoutes(String camelContextName);
+    
+    /**
+     * Get all routes filtered by the regex.
+     *
+     * @param camelContextName the Camel context name. If null, all contexts are considered.
+     * @param filter the filter which supports * and ? as wildcards
+     * @return the list of the Camel routes.
+     */
+    List<Route> getRoutes(String camelContextName, String filter);
 
     /**
      * Get all route definitions. If Camel context name is null, all route definitions from all contexts are listed.

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResume.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResume.java?rev=1351094&r1=1351093&r2=1351094&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResume.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteResume.java Sun Jun 17 12:19:12 2012
@@ -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.karaf.commands.internal.RegexUtil;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -25,10 +28,10 @@ import org.apache.karaf.shell.console.Os
 /**
  * Command to resume a route.
  */
-@Command(scope = "camel", name = "route-resume", description = "Resume a Camel route.")
+@Command(scope = "camel", name = "route-resume", description = "Resume a Camel route or a group of routes.")
 public class RouteResume extends OsgiCommandSupport {
 
-    @Argument(index = 0, name = "route", description = "The Camel route ID.", required = true, multiValued = false)
+    @Argument(index = 0, name = "route", description = "The Camel route ID or a wildcard expression.", required = true, multiValued = false)
     String route;
 
     @Argument(index = 1, name = "context", description = "The Camel context name.", required = false, multiValued = false)
@@ -41,13 +44,15 @@ public class RouteResume extends OsgiCom
     }
 
     public Object doExecute() throws Exception {
-        Route camelRoute = camelController.getRoute(route, context);
-        if (camelRoute == null) {
-            System.err.println("Camel route " + route + " not found.");
+        List<Route> camelRoutes = camelController.getRoutes(context, RegexUtil.wildcardAsRegex(route));
+        if (camelRoutes == null) {
+            System.err.println("Camel routes using  " + route + " not found.");
             return null;
         }
-        CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
-        camelContext.resumeRoute(route);
+        for (Route camelRoute : camelRoutes) {
+            CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
+            camelContext.resumeRoute(camelRoute.getId());
+        }
         return null;
     }
 

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStart.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStart.java?rev=1351094&r1=1351093&r2=1351094&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStart.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStart.java Sun Jun 17 12:19:12 2012
@@ -20,7 +20,7 @@ import java.util.List;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Route;
-import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.karaf.commands.internal.RegexUtil;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -28,10 +28,10 @@ import org.apache.karaf.shell.console.Os
 /**
  * Command to start a route.
  */
-@Command(scope = "camel", name = "route-start", description = "Start a Camel route.")
+@Command(scope = "camel", name = "route-start", description = "Start a Camel route or a group of routes")
 public class RouteStart extends OsgiCommandSupport {
 
-    @Argument(index = 0, name = "route", description = "The Camel route ID.", required = true, multiValued = false)
+    @Argument(index = 0, name = "route", description = "The Camel route ID or a wildcard expression", required = true, multiValued = false)
     String route;
 
     @Argument(index = 1, name = "context", description = "The Camel context name.", required = false, multiValued = false)
@@ -43,24 +43,17 @@ public class RouteStart extends OsgiComm
         this.camelController = camelController;
     }
 
-    @SuppressWarnings("deprecation")
     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.");
+        List<Route> camelRoutes = camelController.getRoutes(context, RegexUtil.wildcardAsRegex(route));
+        if (camelRoutes == null || camelRoutes.isEmpty()) {
+            System.err.println("Camel routes using " + route + " not found.");
             return null;
-        } else {
+        }
+        for (Route camelRoute : camelRoutes) {
             CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
-            camelContext.startRoute(route);
+            camelContext.startRoute(camelRoute.getId());
         }
+
         return null;
     }
 

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStop.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStop.java?rev=1351094&r1=1351093&r2=1351094&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStop.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteStop.java Sun Jun 17 12:19:12 2012
@@ -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.karaf.commands.internal.RegexUtil;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -25,10 +28,10 @@ import org.apache.karaf.shell.console.Os
 /**
  * Command to stop a route.
  */
-@Command(scope = "camel", name = " route-stop", description = "Stop a Camel route.")
+@Command(scope = "camel", name = " route-stop", description = "Stop a Camel route or a group of routes.")
 public class RouteStop extends OsgiCommandSupport {
 
-    @Argument(index = 0, name = "route", description = "The Camel route ID.", required = true, multiValued = false)
+    @Argument(index = 0, name = "route", description = "The Camel route ID or a wildcard expression.", required = true, multiValued = false)
     String route;
 
     @Argument(index = 1, name = "context", description = "The Camel context name.", required = false, multiValued = false)
@@ -41,13 +44,16 @@ public class RouteStop extends OsgiComma
     }
 
     public Object doExecute() throws Exception {
-        Route camelRoute = camelController.getRoute(route, context);
-        if (camelRoute == null) {
-            System.err.println("Camel route " + route + " not found.");
+        List<Route> camelRoutes = camelController.getRoutes(context, RegexUtil.wildcardAsRegex(route));
+        if (camelRoutes == null || camelRoutes.isEmpty()) {
+            System.err.println("Camel routes using " + route + " not found.");
             return null;
         }
-        CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
-        camelContext.stopRoute(route);
+        for (Route camelRoute : camelRoutes) {
+            CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
+            camelContext.stopRoute(camelRoute.getId());
+        }
+
         return null;
     }
 

Modified: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteSuspend.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteSuspend.java?rev=1351094&r1=1351093&r2=1351094&view=diff
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteSuspend.java (original)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/RouteSuspend.java Sun Jun 17 12:19:12 2012
@@ -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.karaf.commands.internal.RegexUtil;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -25,10 +28,10 @@ import org.apache.karaf.shell.console.Os
 /**
  * Command to suspend a route.
  */
-@Command(scope = "camel", name = " route-suspend", description = "Suspend a Camel route.")
+@Command(scope = "camel", name = " route-suspend", description = "Suspend a Camel route or a group of routes.")
 public class RouteSuspend extends OsgiCommandSupport {
 
-    @Argument(index = 0, name = "route", description = "The Camel route ID.", required = true, multiValued = false)
+    @Argument(index = 0, name = "route", description = "The Camel route ID or a wildcard expression.", required = true, multiValued = false)
     String route;
 
     @Argument(index = 1, name = "context", description = "The Camel context name.", required = false, multiValued = false)
@@ -41,13 +44,16 @@ public class RouteSuspend extends OsgiCo
     }
 
     public Object doExecute() throws Exception {
-        Route camelRoute = camelController.getRoute(route, context);
-        if (camelRoute == null) {
-            System.err.println("Camel route " + route + " not found.");
+        List<Route> camelRoutes = camelController.getRoutes(context, RegexUtil.wildcardAsRegex(route));
+        if (camelRoutes == null || camelRoutes.isEmpty()) {
+            System.err.println("Camel routes using " + route + " not found.");
             return null;
         }
-        CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
-        camelContext.suspendRoute(route);
+        for (Route camelRoute : camelRoutes) {
+            CamelContext camelContext = camelRoute.getRouteContext().getCamelContext();
+            camelContext.suspendRoute(camelRoute.getId());
+        }
+
         return null;
     }
 

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=1351094&r1=1351093&r2=1351094&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 Sun Jun 17 12:19:12 2012
@@ -89,6 +89,36 @@ public class CamelControllerImpl impleme
         }
         return null;
     }
+    
+    public List<Route> getRoutes(String camelContextName, String filter) {
+        List<Route> routes = null;
+        if (camelContextName != null) {
+            CamelContext context = this.getCamelContext(camelContextName);
+            if (context != null) {
+                for (Route route : context.getRoutes()) {
+                    if (routes == null) {
+                        routes = new ArrayList<Route>();
+                    }
+                    if (route.getId().matches(filter)) {
+                        routes.add(route);
+                    }
+                }
+            }
+        } else {
+            List<CamelContext> camelContexts = this.getCamelContexts();
+            for (CamelContext camelContext : camelContexts) {
+                for (Route route : camelContext.getRoutes()) {
+                    if (routes == null) {
+                        routes = new ArrayList<Route>();
+                    }
+                    if (route.getId().matches(filter)) {
+                        routes.add(route);
+                    }
+                }
+            }
+        }
+        return routes;
+    }
 
     @SuppressWarnings("deprecation")
     public List<RouteDefinition> getRouteDefinitions(String camelContextName) {

Added: camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/RegexUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/RegexUtil.java?rev=1351094&view=auto
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/RegexUtil.java (added)
+++ camel/trunk/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/RegexUtil.java Sun Jun 17 12:19:12 2012
@@ -0,0 +1,69 @@
+/**
+ * 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.internal;
+
+/**
+ * Util class.
+ */
+public final class RegexUtil {
+
+    private RegexUtil() {
+    }
+
+    /**
+     * convert a wild card containing * and ? to the equivalent regex
+     *
+     * @param wildcard wildcard string describing a file.
+     * @return regex string that could be fed to Pattern.compile
+     */
+    public static String wildcardAsRegex(String wildcard) {
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < wildcard.length(); i++) {
+            final char c = wildcard.charAt(i);
+            switch (c) {
+                case '*':
+                    sb.append(".*?");
+                    break;
+                case '?':
+                    sb.append(".");
+                    break;
+                // chars that have magic regex meaning. They need quoting to be taken literally
+                case '$':
+                case '(':
+                case ')':
+                case '+':
+                case '-':
+                case '.':
+                case '[':
+                case '\\':
+                case ']':
+                case '^':
+                case '{':
+                case '|':
+                case '}':
+                    sb.append('\\');
+                    sb.append(c);
+                    break;
+                default:
+                    sb.append(c);
+                    break;
+            }
+        }
+        return sb.toString();
+    }
+
+}

Added: camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/karaf/commands/RegexUtilTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/karaf/commands/RegexUtilTest.java?rev=1351094&view=auto
==============================================================================
--- camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/karaf/commands/RegexUtilTest.java (added)
+++ camel/trunk/platforms/karaf/commands/src/test/java/org/apache/camel/karaf/commands/RegexUtilTest.java Sun Jun 17 12:19:12 2012
@@ -0,0 +1,53 @@
+/**
+ * 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 java.util.regex.Pattern;
+
+import org.apache.camel.karaf.commands.internal.RegexUtil;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+public class RegexUtilTest {
+    
+    @Test
+    public void testWildcardAsRegex() throws Exception {
+        String testRouteId1 = "route.inbound.systema";
+        String testRouteId2 = "route.inbound.systemb";
+        String testRouteId3 = "route.outbound.systema";
+        String testRouteId4 = "route.outbound.systemb";
+        String testRouteId5 = "outbound.systemc";
+
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("route.inbound*"), testRouteId1));
+        assertTrue(!Pattern.matches(RegexUtil.wildcardAsRegex(".inbound*"), testRouteId2));
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*.inbound*"), testRouteId2));
+
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*outbound*"), testRouteId3));
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*outbound*"), testRouteId4));
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*outbound*"), testRouteId5));
+
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*"), testRouteId1));
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*"), testRouteId2));
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*"), testRouteId3));
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*"), testRouteId4));
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("*"), testRouteId5));
+
+        assertTrue(Pattern.matches(RegexUtil.wildcardAsRegex("route.inbound.systema"), testRouteId1));
+    }
+
+}