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/7791d1f3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7791d1f3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7791d1f3

Branch: refs/heads/master
Commit: 7791d1f3686e623047278ab0964246d839241354
Parents: 55d9d82
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Nov 13 08:06:41 2014 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Nov 13 08:06:41 2014 +0100

----------------------------------------------------------------------
 parent/pom.xml                                  |   2 +-
 platforms/commands/commands-core/pom.xml        |   2 +-
 .../camel/commands/AbstractContextCommand.java  |  14 +-
 .../camel/commands/AbstractRouteCommand.java    |  20 +-
 .../camel/commands/ContextInfoCommand.java      | 268 +++++++++++++++++++
 .../camel/commands/ContextListCommand.java      |   5 +-
 .../org/apache/camel/commands/StringEscape.java |  29 ++
 platforms/karaf/commands/pom.xml                |   6 +-
 .../camel/karaf/commands/ContextInfo.java       | 228 +---------------
 .../karaf/commands/internal/StringEscape.java   |  37 +++
 .../OSGI-INF/blueprint/camel-commands.xml       |   3 +
 .../features/src/main/resources/features.xml    |   2 +-
 12 files changed, 372 insertions(+), 244 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/parent/pom.xml
----------------------------------------------------------------------
diff --git a/parent/pom.xml b/parent/pom.xml
index cc66de3..9153c05 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -1489,7 +1489,7 @@
       <!-- camel commands -->
       <dependency>
         <groupId>org.apache.camel</groupId>
-        <artifactId>commands-core</artifactId>
+        <artifactId>camel-commands-core</artifactId>
         <version>${project.version}</version>
       </dependency>      
       <dependency>

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/commands/commands-core/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/commands/commands-core/pom.xml 
b/platforms/commands/commands-core/pom.xml
index 817e769..cf6af5f 100644
--- a/platforms/commands/commands-core/pom.xml
+++ b/platforms/commands/commands-core/pom.xml
@@ -26,7 +26,7 @@
     <version>2.15-SNAPSHOT</version>
   </parent>
 
