[tor-commits] [collector/release] Remove dependency on metrics-lib's log package (4/4).

karsten at torproject.org karsten at torproject.org
Wed Jan 15 22:25:08 UTC 2020


commit 8263cc7bdbb0a632f12a84fb2051dd9a25c28142
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Sat Nov 23 18:07:41 2019 +0100

    Remove dependency on metrics-lib's log package (4/4).
    
     - Remove package-internal abstract class.
---
 .../collector/webstats/LogDescriptorImpl.java      | 117 ---------------------
 .../collector/webstats/SanitizeWeblogs.java        |   2 +-
 .../collector/webstats/WebServerAccessLogImpl.java |  82 +++++++++++++--
 3 files changed, 73 insertions(+), 128 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/collector/webstats/LogDescriptorImpl.java b/src/main/java/org/torproject/metrics/collector/webstats/LogDescriptorImpl.java
deleted file mode 100644
index e20be27..0000000
--- a/src/main/java/org/torproject/metrics/collector/webstats/LogDescriptorImpl.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/* Copyright 2017--2018 The Tor Project
- * See LICENSE for licensing information */
-
-package org.torproject.metrics.collector.webstats;
-
-import org.torproject.descriptor.DescriptorParseException;
-import org.torproject.descriptor.LogDescriptor;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * Base class for log descriptors.
- *
- * @since 2.2.0
- */
-public abstract class LogDescriptorImpl implements LogDescriptor {
-
-  /** Logfile name parts separator. */
-  public static final String SEP = "_";
-
-  /** The log's file name should contain this string. */
-  public static final String MARKER = ".log";
-
-  private static Pattern filenamePattern = Pattern.compile(
-      "(?:\\S*)" + MARKER + SEP + "(?:[0-9a-zA-Z]*)(?:\\.?)([a-zA-Z2]*)");
-
-  private final File descriptorFile;
-
-  /** Byte array for plain, i.e. uncompressed, log data. */
-  private byte[] logBytes;
-
-  private FileType fileType;
-
-  private List<String> unrecognizedLines = new ArrayList<>();
-
-  /**
-   * This constructor performs basic operations on the given bytes.
-   *
-   * <p>An unknown compression type (see {@link #getCompressionType})
-   * is interpreted as missing compression.  In this case the bytes
-   * will be compressed to the given compression type.</p>
-   *
-   * @since 2.2.0
-   */
-  protected LogDescriptorImpl(byte[] logBytes, File descriptorFile,
-      String logName) throws DescriptorParseException {
-    this.logBytes = logBytes;
-    this.descriptorFile = descriptorFile;
-    try {
-      Matcher mat = filenamePattern.matcher(logName);
-      if (!mat.find()) {
-        throw new DescriptorParseException(
-            "Log file name doesn't comply to standard: " + logName);
-      }
-      this.fileType = FileType.findType(mat.group(1).toUpperCase());
-      if (FileType.PLAIN == this.fileType) {
-        this.fileType = FileType.XZ;
-        this.logBytes = this.fileType.compress(this.logBytes);
-      }
-    } catch (Exception ex) {
-      throw new DescriptorParseException("Cannot parse file "
-          + logName + " from file " + descriptorFile.getName(), ex);
-    }
-  }
-
-  @Override
-  public InputStream decompressedByteStream() throws DescriptorParseException {
-    try {
-      return this.fileType.decompress(new ByteArrayInputStream(this.logBytes));
-    } catch (Exception ex) {
-      throw new DescriptorParseException("Cannot provide deflated stream of "
-          + this.descriptorFile + ".", ex);
-    }
-  }
-
-  public String getCompressionType() {
-    return this.fileType.name().toLowerCase();
-  }
-
-  @Override
-  public byte[] getRawDescriptorBytes() {
-    return this.logBytes;
-  }
-
-  public void setRawDescriptorBytes(byte[] bytes) {
-    this.logBytes = bytes;
-  }
-
-  @Override
-  public int getRawDescriptorLength() {
-    return this.logBytes.length;
-  }
-
-  @Override
-  public List<String> getAnnotations() {
-    return Collections.emptyList();
-  }
-
-  @Override
-  public List<String> getUnrecognizedLines() {
-    return this.unrecognizedLines;
-  }
-
-  @Override
-  public File getDescriptorFile() {
-    return descriptorFile;
-  }
-
-}
-
diff --git a/src/main/java/org/torproject/metrics/collector/webstats/SanitizeWeblogs.java b/src/main/java/org/torproject/metrics/collector/webstats/SanitizeWeblogs.java
index 51318f8..fc7c64f 100644
--- a/src/main/java/org/torproject/metrics/collector/webstats/SanitizeWeblogs.java
+++ b/src/main/java/org/torproject/metrics/collector/webstats/SanitizeWeblogs.java
@@ -135,7 +135,7 @@ public class SanitizeWeblogs extends CollecTorMain {
 
   private void storeSortedAndForget(String virtualHost, String physicalHost,
       LocalDate date, Map<String, Long> lineCounts) {
-    String name = new StringJoiner(LogDescriptorImpl.SEP)
+    String name = new StringJoiner(WebServerAccessLogImpl.SEP)
         .add(virtualHost).add(physicalHost)
         .add(WebServerAccessLogImpl.MARKER)
         .add(date.format(DateTimeFormatter.BASIC_ISO_DATE))
diff --git a/src/main/java/org/torproject/metrics/collector/webstats/WebServerAccessLogImpl.java b/src/main/java/org/torproject/metrics/collector/webstats/WebServerAccessLogImpl.java
index 4ec9e39..af77c94 100644
--- a/src/main/java/org/torproject/metrics/collector/webstats/WebServerAccessLogImpl.java
+++ b/src/main/java/org/torproject/metrics/collector/webstats/WebServerAccessLogImpl.java
@@ -7,13 +7,15 @@ import org.torproject.descriptor.DescriptorParseException;
 import org.torproject.descriptor.WebServerAccessLog;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.nio.file.Paths;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -26,8 +28,10 @@ import java.util.stream.Stream;
  *
  * @since 2.2.0
  */
-public class WebServerAccessLogImpl extends LogDescriptorImpl
-    implements WebServerAccessLog {
+public class WebServerAccessLogImpl implements WebServerAccessLog {
+
+  /** Logfile name parts separator. */
+  public static final String SEP = "_";
 
   /** The log's name should include this string. */
   public static final String MARKER = "access.log";
@@ -37,6 +41,15 @@ public class WebServerAccessLogImpl extends LogDescriptorImpl
       = Pattern.compile("(\\S*)" + SEP + "(\\S*)" + SEP + "" + MARKER
       + SEP + "(\\d*)(?:\\.?)([a-zA-Z]*)");
 
+  private final File descriptorFile;
+
+  /** Byte array for plain, i.e. uncompressed, log data. */
+  private byte[] logBytes;
+
+  private FileType fileType;
+
+  private List<String> unrecognizedLines = new ArrayList<>();
+
   private final String physicalHost;
 
   private final String virtualHost;
@@ -58,25 +71,31 @@ public class WebServerAccessLogImpl extends LogDescriptorImpl
    * The immediate parent name is taken to be the physical host collecting the
    * logs.</p>
    */
-  protected WebServerAccessLogImpl(byte[] logBytes, File file, String logName)
-      throws DescriptorParseException {
-    super(logBytes, file, logName);
+  protected WebServerAccessLogImpl(byte[] logBytes, File descriptorFile,
+      String logName) throws DescriptorParseException {
+    this.logBytes = logBytes;
+    this.descriptorFile = descriptorFile;
     try {
-      String fn = Paths.get(logName).getFileName().toString();
-      Matcher mat = filenamePattern.matcher(fn);
+      Matcher mat = filenamePattern.matcher(logName);
       if (!mat.find()) {
         throw new DescriptorParseException(
-            "WebServerAccessLog file name doesn't comply to standard: " + fn);
+            "Log file name doesn't comply to standard: " + logName);
       }
       this.virtualHost = mat.group(1);
       this.physicalHost = mat.group(2);
       if (null == this.virtualHost || null == this.physicalHost
           || this.virtualHost.isEmpty() || this.physicalHost.isEmpty()) {
         throw new DescriptorParseException(
-            "WebServerAccessLog file name doesn't comply to standard: " + fn);
+            "WebServerAccessLog file name doesn't comply to standard: "
+            + logName);
       }
       String ymd = mat.group(3);
       this.logDate = LocalDate.parse(ymd, DateTimeFormatter.BASIC_ISO_DATE);
+      this.fileType = FileType.findType(mat.group(4).toUpperCase());
+      if (FileType.PLAIN == this.fileType) {
+        this.fileType = FileType.XZ;
+        this.logBytes = this.fileType.compress(this.logBytes);
+      }
     } catch (DescriptorParseException dpe) {
       throw dpe; // escalate
     } catch (Exception pe) {
@@ -86,6 +105,49 @@ public class WebServerAccessLogImpl extends LogDescriptorImpl
   }
 
   @Override
+  public InputStream decompressedByteStream() throws DescriptorParseException {
+    try {
+      return this.fileType.decompress(new ByteArrayInputStream(this.logBytes));
+    } catch (Exception ex) {
+      throw new DescriptorParseException("Cannot provide deflated stream of "
+          + this.descriptorFile + ".", ex);
+    }
+  }
+
+  public String getCompressionType() {
+    return this.fileType.name().toLowerCase();
+  }
+
+  @Override
+  public byte[] getRawDescriptorBytes() {
+    return this.logBytes;
+  }
+
+  public void setRawDescriptorBytes(byte[] bytes) {
+    this.logBytes = bytes;
+  }
+
+  @Override
+  public int getRawDescriptorLength() {
+    return this.logBytes.length;
+  }
+
+  @Override
+  public List<String> getAnnotations() {
+    return Collections.emptyList();
+  }
+
+  @Override
+  public List<String> getUnrecognizedLines() {
+    return this.unrecognizedLines;
+  }
+
+  @Override
+  public File getDescriptorFile() {
+    return descriptorFile;
+  }
+
+  @Override
   public String getPhysicalHost() {
     return this.physicalHost;
   }





More information about the tor-commits mailing list