Skip to content
Permalink
Browse files
Explicitly specifying storage directory as second parameter when laun…
…ching the server
  • Loading branch information
Jaroslav Tulach committed Mar 15, 2017
1 parent e5be764 commit 9e1e7398f9c26b9ca3245e50a09bf8427ccf43de
Showing 4 changed files with 35 additions and 22 deletions.
@@ -106,8 +106,9 @@
<argument>-cp</argument>
<argument>${project.build.directory}/${project.build.finalName}.jar</argument>
<argument>org.apidesign.gate.timing.server.Main</argument>
<argument>8080</argument>
<argument>${basedir}/data</argument>
</arguments>
<workingDirectory>${basedir}/data</workingDirectory>
</configuration>
</plugin>
<plugin>
@@ -1,5 +1,6 @@
package org.apidesign.gate.timing.server;

import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
@@ -19,11 +20,20 @@
final class Main implements ContainerResponseFilter {
public static void main(String... args) throws Exception {
int port = 8080;
if (args.length == 1) {
if (args.length >= 1) {
port = Integer.parseInt(args[0]);
}
URI u = new URI("http://0.0.0.0:" + port + "/");
HttpServer server = createServer(u);

File dir;
if (args.length >= 2) {
dir = new File(args[1]);
dir.mkdirs();
} else {
dir = null;
}

HttpServer server = createServer(u, dir);
System.err.println("Server running on following IP addresses:");
dumpIPs();
System.err.println("Server running, press Ctrl-C to stop it.");
@@ -36,11 +46,11 @@ public static void main(String... args) throws Exception {
}
}

static HttpServer createServer(URI u) {
static HttpServer createServer(URI u, File dir) {
ResourceConfig rc = new ResourceConfig(
TimingResource.class, ContactsResource.class, Main.class
);
final Storage storage = new Storage();
final Storage storage = dir == null ? Storage.empty() : Storage.forDirectory(dir);
rc.registerInstances(new AbstractBinder() {
@Override
protected void configure() {
@@ -53,15 +63,15 @@ protected void configure() {

@Override
public void filter(
ContainerRequestContext requestContext,
ContainerRequestContext requestContext,
ContainerResponseContext r
) throws IOException {
r.getHeaders().add("Access-Control-Allow-Origin", "*");
r.getHeaders().add("Access-Control-Allow-Credentials", "true");
r.getHeaders().add("Access-Control-Allow-Headers", "Content-Type");
r.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
}
}

private static void dumpIPs() throws Exception {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
@@ -19,20 +19,22 @@ final class Storage {
private final File dir;
private final Executor storage;

Storage() {
final String userDir = System.getProperty("user.dir");
if (userDir == null || userDir.isEmpty()) {
dir = null;
storage = new Executor() {
@Override
public void execute(Runnable command) {
}
};
} else {
dir = new File(userDir);
dir.mkdirs();
storage = Executors.newFixedThreadPool(1);
private Storage(File dir, Executor exec) {
this.dir = dir;
this.storage = exec;
}

public static Storage empty() {
class NoOp implements Executor {
@Override
public void execute(Runnable command) {
}
}
return new Storage(null, new NoOp());
}

public static Storage forDirectory(File dir) {
return new Storage(dir, Executors.newFixedThreadPool(1));
}

public <T> void scheduleStore(String prefix, Class<T> type, Collection<T> data) {
@@ -38,7 +38,7 @@ public void setUpMethod() throws Exception {
System.setProperty("user.dir", "");

URI serverURI = new URI("http://0.0.0.0:" + emptyPort + "/");
server = Main.createServer(serverURI);
server = Main.createServer(serverURI, null);
baseUri = serverURI.resolve("timing/");
}

0 comments on commit 9e1e739

Please sign in to comment.