Repository: camel Updated Branches: refs/heads/master fb6601193 -> e55597f17
CAMEL-9224 : Camel shell commands adapter for Spring Boot Remote shell Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/15d4be0c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/15d4be0c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/15d4be0c Branch: refs/heads/master Commit: 15d4be0cf39f70948276f7b1fa30b5edc6885120 Parents: fb66011 Author: Evgeny Minkevich <evgeny.minkev...@gmail.com> Authored: Fri Nov 6 11:27:20 2015 +1100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Nov 7 08:56:08 2015 +0100 ---------------------------------------------------------------------- .../commands/commands-spring-boot/README.md | 34 +++ platforms/commands/commands-spring-boot/pom.xml | 30 ++ .../commands/crsh/ArgumentCamelContext.java | 31 +++ .../commands/crsh/ArgumentRouteID.java | 31 +++ .../commands/crsh/CamelCommandsFacade.java | 111 ++++++++ .../commands/crsh/CamelCommandsPlugin.java | 56 ++++ .../commands/crsh/CamelCompleter.java | 74 +++++ .../commands/crsh/CamelControllerImpl.java | 83 ++++++ .../commands/crsh/NoopStringEscape.java | 34 +++ .../springboot/commands/crsh/OutputBuffer.java | 47 ++++ .../services/org.crsh.plugin.CRaSHPlugin | 1 + .../resources/crash/commands/camel/camel.groovy | 278 +++++++++++++++++++ 12 files changed, 810 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/README.md ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/README.md b/platforms/commands/commands-spring-boot/README.md new file mode 100644 index 0000000..d82732b --- /dev/null +++ b/platforms/commands/commands-spring-boot/README.md @@ -0,0 +1,34 @@ +# Camel Shell Commands for Spring Boot + +This component is implemented as a plugin for [CRuSH Java Shell](http://www.crashub.org/), a component used by the Spring Boot platform for the remote shell. +It is essentially an adapter for the available Camel commands, responsible for passing through the options and arguments. + +# Installation + +To enable Spring Boot remote shell support: + +```xml + +<dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-remote-shell</artifactId> + <version>x.x.x</version> + <!-- use the version that is used as the depndency by the Camel --> +</dependency> + +``` + +To enable Camel Commands for the Spring Boot remote shell: + +```xml + +<dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-commands-springboot</artifactId> + <version>x.x.x</version> + <!-- use the same version as your Camel core version --> +</dependency> + +``` + + http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/pom.xml ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/pom.xml b/platforms/commands/commands-spring-boot/pom.xml new file mode 100755 index 0000000..6904ac1 --- /dev/null +++ b/platforms/commands/commands-spring-boot/pom.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>commands</artifactId> + <version>2.17-SNAPSHOT</version> + </parent> + + <artifactId>camel-commands-springboot</artifactId> + <packaging>jar</packaging> + <name>Camel :: Platforms :: Commands :: Spring Boot</name> + <description>Camel Commands using Spring Boot Shell</description> + <modelVersion>4.0.0</modelVersion> + + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-commands-core</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-remote-shell</artifactId> + <version>${spring-boot-version}</version> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentCamelContext.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentCamelContext.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentCamelContext.java new file mode 100644 index 0000000..226378a --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentCamelContext.java @@ -0,0 +1,31 @@ +/** + * + * 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.springboot.commands.crsh; + +import org.crsh.cli.Argument; +import org.crsh.cli.Man; +import org.crsh.cli.Usage; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +@Usage("Camel context name") +@Man("Camel context name") +@Argument(name = "Camel Context", completer = CamelCompleter.class) +@interface ArgumentCamelContext {} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentRouteID.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentRouteID.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentRouteID.java new file mode 100644 index 0000000..94d61d1 --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/ArgumentRouteID.java @@ -0,0 +1,31 @@ +/** + * + * 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.springboot.commands.crsh; + +import org.crsh.cli.Argument; +import org.crsh.cli.Man; +import org.crsh.cli.Usage; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +@Usage("Route ID") +@Man("Route ID") +@Argument(name = "Route ID", completer = CamelCompleter.class) +@interface ArgumentRouteID {} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsFacade.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsFacade.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsFacade.java new file mode 100644 index 0000000..b73c10d --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsFacade.java @@ -0,0 +1,111 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.springboot.commands.crsh; + +import org.apache.camel.CamelContext; +import org.apache.camel.Route; +import org.apache.camel.commands.AbstractCamelCommand; +import org.apache.camel.commands.AbstractRouteCommand; +import org.apache.camel.commands.LocalCamelController; +import org.apache.camel.commands.StringEscape; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.PrintStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.ArrayList; + +public class CamelCommandsFacade { + + protected final Logger log = LoggerFactory.getLogger(getClass().getName()); + + private LocalCamelController camelController; + private StringEscape stringEscape = new NoopStringEscape(); + + CamelCommandsFacade(LocalCamelController controller) { + this.camelController = controller; + } + + LocalCamelController getCamelController() { + return this.camelController; + } + + public <T extends AbstractCamelCommand> String runCommand(Class<T> clazz, Object... commandArgs) throws Exception { + OutputBuffer buffer = new OutputBuffer(); + PrintStream ops = buffer.getPrintStream(); + + // Trying to infer the camel context if not given + // The order of the varargs for Route Command + // [0] - route id + // [1] - camel context + if (AbstractRouteCommand.class.isAssignableFrom(clazz) && null == commandArgs[1]) { + commandArgs[1] = getCamelContextForRoute((String) commandArgs[0]); + ops.println("Automatically inferred context name : "+commandArgs[1]); + } + + // Finding the right constructor + Class[] types = new Class[commandArgs.length]; + for (int i = 0; i < commandArgs.length; i++) { + types[i] = commandArgs[i].getClass(); + + // Commands require primitives + if (types[i] == Boolean.class) types[i] = boolean.class; + if (types[i] == Integer.class) types[i] = int.class; + } + + // Instantiating an object + Constructor<T> constructor = clazz.getConstructor(types); + T command = constructor.newInstance(commandArgs); + + // Some commands require StringEscape property to be set + try { + Method m = clazz.getMethod("setStringEscape", org.apache.camel.commands.StringEscape.class); + m.invoke(command, stringEscape); + } catch (Exception e) { + } + + // Executing + command.execute(camelController, ops, ops); + return buffer.toString(); + } + + private String getCamelContextForRoute(String routeId) throws Exception { + ArrayList<String> contextNames = new ArrayList<String>(); + + for (CamelContext camelContext : camelController.getLocalCamelContexts()) { + for (Route route : camelContext.getRoutes()) { + if (routeId.equals(route.getId())) { + contextNames.add(camelContext.getName()); + break; + } + } + } + + if (contextNames.size() != 1) { + StringBuffer error = new StringBuffer(); + error.append("Cannot infer Camel Context. Please provide manually."); + + if (contextNames.size() > 1) + error.append(" Contexts : "+contextNames.toString()); + + throw new org.crsh.cli.impl.SyntaxException(error.toString()); + } + + return contextNames.get(0); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsPlugin.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsPlugin.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsPlugin.java new file mode 100644 index 0000000..51e5820 --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCommandsPlugin.java @@ -0,0 +1,56 @@ +/** + * + * 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.springboot.commands.crsh; + +import org.crsh.plugin.CRaSHPlugin; +import org.springframework.beans.factory.ListableBeanFactory; + +public class CamelCommandsPlugin extends CRaSHPlugin<CamelCommandsPlugin> { + + private static CamelCommandsPlugin camelPlugin; + + private static void setCamelPlugin(CamelCommandsPlugin plugin){ + camelPlugin = plugin; + } + + static CamelCommandsPlugin getInstance(){ + return camelPlugin; + } + + private CamelCommandsFacade facade; + + @Override + public CamelCommandsPlugin getImplementation() { + return this; + } + + @Override + public void init() { + ListableBeanFactory beanFactory = (ListableBeanFactory) getContext().getAttributes().get("spring.beanfactory"); + this.facade = new CamelCommandsFacade(new CamelControllerImpl(beanFactory)); + setCamelPlugin(this); + } + + @Override + public void destroy() { + } + + CamelCommandsFacade getCamelCommandsFacade(){ + return this.facade; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCompleter.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCompleter.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCompleter.java new file mode 100644 index 0000000..ffcd78c --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelCompleter.java @@ -0,0 +1,74 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.springboot.commands.crsh; + +import org.apache.camel.CamelContext; +import org.apache.camel.Route; +import org.apache.camel.commands.LocalCamelController; +import org.crsh.cli.descriptor.ParameterDescriptor; +import org.crsh.cli.spi.Completer; +import org.crsh.cli.spi.Completion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.LinkedList; + +public class CamelCompleter implements Completer { + + private LocalCamelController camelController = CamelCommandsPlugin.getInstance().getCamelCommandsFacade().getCamelController(); + private Logger log = LoggerFactory.getLogger(CamelCompleter.class); + + public Completion complete(ParameterDescriptor parameterDescriptor, String prefix) throws Exception { + + + LinkedList<String> values = new LinkedList<String>(); + Completion.Builder builder = new Completion.Builder(prefix); + + if (parameterDescriptor.getAnnotation() instanceof ArgumentCamelContext) { + values.addAll(getContextNames()); + } + + if (parameterDescriptor.getAnnotation() instanceof ArgumentRouteID) { + values.addAll(getRouteIds()); + } + + for (String value : values) { + if (value.startsWith(prefix)) + builder.add(value.substring(prefix.length()), true); + } + + return builder.build(); + } + + private LinkedList<String> getContextNames() throws Exception { + LinkedList<String> values = new LinkedList<String>(); + for (CamelContext camelContext : camelController.getLocalCamelContexts()) { + values.add(camelContext.getName()); + } + return values; + } + + private LinkedList<String> getRouteIds() throws Exception { + LinkedList<String> values = new LinkedList<String>(); + for (CamelContext camelContext : camelController.getLocalCamelContexts()) { + for (Route route : camelContext.getRoutes()) { + values.add(route.getId()); + } + } + return values; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelControllerImpl.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelControllerImpl.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelControllerImpl.java new file mode 100644 index 0000000..3464f9f --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/CamelControllerImpl.java @@ -0,0 +1,83 @@ +/** + * 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.springboot.commands.crsh; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.commands.AbstractLocalCamelController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.ListableBeanFactory; + +public class CamelControllerImpl extends AbstractLocalCamelController { + + private static final Logger LOG = LoggerFactory.getLogger(CamelControllerImpl.class); + + private ListableBeanFactory beanFactory; + + CamelControllerImpl(ListableBeanFactory factory){ + beanFactory = factory; + } + + public List<CamelContext> getLocalCamelContexts() { + List<CamelContext> camelContexts = new ArrayList<CamelContext>(); + try { + camelContexts.addAll( beanFactory.getBeansOfType(CamelContext.class).values() ); + } catch (Exception e) { + LOG.warn("Cannot retrieve the list of Camel contexts.", e); + } + + Collections.sort(camelContexts, new Comparator<CamelContext>() { + public int compare(CamelContext o1, CamelContext o2) { + return o1.getName().compareTo(o2.getName()); + } + }); + + return camelContexts; + } + + public List<Map<String, String>> getCamelContexts() throws Exception { + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + + List<CamelContext> camelContexts = getLocalCamelContexts(); + for (CamelContext camelContext : camelContexts) { + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("name", camelContext.getName()); + row.put("state", camelContext.getStatus().name()); + row.put("uptime", camelContext.getUptime()); + if (camelContext.getManagedCamelContext() != null) { + row.put("exchangesTotal", "" + camelContext.getManagedCamelContext().getExchangesTotal()); + row.put("exchangesInflight", "" + camelContext.getManagedCamelContext().getExchangesInflight()); + row.put("exchangesFailed", "" + camelContext.getManagedCamelContext().getExchangesFailed()); + } else { + row.put("exchangesTotal", "0"); + row.put("exchangesInflight", "0"); + row.put("exchangesFailed", "0"); + } + answer.add(row); + } + + return answer; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/NoopStringEscape.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/NoopStringEscape.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/NoopStringEscape.java new file mode 100644 index 0000000..b46828e --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/NoopStringEscape.java @@ -0,0 +1,34 @@ +/** + * 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.springboot.commands.crsh; + +import org.apache.camel.commands.StringEscape; + +class NoopStringEscape implements StringEscape { + + public String unescapeJava(String s) { + return s; + } + + public String escapeJava(String s) { + return s; + } + + public String hex(char c) { + return Character.toString(c); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/OutputBuffer.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/OutputBuffer.java b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/OutputBuffer.java new file mode 100644 index 0000000..a9b8488 --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/java/org/apache/camel/springboot/commands/crsh/OutputBuffer.java @@ -0,0 +1,47 @@ +/** + * + * 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.springboot.commands.crsh; + +import java.io.*; + +class OutputBuffer { + + private ByteArrayOutputStream baos = new ByteArrayOutputStream(); + private PrintStream ps = new PrintStream(baos); + + public PrintStream getPrintStream(){ + return ps; + } + + @Override + public String toString() { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()))); + StringBuffer sb = new StringBuffer(); + String line = null; + String lineSeparator = System.getProperty("line.separator"); + try { + while ((line = bufferedReader.readLine()) != null) { + sb.append(line); + sb.append(lineSeparator); + } + } catch (IOException e) { + e.printStackTrace(); + } + return sb.toString(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/resources/META-INF/services/org.crsh.plugin.CRaSHPlugin ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/resources/META-INF/services/org.crsh.plugin.CRaSHPlugin b/platforms/commands/commands-spring-boot/src/main/resources/META-INF/services/org.crsh.plugin.CRaSHPlugin new file mode 100755 index 0000000..b3b459a --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/resources/META-INF/services/org.crsh.plugin.CRaSHPlugin @@ -0,0 +1 @@ +org.apache.camel.springboot.commands.crsh.CamelCommandsPlugin \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/15d4be0c/platforms/commands/commands-spring-boot/src/main/resources/crash/commands/camel/camel.groovy ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-spring-boot/src/main/resources/crash/commands/camel/camel.groovy b/platforms/commands/commands-spring-boot/src/main/resources/crash/commands/camel/camel.groovy new file mode 100644 index 0000000..5886f48 --- /dev/null +++ b/platforms/commands/commands-spring-boot/src/main/resources/crash/commands/camel/camel.groovy @@ -0,0 +1,278 @@ +/** + * + * 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 crash.commands.camel + +import org.apache.camel.commands.ComponentListCommand +import org.apache.camel.commands.ContextInflightCommand +import org.apache.camel.commands.ContextInfoCommand +import org.apache.camel.commands.ContextListCommand +import org.apache.camel.commands.ContextResumeCommand +import org.apache.camel.commands.ContextStartCommand +import org.apache.camel.commands.ContextStopCommand +import org.apache.camel.commands.ContextSuspendCommand +import org.apache.camel.commands.EipExplainCommand +import org.apache.camel.commands.EndpointExplainCommand +import org.apache.camel.commands.EndpointListCommand +import org.apache.camel.commands.EndpointStatisticCommand +import org.apache.camel.commands.RestRegistryListCommand +import org.apache.camel.commands.RouteInfoCommand +import org.apache.camel.commands.RouteListCommand +import org.apache.camel.commands.RouteProfileCommand +import org.apache.camel.commands.RouteResetStatsCommand +import org.apache.camel.commands.RouteResumeCommand +import org.apache.camel.commands.RouteShowCommand +import org.apache.camel.commands.RouteStartCommand +import org.apache.camel.commands.RouteStopCommand +import org.apache.camel.commands.RouteSuspendCommand +import org.apache.camel.springboot.commands.crsh.ArgumentCamelContext +import org.apache.camel.springboot.commands.crsh.ArgumentRouteID +import org.apache.camel.springboot.commands.crsh.CamelCommandsFacade +import org.crsh.cli.Argument +import org.crsh.cli.Command +import org.crsh.cli.Option +import org.crsh.cli.Usage +import org.crsh.cli.Named +import org.crsh.cli.Required +import org.apache.camel.springboot.commands.crsh.CamelCommandsPlugin +import org.crsh.groovy.GroovyCommand + +@Usage("Camel related commands") +public class camel extends GroovyCommand { + + private CamelCommandsFacade getCommandsFacade() { + return context.session["crash"]. + context.getPlugin(CamelCommandsPlugin.class).getCamelCommandsFacade() + } + // =============================== + // Components and EIP + // =============================== + @Command + @Usage("Lists all Camel components available in the context.") + @Named("component-list") + public String component_list(@Required @ArgumentCamelContext String camelContext, + @Usage("Verbose output") + @Option(names = ["v", "verbose"]) Boolean verbose) { + Boolean v = (null != verbose && Boolean.valueOf(verbose)) + return getCommandsFacade().runCommand(ComponentListCommand.class, camelContext, v) + } + + @Command + @Usage("Explains the EIP in the Camel context.") + @Named("eip-explain") + public String eip_explain(@Required @ArgumentCamelContext String camelContext, + @Required @Argument String nameOrId, + @Usage("Verbose output") + @Option(names = ["v", "verbose"]) Boolean verbose) { + Boolean v = (null != verbose && Boolean.valueOf(verbose)) + return getCommandsFacade().runCommand(EipExplainCommand.class, camelContext, nameOrId, v) + } + + // =============================== + // Context + // =============================== + @Command + @Usage("Lists all Camel contexts.") + @Named("context-list") + public String context_list() { + return getCommandsFacade().runCommand(ContextListCommand.class) + } + + @Command + @Usage("Displays detailed information about the Camel context.") + @Named("context-info") + public String context_info(@Required @ArgumentCamelContext String camelContext, + @Usage("Verbose output") + @Option(names = ["v", "verbose"]) Boolean verbose) { + Boolean v = (null != verbose && Boolean.valueOf(verbose)) + return getCommandsFacade().runCommand(ContextInfoCommand.class, camelContext, v) + } + + @Command + @Usage("Displays detailed information about the Camel context.") + @Named("context-inflight") + public String context_inflight(@Required @ArgumentCamelContext String camelContext, + @Usage("Sort by longest duration") + @Option(names = ["s", "sort"]) Boolean sort, + @Usage("Limit output to number of messages") + @Option(names = ["l", "limit"]) Integer limit) { + + Boolean _sort = (null != sort && Boolean.valueOf(sort)) + Integer _limit = null != limit ? limit : -1; + + if (_limit != -1) + out.println("Limiting output to "+_limit+" messages.") + + return getCommandsFacade().runCommand(ContextInflightCommand.class, camelContext, _limit, _sort) + } + + @Command + @Usage("Starts the Camel context.") + @Named("context-start") + public String context_start(@Required @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(ContextStartCommand.class, camelContext) + } + + @Command + @Usage("Stops the Camel context.") + @Named("context-stop") + public String context_stop(@Required @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(ContextStopCommand.class, camelContext) + } + + @Command + @Usage("Suspends the Camel context.") + @Named("context-suspend") + public String context_suspend(@Required @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(ContextSuspendCommand.class, camelContext) + } + + @Command + @Usage("Resumes the Camel context.") + @Named("context-resume") + public String context_resume(@Required @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(ContextResumeCommand.class, camelContext) + } + + // =============================== + // Endpoints + // =============================== + @Command + @Usage("Lists Camel endpoints.") + @Named("endpoint-list") + public String endpoint_list(@Required @ArgumentCamelContext String camelContext, + @Usage("Decode URI so they are human readable") + @Option(names = ["d", "decode"]) Boolean decode, + @Usage("Verbose output") + @Option(names = ["v", "verbose"]) Boolean verbose, + @Usage("Explain the endpoint options") + @Option(names = ["e", "explain"]) Boolean explain) { + Boolean _verbose = (null != verbose && Boolean.valueOf(verbose)) + Boolean _decode = (null != decode && Boolean.valueOf(decode)) + Boolean _explain = (null != explain && Boolean.valueOf(explain)) + return getCommandsFacade().runCommand(EndpointListCommand.class, camelContext, _decode, _verbose, _explain); + } + + @Command + @Usage("Explain all Camel endpoints available in the CamelContext.") + @Named("endpoint-explain") + public String endpoint_explain(@Required @ArgumentCamelContext String camelContext, + @Usage("Verbose output") + @Option(names = ["v", "verbose"]) Boolean verbose, + @Usage("Filter endpoint by pattern") + @Option(names = ["f", "filter"]) String filter) { + Boolean _verbose = (null != verbose && Boolean.valueOf(verbose)) + String _filter = null != filter ? filter : "*"; + return getCommandsFacade().runCommand(EndpointExplainCommand.class, camelContext, _verbose, _filter); + } + + @Command + @Usage("Explain all Camel endpoints available in the CamelContext.") + @Named("endpoint-stats") + public String endpoint_stats(@Required @ArgumentCamelContext String camelContext, + @Usage("Decode URI so they are human readable") + @Option(names = ["d", "decode"]) Boolean decode, + @Usage("Filter the list by in,out,static,dynamic") + @Option(names = ["f", "filter"]) String filter) { + Boolean _decode = (null != decode && Boolean.valueOf(decode)) + String[] _filter = null != filter ? filter.split(",") : ["in","out","static","dynamic"]; + return getCommandsFacade().runCommand(EndpointStatisticCommand.class, camelContext, _decode, _filter); + } + + @Command + @Usage("Lists all Camel REST services enlisted in the Rest Registry from a CamelContext.") + @Named("rest-registry-list") + public String rest_registry_list(@Required @ArgumentCamelContext String camelContext, + @Usage("Decode URI so they are human readable") + @Option(names = ["d", "decode"]) Boolean decode, + @Usage("Verbose output") + @Option(names = ["v", "verbose"]) Boolean verbose){ + Boolean _verbose = (null != verbose && Boolean.valueOf(verbose)) + Boolean _decode = (null != decode && Boolean.valueOf(decode)) + return getCommandsFacade().runCommand(RestRegistryListCommand.class, camelContext, _decode, _verbose); + } + + // =============================== + // Route + // =============================== + @Command + @Usage("Lists Camel routes for the given Camel context.") + @Named("route-list") + public String route_list(@Required @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteListCommand.class, camelContext) + } + + @Command + @Usage("Displays information about a Camel route") + @Named("route-info") + public String route_info(@Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteInfoCommand.class, route, camelContext) + } + + @Command + @Usage("Displays Camel route profile") + @Named("route-profile") + public String route_profile(@Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteProfileCommand.class, route, camelContext) + } + + @Command + @Usage("Display the Camel route definition in XML.") + @Named("route-show") + public String route_show(@Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteShowCommand.class, route, camelContext) + } + + @Command + @Usage("Resets route performance stats.") + @Named("route-reset-stats") + public String route_reset_stats( + @Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteResetStatsCommand.class, route, camelContext) + } + + @Command + @Usage("Resumes the route operation.") + @Named("route-resume") + public String route_resume(@Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteResumeCommand.class, route, camelContext) + } + + @Command + @Usage("Suspends the route operation.") + @Named("route-suspend") + public String route_suspend(@Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteSuspendCommand.class, route, camelContext) + } + + @Command + @Usage("Stops the route operation.") + @Named("route-stop") + public String route_stop(@Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteStopCommand.class, route, camelContext) + } + + @Command + @Usage("Starts the route operation.") + @Named("route-start") + public String route_start(@Required @ArgumentRouteID String route, @ArgumentCamelContext String camelContext) { + return getCommandsFacade().runCommand(RouteStartCommand.class, route, camelContext) + } + + +} +