You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/10/04 08:21:10 UTC

[camel] 01/02: CAMEL-14031: Move command line stuff from MainSupport to special class.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 30affd0c599231ae50cc832b1f222d90b6bad290
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Oct 4 10:09:37 2019 +0200

    CAMEL-14031: Move command line stuff from MainSupport to special class.
---
 .../main/java/org/apache/camel/spring/Main.java    |   2 +-
 .../java/org/apache/camel/test/blueprint/Main.java |   4 +-
 .../src/main/java/org/apache/camel/main/Main.java  |   2 +-
 .../apache/camel/main/MainCommandLineSupport.java  | 190 +++++++++++++++++++++
 .../java/org/apache/camel/main/MainSupport.java    | 156 +----------------
 .../camel/main/MainSupportCommandLineTest.java     |   2 +-
 6 files changed, 197 insertions(+), 159 deletions(-)

diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
index 0c53020..3a845d6 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
@@ -47,7 +47,7 @@ import org.springframework.context.support.FileSystemXmlApplicationContext;
  * Each line in the {@link #LOCATION_PROPERTIES} is a reference to a Spring XML file to include,
  * which by default gets loaded from classpath.
  */
-public class Main extends org.apache.camel.main.MainSupport {
+public class Main extends org.apache.camel.main.MainCommandLineSupport {
 
     public static final String LOCATION_PROPERTIES = "META-INF/camel-spring/location.properties";
     protected static Main instance;
diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
index fef2be5..db5efa5 100644
--- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
+++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
@@ -20,13 +20,13 @@ import java.util.LinkedList;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
-import org.apache.camel.main.MainSupport;
+import org.apache.camel.main.MainCommandLineSupport;
 import org.osgi.framework.BundleContext;
 
 /**
  * A command line tool for booting up a CamelContext using an OSGi Blueprint XML file
  */
-public class Main extends MainSupport {
+public class Main extends MainCommandLineSupport {
 
     protected static Main instance;
     private BundleContext bundleContext;
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/Main.java b/core/camel-main/src/main/java/org/apache/camel/main/Main.java
index dd025e2..ac51b86 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/Main.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/Main.java
@@ -26,7 +26,7 @@ import org.apache.camel.spi.Registry;
 /**
  * A Main class for booting up Camel in standalone mode.
  */
-public class Main extends MainSupport {
+public class Main extends MainCommandLineSupport {
 
     protected static Main instance;
     protected final MainRegistry registry = new MainRegistry();
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainCommandLineSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainCommandLineSupport.java
new file mode 100644
index 0000000..06cc53c
--- /dev/null
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainCommandLineSupport.java
@@ -0,0 +1,190 @@
+/*
+ * 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.main;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Support for command line arguments to Camel main.
+ */
+public abstract class MainCommandLineSupport extends MainSupport {
+
+    protected final List<Option> options = new ArrayList<>();
+
+    public MainCommandLineSupport(Class... configurationClasses) {
+        super(configurationClasses);
+    }
+
+    public MainCommandLineSupport() {
+        addOption(new Option("h", "help", "Displays the help screen") {
+            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
+                showOptions();
+                completed();
+            }
+        });
+        addOption(new ParameterOption("r", "routers",
+                "Sets the router builder classes which will be loaded while starting the camel context",
+                "routerBuilderClasses") {
+            @Override
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                setRouteBuilderClasses(parameter);
+            }
+        });
+        addOption(new ParameterOption("d", "duration",
+                "Sets the time duration (seconds) that the application will run for before terminating.",
+                "duration") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                // skip second marker to be backwards compatible
+                if (parameter.endsWith("s") || parameter.endsWith("S")) {
+                    parameter = parameter.substring(0, parameter.length() - 1);
+                }
+                configure().setDurationMaxSeconds(Integer.parseInt(parameter));
+            }
+        });
+        addOption(new ParameterOption("dm", "durationMaxMessages",
+                "Sets the duration of maximum number of messages that the application will process before terminating.",
+                "durationMaxMessages") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                configure().setDurationMaxMessages(Integer.parseInt(parameter));
+            }
+        });
+        addOption(new ParameterOption("di", "durationIdle",
+                "Sets the idle time duration (seconds) duration that the application can be idle before terminating.",
+                "durationIdle") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                // skip second marker to be backwards compatible
+                if (parameter.endsWith("s") || parameter.endsWith("S")) {
+                    parameter = parameter.substring(0, parameter.length() - 1);
+                }
+                configure().setDurationMaxIdleSeconds(Integer.parseInt(parameter));
+            }
+        });
+        addOption(new Option("t", "trace", "Enables tracing") {
+            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
+                enableTrace();
+            }
+        });
+        addOption(new ParameterOption("e", "exitcode",
+                "Sets the exit code if duration was hit",
+                "exitcode") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                configure().setDurationHitExitCode(Integer.parseInt(parameter));
+            }
+        });
+        addOption(new ParameterOption("pl", "propertiesLocation",
+                "Sets location(s) to load properties, such as from classpath or file system.",
+                "propertiesLocation") {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                setPropertyPlaceholderLocations(parameter);
+            }
+        });
+    }
+
+    /**
+     * Displays the command line options.
+     */
+    public void showOptions() {
+        showOptionsHeader();
+
+        for (Option option : options) {
+            System.out.println(option.getInformation());
+        }
+    }
+
+    /**
+     * Parses the command line arguments.
+     */
+    public void parseArguments(String[] arguments) {
+        LinkedList<String> args = new LinkedList<>(Arrays.asList(arguments));
+
+        boolean valid = true;
+        while (!args.isEmpty()) {
+            String arg = args.removeFirst();
+
+            boolean handled = false;
+            for (Option option : options) {
+                if (option.processOption(arg, args)) {
+                    handled = true;
+                    break;
+                }
+            }
+            if (!handled) {
+                System.out.println("Unknown option: " + arg);
+                System.out.println();
+                valid = false;
+                break;
+            }
+        }
+        if (!valid) {
+            showOptions();
+            completed();
+        }
+    }
+
+    public void addOption(Option option) {
+        options.add(option);
+    }
+
+    /**
+     * Parses the command line arguments then runs the program.
+     */
+    public void run(String[] args) throws Exception {
+        parseArguments(args);
+        run();
+        LOG.info("MainSupport exiting code: {}", getExitCode());
+    }
+
+    /**
+     * Displays the header message for the command line options.
+     */
+    public void showOptionsHeader() {
+        System.out.println("Apache Camel Runner takes the following options");
+        System.out.println();
+    }
+
+
+    public abstract class ParameterOption extends Option {
+        private String parameterName;
+
+        protected ParameterOption(String abbreviation, String fullName, String description, String parameterName) {
+            super(abbreviation, fullName, description);
+            this.parameterName = parameterName;
+        }
+
+        @Override
+        protected void doProcess(String arg, LinkedList<String> remainingArgs) {
+            if (remainingArgs.isEmpty()) {
+                System.err.println("Expected fileName for ");
+                showOptions();
+                completed();
+            } else {
+                String parameter = remainingArgs.removeFirst();
+                doProcess(arg, parameter, remainingArgs);
+            }
+        }
+
+        @Override
+        public String getInformation() {
+            return "  " + getAbbreviation() + " or " + getFullName() + " <" + parameterName + "> = " + getDescription();
+        }
+
+        protected abstract void doProcess(String arg, String parameter, LinkedList<String> remainingArgs);
+    }
+}
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
index c98ccff..8af3715 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
@@ -77,11 +77,10 @@ public abstract class MainSupport extends ServiceSupport {
     protected static final Logger LOG = LoggerFactory.getLogger(MainSupport.class);
     protected static final int UNINITIALIZED_EXIT_CODE = Integer.MIN_VALUE;
     protected static final int DEFAULT_EXIT_CODE = 0;
+    protected final AtomicInteger exitCode = new AtomicInteger(UNINITIALIZED_EXIT_CODE);
     protected final List<MainListener> listeners = new ArrayList<>();
-    protected final List<Option> options = new ArrayList<>();
-    protected final CountDownLatch latch = new CountDownLatch(1);
     protected final AtomicBoolean completed = new AtomicBoolean(false);
-    protected final AtomicInteger exitCode = new AtomicInteger(UNINITIALIZED_EXIT_CODE);
+    protected final CountDownLatch latch = new CountDownLatch(1);
 
     protected volatile CamelContext camelContext;
     protected volatile ProducerTemplate camelTemplate;
@@ -122,68 +121,6 @@ public abstract class MainSupport extends ServiceSupport {
     }
 
     protected MainSupport() {
-        addOption(new Option("h", "help", "Displays the help screen") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                showOptions();
-                completed();
-            }
-        });
-        addOption(new ParameterOption("r", "routers",
-            "Sets the router builder classes which will be loaded while starting the camel context",
-            "routerBuilderClasses") {
-            @Override
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                setRouteBuilderClasses(parameter);
-            }
-        });
-        addOption(new ParameterOption("d", "duration",
-            "Sets the time duration (seconds) that the application will run for before terminating.",
-            "duration") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                // skip second marker to be backwards compatible
-                if (parameter.endsWith("s") || parameter.endsWith("S")) {
-                    parameter = parameter.substring(0, parameter.length() - 1);
-                }
-                configure().setDurationMaxSeconds(Integer.parseInt(parameter));
-            }
-        });
-        addOption(new ParameterOption("dm", "durationMaxMessages",
-            "Sets the duration of maximum number of messages that the application will process before terminating.",
-            "durationMaxMessages") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                configure().setDurationMaxMessages(Integer.parseInt(parameter));
-            }
-        });
-        addOption(new ParameterOption("di", "durationIdle",
-            "Sets the idle time duration (seconds) duration that the application can be idle before terminating.",
-            "durationIdle") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                // skip second marker to be backwards compatible
-                if (parameter.endsWith("s") || parameter.endsWith("S")) {
-                    parameter = parameter.substring(0, parameter.length() - 1);
-                }
-                configure().setDurationMaxIdleSeconds(Integer.parseInt(parameter));
-            }
-        });
-        addOption(new Option("t", "trace", "Enables tracing") {
-            protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-                enableTrace();
-            }
-        });
-        addOption(new ParameterOption("e", "exitcode",
-            "Sets the exit code if duration was hit",
-            "exitcode") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                configure().setDurationHitExitCode(Integer.parseInt(parameter));
-            }
-        });
-        addOption(new ParameterOption("pl", "propertiesLocation",
-            "Sets location(s) to load properties, such as from classpath or file system.",
-            "propertiesLocation") {
-            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
-                setPropertyPlaceholderLocations(parameter);
-            }
-        });
     }
 
     /**
@@ -317,51 +254,6 @@ public abstract class MainSupport extends ServiceSupport {
     }
 
     /**
-     * Displays the command line options.
-     */
-    public void showOptions() {
-        showOptionsHeader();
-
-        for (Option option : options) {
-            System.out.println(option.getInformation());
-        }
-    }
-
-    /**
-     * Parses the command line arguments.
-     */
-    public void parseArguments(String[] arguments) {
-        LinkedList<String> args = new LinkedList<>(Arrays.asList(arguments));
-
-        boolean valid = true;
-        while (!args.isEmpty()) {
-            String arg = args.removeFirst();
-
-            boolean handled = false;
-            for (Option option : options) {
-                if (option.processOption(arg, args)) {
-                    handled = true;
-                    break;
-                }
-            }
-            if (!handled) {
-                System.out.println("Unknown option: " + arg);
-                System.out.println();
-                valid = false;
-                break;
-            }
-        }
-        if (!valid) {
-            showOptions();
-            completed();
-        }
-    }
-
-    public void addOption(Option option) {
-        options.add(option);
-    }
-
-    /**
      * To configure options on Camel Main.
      */
     public MainConfigurationProperties configure() {
@@ -630,23 +522,6 @@ public abstract class MainSupport extends ServiceSupport {
         }
     }
 
