CAMEL-8044: Camel commands should be more reusable for remote JVMs
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8048c6ce Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8048c6ce Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8048c6ce Branch: refs/heads/master Commit: 8048c6cec1a44bfed21ada3a8d21d93dbcdec75e Parents: b33acc3 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Dec 10 18:38:45 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Dec 10 18:38:45 2014 +0100 ---------------------------------------------------------------------- .../camel/commands/AbstractCamelController.java | 41 ++++++++++++++----- .../camel/commands/AbstractRouteCommand.java | 43 ++++++-------------- .../apache/camel/commands/CamelController.java | 9 ++-- .../apache/camel/commands/RouteListCommand.java | 25 +++++------- .../commands/completers/RouteCompleter.java | 8 ++-- 5 files changed, 62 insertions(+), 64 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8048c6ce/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java index eaf42c5..9480662 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java @@ -62,19 +62,23 @@ public abstract class AbstractCamelController implements CamelController { return null; } - public List<Route> getRoutes(String camelContextName) throws Exception { + public List<Map<String, String>> getRoutes(String camelContextName) throws Exception { return getRoutes(camelContextName, null); } - public List<Route> getRoutes(String camelContextName, String filter) throws Exception { - List<Route> routes = new ArrayList<Route>(); + public List<Map<String, String>> getRoutes(String camelContextName, String filter) throws Exception { + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); if (camelContextName != null) { CamelContext context = this.getCamelContext(camelContextName); if (context != null) { for (Route route : context.getRoutes()) { if (filter == null || route.getId().matches(filter)) { - routes.add(route); + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("camelContextName", context.getName()); + row.put("routeId", route.getId()); + row.put("state", getRouteState(route)); + answer.add(row); } } } @@ -83,29 +87,33 @@ public abstract class AbstractCamelController implements CamelController { for (CamelContext camelContext : camelContexts) { for (Route route : camelContext.getRoutes()) { if (filter == null || route.getId().matches(filter)) { - routes.add(route); + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("camelContextName", camelContext.getName()); + row.put("routeId", route.getId()); + row.put("state", getRouteState(route)); + answer.add(row); } } } } // sort the list - Collections.sort(routes, new Comparator<Route>() { + Collections.sort(answer, new Comparator<Map<String, String>>() { @Override - public int compare(Route o1, Route o2) { + public int compare(Map<String, String> o1, Map<String, String> o2) { // group by camel context first, then by route name - String c1 = o1.getRouteContext().getCamelContext().getName(); - String c2 = o2.getRouteContext().getCamelContext().getName(); + String c1 = o1.get("camelContextName"); + String c2 = o2.get("camelContextName"); int answer = c1.compareTo(c2); if (answer == 0) { // okay from same camel context, then sort by route id - answer = o1.getId().compareTo(o2.getId()); + answer = o1.get("routeId").compareTo(o2.get("routeId")); } return answer; } }); - return routes; + return answer; } public void resetRouteStats(String camelContextName) throws Exception{ @@ -458,4 +466,15 @@ public abstract class AbstractCamelController implements CamelController { return ServiceStatus.Started.name(); } + private static String getRouteState(Route route) { + // must use String type to be sure remote JMX can read the attribute without requiring Camel classes. + if (route instanceof StatefulService) { + ServiceStatus status = ((StatefulService) route).getStatus(); + return status.name(); + } + + // assume started if not a ServiceSupport instance + return ServiceStatus.Started.name(); + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/8048c6ce/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractRouteCommand.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractRouteCommand.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractRouteCommand.java index 81fd2a6..74611e8 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractRouteCommand.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractRouteCommand.java @@ -20,13 +20,10 @@ import java.io.PrintStream; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Map; -import org.apache.camel.CamelContext; -import org.apache.camel.Route; import org.apache.camel.commands.internal.RegexUtil; -import static org.apache.camel.util.CamelContextHelper.getRouteStartupOrder; - /** * Abstract command for working with a one ore more routes. */ @@ -45,7 +42,7 @@ public abstract class AbstractRouteCommand extends AbstractCamelCommand { } public Object execute(CamelController camelController, PrintStream out, PrintStream err) throws Exception { - List<Route> camelRoutes = camelController.getRoutes(context, RegexUtil.wildcardAsRegex(route)); + List<Map<String, String>> camelRoutes = camelController.getRoutes(context, RegexUtil.wildcardAsRegex(route)); if (camelRoutes == null || camelRoutes.isEmpty()) { err.println("Camel routes using " + route + " not found."); return null; @@ -53,16 +50,10 @@ public abstract class AbstractRouteCommand extends AbstractCamelCommand { // we want the routes sorted Collections.sort(camelRoutes, new RouteComparator()); - for (Route camelRoute : camelRoutes) { - CamelContext camelContext = camelRoute.getRouteContext().getCamelContext(); - // Setting thread context classloader to the bundle classloader to enable legacy code that relies on it - ClassLoader oldClassloader = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(camelContext.getApplicationContextClassLoader()); - try { - executeOnRoute(camelController, camelContext.getName(), camelRoute.getId(), out, err); - } finally { - Thread.currentThread().setContextClassLoader(oldClassloader); - } + for (Map<String, String> row : camelRoutes) { + String camelContextName = row.get("camelContextName"); + String routeId = row.get("routeId"); + executeOnRoute(camelController, camelContextName, routeId, out, err); } return null; @@ -73,26 +64,18 @@ public abstract class AbstractRouteCommand extends AbstractCamelCommand { /** * To sort the routes. */ - private static final class RouteComparator implements Comparator<Route> { + private static final class RouteComparator implements Comparator<Map<String, String>> { @Override - public int compare(Route route1, Route route2) { + public int compare(Map<String, String> route1, Map<String, String> route2) { // sort by camel context first - CamelContext camel1 = route1.getRouteContext().getCamelContext(); - CamelContext camel2 = route2.getRouteContext().getCamelContext(); + String camel1 = route1.get("camelContextName"); + String camel2 = route2.get("camelContextName"); - if (camel1.getName().equals(camel2.getName())) { - // and then accordingly to startup order - Integer order1 = getRouteStartupOrder(camel1, route1.getId()); - Integer order2 = getRouteStartupOrder(camel2, route2.getId()); - if (order1 == 0 && order2 == 0) { - // fallback and use name if not startup order was found - return route1.getId().compareTo(route2.getId()); - } else { - return order1.compareTo(order2); - } + if (camel1.equals(camel2)) { + return route1.get("routeId").compareTo(route2.get("routeId")); } else { - return camel1.getName().compareTo(camel2.getName()); + return camel1.compareTo(camel2); } } } http://git-wip-us.apache.org/repos/asf/camel/blob/8048c6ce/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java index 5f61140..378cdb1 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java @@ -21,7 +21,6 @@ import java.util.Map; import java.util.Set; import org.apache.camel.CamelContext; -import org.apache.camel.Endpoint; import org.apache.camel.Route; /** @@ -50,20 +49,20 @@ public interface CamelController { * Get all routes. If Camel context name is null, all routes from all contexts are listed. * * @param camelContextName the Camel context name. If null, all contexts are considered. - * @return the list of the Camel routes. + * @return a list of key/value pairs with routes information * @throws java.lang.Exception can be thrown */ - List<Route> getRoutes(String camelContextName) throws Exception; + List<Map<String, String>> getRoutes(String camelContextName) throws Exception; /** * 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. + * @return a list of key/value pairs with routes information * @throws java.lang.Exception can be thrown */ - List<Route> getRoutes(String camelContextName, String filter) throws Exception; + List<Map<String, String>> getRoutes(String camelContextName, String filter) throws Exception; /** * Reset all the route stats for the given Camel context http://git-wip-us.apache.org/repos/asf/camel/blob/8048c6ce/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteListCommand.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteListCommand.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteListCommand.java index b2e63ca..8c00d7a 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteListCommand.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/RouteListCommand.java @@ -21,9 +21,6 @@ import java.util.Hashtable; import java.util.List; import java.util.Map; -import org.apache.camel.Route; -import org.apache.camel.ServiceStatus; - /** * Command to list all Camel routes. */ @@ -50,7 +47,7 @@ public class RouteListCommand extends AbstractCamelCommand { @Override public Object execute(CamelController camelController, PrintStream out, PrintStream err) throws Exception { - List<Route> routes = camelController.getRoutes(name); + List<Map<String, String>> routes = camelController.getRoutes(name); final Map<String, Integer> columnWidths = computeColumnWidths(routes); final String headerFormat = buildFormatString(columnWidths, true); @@ -59,18 +56,18 @@ public class RouteListCommand extends AbstractCamelCommand { if (routes.size() > 0) { out.println(String.format(headerFormat, CONTEXT_COLUMN_LABEL, ROUTE_COLUMN_LABEL, STATUS_COLUMN_LABEL)); out.println(String.format(headerFormat, "-------", "-----", "------")); - for (Route route : routes) { - String contextId = route.getRouteContext().getCamelContext().getName(); - String routeId = route.getId(); - ServiceStatus status = route.getRouteContext().getCamelContext().getRouteStatus(routeId); - out.println(String.format(rowFormat, contextId, routeId, status)); + for (Map<String, String> row : routes) { + String contextId = row.get("camelContextName"); + String routeId = row.get("routeId"); + String state = row.get("state"); + out.println(String.format(rowFormat, contextId, routeId, state)); } } return null; } - private static Map<String, Integer> computeColumnWidths(final Iterable<Route> routes) throws Exception { + private static Map<String, Integer> computeColumnWidths(final Iterable<Map<String, String>> routes) throws Exception { if (routes == null) { throw new IllegalArgumentException("Unable to determine column widths from null Iterable<Route>"); } else { @@ -78,14 +75,14 @@ public class RouteListCommand extends AbstractCamelCommand { int maxRouteLen = 0; int maxStatusLen = 0; - for (final Route route : routes) { - final String contextId = route.getRouteContext().getCamelContext().getName(); + for (Map<String, String> row : routes) { + final String contextId = row.get("camelContextName"); maxContextLen = java.lang.Math.max(maxContextLen, contextId == null ? 0 : contextId.length()); - final String routeId = route.getId(); + final String routeId = row.get("routeId"); maxRouteLen = java.lang.Math.max(maxRouteLen, routeId == null ? 0 : routeId.length()); - final String status = route.getRouteContext().getCamelContext().getRouteStatus(routeId).name(); + final String status = row.get("state"); maxStatusLen = java.lang.Math.max(maxStatusLen, status == null ? 0 : status.length()); } http://git-wip-us.apache.org/repos/asf/camel/blob/8048c6ce/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/completers/RouteCompleter.java ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/completers/RouteCompleter.java b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/completers/RouteCompleter.java index 53a3d4d..fbcd1d3 100644 --- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/completers/RouteCompleter.java +++ b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/completers/RouteCompleter.java @@ -17,9 +17,9 @@ package org.apache.camel.karaf.commands.completers; import java.util.List; +import java.util.Map; import jline.console.completer.StringsCompleter; -import org.apache.camel.Route; /** * A Jline completer for the Camel routes. @@ -30,9 +30,9 @@ public class RouteCompleter extends CamelCompleterSupport { public int complete(String buffer, int cursor, List candidates) { try { StringsCompleter delegate = new StringsCompleter(); - List<Route> routes = camelController.getRoutes(null); - for (Route route : routes) { - delegate.getStrings().add(route.getId()); + List<Map<String, String>> routes = camelController.getRoutes(null); + for (Map<String, String> row : routes) { + delegate.getStrings().add(row.get("routeId")); } return delegate.complete(buffer, cursor, candidates); } catch (Exception e) {