From 4fee68ef7fe77e417a21a84d7b23ec8d03c455db Mon Jan 18 00:00:00 2016
From: iwakeh <iwakeh@torproject.org>
Date: Tue, 3 May 2016 20:00:52 +0000
Subject: [PATCH] Provide executable jar containing all dependencies, removed
 obsolete ant-tasks, changed scripts for implementing task-18719.

---
 bin/run-bridgedescs                    |  2 +-
 bin/run-bridgepools                    |  2 +-
 bin/run-exitlists                      |  2 +-
 bin/run-relaydescs                     |  2 +-
 bin/run-torperf                        |  2 +-
 bin/update-index                       |  2 +-
 build.xml                              | 56 +++++-----------------
 src/org/torproject/collector/Main.java | 85 ++++++++++++++++++++++++++++++++++
 8 files changed, 103 insertions(+), 50 deletions(-)
 create mode 100644 src/org/torproject/collector/Main.java

diff --git a/bin/run-bridgedescs b/bin/run-bridgedescs
index 78e4cab..37eeea5 100755
--- a/bin/run-bridgedescs
+++ b/bin/run-bridgedescs
@@ -1,3 +1,3 @@
 #!/bin/sh
-ant bridgedescs | grep "\[java\]"
+java -Xmx2g -jar collector-0.9.0-dev.jar bridgedescs
 
diff --git a/bin/run-bridgepools b/bin/run-bridgepools
index 7c34e91..9c5c53a 100755
--- a/bin/run-bridgepools
+++ b/bin/run-bridgepools
@@ -1,3 +1,3 @@
 #!/bin/sh
-ant bridgepools | grep "\[java\]"
+java -Xmx2g -jar collector-0.9.0-dev.jar bridgepools
 
diff --git a/bin/run-exitlists b/bin/run-exitlists
index 20064d6..3bdef11 100755
--- a/bin/run-exitlists
+++ b/bin/run-exitlists
@@ -1,3 +1,3 @@
 #!/bin/sh
-ant exitlists | grep "\[java\]"
+java -Xmx2g -jar collector-0.9.0-dev.jar exitlists
 
diff --git a/bin/run-relaydescs b/bin/run-relaydescs
index e39c31a..8ad7506 100755
--- a/bin/run-relaydescs
+++ b/bin/run-relaydescs
@@ -1,3 +1,3 @@
 #!/bin/sh
-ant relaydescs | grep "\[java\]"
+java -Xmx2g -jar collector-0.9.0-dev.jar relaydescs
 
diff --git a/bin/run-torperf b/bin/run-torperf
index c1bc2ba..267df58 100755
--- a/bin/run-torperf
+++ b/bin/run-torperf
@@ -1,3 +1,3 @@
 #!/bin/sh
-ant torperf | grep "\[java\]"
+java -Xmx2g -jar collector-0.9.0-dev.jar torperf
 
diff --git a/bin/update-index b/bin/update-index
index 4a3f2f8..70b0067 100755
--- a/bin/update-index
+++ b/bin/update-index
@@ -1,3 +1,3 @@
 #!/bin/sh
-ant updateindex | grep "\[java\]"
+java -Xmx2g -jar collector-0.9.0-dev.jar updateindex
 
diff --git a/build.xml b/build.xml
index 8e99076..e3543f4 100644
--- a/build.xml
+++ b/build.xml
@@ -18,14 +18,17 @@
   <property name="jarfile" value="collector-${release.version}.jar" />
   <property name="jarsourcesfile" value="collector-${release.version}-sources.jar" />
   <property name="jarjavadocfile" value="collector-${release.version}-javadoc.jar" />
-  <path id="classpath">
-    <pathelement path="${classes}"/>
-    <fileset dir="${libs}">
+  <patternset id="runtime" >
     <include name="commons-codec-1.9.jar"/>
     <include name="commons-compress-1.9.jar"/>
     <include name="gson-2.2.4.jar"/>
     <include name="xz-1.5.jar"/>
     <include name="descriptor-${descriptorversion}.jar"/>
+  </patternset>
+  <path id="classpath">
+    <pathelement path="${classes}"/>
+    <fileset dir="${libs}">
+      <patternset refid="runtime" />
     </fileset>
   </path>
   <path id="checkstyle.classpath" >
@@ -114,11 +117,17 @@
     </exec>
     <jar destfile="${jarfile}"
          basedir="${classes}">
+      <fileset dir="${classes}"/>
+      <zipgroupfileset dir="${libs}" >
+        <patternset refid="runtime" />
+      </zipgroupfileset>
       <manifest>
         <attribute name="Created-By" value="The Tor Project" />
         <attribute name="Implementation-Title" value="${name}"/>
         <attribute name="Implementation-Version"
                    value="${release.version}-${git.revision}"/>
+        <attribute name="Main-Class"
+                   value="org.torproject.collector.Main" />
       </manifest>
     </jar>
     <jar destfile="${jarsourcesfile}">
