Browse Source

Minor refactoring.

Matt Clark 1 year ago
parent
commit
2f2b6117e8

+ 0 - 28
.classpath

@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="src" output="target/classes" path="src/main/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="src" path="resources"/>
-	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-			<attribute name="test" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="output" path="target/classes"/>
-</classpath>

+ 2 - 0
.gitignore

@@ -1,3 +1,5 @@
+.classpath
+.projet
 .settings/
 target/
 cache/

+ 0 - 23
.project

@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
-	<name>server</name>
-	<comment></comment>
-	<projects>
-	</projects>
-	<buildSpec>
-		<buildCommand>
-			<name>org.eclipse.jdt.core.javabuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-		<buildCommand>
-			<name>org.eclipse.m2e.core.maven2Builder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-	</buildSpec>
-	<natures>
-		<nature>org.eclipse.jdt.core.javanature</nature>
-		<nature>org.eclipse.m2e.core.maven2Nature</nature>
-	</natures>
-</projectDescription>

+ 2 - 0
config/admin.conf

@@ -0,0 +1,2 @@
+admin=BarcodeAPI.org
+contact=info@barcodeapi.org

+ 0 - 0
authlist.conf → config/authlist.conf


+ 0 - 0
blacklist.conf → config/blacklist.conf


+ 1 - 4
pom.xml

@@ -5,10 +5,9 @@
 
 	<groupId>org.barcodeapi</groupId>
 	<artifactId>server</artifactId>
-	<name>server</name>
-
 	<version>0.0.1-SNAPSHOT</version>
 
+	<name>server</name>
 	<packaging>jar</packaging>
 
 	<properties>
@@ -37,7 +36,6 @@
 						<descriptorRef>jar-with-dependencies</descriptorRef>
 					</descriptorRefs>
 				</configuration>
-
 				<executions>
 					<execution>
 						<id>make-assembly</id>
@@ -52,7 +50,6 @@
 	</build>
 
 	<dependencies>
-
 		<dependency>
 			<groupId>org.eclipse.jetty</groupId>
 			<artifactId>jetty-server</artifactId>

BIN
resources/.DS_Store


+ 3 - 0
resources/VERSION

@@ -0,0 +1,3 @@
+${project.name}
+${project.description}
+${project.version}

+ 27 - 3
src/main/java/org/barcodeapi/core/ServerRuntime.java

@@ -1,18 +1,34 @@
 package org.barcodeapi.core;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.UUID;
 
 public class ServerRuntime {
 
 	// System runtime identifier
-	private static final String _RUNTIME_ID = UUID.randomUUID().toString();
+	private static final String _RUNTIME_ID;
+	private static final long _RUNTIME_TIMESTART;
+	private static final String _RUNTIME_VERSION;
+	private static final String _RUNTIME_HOST;
+
+	static {
+
+		_RUNTIME_ID = UUID.randomUUID().toString();
+		_RUNTIME_TIMESTART = System.currentTimeMillis();
+		_RUNTIME_VERSION = "";
+
+		try {
+			_RUNTIME_HOST = InetAddress.getLocalHost().getCanonicalHostName();
+		} catch (UnknownHostException e) {
+			throw new RuntimeException(e);
+		}
+	}
 
 	public static final String getRuntimeID() {
 		return _RUNTIME_ID;
 	}
 
-	private static final long _RUNTIME_TIMESTART = System.currentTimeMillis();
-
 	public static final long getTimeStart() {
 		return _RUNTIME_TIMESTART;
 	}
@@ -20,4 +36,12 @@ public class ServerRuntime {
 	public static final long getTimeRunning() {
 		return System.currentTimeMillis() - getTimeStart();
 	}
+
+	public static final String getVersion() {
+		return _RUNTIME_VERSION;
+	}
+
+	public static final String getHostname() {
+		return _RUNTIME_HOST;
+	}
 }

+ 3 - 2
src/main/java/org/barcodeapi/server/api/AboutHandler.java

@@ -22,9 +22,10 @@ public class AboutHandler extends RestHandler {
 
 		response.getOutputStream().println((new JSONObject()//
 				.put("runtimeId", ServerRuntime.getRuntimeID())//
-				.put("uptime", "---")//
+				.put("uptime", ServerRuntime.getTimeRunning())//
 				.put("admin", "---")//
-				.put("hostname", "---")//
+				.put("hostname", ServerRuntime.getHostname())//
+				.put("version", ServerRuntime.getVersion())//
 		).toString(4));
 	}
 }

+ 1 - 1
src/main/java/org/barcodeapi/server/core/Authlist.java

@@ -12,7 +12,7 @@ public class Authlist {
 
 		try {
 
-			authlist = Files.readAllLines(Paths.get("authlist.conf"));
+			authlist = Files.readAllLines(Paths.get("config/authlist.conf"));
 		} catch (Exception e) {
 
 			throw new RuntimeException("Failed to initialize authlist.");

+ 1 - 1
src/main/java/org/barcodeapi/server/core/Blacklist.java

@@ -12,7 +12,7 @@ public class Blacklist {
 
 		try {
 
-			blacklist = Files.readAllLines(Paths.get("blacklist.conf"));
+			blacklist = Files.readAllLines(Paths.get("config/blacklist.conf"));
 		} catch (Exception e) {
 
 			throw new RuntimeException("Failed to initialize blacklist.");

+ 13 - 13
src/main/java/org/barcodeapi/server/gen/CodeType.java

@@ -29,7 +29,7 @@ public enum CodeType {
 			"^[0-9]{8}$", //
 			"^[0-9]{7,8}$", //
 			"01023459", //
-			""),
+			"A UPC type barcode, derived from the longer EAN-13 code, for use on smaller pakages."),
 
 	/**
 	 * EAN-13 type UPC code;
@@ -40,7 +40,7 @@ public enum CodeType {
 			"^[0-9]{13}$", //
 			"^[0-9]{12,13}$", //
 			"1234567890128", //
-			""),
+			"A globally recognized product identification code."),
 
 	/**
 	 * Codabar type code;
@@ -49,7 +49,7 @@ public enum CodeType {
 			"^[0-9:$]{4,12}$", //
 			"^[0-9-:$\\/.+]+$", //
 			"1234567890", //
-			""),
+			"An early barode designed to be printer on dot-matrix printers."),
 
 	/**
 	 * Code39 type code;
@@ -60,7 +60,7 @@ public enum CodeType {
 			"^[A-Z0-9 $.\\/]{1,12}$", //
 			"^[A-Z*0-9 -$%.\\/+]+$", //
 			"TRY 39 ME", //
-			""),
+			"A basic alphanumeric barcode."),
 
 	/**
 	 * Code128 type code;
@@ -71,7 +71,7 @@ public enum CodeType {
 			"^[ !#$()*.\\/0-9=?A-Z_a-z~]{1,16}$", //
 			"^[ !\"#$%&'()*+,-.\\/0-9:;<=>?@A-Z\\[\\\\\\]^_`a-z{|}~]+$", //
 			"Try Me!", //
-			""),
+			"A high desnisty alphanumeric barcode."),
 
 	/**
 	 * Aztec type barcode.
@@ -81,8 +81,8 @@ public enum CodeType {
 	Aztec(new String[] { "aztec" }, //
 			"^[ !#$()*.\\/0-9=?A-Z_a-z~]{1,16}$", //
 			"^[ !\"#$%&'()*+,-.\\/0-9:;<=>?@A-Z\\[\\\\\\]^_`a-z{|}~]+$", //
-			"", //
-			""),
+			"Aztec Barcode", //
+			"A 2D data barcode whose timing markers radiate outward from the middle."),
 
 	/**
 	 * QR type code;
@@ -92,8 +92,8 @@ public enum CodeType {
 	QRCode(new String[] { "qr", "qr-code", "qrcode" }, //
 			"^.{1,64}$", //
 			"^.{1,65535}$", //
-			"", //
-			""),
+			"QR Barcode", //
+			"A 2D data barcode whose timing markers are aligned at the corners."),
 
 	/**
 	 * Data Matrix type code;
@@ -103,8 +103,8 @@ public enum CodeType {
 	DataMatrix(new String[] { "dm", "data-matrix", "datamatrix", "matrix", "data" }, //
 			"^[ !\"#$%&'()*+,-.\\/0-9:;<=>?@A-Z\\[\\\\\\]^_`a-z{|}~]{1,2335}$", //
 			"^[ !\"#$%&'()*+,-.\\/0-9:;<=>?@A-Z\\[\\\\\\]^_`a-z{|}~]{1,2335}$", //
-			"", //
-			""),
+			"Data Matrix Barcode", //
+			"A 2D data barode whose timing markers are along the the edges."),
 
 	/**
 	 * PDF417
@@ -114,8 +114,8 @@ public enum CodeType {
 	PDF417(new String[] { "417", "pdf417", "pdf" }, //
 			"^[ !\"#$%&'()*+,-.\\/0-9:;<=>?@A-Z\\[\\\\\\]^_`a-z{|}~]{1,2335}$", //
 			"^[ !\"#$%&'()*+,-.\\/0-9:;<=>?@A-Z\\[\\\\\\]^_`a-z{|}~]{1,2335}$", //
-			"", //
-			"");
+			"PDF - 417", //
+			"A stacked linear barcode most commonly found on identification cards.");
 
 	/**
 	 * Local Variables

+ 7 - 2
src/main/java/org/barcodeapi/server/statistics/StatsCollector.java

@@ -11,7 +11,7 @@ public class StatsCollector {
 
 	public StatsCollector() {
 
-		counterCache.put("runtimeId", ServerRuntime.getRuntimeID());
+		setValue(ServerRuntime.getRuntimeID(), "system", "runtimeId");
 	}
 
 	/**
@@ -79,7 +79,7 @@ public class StatsCollector {
 	 * @param name
 	 * @param value
 	 */
-	public void setCounter(double value, String... name) {
+	public void setValue(Object value, String... name) {
 
 		synchronized (counterCache) {
 
@@ -110,6 +110,11 @@ public class StatsCollector {
 		return counterCache;
 	}
 
+	/**
+	 * Get a reference to the shared statistics collector.
+	 * 
+	 * @return
+	 */
 	public static synchronized StatsCollector getInstance() {
 
 		if (statsCollector == null) {

+ 10 - 10
src/main/java/org/barcodeapi/server/tasks/WatchdogTask.java

@@ -23,34 +23,34 @@ public class WatchdogTask extends BackgroundTask {
 	public void onRun() {
 
 		// update system runtime statistics
-		getStats().setCounter(System.currentTimeMillis(), "system", "time");
-		getStats().setCounter(ServerRuntime.getTimeRunning(), "system", "uptime");
+		getStats().setValue(System.currentTimeMillis(), "system", "time");
+		getStats().setValue(ServerRuntime.getTimeRunning(), "system", "uptime");
 
 		// update jvm memory statistics
 		double memUsed = (double) (runtime.totalMemory() - runtime.freeMemory());
-		getStats().setCounter(memUsed, "system", "jvm", "memory", "used");
+		getStats().setValue(memUsed, "system", "jvm", "memory", "used");
 
 		double memFree = (double) (runtime.freeMemory());
-		getStats().setCounter(memFree, "system", "jvm", "memory", "free");
+		getStats().setValue(memFree, "system", "jvm", "memory", "free");
 
 		double memTotal = (double) (runtime.totalMemory());
-		getStats().setCounter(memTotal, "system", "jvm", "memory", "total");
+		getStats().setValue(memTotal, "system", "jvm", "memory", "total");
 
 		double memMax = (double) (runtime.maxMemory());
-		getStats().setCounter(memMax, "system", "jvm", "memory", "max");
+		getStats().setValue(memMax, "system", "jvm", "memory", "max");
 
 		// update jvm gc statistics
 		for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
 
 			String name = gc.getName().replace(" ", "_");
-			getStats().setCounter(gc.getCollectionCount(), "system", "jvm", "gc", name, "count");
-			getStats().setCounter(gc.getCollectionTime(), "system", "jvm", "gc", name, "time");
+			getStats().setValue(gc.getCollectionCount(), "system", "jvm", "gc", name, "count");
+			getStats().setValue(gc.getCollectionTime(), "system", "jvm", "gc", name, "time");
 		}
 
 		// update jvm thread statistics
 		Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
 		Thread[] threads = threadSet.toArray(new Thread[threadSet.size()]);
-		getStats().setCounter(threads.length, "system", "jvm", "threads", "count");
+		getStats().setValue(threads.length, "system", "jvm", "threads", "count");
 
 		// get state of each thread
 		HashMap<State, Integer> threadStates = new HashMap<>();
@@ -61,7 +61,7 @@ public class WatchdogTask extends BackgroundTask {
 
 		// update thread state counters
 		for (State state : threadStates.keySet()) {
-			getStats().setCounter(threadStates.get(state), "system", "jvm", "threads", "state", state.toString());
+			getStats().setValue(threadStates.get(state), "system", "jvm", "threads", "state", state.toString());
 		}
 	}
 }