You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2016/09/23 22:49:24 UTC

logging-log4j2 git commit: Refactor into a basic command line class to hold common options, right now only "help" but "version" makes sense too and I'll add that later.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 96cc528db -> e4665a246


Refactor into a basic command line class to hold common options, right
now only "help" but "version" makes sense too and I'll add that later.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/e4665a24
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/e4665a24
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/e4665a24

Branch: refs/heads/master
Commit: e4665a2467e714ccd117a6877e583cd71d1788d0
Parents: 96cc528
Author: Gary Gregory <gg...@apache.org>
Authored: Fri Sep 23 15:49:21 2016 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Fri Sep 23 15:49:21 2016 -0700

----------------------------------------------------------------------
 .../core/net/server/AbstractSocketServer.java   | 14 +-----
 .../core/util/BasicCommandLineArguments.java    | 47 ++++++++++++++++++++
 2 files changed, 49 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e4665a24/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/AbstractSocketServer.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/AbstractSocketServer.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/AbstractSocketServer.java
index 8e24c25..ab0c945 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/AbstractSocketServer.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/AbstractSocketServer.java
@@ -35,6 +35,7 @@ import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationSource;
 import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
 import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
+import org.apache.logging.log4j.core.util.BasicCommandLineArguments;
 import org.apache.logging.log4j.core.util.InetAddressConverter;
 import org.apache.logging.log4j.core.util.Log4jThread;
 import org.apache.logging.log4j.util.Strings;
@@ -53,14 +54,11 @@ import com.beust.jcommander.validators.PositiveInteger;
  */
 public abstract class AbstractSocketServer<T extends InputStream> extends LogEventListener implements Runnable {
 
-    protected static class CommandLineArguments {
+    protected static class CommandLineArguments extends BasicCommandLineArguments {
 
         @Parameter(names = { "--config", "-c" }, description = "Log4j configuration file location (path or URL).")
         private String configLocation;
 
-        @Parameter(names = { "--help", "-?", "-h" }, help = true, description = "Prints this help.")
-        private boolean help;
-
         @Parameter(names = { "--interactive",
                 "-i" }, description = "Accepts commands on standard input (\"exit\" is the only command).")
         private boolean interactive;
@@ -81,10 +79,6 @@ public abstract class AbstractSocketServer<T extends InputStream> extends LogEve
             return port;
         }
 
-        boolean isHelp() {
-            return help;
-        }
-
         protected boolean isInteractive() {
             return interactive;
         }
@@ -93,10 +87,6 @@ public abstract class AbstractSocketServer<T extends InputStream> extends LogEve
             this.configLocation = configLocation;
         }
 
-        void setHelp(final boolean help) {
-            this.help = help;
-        }
-
         void setInteractive(final boolean interactive) {
             this.interactive = interactive;
         }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e4665a24/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java
new file mode 100644
index 0000000..71271d8
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.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.logging.log4j.core.util;
+
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+
+public class BasicCommandLineArguments {
+
+    public static <T extends BasicCommandLineArguments> T parseCommandLine(final String[] mainArgs, final Class<?> clazz,
+            final T args) {
+        final JCommander jCommander = new JCommander(args);
+        jCommander.setProgramName(clazz.getName());
+        jCommander.setCaseSensitiveOptions(false); // for sanity
+        jCommander.parse(mainArgs);
+        if (args.isHelp()) {
+            jCommander.usage();
+        }
+        return args;
+    }
+
+    @Parameter(names = { "--help", "-?", "-h" }, help = true, description = "Prints this help.")
+    private boolean help;
+
+    public boolean isHelp() {
+        return help;
+    }
+
+    public void setHelp(boolean help) {
+        this.help = help;
+    }
+
+}