Author: wkeil
Date: Mon Dec 21 21:53:03 2015
New Revision: 1721268
URL: http://svn.apache.org/viewvc?rev=1721268&view=rev
Log:
DMAP-186: Some arguments like debug/log won't work in Java Console
Added map() method similar to .NET console
Task-Url: https://issues.apache.org/jira/browse/DMAP-186
Modified:
devicemap/trunk/clients/1.0/java/console/NOTICE
devicemap/trunk/clients/1.0/java/console/pom.xml
devicemap/trunk/clients/1.0/java/console/src/main/java/org/apache/devicemap/console/DeviceMapConsole.java
devicemap/trunk/clients/1.0/java/pom.xml
Modified: devicemap/trunk/clients/1.0/java/console/NOTICE
URL:
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/console/NOTICE?rev=1721268&r1=1721267&r2=1721268&view=diff
==============================================================================
--- devicemap/trunk/clients/1.0/java/console/NOTICE (original)
+++ devicemap/trunk/clients/1.0/java/console/NOTICE Mon Dec 21 21:53:03 2015
@@ -1,7 +1,10 @@
=============
Bundled APIs:
=============
-This module uses Apache Commons CLI.
+This module uses Apache Commons CLI and Apache Commons Lang (TM).
The source code of Apache Commons CLI is available at
-https://commons.apache.org/proper/commons-cli/source-repository.html
\ No newline at end of file
+https://commons.apache.org/proper/commons-cli/source-repository.html
+
+The source code of Apache Commons Lang (TM) is available at
+https://commons.apache.org/proper/commons-lang/
\ No newline at end of file
Modified: devicemap/trunk/clients/1.0/java/console/pom.xml
URL:
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/console/pom.xml?rev=1721268&r1=1721267&r2=1721268&view=diff
==============================================================================
--- devicemap/trunk/clients/1.0/java/console/pom.xml (original)
+++ devicemap/trunk/clients/1.0/java/console/pom.xml Mon Dec 21 21:53:03 2015
@@ -45,6 +45,10 @@
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
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=1721268&r1=1721267&r2=1721268&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 21:53:03 2015
@@ -21,6 +21,7 @@ package org.apache.devicemap.console;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
+import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
@@ -33,6 +34,7 @@ import org.apache.commons.cli.HelpFormat
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
+import org.apache.commons.lang3.time.StopWatch;
import org.apache.devicemap.DeviceMapClient;
import org.apache.devicemap.data.Device;
import org.apache.devicemap.loader.LoaderOption;
@@ -41,261 +43,222 @@ import org.apache.devicemap.loader.impl.
/**
* @author Reza Naghibi
* @author Werner Keil
- * @version 1.1.2
+ * @version 1.1.3
*/
public class DeviceMapConsole {
private static final String APP_NAME = "DeviceMap Java Console";
private static final String CLASS_NAME =
DeviceMapConsole.class.getName();
private final static Logger LOG = Logger.getLogger(CLASS_NAME);
- /*
- static class ToolCommand implements Runnable {
- @Option(type = OptionType.GLOBAL, name = "-v", description =
"Verbose mode")
- public boolean verbose;
+ public static void main(String[] args) throws Exception {
+ System.out.println(APP_NAME
+ + " "
+ + DeviceMapConsole.class.getPackage()
+ .getImplementationVersion());
- public void run() {
- System.out.println(APP_NAME);
- }
- }
+ boolean debug = false;
+ String loaderPath = null;
+ LoaderOption option = LoaderOption.UNINITIALIZED;
+ String parameter = null;
+ Level debugLevel = Level.OFF;
- @Command(name = "classify", description = "Classify User Agent")
- public static final class Classify extends ToolCommand {
- @Option(name = "-f", description = "Load DeviceMap resouces
from folder or \"default\"")
- public String folder;
-
- @Option(name = "-u", description = "Load DeviceMap resouces
from URL or \"default\"")
- public String url;
-
- @Option(name = "-j", description = "Load DeviceMap resouces
from JAR file in classpath")
- public String jarFile="default";
-
- @Arguments(description = "User Agent String to test")
- public String userAgent;
-
- @Override
- public void run() {
- isVerbose = verbose;
-
- if (jarFile != null) {
- if (jarFile.length() > 0) {
- LOG.info(getClass().getSimpleName() + "
" + jarFile);
- } else {
- LOG.info(getClass().getSimpleName());
- }
+ CommandLine lvCmd = null;
+ final HelpFormatter lvFormater = new HelpFormatter();
+ final CommandLineParser lvParser = new DefaultParser();
+ final Options lvOptions = new Options();
+
+ final Option lvHelp = new Option("h", "help", false, "Show
Help.");
+ final Option lverbose = new Option("v", "verbose", false,
+ "Verbose mode.");
+
+ lvOptions.addOption(lvHelp);
+ lvOptions.addOption(lverbose);
+ lvOptions.addOption(new Option("o", "json", false, "Output as
JSON."));
+
+
lvOptions.addOption(Option.builder("d").longOpt("device").argName("UA")
+ .desc("User Agent of device to test").hasArg()
+ .optionalArg(true).build());
+
+
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);
+
+ if (lvCmd.hasOption('h')) {
+ lvFormater.printHelp(CLASS_NAME, lvOptions);
+ return;
}
-
-
- if (verbose) {
- LOG.info("UA: " + userAgent + " (" + verbose +
")");
+ 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')) {
+ 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('d')) {
+ parameter = lvCmd.getOptionValue('d');
+ LOG.fine("UA: " + lvCmd.getOptionObject('d'));
+ }
+ // 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(" 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;
+ }
+
+ final 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");
+
+ System.out.println("Cold run");
+ long startn = System.nanoTime();
+ final long startm = System.currentTimeMillis();
+ map(client, "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");
+ map(client, "Mozilla/5.0 (iPad; U; CPU OS 4_3_5 like
Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8L1");
+ map(client, "Mozilla/5.0 (BlackBerry; U; BlackBerry
9810; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.261 Mobile
Safari/534.11+");
+ map(client, "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0
like Mac OS X; en-us) AppleWebKit/536.26 (KHTML, like Gecko) CriOS/23.0.1271.91
Mobile/10A403 Safari/8536.25");
+ //long diffn = (System.nanoTime() - startn) / 1000;
+ final long diffm = System.currentTimeMillis() - startm;
+ System.out.println("End cold run : " + diffm + " ms");
+
+ long diffn;
+ 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;
+ Device device;
+ 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("UA: '" + parameter + "'");
+ startn = System.nanoTime();
+ final Device device =
client.classifyDevice(parameter);
+ diffn = System.nanoTime() - startn;
+ System.out.println("UA lookup: '" +
device.getId() + "' time: "
+ + (diffn / 1000) + "usec");
+ if (lvCmd.hasOption('o')) {
+ System.out
+ .println("DeviceMap
JSON => " + device.toString());
+ } else {
+ Map<String, String> m =
device.getAttributes();
+ // iterate thru the attributes
+ if (m != null
+ && (m.keySet() != null
&& m.keySet().size() > 0)) {
+ System.out.println("===
ATTRIBUTES ===");
+ for (String attr : m.keySet()) {
+ System.out.println(attr
+ ": " + m.get(attr));
+ }
+ } else {
+ System.out.println("No
attributes found for '"
+ + parameter +
"'.");
+ }
+ }
+ }
+
+ return;
+ } catch (ParseException pvException) {
+ lvFormater.printHelp(CLASS_NAME, lvOptions);
+ System.out.println("Parse Error:" +
pvException.getMessage());
+ return;
}
}
-
- */
- 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;
- final HelpFormatter lvFormater = new HelpFormatter();
- final CommandLineParser lvParser = new DefaultParser();
- final Options lvOptions = new Options();
-
- Option lvHilfe = new Option("h", "help", false, "Show Help.");
- //Option lvName = new Option("u", true, "Load DeviceMap resouces from
URL or \"default\"");
- Option lverbose = new Option("v", "verbose", false, "Verbose mode.");
-
-
- lvOptions.addOption(lvHilfe);
- //lvOptions.addOption(lvName);
- lvOptions.addOption(lverbose);
-
- lvOptions.addOption(Option.builder("c")
- .longOpt("classify")
- .argName("UA")
- .desc("User Agent String to test")
- .hasArg()
- .optionalArg(true)
- .build());
-
- 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);
-
- if (lvCmd.hasOption('h')) {
- lvFormater.printHelp(CLASS_NAME, lvOptions);
- return;
- }
-
- 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')) {
- 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')) {
- 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;
- }
+ private static void map(DeviceMapClient client, String text) {
+ StopWatch stopWatch = new StopWatch();
+ stopWatch.start();
+
+ Device device = client.classifyDevice(text);
+ stopWatch.stop();
+ String deviceId = "unknown";
+ if (device != null) {
+ deviceId = device.getId();
+ }
+ System.out.println("Result: " + deviceId + " took "
+ + stopWatch.getTime() + " ms");
}
}
Modified: devicemap/trunk/clients/1.0/java/pom.xml
URL:
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/pom.xml?rev=1721268&r1=1721267&r2=1721268&view=diff
==============================================================================
--- devicemap/trunk/clients/1.0/java/pom.xml (original)
+++ devicemap/trunk/clients/1.0/java/pom.xml Mon Dec 21 21:53:03 2015
@@ -57,9 +57,9 @@
<version>1.3.1</version>
</dependency>
<dependency>
- <groupId>io.airlift</groupId>
- <artifactId>airline</artifactId>
- <version>0.7</version>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>