You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by wk...@apache.org on 2015/12/20 14:59:57 UTC

svn commit: r1721028 - in /devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console: DeviceMapConsole.java Main.java

Author: wkeil
Date: Sun Dec 20 13:59:56 2015
New Revision: 1721028

URL: http://svn.apache.org/viewvc?rev=1721028&view=rev
Log:
DMAP-186: Some arguments like debug/log won't work in Java Console 

Task-Url: https://issues.apache.org/jira/browse/DMAP-186

Added:
    devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java
      - copied, changed from r1721024, devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/Main.java
Removed:
    devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/Main.java

Copied: devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java (from r1721024, devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/Main.java)
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java?p2=devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java&p1=devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/Main.java&r1=1721024&r2=1721028&rev=1721028&view=diff
==============================================================================
--- devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/Main.java (original)
+++ devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java Sun Dec 20 13:59:56 2015
@@ -18,6 +18,14 @@
  */
 package org.apache.devicemap.console;
 
+import io.airlift.airline.Arguments;
+import io.airlift.airline.Cli;
+import io.airlift.airline.Cli.CliBuilder;
+import io.airlift.airline.Command;
+import io.airlift.airline.Help;
+import io.airlift.airline.Option;
+import io.airlift.airline.OptionType;
+
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
@@ -36,124 +44,195 @@ import org.apache.devicemap.loader.impl.
  * @author Werner Keil
  * @version 1.1.1
  */
-public class Main {
-
-    public static void main(String[] args) throws Exception {
-
-        System.out.println("DeviceMap Java Console " + Main.class.getPackage().getImplementationVersion());
-        
-        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: " + Main.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());
-        }
-    }
+public class DeviceMapConsole {
+	private static final String APP_NAME = "DeviceMap Java Console";
+	private final static Logger LOG = Logger.getLogger(DeviceMapConsole.class.getName());
+
+	private static boolean isVerbose;
+
+	static class ToolCommand implements Runnable {
+		@Option(type = OptionType.GLOBAL, name = "-v", description = "Verbose mode")
+		public boolean verbose;
+
+		public void run() {
+			System.out.println(APP_NAME);
+		}
+	}
+
+	@Command(name = "classify", description = "Classify User Agent")
+	public static final class Classify extends ToolCommand {
+		@Option(name = "-i", description = "Input file")
+		public String inFile;
+
+	    @Arguments(description = "User Agent String to test")
+        public String userAgent;
+	    
+		@Override
+		public void run() {
+			if (inFile != null && inFile.length() > 0) {
+				LOG.info(getClass().getSimpleName() + " " + inFile);
+			} else {
+				LOG.info(getClass().getSimpleName());
+			}
+
+			isVerbose = verbose;
+
+			if (verbose) {
+				LOG.info("UA: " + userAgent + " ("  + verbose + ")");
+			}
+		}
+	}
+
+	public static void main(String[] args) throws Exception {
+
+		System.out.println(APP_NAME + " "
+				+ DeviceMapConsole.class.getPackage().getImplementationVersion());
+		
+		try {
+			@SuppressWarnings("unchecked")
+			CliBuilder<Runnable> builder = Cli.<Runnable>builder(DeviceMapConsole.class.getSimpleName())
+		                .withDescription("Unicode CLDR Importer Tool")
+		                .withDefaultCommand(Help.class)
+		                .withCommands(Help.class, Classify.class)
+		                //.withCommands(Help.class, Write.class)
+		                ;
+		        Cli<Runnable> toolParser = builder.build();
+		        toolParser.parse(args).run();	
+		} catch (Exception e) {
+			LOG.severe(e.getMessage());
+		}
+		
+		/*
+		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());
+		}
+		*/
+	}
 }