CAMEL-8041: Camel commands should be reusable.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c9ddd4ed Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c9ddd4ed Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c9ddd4ed Branch: refs/heads/master Commit: c9ddd4edad26c4b67e0663573717cd6f8a3f6bc8 Parents: c30e455 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Nov 13 11:15:42 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Nov 13 11:15:42 2014 +0100 ---------------------------------------------------------------------- .../camel/commands/AbstractCamelController.java | 269 +++++++++++++++++++ .../camel/commands/AbstractContextCommand.java | 6 +- .../camel/commands/ComponentListCommand.java | 12 +- .../karaf/commands/AbstractContextCommand.java | 45 ---- .../commands/internal/CamelControllerImpl.java | 240 +---------------- 5 files changed, 280 insertions(+), 292 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c9ddd4ed/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 new file mode 100644 index 0000000..e96a069 --- /dev/null +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractCamelController.java @@ -0,0 +1,269 @@ +/** + * 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.commands; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.Route; +import org.apache.camel.model.RouteDefinition; +import org.apache.camel.model.rest.RestDefinition; +import org.apache.camel.spi.RestRegistry; +import org.apache.camel.util.JsonSchemaHelper; + +/** + * Abstract {@link org.apache.camel.commands.CamelController} that implementators should extend. + */ +public abstract class AbstractCamelController implements CamelController { + + public CamelContext getCamelContext(String name) { + for (CamelContext camelContext : this.getCamelContexts()) { + if (camelContext.getName().equals(name)) { + return camelContext; + } + } + return null; + } + + public List<Route> getRoutes(String camelContextName) { + return getRoutes(camelContextName, null); + } + + public List<Route> getRoutes(String camelContextName, String filter) { + List<Route> routes = new ArrayList<Route>(); + + 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); + } + } + } + } else { + List<CamelContext> camelContexts = this.getCamelContexts(); + for (CamelContext camelContext : camelContexts) { + for (Route route : camelContext.getRoutes()) { + if (filter == null || route.getId().matches(filter)) { + routes.add(route); + } + } + } + } + + // sort the list + Collections.sort(routes, new Comparator<Route>() { + @Override + public int compare(Route o1, Route o2) { + // group by camel context first, then by route name + String c1 = o1.getRouteContext().getCamelContext().getName(); + String c2 = o2.getRouteContext().getCamelContext().getName(); + + int answer = c1.compareTo(c2); + if (answer == 0) { + // okay from same camel context, then sort by route id + answer = o1.getId().compareTo(o2.getId()); + } + return answer; + } + }); + return routes; + } + + public Route getRoute(String routeId, String camelContextName) { + List<Route> routes = this.getRoutes(camelContextName); + for (Route route : routes) { + if (route.getId().equals(routeId)) { + return route; + } + } + return null; + } + + @SuppressWarnings("deprecation") + public RouteDefinition getRouteDefinition(String routeId, String camelContextName) { + CamelContext context = this.getCamelContext(camelContextName); + if (context == null) { + return null; + } + return context.getRouteDefinition(routeId); + } + + @SuppressWarnings("deprecation") + public List<RestDefinition> getRestDefinitions(String camelContextName) { + CamelContext context = this.getCamelContext(camelContextName); + if (context == null) { + return null; + } + return context.getRestDefinitions(); + } + + public List<Endpoint> getEndpoints(String camelContextName) { + List<Endpoint> answer = new ArrayList<Endpoint>(); + + if (camelContextName != null) { + CamelContext context = this.getCamelContext(camelContextName); + if (context != null) { + List<Endpoint> endpoints = new ArrayList<Endpoint>(context.getEndpoints()); + // sort routes + Collections.sort(endpoints, new Comparator<Endpoint>() { + @Override + public int compare(Endpoint o1, Endpoint o2) { + return o1.getEndpointKey().compareTo(o2.getEndpointKey()); + } + }); + answer.addAll(endpoints); + } + } else { + // already sorted by camel context + List<CamelContext> camelContexts = this.getCamelContexts(); + for (CamelContext camelContext : camelContexts) { + List<Endpoint> endpoints = new ArrayList<Endpoint>(camelContext.getEndpoints()); + // sort routes + Collections.sort(endpoints, new Comparator<Endpoint>() { + @Override + public int compare(Endpoint o1, Endpoint o2) { + return o1.getEndpointKey().compareTo(o2.getEndpointKey()); + } + }); + answer.addAll(endpoints); + } + } + return answer; + } + + public Map<String, List<RestRegistry.RestService>> getRestServices(String camelContextName) { + Map<String, List<RestRegistry.RestService>> answer = new LinkedHashMap<String, List<RestRegistry.RestService>>(); + + if (camelContextName != null) { + CamelContext context = this.getCamelContext(camelContextName); + if (context != null) { + List<RestRegistry.RestService> services = new ArrayList<RestRegistry.RestService>(context.getRestRegistry().listAllRestServices()); + Collections.sort(services, new Comparator<RestRegistry.RestService>() { + @Override + public int compare(RestRegistry.RestService o1, RestRegistry.RestService o2) { + return o1.getUrl().compareTo(o2.getUrl()); + } + }); + if (!services.isEmpty()) { + answer.put(camelContextName, services); + } + } + } else { + // already sorted by camel context + List<CamelContext> camelContexts = this.getCamelContexts(); + for (CamelContext camelContext : camelContexts) { + List<RestRegistry.RestService> services = new ArrayList<RestRegistry.RestService>(camelContext.getRestRegistry().listAllRestServices()); + Collections.sort(services, new Comparator<RestRegistry.RestService>() { + @Override + public int compare(RestRegistry.RestService o1, RestRegistry.RestService o2) { + return o1.getUrl().compareTo(o2.getUrl()); + } + }); + if (!services.isEmpty()) { + answer.put(camelContext.getName(), services); + } + } + } + return answer; + } + + public String explainEndpoint(String camelContextName, String uri, boolean allOptions) throws Exception { + CamelContext context = this.getCamelContext(camelContextName); + if (context == null) { + return null; + } + return context.explainEndpointJson(uri, allOptions); + } + + public List<Map<String, String>> listComponents(String camelContextName) throws Exception { + CamelContext context = this.getCamelContext(camelContextName); + if (context == null) { + return null; + } + + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + + // find all components + Map<String, Properties> components = context.findComponents(); + + // gather component detail for each component + for (Map.Entry<String, Properties> entry : components.entrySet()) { + String name = entry.getKey(); + String description = null; + // the status can be: + // - loaded = in use + // - classpath = on the classpath + // - release = available from the Apache Camel release + String status = context.hasComponent(name) != null ? "in use" : "on classpath"; + String type = null; + String groupId = null; + String artifactId = null; + String version = null; + + // load component json data, and parse it to gather the component meta-data + String json = context.getComponentParameterJsonSchema(name); + List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("component", json, false); + for (Map<String, String> row : rows) { + if (row.containsKey("description")) { + description = row.get("description"); + } else if (row.containsKey("javaType")) { + type = row.get("javaType"); + } else if (row.containsKey("groupId")) { + groupId = row.get("groupId"); + } else if (row.containsKey("artifactId")) { + artifactId = row.get("artifactId"); + } else if (row.containsKey("version")) { + version = row.get("version"); + } + } + + Map<String, String> row = new HashMap<String, String>(); + row.put("name", name); + row.put("status", status); + if (description != null) { + row.put("description", description); + } + if (type != null) { + row.put("type", type); + } + if (groupId != null) { + row.put("groupId", groupId); + } + if (artifactId != null) { + row.put("artifactId", artifactId); + } + if (version != null) { + row.put("version", version); + } + + answer.add(row); + } + + return answer; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/c9ddd4ed/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractContextCommand.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractContextCommand.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractContextCommand.java index 9facb48..7212b15 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractContextCommand.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractContextCommand.java @@ -43,12 +43,12 @@ public abstract class AbstractContextCommand extends AbstractCamelCommand { } // 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()); + ClassLoader oldClassloader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(camelContext.getApplicationContextClassLoader()); try { return performContextCommand(camelController, camelContext, out, err); } finally { -// Thread.currentThread().setContextClassLoader(oldClassloader); + Thread.currentThread().setContextClassLoader(oldClassloader); } } http://git-wip-us.apache.org/repos/asf/camel/blob/c9ddd4ed/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java index 290260e..e03d615 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ComponentListCommand.java @@ -91,10 +91,10 @@ public class ComponentListCommand extends AbstractContextCommand { return null; } else { // some of the options is optional so we need to start from 1 - int maxNameLen = 0; - int maxStatusLen = 0; - int maxMavenLen = 0; - int maxDescriptionLen = 0; + int maxNameLen = NAME_COLUMN_LABEL.length(); + int maxStatusLen = STATUS_COLUMN_LABEL.length(); + int maxMavenLen = MAVEN_COLUMN_LABEL.length(); + int maxDescriptionLen = DESCRIPTION_COLUMN_LABEL.length(); for (final Map<String, String> component : components) { @@ -150,7 +150,7 @@ public class ComponentListCommand extends AbstractContextCommand { nameLen = Math.max(MIN_COLUMN_WIDTH, nameLen); statusLen = Math.max(MIN_COLUMN_WIDTH, statusLen); mavenLen = Math.max(MIN_COLUMN_WIDTH, mavenLen); - // last row does not have min width + descriptionLen = Math.max(MIN_COLUMN_WIDTH, descriptionLen); final StringBuilder retval = new StringBuilder(DEFAULT_FORMAT_BUFFER_LENGTH); retval.append(fieldPreamble).append("%-").append(nameLen).append('.').append(nameLen).append('s').append(fieldPostamble).append(' '); @@ -163,7 +163,7 @@ public class ComponentListCommand extends AbstractContextCommand { int descriptionLen = Math.min(columnWidths.get(DESCRIPTION_COLUMN_LABEL) + columnWidthIncrement, getMaxColumnWidth()); nameLen = Math.max(MIN_COLUMN_WIDTH, nameLen); - // last row does not have min width + descriptionLen = Math.max(MIN_COLUMN_WIDTH, descriptionLen); final StringBuilder retval = new StringBuilder(DEFAULT_FORMAT_BUFFER_LENGTH); retval.append(fieldPreamble).append("%-").append(nameLen).append('.').append(nameLen).append('s').append(fieldPostamble).append(' '); http://git-wip-us.apache.org/repos/asf/camel/blob/c9ddd4ed/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractContextCommand.java ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractContextCommand.java b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractContextCommand.java deleted file mode 100644 index bb781d7..0000000 --- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/AbstractContextCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * 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.felix.gogo.commands.Argument; - -@Deprecated -public abstract class AbstractContextCommand extends CamelCommandSupport { - - @Argument(index = 0, name = "context", description = "The name of the Camel context.", required = true, multiValued = false) - String context; - - public Object doExecute() throws Exception { - CamelContext camelContext = camelController.getCamelContext(context); - if (camelContext == null) { - System.err.println("Camel context " + context + " not found."); - return null; - } - performContextCommand(camelContext); - return null; - } - - /** - * Perform Context-specific command - * - * @param camelContext non-null {@link CamelContext} - */ - protected abstract void performContextCommand(CamelContext camelContext) throws Exception; - -} http://git-wip-us.apache.org/repos/asf/camel/blob/c9ddd4ed/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java index f695f6a..e0d7d6f 100644 --- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java +++ b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/CamelControllerImpl.java @@ -19,20 +19,10 @@ package org.apache.camel.karaf.commands.internal; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; -import java.util.Properties; import org.apache.camel.CamelContext; -import org.apache.camel.Endpoint; -import org.apache.camel.Route; -import org.apache.camel.commands.CamelController; -import org.apache.camel.model.RouteDefinition; -import org.apache.camel.model.rest.RestDefinition; -import org.apache.camel.spi.RestRegistry; -import org.apache.camel.util.JsonSchemaHelper; +import org.apache.camel.commands.AbstractCamelController; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.slf4j.Logger; @@ -41,7 +31,7 @@ import org.slf4j.LoggerFactory; /** * Implementation of <code>CamelController</code>. */ -public class CamelControllerImpl implements CamelController { +public class CamelControllerImpl extends AbstractCamelController { private static final Logger LOG = LoggerFactory.getLogger(CamelControllerImpl.class); @@ -80,230 +70,4 @@ public class CamelControllerImpl implements CamelController { return camelContexts; } - public CamelContext getCamelContext(String name) { - for (CamelContext camelContext : this.getCamelContexts()) { - if (camelContext.getName().equals(name)) { - return camelContext; - } - } - return null; - } - - public List<Route> getRoutes(String camelContextName) { - return getRoutes(camelContextName, null); - } - - public List<Route> getRoutes(String camelContextName, String filter) { - List<Route> routes = new ArrayList<Route>(); - - 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); - } - } - } - } else { - List<CamelContext> camelContexts = this.getCamelContexts(); - for (CamelContext camelContext : camelContexts) { - for (Route route : camelContext.getRoutes()) { - if (filter == null || route.getId().matches(filter)) { - routes.add(route); - } - } - } - } - - // sort the list - Collections.sort(routes, new Comparator<Route>() { - @Override - public int compare(Route o1, Route o2) { - // group by camel context first, then by route name - String c1 = o1.getRouteContext().getCamelContext().getName(); - String c2 = o2.getRouteContext().getCamelContext().getName(); - - int answer = c1.compareTo(c2); - if (answer == 0) { - // okay from same camel context, then sort by route id - answer = o1.getId().compareTo(o2.getId()); - } - return answer; - } - }); - return routes; - } - - public Route getRoute(String routeId, String camelContextName) { - List<Route> routes = this.getRoutes(camelContextName); - for (Route route : routes) { - if (route.getId().equals(routeId)) { - return route; - } - } - return null; - } - - @SuppressWarnings("deprecation") - public RouteDefinition getRouteDefinition(String routeId, String camelContextName) { - CamelContext context = this.getCamelContext(camelContextName); - if (context == null) { - return null; - } - return context.getRouteDefinition(routeId); - } - - public List<RestDefinition> getRestDefinitions(String camelContextName) { - CamelContext context = this.getCamelContext(camelContextName); - if (context == null) { - return null; - } - return context.getRestDefinitions(); - } - - public List<Endpoint> getEndpoints(String camelContextName) { - List<Endpoint> answer = new ArrayList<Endpoint>(); - - if (camelContextName != null) { - CamelContext context = this.getCamelContext(camelContextName); - if (context != null) { - List<Endpoint> endpoints = new ArrayList<Endpoint>(context.getEndpoints()); - // sort routes - Collections.sort(endpoints, new Comparator<Endpoint>() { - @Override - public int compare(Endpoint o1, Endpoint o2) { - return o1.getEndpointKey().compareTo(o2.getEndpointKey()); - } - }); - answer.addAll(endpoints); - } - } else { - // already sorted by camel context - List<CamelContext> camelContexts = this.getCamelContexts(); - for (CamelContext camelContext : camelContexts) { - List<Endpoint> endpoints = new ArrayList<Endpoint>(camelContext.getEndpoints()); - // sort routes - Collections.sort(endpoints, new Comparator<Endpoint>() { - @Override - public int compare(Endpoint o1, Endpoint o2) { - return o1.getEndpointKey().compareTo(o2.getEndpointKey()); - } - }); - answer.addAll(endpoints); - } - } - return answer; - } - - public Map<String, List<RestRegistry.RestService>> getRestServices(String camelContextName) { - Map<String, List<RestRegistry.RestService>> answer = new LinkedHashMap<String, List<RestRegistry.RestService>>(); - - if (camelContextName != null) { - CamelContext context = this.getCamelContext(camelContextName); - if (context != null) { - List<RestRegistry.RestService> services = new ArrayList<RestRegistry.RestService>(context.getRestRegistry().listAllRestServices()); - Collections.sort(services, new Comparator<RestRegistry.RestService>() { - @Override - public int compare(RestRegistry.RestService o1, RestRegistry.RestService o2) { - return o1.getUrl().compareTo(o2.getUrl()); - } - }); - if (!services.isEmpty()) { - answer.put(camelContextName, services); - } - } - } else { - // already sorted by camel context - List<CamelContext> camelContexts = this.getCamelContexts(); - for (CamelContext camelContext : camelContexts) { - List<RestRegistry.RestService> services = new ArrayList<RestRegistry.RestService>(camelContext.getRestRegistry().listAllRestServices()); - Collections.sort(services, new Comparator<RestRegistry.RestService>() { - @Override - public int compare(RestRegistry.RestService o1, RestRegistry.RestService o2) { - return o1.getUrl().compareTo(o2.getUrl()); - } - }); - if (!services.isEmpty()) { - answer.put(camelContext.getName(), services); - } - } - } - return answer; - } - - public String explainEndpoint(String camelContextName, String uri, boolean allOptions) throws Exception { - CamelContext context = this.getCamelContext(camelContextName); - if (context == null) { - return null; - } - return context.explainEndpointJson(uri, allOptions); - } - - public List<Map<String, String>> listComponents(String camelContextName) throws Exception { - CamelContext context = this.getCamelContext(camelContextName); - if (context == null) { - return null; - } - - List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); - - // find all components - Map<String, Properties> components = context.findComponents(); - - // gather component detail for each component - for (Map.Entry<String, Properties> entry : components.entrySet()) { - String name = entry.getKey(); - String description = null; - // the status can be: - // - loaded = in use - // - classpath = on the classpath - // - release = available from the Apache Camel release - String status = context.hasComponent(name) != null ? "in use" : "on classpath"; - String type = null; - String groupId = null; - String artifactId = null; - String version = null; - - // load component json data, and parse it to gather the component meta-data - String json = context.getComponentParameterJsonSchema(name); - List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("component", json, false); - for (Map<String, String> row : rows) { - if (row.containsKey("description")) { - description = row.get("description"); - } else if (row.containsKey("javaType")) { - type = row.get("javaType"); - } else if (row.containsKey("groupId")) { - groupId = row.get("groupId"); - } else if (row.containsKey("artifactId")) { - artifactId = row.get("artifactId"); - } else if (row.containsKey("version")) { - version = row.get("version"); - } - } - - Map<String, String> row = new HashMap<String, String>(); - row.put("name", name); - row.put("status", status); - if (description != null) { - row.put("description", description); - } - if (type != null) { - row.put("type", type); - } - if (groupId != null) { - row.put("groupId", groupId); - } - if (artifactId != null) { - row.put("artifactId", artifactId); - } - if (version != null) { - row.put("version", version); - } - - answer.add(row); - } - - return answer; - } }