-  <artifactId>commands-core</artifactId>
+  <artifactId>camel-commands-core</artifactId>
   <packaging>bundle</packaging>
   <name>Camel :: Platforms :: Commands :: Core</name>
   <description>Core Camel Commands</description>

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/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 7e54b72..b796c65 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
@@ -19,24 +19,26 @@ package org.apache.camel.commands;
 import java.io.PrintStream;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.spi.Required;
 
 /**
  * Abstract command for working with a single {@link 
org.apache.camel.CamelContext}
  */
 public abstract class AbstractContextCommand extends AbstractCamelCommand {
 
+    private String context;
+
     /**
-     * The name of the Camel context.
+     * @param context The name of the Camel context.
      */
-    @Required
-    public abstract String getContext();
+    protected AbstractContextCommand(String route, String context) {
+        this.context = context;
+    }
 
     @Override
     public Object execute(CamelController camelController, PrintStream out, 
PrintStream err) throws Exception {
-        CamelContext camelContext = 
camelController.getCamelContext(getContext());
+        CamelContext camelContext = camelController.getCamelContext(context);
         if (camelContext == null) {
-            err.println("Camel context " + getContext() + " not found.");
+            err.println("Camel context " + context + " not found.");
             return null;
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/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 0a8a71d..678f175 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
@@ -24,7 +24,6 @@ import java.util.List;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Route;
 import org.apache.camel.commands.internal.RegexUtil;
-import org.apache.camel.spi.Required;
 
 import static org.apache.camel.util.CamelContextHelper.getRouteStartupOrder;
 
@@ -33,21 +32,22 @@ import static 
org.apache.camel.util.CamelContextHelper.getRouteStartupOrder;
  */
 public abstract class AbstractRouteCommand extends AbstractCamelCommand {
 
-    /**
-     * The Camel route ID or a wildcard expression
-     */
-    @Required
-    public abstract String getRoute();
+    private String route;
+    private String context;
 
     /**
-     * The name of the Camel context.
+     * @param route The Camel route ID or a wildcard expression
+     * @param context The name of the Camel context.
      */
-    public abstract String getContext();
+    protected AbstractRouteCommand(String route, String context) {
+        this.route = route;
+        this.context = context;
+    }
 
     public Object execute(CamelController camelController, PrintStream out, 
PrintStream err) throws Exception {
-        List<Route> camelRoutes = camelController.getRoutes(getContext(), 
RegexUtil.wildcardAsRegex(getRoute()));
+        List<Route> camelRoutes = camelController.getRoutes(context, 
RegexUtil.wildcardAsRegex(route));
         if (camelRoutes == null || camelRoutes.isEmpty()) {
-            err.println("Camel routes using " + getRoute() + " not found.");
+            err.println("Camel routes using " + route + " not found.");
             return null;
         }
         // we want the routes sorted

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInfoCommand.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInfoCommand.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInfoCommand.java
new file mode 100644
index 0000000..299d7f5
--- /dev/null
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInfoCommand.java
@@ -0,0 +1,268 @@
+/**
+ * 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.io.PrintStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Route;
+import org.apache.camel.spi.ManagementAgent;
+
+import static org.apache.camel.util.UnitUtils.printUnitFromBytes;
+
+/**
+ * Command to display detailed information about a given {@link 
org.apache.camel.CamelContext}.
+ */
+public class ContextInfoCommand extends AbstractCamelCommand {
+
+    private StringEscape stringEscape;
+    private String name;
+    private String mode;
+
+    /**
+     * @param name The name of the Camel context
+     * @param mode Allows for different display modes (--verbose, etc)
+     */
+    public ContextInfoCommand(String name, String mode) {
+        this.name = name;
+        this.mode = mode;
+    }
+
+    /**
+     * Sets the {@link org.apache.camel.commands.StringEscape} to use.
+     */
+    public void setStringEscape(StringEscape stringEscape) {
+        this.stringEscape = stringEscape;
+    }
+
+    @Override
+    public Object execute(CamelController camelController, PrintStream out, 
PrintStream err) throws Exception {
+        CamelContext camelContext = camelController.getCamelContext(name);
+
+        if (camelContext == null) {
+            err.println("Camel context " + name + " not found.");
+            return null;
+        }
+
+        out.println(stringEscape.unescapeJava("\u001B[1m\u001B[33mCamel 
Context " + name + "\u001B[0m"));
+        out.println(stringEscape.unescapeJava("\tName: " + 
camelContext.getName()));
+        out.println(stringEscape.unescapeJava("\tManagementName: " + 
camelContext.getManagementName()));
+        out.println(stringEscape.unescapeJava("\tVersion: " + 
camelContext.getVersion()));
+        out.println(stringEscape.unescapeJava("\tStatus: " + 
camelContext.getStatus()));
+        out.println(stringEscape.unescapeJava("\tUptime: " + 
camelContext.getUptime()));
+
+        // the statistics are in the mbeans
+        printCamelManagedBeansStatus(camelContext, out);
+
+        out.println("");
+        
out.println(stringEscape.unescapeJava("\u001B[1mMiscellaneous\u001B[0m"));
+        out.println(stringEscape.unescapeJava("\tAuto Startup: " + 
camelContext.isAutoStartup()));
+        out.println(stringEscape.unescapeJava("\tStarting Routes: " + 
camelContext.isStartingRoutes()));
+        out.println(stringEscape.unescapeJava("\tSuspended: " + 
camelContext.isSuspended()));
+        out.println(stringEscape.unescapeJava("\tShutdown timeout: "
+                + 
camelContext.getShutdownStrategy().getTimeUnit().toSeconds(camelContext.getShutdownStrategy().getTimeout())
 + " sec."));
+        out.println(stringEscape.unescapeJava("\tAllow UseOriginalMessage: " + 
camelContext.isAllowUseOriginalMessage()));
+        out.println(stringEscape.unescapeJava("\tMessage History: " + 
camelContext.isMessageHistory()));
+        out.println(stringEscape.unescapeJava("\tTracing: " + 
camelContext.isTracing()));
+        out.println("");
+        out.println(stringEscape.unescapeJava("\u001B[1mProperties\u001B[0m"));
+        for (Map.Entry<String, String> entry : 
camelContext.getProperties().entrySet()) {
+            out.println(stringEscape.unescapeJava("\t" + entry.getKey() + " = 
" + entry.getValue()));
+        }
+
+        out.println("");
+        out.println(stringEscape.unescapeJava("\u001B[1mAdvanced\u001B[0m"));
+        out.println(stringEscape.unescapeJava("\tClassResolver: " + 
camelContext.getClassResolver()));
+        out.println(stringEscape.unescapeJava("\tPackageScanClassResolver: " + 
camelContext.getPackageScanClassResolver()));
+        
out.println(stringEscape.unescapeJava("\tApplicationContextClassLoader: " + 
camelContext.getApplicationContextClassLoader()));
+
+        out.println("");
+        out.println(stringEscape.unescapeJava("\u001B[1mComponents\u001B[0m"));
+        for (String component : camelContext.getComponentNames()) {
+            System.out.println(stringEscape.unescapeJava("\t" + component));
+        }
+
+        out.println("");
+        
out.println(stringEscape.unescapeJava("\u001B[1mDataformats\u001B[0m"));
+        for (String name : camelContext.getDataFormats().keySet()) {
+            out.println(stringEscape.unescapeJava("\t" + name));
+        }
+
+        if (mode != null && mode.equals("--verbose")) {
+            out.println("");
+            
out.println(stringEscape.unescapeJava("\u001B[1mLanguages\u001B[0m"));
+            for (String language : camelContext.getLanguageNames()) {
+                out.println(stringEscape.unescapeJava("\t" + language));
+            }
+
+            out.println("");
+            
out.println(stringEscape.unescapeJava("\u001B[1mEndpoints\u001B[0m"));
+            for (Endpoint endpoint : camelContext.getEndpoints()) {
+                out.println(stringEscape.unescapeJava("\t" + 
endpoint.getEndpointUri()));
+            }
+        }
+
+        out.println("");
+        out.println(stringEscape.unescapeJava("\u001B[1mRoutes\u001B[0m"));
+        for (Route route : camelContext.getRoutes()) {
+            out.println(stringEscape.unescapeJava("\t" + route.getId()));
+        }
+
+        return null;
+    }
+
+    protected void printCamelManagedBeansStatus(CamelContext camelContext, 
PrintStream out) throws Exception {
+        // the statistics are in the mbeans
+        out.println("");
+        out.println(stringEscape.unescapeJava("\u001B[1mStatistics\u001B[0m"));
+        ObjectName contextMBean = null;
+        ManagementAgent agent = 
camelContext.getManagementStrategy().getManagementAgent();
+        if (agent != null) {
+            MBeanServer mBeanServer = agent.getMBeanServer();
+
+            Set<ObjectName> set = mBeanServer.queryNames(new 
ObjectName(agent.getMBeanObjectDomainName() + ":type=context,name=\"" + name + 
"\",*"), null);
+            Iterator<ObjectName> iterator = set.iterator();
+            if (iterator.hasNext()) {
+                contextMBean = iterator.next();
+            }
+
+            if (mBeanServer.isRegistered(contextMBean)) {
+                Long exchangesTotal = (Long) 
mBeanServer.getAttribute(contextMBean, "ExchangesTotal");
+                out.println(stringEscape.unescapeJava("\tExchanges Total: " + 
exchangesTotal));
+                Long exchangesCompleted = (Long) 
mBeanServer.getAttribute(contextMBean, "ExchangesCompleted");
+                out.println(stringEscape.unescapeJava("\tExchanges Completed: 
" + exchangesCompleted));
+                Long exchangesFailed = (Long) 
mBeanServer.getAttribute(contextMBean, "ExchangesFailed");
+                out.println(stringEscape.unescapeJava("\tExchanges Failed: " + 
exchangesFailed));
+                Long minProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "MinProcessingTime");
+                out.println(stringEscape.unescapeJava("\tMin Processing Time: 
" + minProcessingTime + "ms"));
+                Long maxProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "MaxProcessingTime");
+                out.println(stringEscape.unescapeJava("\tMax Processing Time: 
" + maxProcessingTime + "ms"));
+                Long meanProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "MeanProcessingTime");
+                out.println(stringEscape.unescapeJava("\tMean Processing Time: 
" + meanProcessingTime + "ms"));
+                Long totalProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "TotalProcessingTime");
+                out.println(stringEscape.unescapeJava("\tTotal Processing 
Time: " + totalProcessingTime + "ms"));
+                Long lastProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "LastProcessingTime");
+                out.println(stringEscape.unescapeJava("\tLast Processing Time: 
" + lastProcessingTime + "ms"));
+                Long deltaProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "DeltaProcessingTime");
+                out.println(stringEscape.unescapeJava("\tDelta Processing 
Time: " + deltaProcessingTime + "ms"));
+
+                String load01 = (String) 
mBeanServer.getAttribute(contextMBean, "Load01");
+                String load05 = (String) 
mBeanServer.getAttribute(contextMBean, "Load05");
+                String load15 = (String) 
mBeanServer.getAttribute(contextMBean, "Load15");
+                out.println(stringEscape.unescapeJava("\tLoad Avg: " + load01 
+ ", " + load05 + ", " + load15));
+
+                // Test for null to see if a any exchanges have been processed 
first to avoid NPE
+                Object resetTimestampObj = 
mBeanServer.getAttribute(contextMBean, "ResetTimestamp");
+                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss");
+                if (resetTimestampObj == null) {
+                    // Print an empty value for scripting
+                    out.println(stringEscape.unescapeJava("\tReset Statistics 
Date:"));
+                } else {
+                    Date firstExchangeTimestamp = (Date) resetTimestampObj;
+                    out.println(stringEscape.unescapeJava("\tReset Statistics 
Date: " + format.format(firstExchangeTimestamp)));
+                }
+
+                // Test for null to see if a any exchanges have been processed 
first to avoid NPE
+                Object firstExchangeTimestampObj = 
mBeanServer.getAttribute(contextMBean, "FirstExchangeCompletedTimestamp");
+                if (firstExchangeTimestampObj == null) {
+                    // Print an empty value for scripting
+                    out.println(stringEscape.unescapeJava("\tFirst Exchange 
Date:"));
+                } else {
+                    Date firstExchangeTimestamp = (Date) 
firstExchangeTimestampObj;
+                    out.println(stringEscape.unescapeJava("\tFirst Exchange 
Date: " + format.format(firstExchangeTimestamp)));
+                }
+
+                // Again, check for null to avoid NPE
+                Object lastExchangeCompletedTimestampObj = 
mBeanServer.getAttribute(contextMBean, "LastExchangeCompletedTimestamp");
+                if (lastExchangeCompletedTimestampObj == null) {
+                    // Print an empty value for scripting
+                    out.println(stringEscape.unescapeJava("\tLast Exchange 
Completed Date:"));
+                } else {
+                    Date lastExchangeCompletedTimestamp = (Date) 
lastExchangeCompletedTimestampObj;
+                    out.println(stringEscape.unescapeJava("\tLast Exchange 
Completed Date: " + format.format(lastExchangeCompletedTimestamp)));
+                }
+
+                // add type converter statistics if enabled
+                if 
(camelContext.getTypeConverterRegistry().getStatistics().isStatisticsEnabled()) 
{
+                    
out.println(stringEscape.unescapeJava(String.format("\tTypeConverterRegistry 
utilization: [attempts=%s, hits=%s, misses=%s, failures=%s]",
+                            
camelContext.getTypeConverterRegistry().getStatistics().getAttemptCounter(),
+                            
camelContext.getTypeConverterRegistry().getStatistics().getHitCounter(),
+                            
camelContext.getTypeConverterRegistry().getStatistics().getMissCounter(),
+                            
camelContext.getTypeConverterRegistry().getStatistics().getFailedCounter())));
+                }
+
+                // add stream caching details if enabled
+                if (camelContext.getStreamCachingStrategy().isEnabled()) {
+                    out.println(stringEscape.unescapeJava(
+                            String.format("\tStreamCachingStrategy: 
[spoolDirectory=%s, spoolChiper=%s, spoolThreshold=%s, 
spoolUsedHeapMemoryThreshold=%s, "
+                                            + "spoolUsedHeapMemoryLimit=%s, 
anySpoolRules=%s, bufferSize=%s, removeSpoolDirectoryWhenStopping=%s, 
statisticsEnabled=%s]",
+                                    
camelContext.getStreamCachingStrategy().getSpoolDirectory(),
+                                    
camelContext.getStreamCachingStrategy().getSpoolChiper(),
+                                    
camelContext.getStreamCachingStrategy().getSpoolThreshold(),
+                                    
camelContext.getStreamCachingStrategy().getSpoolUsedHeapMemoryThreshold(),
+                                    
camelContext.getStreamCachingStrategy().getSpoolUsedHeapMemoryLimit(),
+                                    
camelContext.getStreamCachingStrategy().isAnySpoolRules(),
+                                    
camelContext.getStreamCachingStrategy().getBufferSize(),
+                                    
camelContext.getStreamCachingStrategy().isRemoveSpoolDirectoryWhenStopping(),
+                                    
camelContext.getStreamCachingStrategy().getStatistics().isStatisticsEnabled())));
+
+                    if 
(camelContext.getStreamCachingStrategy().getStatistics().isStatisticsEnabled()) 
{
+                        out.println(stringEscape.unescapeJava(
+                                String.format("\t                       
[cacheMemoryCounter=%s, cacheMemorySize=%s, cacheMemoryAverageSize=%s, 
cacheSpoolCounter=%s, "
+                                                + "cacheSpoolSize=%s, 
cacheSpoolAverageSize=%s]",
+                                        
camelContext.getStreamCachingStrategy().getStatistics().getCacheMemoryCounter(),
+                                        
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheMemorySize()),
+                                        
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheMemoryAverageSize()),
+                                        
camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolCounter(),
+                                        
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolSize()),
+                                        
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolAverageSize()))));
+                    }
+                }
+
+                long activeRoutes = 0;
+                long inactiveRoutes = 0;
+                List<Route> routeList = camelContext.getRoutes();
+                for (Route route : routeList) {
+                    if 
(camelContext.getRouteStatus(route.getId()).isStarted()) {
+                        activeRoutes++;
+                    } else {
+                        inactiveRoutes++;
+                    }
+                }
+
+                out.println(stringEscape.unescapeJava("\tNumber of running 
routes: " + activeRoutes));
+                out.println(stringEscape.unescapeJava("\tNumber of not running 
routes: " + inactiveRoutes));
+            }
+
+        } else {
+            out.println("");
+            out.println(stringEscape.unescapeJava("\u001B[31mJMX Agent of 
Camel is not reachable. Maybe it has been disabled on the Camel context"));
+            out.println(stringEscape.unescapeJava("In consequence, some 
statistics are not available.\u001B[0m"));
+        }
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextListCommand.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextListCommand.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextListCommand.java
index 0cfe008..3c1ab83 100644
--- 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextListCommand.java
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextListCommand.java
@@ -23,6 +23,9 @@ import java.util.Map;
 
 import org.apache.camel.CamelContext;
 
+/**
+ * Command to list all {@link org.apache.camel.CamelContext} in the JVM.
+ */
 public class ContextListCommand extends AbstractCamelCommand {
 
     private static final String CONTEXT_COLUMN_LABEL = "Context";
@@ -45,8 +48,6 @@ public class ContextListCommand extends AbstractCamelCommand {
         final String headerFormat = buildFormatString(columnWidths, true);
         final String rowFormat = buildFormatString(columnWidths, false);
 
-        out.println("Hello from commands-core");
-
         if (camelContexts.size() > 0) {
             out.println(String.format(headerFormat, CONTEXT_COLUMN_LABEL, 
STATUS_COLUMN_LABEL, UPTIME_COLUMN_LABEL));
             out.println(String.format(headerFormat, "-------", "------", 
"------"));

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/StringEscape.java
----------------------------------------------------------------------
diff --git 
a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/StringEscape.java
 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/StringEscape.java
new file mode 100644
index 0000000..b92b8fd
--- /dev/null
+++ 
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/StringEscape.java
@@ -0,0 +1,29 @@
+/**
+ * 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;
+
+/**
+ * To escape strings.
+ */
+public interface StringEscape {
+
+    String unescapeJava(String str);
+
+    String escapeJava(String str);
+
+    String hex(char ch);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/karaf/commands/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/karaf/commands/pom.xml b/platforms/karaf/commands/pom.xml
index 11aaa26..775b9a3 100644
--- a/platforms/karaf/commands/pom.xml
+++ b/platforms/karaf/commands/pom.xml
@@ -39,11 +39,7 @@
     <!-- camel -->
     <dependency>
       <groupId>org.apache.camel</groupId>
-      <artifactId>camel-core</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>commands-core</artifactId>
+      <artifactId>camel-commands-core</artifactId>
     </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
index d82a8fa..e790e4c 100644
--- 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
@@ -16,24 +16,10 @@
  */
 package org.apache.camel.karaf.commands;
 
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.Endpoint;
-import org.apache.camel.Route;
-import org.apache.camel.spi.ManagementAgent;
+import org.apache.camel.commands.ContextInfoCommand;
+import org.apache.camel.commands.StringEscape;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
-import org.apache.karaf.util.StringEscapeUtils;
-
-import static org.apache.camel.util.UnitUtils.printUnitFromBytes;
 
 /**
  * Command to display detailed information about a Camel context.
@@ -47,210 +33,16 @@ public class ContextInfo extends CamelCommandSupport {
     @Argument(index = 1, name = "mode", description = "Allows for different 
display modes (--verbose, etc)", required = false, multiValued = false)
     String mode;
 
-    public Object doExecute() throws Exception {
-        CamelContext camelContext = camelController.getCamelContext(name);
-
-        if (camelContext == null) {
-            System.err.println("Camel context " + name + " not found.");
-            return null;
-        }
-
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1m\u001B[33mCamel 
Context " + name + "\u001B[0m"));
-        System.out.println(StringEscapeUtils.unescapeJava("\tName: " + 
camelContext.getName()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tManagementName: " 
+ camelContext.getManagementName()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tVersion: " + 
camelContext.getVersion()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tStatus: " + 
camelContext.getStatus()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tUptime: " + 
camelContext.getUptime()));
-
-        // the statistics are in the mbeans
-        printCamelManagedBeansStatus(camelContext);
-
-        System.out.println("");
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mMiscellaneous\u001B[0m"));
-        System.out.println(StringEscapeUtils.unescapeJava("\tAuto Startup: " + 
camelContext.isAutoStartup()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tStarting Routes: 
" + camelContext.isStartingRoutes()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tSuspended: " + 
camelContext.isSuspended()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tShutdown timeout: 
"
-                + 
camelContext.getShutdownStrategy().getTimeUnit().toSeconds(camelContext.getShutdownStrategy().getTimeout())
 + " sec."));
-        System.out.println(StringEscapeUtils.unescapeJava("\tAllow 
UseOriginalMessage: " + camelContext.isAllowUseOriginalMessage()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tMessage History: 
" + camelContext.isMessageHistory()));
-        System.out.println(StringEscapeUtils.unescapeJava("\tTracing: " + 
camelContext.isTracing()));
-        System.out.println("");
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mProperties\u001B[0m"));
-        for (Map.Entry<String, String> entry : 
camelContext.getProperties().entrySet()) {
-            System.out.println(StringEscapeUtils.unescapeJava("\t" + 
entry.getKey() + " = " + entry.getValue()));
-        }
-
-        System.out.println("");
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mAdvanced\u001B[0m"));
-        System.out.println(StringEscapeUtils.unescapeJava("\tClassResolver: " 
+ camelContext.getClassResolver()));
-        
System.out.println(StringEscapeUtils.unescapeJava("\tPackageScanClassResolver: 
" + camelContext.getPackageScanClassResolver()));
-        
System.out.println(StringEscapeUtils.unescapeJava("\tApplicationContextClassLoader:
 " + camelContext.getApplicationContextClassLoader()));
+    private StringEscape stringEscape;
 
-        System.out.println("");
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mComponents\u001B[0m"));
-        for (String component : camelContext.getComponentNames()) {
-            System.out.println(StringEscapeUtils.unescapeJava("\t" + 
component));
-        }
-
-        System.out.println("");
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mDataformats\u001B[0m"));
-        for (String name : camelContext.getDataFormats().keySet()) {
-            System.out.println(StringEscapeUtils.unescapeJava("\t" + name));
-        }
-
-        if (mode != null && mode.equals("--verbose")) {
-            System.out.println("");
-            
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mLanguages\u001B[0m"));
-            for (String language : camelContext.getLanguageNames()) {
-                System.out.println(StringEscapeUtils.unescapeJava("\t" + 
language));
-            }
-
-            System.out.println("");
-            
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mEndpoints\u001B[0m"));
-            for (Endpoint endpoint : camelContext.getEndpoints()) {
-                System.out.println(StringEscapeUtils.unescapeJava("\t" + 
endpoint.getEndpointUri()));
-            }
-        }
-
-        System.out.println("");
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mRoutes\u001B[0m"));
-        for (Route route : camelContext.getRoutes()) {
-            System.out.println(StringEscapeUtils.unescapeJava("\t" + 
route.getId()));
-        }
-
-        return null;
+    public void setStringEscape(StringEscape stringEscape) {
+        this.stringEscape = stringEscape;
     }
 
-    protected void printCamelManagedBeansStatus(CamelContext camelContext) 
throws Exception {
-        // the statistics are in the mbeans
-        System.out.println("");
-        
System.out.println(StringEscapeUtils.unescapeJava("\u001B[1mStatistics\u001B[0m"));
-        ObjectName contextMBean = null;
-        ManagementAgent agent = 
camelContext.getManagementStrategy().getManagementAgent();
-        if (agent != null) {
-            MBeanServer mBeanServer = agent.getMBeanServer();
-
-            Set<ObjectName> set = mBeanServer.queryNames(new 
ObjectName(agent.getMBeanObjectDomainName() + ":type=context,name=\"" + name + 
"\",*"), null);
-            Iterator<ObjectName> iterator = set.iterator();
-            if (iterator.hasNext()) {
-                contextMBean = iterator.next();
-            }
-
-            if (mBeanServer.isRegistered(contextMBean)) {
-                Long exchangesTotal = (Long) 
mBeanServer.getAttribute(contextMBean, "ExchangesTotal");
-                System.out.println(StringEscapeUtils.unescapeJava("\tExchanges 
Total: " + exchangesTotal));
-                Long exchangesCompleted = (Long) 
mBeanServer.getAttribute(contextMBean, "ExchangesCompleted");
-                System.out.println(StringEscapeUtils.unescapeJava("\tExchanges 
Completed: " + exchangesCompleted));
-                Long exchangesFailed = (Long) 
mBeanServer.getAttribute(contextMBean, "ExchangesFailed");
-                System.out.println(StringEscapeUtils.unescapeJava("\tExchanges 
Failed: " + exchangesFailed));
-                Long minProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "MinProcessingTime");
-                System.out.println(StringEscapeUtils.unescapeJava("\tMin 
Processing Time: " + minProcessingTime + "ms"));
-                Long maxProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "MaxProcessingTime");
-                System.out.println(StringEscapeUtils.unescapeJava("\tMax 
Processing Time: " + maxProcessingTime + "ms"));
-                Long meanProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "MeanProcessingTime");
-                System.out.println(StringEscapeUtils.unescapeJava("\tMean 
Processing Time: " + meanProcessingTime + "ms"));
-                Long totalProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "TotalProcessingTime");
-                System.out.println(StringEscapeUtils.unescapeJava("\tTotal 
Processing Time: " + totalProcessingTime + "ms"));
-                Long lastProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "LastProcessingTime");
-                System.out.println(StringEscapeUtils.unescapeJava("\tLast 
Processing Time: " + lastProcessingTime + "ms"));
-                Long deltaProcessingTime = (Long) 
mBeanServer.getAttribute(contextMBean, "DeltaProcessingTime");
-                System.out.println(StringEscapeUtils.unescapeJava("\tDelta 
Processing Time: " + deltaProcessingTime + "ms"));
-
-                String load01 = (String) 
mBeanServer.getAttribute(contextMBean, "Load01");
-                String load05 = (String) 
mBeanServer.getAttribute(contextMBean, "Load05");
-                String load15 = (String) 
mBeanServer.getAttribute(contextMBean, "Load15");
-                System.out.println(StringEscapeUtils.unescapeJava("\tLoad Avg: 
" + load01 + ", " + load05 + ", " + load15));
-
-                // Test for null to see if a any exchanges have been processed 
first to avoid NPE
-                Object resetTimestampObj = 
mBeanServer.getAttribute(contextMBean, "ResetTimestamp");
-                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss");
-                if (resetTimestampObj == null) {
-                    // Print an empty value for scripting
-                    System.out.println(StringEscapeUtils.unescapeJava("\tReset 
Statistics Date:"));
-                } else {
-                    Date firstExchangeTimestamp = (Date) resetTimestampObj;
-                    System.out.println(StringEscapeUtils.unescapeJava("\tReset 
Statistics Date: " + format.format(firstExchangeTimestamp)));
-                }
-
-                // Test for null to see if a any exchanges have been processed 
first to avoid NPE
-                Object firstExchangeTimestampObj = 
mBeanServer.getAttribute(contextMBean, "FirstExchangeCompletedTimestamp");
-                if (firstExchangeTimestampObj == null) {
-                    // Print an empty value for scripting
-                    System.out.println(StringEscapeUtils.unescapeJava("\tFirst 
Exchange Date:"));
-                } else {
-                    Date firstExchangeTimestamp = (Date) 
firstExchangeTimestampObj;
-                    System.out.println(StringEscapeUtils.unescapeJava("\tFirst 
Exchange Date: " + format.format(firstExchangeTimestamp)));
-                }
-
-                // Again, check for null to avoid NPE
-                Object lastExchangeCompletedTimestampObj = 
mBeanServer.getAttribute(contextMBean, "LastExchangeCompletedTimestamp");
-                if (lastExchangeCompletedTimestampObj == null) {
-                    // Print an empty value for scripting
-                    System.out.println(StringEscapeUtils.unescapeJava("\tLast 
Exchange Completed Date:"));
-                } else {
-                    Date lastExchangeCompletedTimestamp = (Date) 
lastExchangeCompletedTimestampObj;
-                    System.out.println(StringEscapeUtils.unescapeJava("\tLast 
Exchange Completed Date: " + format.format(lastExchangeCompletedTimestamp)));
-                }
-
-                // add type converter statistics if enabled
-                if 
(camelContext.getTypeConverterRegistry().getStatistics().isStatisticsEnabled()) 
{
-                    
System.out.println(StringEscapeUtils.unescapeJava(String.format("\tTypeConverterRegistry
 utilization: [attempts=%s, hits=%s, misses=%s, failures=%s]",
-                            
camelContext.getTypeConverterRegistry().getStatistics().getAttemptCounter(),
-                            
camelContext.getTypeConverterRegistry().getStatistics().getHitCounter(),
-                            
camelContext.getTypeConverterRegistry().getStatistics().getMissCounter(),
-                            
camelContext.getTypeConverterRegistry().getStatistics().getFailedCounter())));
-                }
-
-                // add stream caching details if enabled
-                if (camelContext.getStreamCachingStrategy().isEnabled()) {
-                    System.out.println(StringEscapeUtils.unescapeJava(
-                        String.format("\tStreamCachingStrategy: 
[spoolDirectory=%s, spoolChiper=%s, spoolThreshold=%s, 
spoolUsedHeapMemoryThreshold=%s, "
-                                + "spoolUsedHeapMemoryLimit=%s, 
anySpoolRules=%s, bufferSize=%s, removeSpoolDirectoryWhenStopping=%s, 
statisticsEnabled=%s]",
-                            
camelContext.getStreamCachingStrategy().getSpoolDirectory(),
-                            
camelContext.getStreamCachingStrategy().getSpoolChiper(),
-                            
camelContext.getStreamCachingStrategy().getSpoolThreshold(),
-                            
camelContext.getStreamCachingStrategy().getSpoolUsedHeapMemoryThreshold(),
-                            
camelContext.getStreamCachingStrategy().getSpoolUsedHeapMemoryLimit(),
-                            
camelContext.getStreamCachingStrategy().isAnySpoolRules(),
-                            
camelContext.getStreamCachingStrategy().getBufferSize(),
-                            
camelContext.getStreamCachingStrategy().isRemoveSpoolDirectoryWhenStopping(),
-                            
camelContext.getStreamCachingStrategy().getStatistics().isStatisticsEnabled())));
-
-                    if 
(camelContext.getStreamCachingStrategy().getStatistics().isStatisticsEnabled()) 
{
-                        System.out.println(StringEscapeUtils.unescapeJava(
-                            String.format("\t                       
[cacheMemoryCounter=%s, cacheMemorySize=%s, cacheMemoryAverageSize=%s, 
cacheSpoolCounter=%s, "
-                                    + "cacheSpoolSize=%s, 
cacheSpoolAverageSize=%s]",
-                                
camelContext.getStreamCachingStrategy().getStatistics().getCacheMemoryCounter(),
-                                
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheMemorySize()),
-                                
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheMemoryAverageSize()),
-                                
camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolCounter(),
-                                
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolSize()),
-                                
printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolAverageSize()))));
-                    }
-                }
-
-                long activeRoutes = 0;
-                long inactiveRoutes = 0;
-                List<Route> routeList = camelContext.getRoutes();
-                for (Route route : routeList) {
-                    if 
(camelContext.getRouteStatus(route.getId()).isStarted()) {
-                        activeRoutes++;
-                    } else {
-                        inactiveRoutes++;
-                    }
-                }
-
-                System.out.println(StringEscapeUtils.unescapeJava("\tNumber of 
running routes: " + activeRoutes));
-                System.out.println(StringEscapeUtils.unescapeJava("\tNumber of 
not running routes: " + inactiveRoutes));
-            }
-
-        } else {
-            System.out.println("");
-            System.out.println(StringEscapeUtils.unescapeJava("\u001B[31mJMX 
Agent of Camel is not reachable. Maybe it has been disabled on the Camel 
context"));
-            System.out.println(StringEscapeUtils.unescapeJava("In consequence, 
some statistics are not available.\u001B[0m"));
-        }
-        
+    @Override
+    protected Object doExecute() throws Exception {
+        ContextInfoCommand command = new ContextInfoCommand(name, mode);
+        command.setStringEscape(stringEscape);
+        return command.execute(camelController, System.out, System.err);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/StringEscape.java
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/StringEscape.java
 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/StringEscape.java
new file mode 100644
index 0000000..d9e71a8
--- /dev/null
+++ 
b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/internal/StringEscape.java
@@ -0,0 +1,37 @@
+/**
+ * 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;
+
+import org.apache.karaf.util.StringEscapeUtils;
+
+public class StringEscape implements org.apache.camel.commands.StringEscape {
+
+    @Override
+    public String unescapeJava(String str) {
+        return StringEscapeUtils.unescapeJava(str);
+    }
+
+    @Override
+    public String escapeJava(String str) {
+        return StringEscapeUtils.escapeJava(str);
+    }
+
+    @Override
+    public String hex(char ch) {
+        return StringEscapeUtils.hex(ch);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
----------------------------------------------------------------------
diff --git 
a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
 
b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
index d3a38f0..424e2e6 100644
--- 
a/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
+++ 
b/platforms/karaf/commands/src/main/resources/OSGI-INF/blueprint/camel-commands.xml
@@ -26,6 +26,7 @@
     <command name="camel/context-info">
       <action class="org.apache.camel.karaf.commands.ContextInfo">
         <property name="camelController" ref="camelController"/>
+        <property name="stringEscape" ref="stringEscape"/>
       </action>
       <completers>
         <ref component-id="camelContextCompleter"/>
@@ -244,4 +245,6 @@
     <property name="bundleContext" ref="blueprintBundleContext"/>
   </bean>
 
+  <bean id="stringEscape" 
class="org.apache.camel.karaf.commands.internal.StringEscape"/>
+
 </blueprint>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/7791d1f3/platforms/karaf/features/src/main/resources/features.xml
----------------------------------------------------------------------
diff --git a/platforms/karaf/features/src/main/resources/features.xml 
b/platforms/karaf/features/src/main/resources/features.xml
index 249f814..c00347f 100644
--- a/platforms/karaf/features/src/main/resources/features.xml
+++ b/platforms/karaf/features/src/main/resources/features.xml
@@ -36,7 +36,7 @@
   <feature name='camel-core' version='${project.version}' resolver='(obr)' 
start-level='50'>
     <feature version='${servicemix-specs-version}'>xml-specs-api</feature>
     <bundle>mvn:org.apache.camel/camel-core/${project.version}</bundle>
-    <bundle>mvn:org.apache.camel/commands-core/${project.version}</bundle>
+    
<bundle>mvn:org.apache.camel/camel-commands-core/${project.version}</bundle>
     
<bundle>mvn:org.apache.camel.karaf/camel-karaf-commands/${project.version}</bundle>
   </feature>
   <feature name='camel-spring' version='${project.version}' resolver='(obr)' 
start-level='50'>

Reply via email to