-    /**
-     * Parses the command line arguments then runs the program.
-     */
-    public void run(String[] args) throws Exception {
-        parseArguments(args);
-        run();
-        LOG.info("MainSupport exiting code: {}", getExitCode());
-    }
-
-    /**
-     * Displays the header message for the command line options.
-     */
-    public void showOptionsHeader() {
-        System.out.println("Apache Camel Runner takes the following options");
-        System.out.println();
-    }
-
     public CamelContext getCamelContext() {
         return camelContext;
     }
@@ -1417,31 +1292,4 @@ public abstract class MainSupport extends ServiceSupport {
         protected abstract void doProcess(String arg, LinkedList<String> remainingArgs);
     }
 
-    public abstract class ParameterOption extends Option {
-        private String parameterName;
-
-        protected ParameterOption(String abbreviation, String fullName, String description, String parameterName) {
-            super(abbreviation, fullName, description);
-            this.parameterName = parameterName;
-        }
-
-        @Override
-        protected void doProcess(String arg, LinkedList<String> remainingArgs) {
-            if (remainingArgs.isEmpty()) {
-                System.err.println("Expected fileName for ");
-                showOptions();
-                completed();
-            } else {
-                String parameter = remainingArgs.removeFirst();
-                doProcess(arg, parameter, remainingArgs);
-            }
-        }
-
-        @Override
-        public String getInformation() {
-            return "  " + getAbbreviation() + " or " + getFullName() + " <" + parameterName + "> = " + getDescription();
-        }
-
-        protected abstract void doProcess(String arg, String parameter, LinkedList<String> remainingArgs);
-    }
 }
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainSupportCommandLineTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainSupportCommandLineTest.java
index 78449e9..6d9c30f 100644
--- a/core/camel-main/src/test/java/org/apache/camel/main/MainSupportCommandLineTest.java
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainSupportCommandLineTest.java
@@ -23,7 +23,7 @@ import org.junit.Test;
 
 public class MainSupportCommandLineTest {
 
-    private class MyMainSupport extends MainSupport {
+    private class MyMainSupport extends MainCommandLineSupport {
 
         private CamelContext context = new DefaultCamelContext();