@@ -197,46 +206,5 @@
       </batchtest>
     </junit>
   </target>
-  <target name="bridgedescs" depends="compile">
-    <java fork="true"
-          maxmemory="2048m"
-          classname="org.torproject.collector.bridgedescs.SanitizedBridgesWriter">
-      <classpath refid="classpath"/>
-    </java>
-  </target>
-  <target name="bridgepools" depends="compile">
-    <java fork="true"
-          maxmemory="2048m"
-          classname="org.torproject.collector.bridgepools.BridgePoolAssignmentsProcessor">
-      <classpath refid="classpath"/>
-    </java>
-  </target>
-  <target name="exitlists" depends="compile">
-    <java fork="true"
-          maxmemory="2048m"
-          classname="org.torproject.collector.exitlists.ExitListDownloader">
-      <classpath refid="classpath"/>
-    </java>
-  </target>
-  <target name="relaydescs" depends="compile">
-    <java fork="true"
-          maxmemory="2048m"
-          classname="org.torproject.collector.relaydescs.ArchiveWriter">
-      <classpath refid="classpath"/>
-    </java>
-  </target>
-  <target name="torperf" depends="compile">
-    <java fork="true"
-          maxmemory="2048m"
-          classname="org.torproject.collector.torperf.TorperfDownloader">
-      <classpath refid="classpath"/>
-    </java>
-  </target>
-  <target name="updateindex" depends="compile">
-    <java fork="true"
-          classname="org.torproject.collector.index.CreateIndexJson">
-      <classpath refid="classpath"/>
-    </java>
-  </target>
 </project>
 
diff --git a/src/org/torproject/collector/Main.java b/src/org/torproject/collector/Main.java
new file mode 100644
index 0000000..44182ed
--- /dev/null
+++ b/src/org/torproject/collector/Main.java
@@ -0,0 +1,85 @@
+/* Copyright 2016 The Tor Project
+ * See LICENSE for licensing information */
+
+package org.torproject.collector;
+
+import org.torproject.collector.bridgedescs.SanitizedBridgesWriter;
+import org.torproject.collector.bridgepools.BridgePoolAssignmentsProcessor;
+import org.torproject.collector.exitlists.ExitListDownloader;
+import org.torproject.collector.index.CreateIndexJson;
+import org.torproject.collector.relaydescs.ArchiveWriter;
+import org.torproject.collector.torperf.TorperfDownloader;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Logger;
+
+/**
+ * Main class for starting a CollecTor instance.
+ * <br>
+ * Run without arguments in order to read the usage information, i.e.
+ * <br>
+ * <code>java -jar collector.jar</code>
+ */
+public class Main {
+
+  private static Logger log = Logger.getLogger(Main.class.getName());
+
+  /** All possible main classes.
+   * If a new CollecTorMain class is available, just add it to this map.
+   */
+  private static final Map<String, Class> collecTorMains = new HashMap<>();
+
+  static { // add a new main class here
+    collecTorMains.put("bridgedescs", SanitizedBridgesWriter.class);
+    collecTorMains.put("bridgepools", BridgePoolAssignmentsProcessor.class);
+    collecTorMains.put("exitlists", ExitListDownloader.class);
+    collecTorMains.put("updateindex", CreateIndexJson.class);
+    collecTorMains.put("relaydescs", ArchiveWriter.class);
+    collecTorMains.put("torperf", TorperfDownloader.class);
+  }
+
+  private static final String modules = collecTorMains.keySet().toString()
+      .replace("[", "").replace("]", "").replaceAll(", ", "|");
+
+  /**
+   * One argument is necessary.
+   * See class description {@link Main}.
+   */
+  public static void main(String[] args) {
+    if (null == args || args.length != 1) {
+      printUsageAndExit("CollecTor needs exactly one argument.");
+    } else {
+      invokeGivenMainAndExit(args[0]);
+    }
+  }
+
+  private static void printUsageAndExit(String msg) {
+    final String usage = "Usage:\njava -jar collector.jar "
+        + "<" + modules + ">";
+    System.out.println(msg + "\n" + usage);
+    System.exit(0);
+  }
+
+  private static void invokeGivenMainAndExit(String mainId) {
+    Class clazz = collecTorMains.get(mainId);
+    if (null == clazz) {
+      printUsageAndExit("Unknown argument: " + mainId);
+    }
+    invokeMainOnClassAndExit(clazz);
+  }
+
+  private static void invokeMainOnClassAndExit(Class clazz) {
+    try {
+      clazz.getMethod("main", new Class[] { String[].class })
+          .invoke(null, (Object) new String[]{});
+      System.exit(0);
+    } catch (NoSuchMethodException | IllegalAccessException
+       | InvocationTargetException e) {
+      log.severe("Cannot invoke 'main' method on "
+          + clazz.getName() + ". " + e);
+    }
+  }
+}
+
-- 
2.5.0

