Author: wkeil
Date: Mon Dec 21 18:15:58 2015
New Revision: 1721223

URL: http://svn.apache.org/viewvc?rev=1721223&view=rev
Log:
DMAP-186: Some arguments like debug/log won't work in Java Console 
Switched to Commons CLI 1.3.1
Task-Url: https://issues.apache.org/jira/browse/DMAP-186

Modified:
    
devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java

Modified: 
devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java
URL: 
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java?rev=1721223&r1=1721222&r2=1721223&view=diff
==============================================================================
--- 
devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java
 (original)
+++ 
devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java
 Mon Dec 21 18:15:58 2015
@@ -26,12 +26,11 @@ import java.util.logging.Handler;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import org.apache.commons.cli.BasicParser;
+import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.devicemap.DeviceMapClient;
@@ -49,8 +48,6 @@ public class DeviceMapConsole {
        private static final String CLASS_NAME = 
DeviceMapConsole.class.getName();
        private final static Logger LOG = Logger.getLogger(CLASS_NAME);
 
-       private static boolean isVerbose;
-
        /*
        static class ToolCommand implements Runnable {
                @Option(type = OptionType.GLOBAL, name = "-v", description = 
"Verbose mode")
@@ -98,40 +95,64 @@ public class DeviceMapConsole {
        */
 
        public static void main(String[] args) throws Exception {
-
+               final String DEFAULT_UA = "Mozilla/5.0 (Linux; U; Android 2.2; 
en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) 
Version/3.1 Mobile Safari/524.15.0";
+               
                System.out.println(APP_NAME + " "
                                + 
DeviceMapConsole.class.getPackage().getImplementationVersion());
+               
+               boolean debug = false;
+               String test = DEFAULT_UA;
+       String loaderPath = null;
+               LoaderOption option = LoaderOption.UNINITIALIZED;
+               String parameter = null;
+               Level debugLevel = Level.OFF;
+       
            CommandLine lvCmd = null;
-        HelpFormatter lvFormater = new HelpFormatter();
-        CommandLineParser lvParser = new BasicParser();
-        Options lvOptions = new Options();
+        final HelpFormatter lvFormater = new HelpFormatter();
+        final CommandLineParser lvParser = new DefaultParser();
+        final Options lvOptions = new Options();
                
-        Option lvClassify = new Option("c", "classify", true, "User Agent 
String to test");
         Option lvHilfe = new Option("h", "help", false, "Show Help.");
-        Option lvName = new Option("u", true, "Load DeviceMap resouces from 
URL or \"default\"");
-        Option lvJar = new Option("j", true, "Load DeviceMap resouces from JAR 
file in classpath");
+        //Option lvName = new Option("u", true, "Load DeviceMap resouces from 
URL or \"default\"");
         Option lverbose = new Option("v", "verbose", false, "Verbose mode.");
         
-        lvOptions.addOption(lvClassify);
+        
         lvOptions.addOption(lvHilfe);
-        lvOptions.addOption(lvName);
-        lvOptions.addOption(lvJar);
+        //lvOptions.addOption(lvName);
         lvOptions.addOption(lverbose);
         
-       /* lvOptions.addOption(OptionBuilder
-                .withLongOpt("url")
-                .withArgName("URL")
-                .withDescription("Load DeviceMap resouces from URL or 
\"default\"")
+        lvOptions.addOption(Option.builder("c")
+                   .longOpt("classify")
+                .argName("UA")
+                .desc("User Agent String to test")
                 .hasArg()
-                .create("u"));
+                .optionalArg(true)
+                .build());
         
-        lvOptions.addOption(OptionBuilder
-                       .withLongOpt("classify")
-                       .withArgName("userAgent")
-                       .withDescription("User Agent String to test")
-                       //.isRequired()
-                       .create("c"));
-        */
+        lvOptions.addOption(Option.builder("u")
+                   .longOpt("url")
+                .argName("url")
+                .desc("Load DeviceMap resouces from URL or \"default\"")
+                .hasArg()
+                .optionalArg(true)
+                .build());
+        
+        lvOptions.addOption(Option.builder("f")
+                           .longOpt("folder")
+                   .argName("path")
+                   .desc("Load DeviceMap resouces from folder or \"default\"")
+                   .hasArg()
+                   .optionalArg(true)
+                   .build());
+       
+        lvOptions.addOption(Option.builder("j")
+                   .longOpt("jar")
+               .argName("jar")
+               .desc("Load DeviceMap resouces from JAR file in classpath or 
\"default\"")
+               .hasArg()
+               .optionalArg(true)
+               .build());
+
         
         try {
             lvCmd = lvParser.parse(lvOptions, args);
@@ -141,155 +162,140 @@ public class DeviceMapConsole {
                 return;
             }
             
-            if (lvCmd.hasOption('u')) {
-               System.out.println("URL: " + lvCmd.getOptionObject('u'));
+            if (lvCmd.hasOption('v')) {
+               debug = true;
+                       debugLevel = Level.ALL;
+                       LOG.setLevel(debugLevel);
+                       
Logger.getLogger(DeviceMapClient.class.getName()).setLevel(
+                                       debugLevel);
+                       
Logger.getLogger(DDRLoader.class.getName()).setLevel(debugLevel);
+                       for (Handler h : 
Logger.getLogger(DeviceMapClient.class.getName())
+                                       .getParent().getHandlers()) {
+                               if (h instanceof ConsoleHandler) {
+                                       h.setLevel(debugLevel);
+                               }
+                       }
+               }
+            
+            if (lvCmd.hasOption('f')) {
+               option = LoaderOption.FOLDER;
+               loaderPath = lvCmd.getOptionValue('f');
             }
             
             if (lvCmd.hasOption('j')) {
-               System.out.println("JAR: " + lvCmd.getOptionObject('j'));
+               LOG.fine("JAR: " + lvCmd.getOptionObject('j'));
+               option = LoaderOption.JAR;
+            }
+            
+            if (lvCmd.hasOption('u')) {
+               option = LoaderOption.URL;
+               loaderPath = lvCmd.getOptionValue('u');
+               LOG.fine("URL: " + lvCmd.getOptionObject('u'));
             }
             
             if (lvCmd.hasOption('c')) {
-               System.out.println("UA: " + lvCmd.getOptionObject("classify"));
+               parameter = lvCmd.getOptionValue('c');
+               LOG.fine("UA: " + lvCmd.getOptionObject('c'));
             }
-
+            //lvFormater.printHelp(CLASS_NAME, lvOptions); // or shall we 
classify with default UA?
+            
+               /*      
+               } else if (args[i].startsWith("-h") || 
args[i].startsWith("--h")) {
+                               System.out.println("Usage: " + 
DeviceMapConsole.class.getName()
+                                               + " [OPTIONS] [FILE|STRING]\n");
+                               System.out
+                                               .println("  -f <path>           
 load DeviceMap resouces from folder or \"default\"");
+                               System.out
+                                               .println("  -j                  
 load DeviceMap resouces from jar file in classpath");
+                               System.out
+                                               .println("  -u <url>            
 load DeviceMap resouces from URL or \"default\"");
+                               System.out
+                                               .println("  -l <level>          
 log level set to <level>");
+                               System.out
+                                               .println("  FILE                
 text file of strings");
+                               System.out.println("  STRING               test 
string");
+
+                               return;
+                       } // [test string] | [test file]
+                       else if (!args[i].isEmpty()) {
+                               parameter = args[i];
+                               System.out.println(parameter);
+                       }
+               }
+               */
+
+               if ("default".equals(loaderPath)) {
+                       loaderPath = null;
+               }
+
+               DeviceMapClient client = new DeviceMapClient();
+               final long start = System.currentTimeMillis();
+               client.initDeviceData(option, loaderPath);
+               long diff = System.currentTimeMillis() - start;
+
+               System.out.println("Loaded " + client.getDeviceCount()
+                               + " devices with " + client.getPatternCount()
+                               + " patterns and " + client.getNodeCount() + " 
nodes in "
+                               + diff + "ms");
+
+               
+               long startn = System.nanoTime();
+               Device device = client.classifyDevice(test);
+               long diffn = (System.nanoTime() - startn) / 1000;
+
+               System.out.println("Test lookup: '" + device.getId() + "' time: 
"
+                               + diffn + "usec");
+
+               if (parameter == null) {
+               } else if ((new File(parameter)).exists()) {
+                       System.out.println("Text file: " + parameter);
+
+                       int count = 0;
+                       int total = 0;
+
+                       BufferedReader in = new BufferedReader(new 
FileReader(parameter));
+                       String line;
+
+                       while ((line = in.readLine()) != null) {
+                               System.out.println("Text: '" + line + "'");
+                               startn = System.nanoTime();
+                               device = client.classifyDevice(line);
+                               diffn = System.nanoTime() - startn;
+                               total += diffn;
+                               count++;
+
+                               System.out
+                                               .println("Text lookup " + count 
+ ": '"
+                                                               + 
device.getId() + "' time: " + (diffn / 1000)
+                                                               + "usec");
+                       }
+
+                       in.close();
+
+                       if (count == 0) {
+                               count = 1;
+                       }
+
+                       total /= count;
+
+                       System.out.println("TOTAL lookups: " + count + ", 
average time: "
+                                       + (total / 1000) + "usec");
+               } else {
+                       System.out.println("Text: '" + parameter + "'");
+
+                       startn = System.nanoTime();
+                       device = client.classifyDevice(parameter);
+                       diffn = System.nanoTime() - startn;
+                       System.out.println("Text lookup: '" + device.getId() + 
"' time: "
+                                       + (diffn / 1000) + "usec");
+                       System.out.println("DeviceMap JSON => " + 
device.toString());
+               }
+            
+            return;
         } catch (ParseException pvException) {
             lvFormater.printHelp(CLASS_NAME, lvOptions);
             System.out.println("Parse Error:" + pvException.getMessage());
             return;
         }
-               
-               /*
-               if (args.length == 0) {
-                       System.out.println("Usage: -h for help");
-               }
-
-
-               String loaderPath = null;
-               LoaderOption option = LoaderOption.UNINITIALIZED;
-               String parameter = null;
-               boolean debug = false;
-               Level debugLevel = Level.OFF;
-
-               for (int i = 0; i < args.length; i++) {
-                       if (args[i].equals("-f")) {
-                               option = LoaderOption.FOLDER;
-                               if (args.length > (++i)) {
-                                       loaderPath = args[i];
-                               }
-                       } else if (args[i].equals("-u")) {
-                               option = LoaderOption.URL;
-                               if (args.length > (++i)) {
-                                       loaderPath = args[i];
-                               }
-                       } else if (args[i].equals("-j")) {
-                               option = LoaderOption.JAR;
-                       } else if (args[i].equals("-l")) {
-                               debug = true;
-                               if (args.length > (++i)) {
-                                       debugLevel = Level.parse(args[i]);
-                               }
-                       } else if (args[i].startsWith("-h") || 
args[i].startsWith("--h")) {
-                               System.out.println("Usage: " + 
DeviceMapConsole.class.getName()
-                                               + " [OPTIONS] [FILE|STRING]\n");
-                               System.out
-                                               .println("  -f <path>           
 load DeviceMap resouces from folder or \"default\"");
-                               System.out
-                                               .println("  -j                  
 load DeviceMap resouces from jar file in classpath");
-                               System.out
-                                               .println("  -u <url>            
 load DeviceMap resouces from URL or \"default\"");
-                               System.out
-                                               .println("  -l <level>          
 log level set to <level>");
-                               System.out
-                                               .println("  FILE                
 text file of strings");
-                               System.out.println("  STRING               test 
string");
-
-                               return;
-                       } // [test string] | [test file]
-                       else if (!args[i].isEmpty()) {
-                               parameter = args[i];
-                               System.out.println(parameter);
-                       }
-               }
-
-               if (debug) {
-                       
Logger.getLogger(DeviceMapClient.class.getName()).setLevel(
-                                       debugLevel);
-                       
Logger.getLogger(DDRLoader.class.getName()).setLevel(debugLevel);
-                       for (Handler h : 
Logger.getLogger(DeviceMapClient.class.getName())
-                                       .getParent().getHandlers()) {
-                               if (h instanceof ConsoleHandler) {
-                                       h.setLevel(debugLevel);
-                               }
-                       }
-               }
-
-               if ("default".equals(loaderPath)) {
-                       loaderPath = null;
-               }
-
-               DeviceMapClient client = new DeviceMapClient();
-               final long start = System.currentTimeMillis();
-               client.initDeviceData(option, loaderPath);
-               long diff = System.currentTimeMillis() - start;
-
-               System.out.println("Loaded " + client.getDeviceCount()
-                               + " devices with " + client.getPatternCount()
-                               + " patterns and " + client.getNodeCount() + " 
nodes in "
-                               + diff + "ms");
-
-               final String test = "Mozilla/5.0 (Linux; U; Android 2.2; en; 
HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 
Mobile Safari/524.15.0";
-               long startn = System.nanoTime();
-               Device device = client.classifyDevice(test);
-               long diffn = (System.nanoTime() - startn) / 1000;
-
-               System.out.println("Test lookup: '" + device.getId() + "' time: 
"
-                               + diffn + "usec");
-
-               if (parameter == null) {
-               } else if ((new File(parameter)).exists()) {
-                       System.out.println("Text file: " + parameter);
-
-                       int count = 0;
-                       int total = 0;
-
-                       BufferedReader in = new BufferedReader(new 
FileReader(parameter));
-                       String line;
-
-                       while ((line = in.readLine()) != null) {
-                               System.out.println("Text: '" + line + "'");
-                               startn = System.nanoTime();
-                               device = client.classifyDevice(line);
-                               diffn = System.nanoTime() - startn;
-                               total += diffn;
-                               count++;
-
-                               System.out
-                                               .println("Text lookup " + count 
+ ": '"
-                                                               + 
device.getId() + "' time: " + (diffn / 1000)
-                                                               + "usec");
-                       }
-
-                       in.close();
-
-                       if (count == 0) {
-                               count = 1;
-                       }
-
-                       total /= count;
-
-                       System.out.println("TOTAL lookups: " + count + ", 
average time: "
-                                       + (total / 1000) + "usec");
-               } else {
-                       System.out.println("Text: '" + parameter + "'");
-
-                       startn = System.nanoTime();
-                       device = client.classifyDevice(parameter);
-                       diffn = System.nanoTime() - startn;
-                       System.out.println("Text lookup: '" + device.getId() + 
"' time: "
-                                       + (diffn / 1000) + "usec");
-                       System.out.println("DeviceMap JSON => " + 
device.toString());
-               }
-               */
        }
 }


Reply via email to