[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [collector/master] Remove dependency on metrics-lib's internal package.



commit 5a0e6be21c2de4b35e4111364b3ecd7caf424164
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Fri Nov 22 17:54:26 2019 +0100

    Remove dependency on metrics-lib's internal package.
    
    The only functionality contained in metrics-lib's internal package is
    file (de-)compression, which in turn uses a third-party library that
    we're using anyway. This is a weak reason for depending on our own
    library for this functionality. Removing this dependency will make it
    easier to make changes to our library in the future.
    
    The new FileType class is based on a copy of the same enum type in
    metrics-lib without @since tags and without methods that we don't use.
---
 CHANGELOG.md                                       |  3 +
 .../persist/WebServerAccessLogPersistence.java     |  2 +-
 .../metrics/collector/webstats/FileType.java       | 78 ++++++++++++++++++++++
 .../metrics/collector/webstats/LogMetadata.java    |  2 -
 .../collector/webstats/SanitizeWeblogs.java        |  1 -
 5 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b55d7a4..73abdea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,9 @@
    - Give up on periodically checking the configuration file for
      updates and reloading it in case of changes.
 
+ * Minor changes
+   - Remove dependency on metrics-lib's internal package.
+
 
 # Changes in version 1.13.1 - 2019-11-11
 
diff --git a/src/main/java/org/torproject/metrics/collector/persist/WebServerAccessLogPersistence.java b/src/main/java/org/torproject/metrics/collector/persist/WebServerAccessLogPersistence.java
index b10f706..848fa2e 100644
--- a/src/main/java/org/torproject/metrics/collector/persist/WebServerAccessLogPersistence.java
+++ b/src/main/java/org/torproject/metrics/collector/persist/WebServerAccessLogPersistence.java
@@ -4,8 +4,8 @@
 package org.torproject.metrics.collector.persist;
 
 import org.torproject.descriptor.WebServerAccessLog;
-import org.torproject.descriptor.internal.FileType;
 import org.torproject.descriptor.log.InternalWebServerAccessLog;
+import org.torproject.metrics.collector.webstats.FileType;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
diff --git a/src/main/java/org/torproject/metrics/collector/webstats/FileType.java b/src/main/java/org/torproject/metrics/collector/webstats/FileType.java
new file mode 100644
index 0000000..79dcf21
--- /dev/null
+++ b/src/main/java/org/torproject/metrics/collector/webstats/FileType.java
@@ -0,0 +1,78 @@
+/* Copyright 2016--2019 The Tor Project
+ * See LICENSE for licensing information */
+
+package org.torproject.metrics.collector.webstats;
+
+import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
+import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
+import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
+import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * These enums provide compression functionality.
+ *
+ * <p>{@link #findType} determines the compression type by the given extension.
+ * Compression can also be zero-compression, which is currently provided by
+ * the PLAIN and JSON enums.</p>
+ */
+public enum FileType {
+
+  BZ2(BZip2CompressorInputStream.class, BZip2CompressorOutputStream.class),
+  GZ(GzipCompressorInputStream.class, GzipCompressorOutputStream.class),
+  JSON(BufferedInputStream.class, BufferedOutputStream.class),
+  PLAIN(BufferedInputStream.class, BufferedOutputStream.class),
+  XZ(XZCompressorInputStream.class, XZCompressorOutputStream.class);
+
+  private final Class<? extends InputStream> inClass;
+  private final Class<? extends OutputStream> outClass;
+
+  FileType(Class<? extends InputStream> in, Class<? extends OutputStream> out) {
+    this.inClass = in;
+    this.outClass = out;
+  }
+
+  /**
+   * Returns {@code valueOf} or the default enum {@link #PLAIN}, i.e.,
+   * this method doesn't throw any exceptions and allways returns a valid enum.
+   */
+  public static FileType findType(String ext) {
+    FileType res;
+    try {
+      res = FileType.valueOf(ext.toUpperCase());
+      return res;
+    } catch (IllegalArgumentException | NullPointerException re) {
+      return PLAIN;
+    }
+  }
+
+  /**
+   * Return the appropriate input stream.
+   */
+  public InputStream inputStream(InputStream is) throws Exception {
+    return this.inClass.getConstructor(new Class[]{InputStream.class})
+        .newInstance(is);
+  }
+
+  /**
+   * Return the appropriate output stream.
+   */
+  public OutputStream outputStream(OutputStream os) throws Exception {
+    return this.outClass.getConstructor(new Class[]{OutputStream.class})
+        .newInstance(os);
+  }
+
+  /**
+   * Decompresses the given InputStream and returns an OutputStream.
+   */
+  public InputStream decompress(InputStream is) throws Exception {
+    return this.inputStream(is);
+  }
+}
+
diff --git a/src/main/java/org/torproject/metrics/collector/webstats/LogMetadata.java b/src/main/java/org/torproject/metrics/collector/webstats/LogMetadata.java
index c74cbcb..b04fcb5 100644
--- a/src/main/java/org/torproject/metrics/collector/webstats/LogMetadata.java
+++ b/src/main/java/org/torproject/metrics/collector/webstats/LogMetadata.java
@@ -5,8 +5,6 @@ package org.torproject.metrics.collector.webstats;
 
 import static org.torproject.descriptor.log.WebServerAccessLogImpl.MARKER;
 
-import org.torproject.descriptor.internal.FileType;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
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 027cfde..5c001b6 100644
--- a/src/main/java/org/torproject/metrics/collector/webstats/SanitizeWeblogs.java
+++ b/src/main/java/org/torproject/metrics/collector/webstats/SanitizeWeblogs.java
@@ -12,7 +12,6 @@ import static java.util.stream.Collectors.summingLong;
 import org.torproject.descriptor.DescriptorParseException;
 import org.torproject.descriptor.Method;
 import org.torproject.descriptor.WebServerAccessLog;
-import org.torproject.descriptor.internal.FileType;
 import org.torproject.descriptor.log.InternalLogDescriptor;
 import org.torproject.descriptor.log.InternalWebServerAccessLog;
 import org.torproject.descriptor.log.WebServerAccessLogImpl;

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits