Author: wkeil
Date: Sun Jun 19 02:27:23 2016
New Revision: 1749123

URL: http://svn.apache.org/viewvc?rev=1749123&view=rev
Log:
DMAP-111: Improve Web Examples 

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

Modified:
    devicemap/trunk/clients/1.0/java/client/pom.xml
    
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapClient.java
    
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapFactory.java
    
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/LoaderFactory.java
    
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/resource/URLResource.java
    devicemap/trunk/clients/1.0/java/console/pom.xml
    devicemap/trunk/clients/1.0/java/pom.xml

Modified: devicemap/trunk/clients/1.0/java/client/pom.xml
URL: 
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/client/pom.xml?rev=1749123&r1=1749122&r2=1749123&view=diff
==============================================================================
--- devicemap/trunk/clients/1.0/java/client/pom.xml (original)
+++ devicemap/trunk/clients/1.0/java/client/pom.xml Sun Jun 19 02:27:23 2016
@@ -23,7 +23,7 @@
   <parent>
     <groupId>org.apache.devicemap</groupId>
     <artifactId>devicemap-java</artifactId>
-    <version>1.1.1-SNAPSHOT</version>
+    <version>1.2.0-SNAPSHOT</version>
   </parent>
 
   <groupId>org.apache.devicemap</groupId>

Modified: 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapClient.java
URL: 
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapClient.java?rev=1749123&r1=1749122&r2=1749123&view=diff
==============================================================================
--- 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapClient.java
 (original)
+++ 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapClient.java
 Sun Jun 19 02:27:23 2016
@@ -34,187 +34,195 @@ import org.apache.devicemap.loader.Loade
 
 /**
  * @author Werner Keil
- * @version 1.0.12
+ * @version 1.0.13
  */
 public class DeviceMapClient {
-       private static final Logger LOG = Logger.getLogger(DeviceMapClient.class
-                       .getName());
-       private static final java.util.regex.Pattern TEXT_SPLIT_PATTERN = 
java.util.regex.Pattern
-                       .compile(" |-|_|/|\\\\|\\[|\\]|\\(|\\)|;");
-
-       private static long initCount = 0;
-
-       // indexes
-       private Map<String, DeviceType> devices;
-       private Map<String, List<DeviceType>> patterns;
-
-       private final Device unknown;
-
-       public DeviceMapClient() {
-               devices = null;
-               patterns = null;
-               Map<String, String> uAttributes = new HashMap<String, String>();
-               uAttributes.put("id", Device.UNKNOWN_ID);
-               uAttributes = Collections.unmodifiableMap(uAttributes);
-               unknown = new Device(Device.UNKNOWN_ID, uAttributes);
-       }
-
-       public void initDeviceData(LoaderOption option) throws IOException {
-               initDeviceData(option, null);
-       }
-
-       public synchronized void initDeviceData(LoaderOption option, String 
path)
-                       throws IOException {
-               devices = LoaderFactory.getLoader(option, path).getData();
-               initCount++;
-
-               if (initCount % 1000 == 0) {
-                       LOG.log(Level.WARNING,
-                                       "Possible device data 
over-initialization detected");
-               }
-
-               if (devices == null) {
-                       patterns = null;
-                       return;
-               }
-               createIndex();
-       }
-
-       private void createIndex() {
-               patterns = new HashMap<String, List<DeviceType>>(8000);
-
-               for (DeviceType device : devices.values()) {
-                       for (Pattern pattern : 
device.getPatternSet().getPatterns()) {
-                               for (int i = 0; i < 
pattern.getPatternParts().size(); i++) {
-                                       String part = 
pattern.getPatternParts().get(i);
-
-                                       // duplicate
-                                       if (patterns.get(part) != null) {
-                                               if (i == 
(pattern.getPatternParts().size() - 1)
-                                                               && 
!patterns.get(part).contains(device)) {
-                                                       
patterns.get(part).add(device);
-                                               }
-                                       } else {
-                                               List<DeviceType> single = new 
ArrayList<DeviceType>();
-                                               single.add(device);
-                                               patterns.put(part, single);
-                                       }
-                               }
-                       }
-               }
-       }
-
-       public Map<String, String> classify(String text) {
-               if (devices == null) {
-                       throw new RuntimeException("Uninitialized device 
index");
-               }
-
-               if (text == null) {
-                       return null;
-               }
-
-               Set<String> hitPatterns = new HashSet<String>();
-               Set<DeviceType> hitDevices = new HashSet<DeviceType>();
-               DeviceType winner = null;
-               Pattern winnerPattern = null;
-               LOG.log(Level.FINE, "classify: ''{0}''", text);
-               List<String> parts = split(text);
-
-               // generate ngrams upto size 4
-               for (int i = 0; i < parts.size(); i++) {
-                       String pattern = "";
-                       for (int j = 0; j < 4 && (j + i) < parts.size(); j++) {
-                               pattern += parts.get(i + j);
-                               List<DeviceType> dlist = patterns.get(pattern);
-                               if (dlist != null) {
-                                       hitPatterns.add(pattern);
-                                       hitDevices.addAll(dlist);
-                                       for (DeviceType device : dlist) {
-                                               LOG.log(Level.FINER,
-                                                               "Hit found: 
''{0}'' => id: ''{1}'' {2}",
-                                                               new Object[] { 
pattern, device.getId(),
-                                                                               
device.getPatternSet() });
-                                       }
-                               }
-                       }
-               }
-
-               // look for the strongest hit
-               for (DeviceType device : hitDevices) {
-                       Pattern pattern = 
device.getPatternSet().isValid(hitPatterns);
-                       if (pattern == null) {
-                               continue;
+    private static final Logger LOG = 
Logger.getLogger(DeviceMapClient.class.getName());
+    private static final java.util.regex.Pattern TEXT_SPLIT_PATTERN = 
java.util.regex.Pattern
+           .compile(" |-|_|/|\\\\|\\[|\\]|\\(|\\)|;");
+
+    private static long initCount = 0;
+
+    // indexes
+    private Map<String, DeviceType> devices;
+    private Map<String, List<DeviceType>> patterns;
+
+    private final Device unknown;
+
+    public DeviceMapClient() {
+       devices = null;
+       patterns = null;
+       Map<String, String> uAttributes = new HashMap<String, String>();
+       uAttributes.put("id", Device.UNKNOWN_ID);
+       uAttributes = Collections.unmodifiableMap(uAttributes);
+       unknown = new Device(Device.UNKNOWN_ID, uAttributes);
+    }
+
+    public void initDeviceData(LoaderOption option, boolean snapshot) throws 
IOException {
+       initDeviceData(option, null, snapshot);
+    }
+    
+    public void initDeviceData(LoaderOption option) throws IOException {
+       initDeviceData(option, false);
+    }
+
+    public synchronized void initDeviceData(LoaderOption option, String path,
+           boolean snapshot) throws IOException {
+       devices = LoaderFactory.getLoader(option, path, snapshot).getData();
+       initCount++;
+
+       if (initCount % 1000 == 0) {
+           LOG.log(Level.WARNING,
+                   "Possible device data over-initialization detected");
+       }
+
+       if (devices == null) {
+           patterns = null;
+           return;
+       }
+       createIndex();
+    }
+
+    public void initDeviceData(LoaderOption option, String path)
+           throws IOException {
+       initDeviceData(option, path, false);
+    }
+
+    private void createIndex() {
+       patterns = new HashMap<String, List<DeviceType>>(8000);
+
+       for (DeviceType device : devices.values()) {
+           for (Pattern pattern : device.getPatternSet().getPatterns()) {
+               for (int i = 0; i < pattern.getPatternParts().size(); i++) {
+                   String part = pattern.getPatternParts().get(i);
+
+                   // duplicate
+                   if (patterns.get(part) != null) {
+                       if (i == (pattern.getPatternParts().size() - 1)
+                               && !patterns.get(part).contains(device)) {
+                           patterns.get(part).add(device);
                        }
-
+                   } else {
+                       List<DeviceType> single = new ArrayList<DeviceType>();
+                       single.add(device);
+                       patterns.put(part, single);
+                   }
+               }
+           }
+       }
+    }
+
+    public Map<String, String> classify(String text) {
+       if (devices == null) {
+           throw new RuntimeException("Uninitialized device index");
+       }
+
+       if (text == null) {
+           return null;
+       }
+
+       Set<String> hitPatterns = new HashSet<String>();
+       Set<DeviceType> hitDevices = new HashSet<DeviceType>();
+       DeviceType winner = null;
+       Pattern winnerPattern = null;
+       LOG.log(Level.FINE, "classify: ''{0}''", text);
+       List<String> parts = split(text);
+
+       // generate ngrams upto size 4
+       for (int i = 0; i < parts.size(); i++) {
+           String pattern = "";
+           for (int j = 0; j < 4 && (j + i) < parts.size(); j++) {
+               pattern += parts.get(i + j);
+               List<DeviceType> dlist = patterns.get(pattern);
+               if (dlist != null) {
+                   hitPatterns.add(pattern);
+                   hitDevices.addAll(dlist);
+                   for (DeviceType device : dlist) {
                        LOG.log(Level.FINER,
-                                       "Hit candidate: ''{0}'' => ({1},{2})",
-                                       new Object[] { device.getId(), 
pattern.getType(),
-                                                       pattern.getRank() });
-
-                       if (winnerPattern == null
-                                       || pattern.getRank() > 
winnerPattern.getRank()) {
-                               winner = device;
-                               winnerPattern = pattern;
-                       }
-               }
-
-               if (winner != null) {
-                       LOG.log(Level.FINE, "Result: {0}", winner);
-
-                       UserAgent userAgent = UserAgent.of(text);
-                       LOG.log(Level.FINE, "User Agent: {0}", userAgent);
-                       // fixFromUserAgent(winner, userAgent);
-                       return fixFromUserAgent(winner, 
userAgent).getAttributes();
-               } else {
-                       return null;
-               }
-       }
-
-       private static List<String> split(String text) {
-               String[] parts = TEXT_SPLIT_PATTERN.split(text);
-               List<String> nonemptyParts = new ArrayList<String>();
-               for (String part : parts) {
-                       String normalizedPart = Pattern.normalize(part);
-                       if (normalizedPart != null && !normalizedPart.isEmpty())
-                               nonemptyParts.add(normalizedPart);
-               }
-               return nonemptyParts;
-       }
-
-       public Device classifyDevice(String text) {
-               Map<String, String> m = classify(text);
-               if (m == null) {
-                       return unknown;
-               }
-               return new Device(m.get("id"), m);
-       }
-
-       public int getDeviceCount() {
-               if (devices == null) {
-                       return -1;
-               }
-               return devices.size();
-       }
-
-       public int getPatternCount() {
-               if (patterns == null) {
-                       return -1;
-               }
-               return patterns.size();
-       }
-
-       public long getNodeCount() {
-               if (patterns == null) {
-                       return -1;
-               }
-               long count = 0;
-               for (List<DeviceType> pDevices : patterns.values()) {
-                       count += pDevices.size();
-               }
-               return count;
-       }
-
-       Map<String, DeviceType> getDevices() {
-               return devices;
-       }
+                               "Hit found: ''{0}'' => id: ''{1}'' {2}",
+                               new Object[] { pattern, device.getId(),
+                                       device.getPatternSet() });
+                   }
+               }
+           }
+       }
+
+       // look for the strongest hit
+       for (DeviceType device : hitDevices) {
+           Pattern pattern = device.getPatternSet().isValid(hitPatterns);
+           if (pattern == null) {
+               continue;
+           }
+
+           LOG.log(Level.FINER,
+                   "Hit candidate: ''{0}'' => ({1},{2})",
+                   new Object[] { device.getId(), pattern.getType(),
+                           pattern.getRank() });
+
+           if (winnerPattern == null
+                   || pattern.getRank() > winnerPattern.getRank()) {
+               winner = device;
+               winnerPattern = pattern;
+           }
+       }
+
+       if (winner != null) {
+           LOG.log(Level.FINE, "Result: {0}", winner);
+
+           UserAgent userAgent = UserAgent.of(text);
+           LOG.log(Level.FINE, "User Agent: {0}", userAgent);
+           // fixFromUserAgent(winner, userAgent);
+           return fixFromUserAgent(winner, userAgent).getAttributes();
+       } else {
+           return null;
+       }
+    }
+
+    private static List<String> split(String text) {
+       String[] parts = TEXT_SPLIT_PATTERN.split(text);
+       List<String> nonemptyParts = new ArrayList<String>();
+       for (String part : parts) {
+           String normalizedPart = Pattern.normalize(part);
+           if (normalizedPart != null && !normalizedPart.isEmpty())
+               nonemptyParts.add(normalizedPart);
+       }
+       return nonemptyParts;
+    }
+
+    public Device classifyDevice(String text) {
+       Map<String, String> m = classify(text);
+       if (m == null) {
+           return unknown;
+       }
+       return new Device(m.get("id"), m);
+    }
+
+    public int getDeviceCount() {
+       if (devices == null) {
+           return -1;
+       }
+       return devices.size();
+    }
+
+    public int getPatternCount() {
+       if (patterns == null) {
+           return -1;
+       }
+       return patterns.size();
+    }
+
+    public long getNodeCount() {
+       if (patterns == null) {
+           return -1;
+       }
+       long count = 0;
+       for (List<DeviceType> pDevices : patterns.values()) {
+           count += pDevices.size();
+       }
+       return count;
+    }
+
+    Map<String, DeviceType> getDevices() {
+       return devices;
+    }
 }

Modified: 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapFactory.java
URL: 
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapFactory.java?rev=1749123&r1=1749122&r2=1749123&view=diff
==============================================================================
--- 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapFactory.java
 (original)
+++ 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/DeviceMapFactory.java
 Sun Jun 19 02:27:23 2016
@@ -21,11 +21,14 @@ package org.apache.devicemap;
 import java.io.IOException;
 import org.apache.devicemap.loader.LoaderOption;
 
-public class DeviceMapFactory {
-
+/**
+ * @author Werner Keil
+ * @version 1.1
+ */
+public final class DeviceMapFactory {
     private static DeviceMapClient client = null;
     private static volatile boolean initialized = false;
-
+    
     private static final LoaderOption DEFAULT = LoaderOption.JAR;
 
     private DeviceMapFactory() {
@@ -35,17 +38,21 @@ public class DeviceMapFactory {
         return getClient(DEFAULT, null);
     }
 
+    public static DeviceMapClient getClient(LoaderOption option, boolean 
snapshot) {
+        return getClient(option, null, snapshot);
+    }
+    
     public static DeviceMapClient getClient(LoaderOption option) {
-        return getClient(option, null);
+        return getClient(option, false);
     }
 
-    public static DeviceMapClient getClient(LoaderOption option, String path) {
+    public static DeviceMapClient getClient(LoaderOption option, String path, 
boolean snapshot) {
         if (!initialized) {
             synchronized (DeviceMapFactory.class) {
                 if (!initialized) {
                     client = new DeviceMapClient();
                     try {
-                        client.initDeviceData(option, path);
+                        client.initDeviceData(option, path, snapshot);
                     } catch (IOException ex) {
                         throw new RuntimeException(ex);
                     }
@@ -56,6 +63,10 @@ public class DeviceMapFactory {
         return client;
     }
     
+    public static DeviceMapClient getClient(LoaderOption option, String path) {
+       return getClient(option, path, false);
+    }
+    
     public static void resetClient() {
         initialized = false;
     }

Modified: 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/LoaderFactory.java
URL: 
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/LoaderFactory.java?rev=1749123&r1=1749122&r2=1749123&view=diff
==============================================================================
--- 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/LoaderFactory.java
 (original)
+++ 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/LoaderFactory.java
 Sun Jun 19 02:27:23 2016
@@ -26,9 +26,13 @@ import org.apache.devicemap.loader.impl.
 import org.apache.devicemap.loader.resource.URLResource;
 import org.apache.devicemap.loader.impl.UninitializedLoader;
 
-public class LoaderFactory {
+/**
+ * @author Werner Keil
+ * @version 1.1
+ */
+public abstract class LoaderFactory {
 
-    public static Loader getLoader(LoaderOption option, String path) {
+    public static Loader getLoader(LoaderOption option, String path, boolean 
snapshot) {
         switch (option) {
             case JAR: {
                 return new DDRLoader(new JarResource(path));
@@ -42,7 +46,12 @@ public class LoaderFactory {
             case NOOP: {
                 return new NoopLoader();
             }
-        }
-        return new UninitializedLoader();
+            default:
+               return new UninitializedLoader();
+        }       
+    }
+    
+    public static Loader getLoader(LoaderOption option, String path) {
+       return getLoader(option, path, false);
     }
-}
+}
\ No newline at end of file

Modified: 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/resource/URLResource.java
URL: 
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/resource/URLResource.java?rev=1749123&r1=1749122&r2=1749123&view=diff
==============================================================================
--- 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/resource/URLResource.java
 (original)
+++ 
devicemap/trunk/clients/1.0/java/client/src/main/java/org/apache/devicemap/loader/resource/URLResource.java
 Sun Jun 19 02:27:23 2016
@@ -24,25 +24,38 @@ import java.net.URL;
 
 import org.apache.devicemap.loader.Resource;
 
+/**
+ * @author Werner Keil
+ * @version 1.1
+ */
 public class URLResource implements Resource {
-
     //TODO: put on CDN of some kind
-    private static final String DEFAULT_PATH = 
"http://devicemap-vm.apache.org/data/latest";;
+    private static final String LATEST_PATH = 
"http://devicemap-vm.apache.org/data/latest";;
+    private static final String SNAPSHOT_PATH = 
"http://devicemap-vm.apache.org/data/snapshot";;
 
     private final String path;
-
-    public URLResource(String path) {
+    private final boolean isSnapshot;
+    
+    public URLResource(String path, boolean snapshot) {
+       this.isSnapshot = snapshot;
         if (path == null) {
-            this.path = DEFAULT_PATH;
+            if (isSnapshot) {
+               this.path = SNAPSHOT_PATH;
+            } else {
+               this.path = LATEST_PATH;
+            }
         } else {
             this.path = path;
         }
     }
+    
+    public URLResource(String path) {
+       this(path, false);
+    }
 
     @Override
     public InputStream getResource(String file) throws IOException {
         String rpath = path + "/" + file;
         return new URL(rpath).openStream();
     }
-
 }

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=1749123&r1=1749122&r2=1749123&view=diff
==============================================================================
--- devicemap/trunk/clients/1.0/java/console/pom.xml (original)
+++ devicemap/trunk/clients/1.0/java/console/pom.xml Sun Jun 19 02:27:23 2016
@@ -16,7 +16,7 @@
        <parent>
                <groupId>org.apache.devicemap</groupId>
                <artifactId>devicemap-java</artifactId>
-               <version>1.1.1-SNAPSHOT</version>
+               <version>1.2.0-SNAPSHOT</version>
        </parent>
 
        <artifactId>devicemap-java-console</artifactId>

Modified: devicemap/trunk/clients/1.0/java/pom.xml
URL: 
http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/pom.xml?rev=1749123&r1=1749122&r2=1749123&view=diff
==============================================================================
--- devicemap/trunk/clients/1.0/java/pom.xml (original)
+++ devicemap/trunk/clients/1.0/java/pom.xml Sun Jun 19 02:27:23 2016
@@ -23,7 +23,7 @@
        <groupId>org.apache.devicemap</groupId>
        <artifactId>devicemap-java</artifactId>
        <packaging>pom</packaging>
-       <version>1.1.1-SNAPSHOT</version>
+       <version>1.2.0-SNAPSHOT</version>
        <name>Apache DeviceMap for Java</name>
        <description>Apache DeviceMap Java API</description>
        <url>http://devicemap.apache.org/</url>


Reply via email to