Skip to content

Commit

Permalink
Adapted petshop demo application to use stand-alone application
Browse files Browse the repository at this point in the history
  • Loading branch information
kwakeroni committed Dec 5, 2018
1 parent aef2b08 commit d8b5fd6
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 240 deletions.
12 changes: 6 additions & 6 deletions parameters-application/parameters-app-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>com.sun.jersey</groupId>-->
<!--<artifactId>jersey-client</artifactId>-->
<!--<version>1.19.1</version>-->
<!--<scope>runtime</scope>-->
<!--</dependency>-->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.1</version>
<scope>runtime</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>com.sun.jersey</groupId>-->
<!--<artifactId>jersey-json</artifactId>-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package be.kwakeroni.parameters.app.support;

import java.io.IOException;
import java.io.UncheckedIOException;

public class MainWaiter {

public static void waitForExit() {
try {
System.out.println("Press 'q' to exit");
int input = 0;
while (input != 'q') {
// Thread.onSpinWait();
input = System.in.read();
}
} catch (IOException exc) {
throw new UncheckedIOException(exc);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected synchronized void start() throws IOException {

protected synchronized void stop() {
if (this.httpServer != null) {
LOG.info("Shutting down server at {}", this.httpServer.getAddress());
this.httpServer.stop(0);
this.httpServer = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private StaticContentFactory() {
}

public static StaticContent fromZip(Supplier<InputStream> zipFile, String indexFile, Path workDir) {
LOG.info("Buffering web application into {}", workDir);
LOG.info("Buffering web application into {}", workDir.toAbsolutePath());

try {
Files.createDirectories(workDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package be.kwakeroni.parameters.app;

import be.kwakeroni.parameters.app.support.MainWaiter;
import be.kwakeroni.parameters.backend.api.Configuration;
import be.kwakeroni.parameters.core.support.backend.ConfigurationSupport;
import be.kwakeroni.parameters.core.support.util.function.ThrowingPredicate;
Expand All @@ -13,29 +14,49 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.Properties;
import java.util.function.UnaryOperator;

import static be.kwakeroni.parameters.core.support.util.function.ThrowingFunction.unchecked;

public class Application {

private final Server server;

public static void main(String[] args) throws Exception {
initLogging();
try {
createServer(args)
.ifPresent(Application::start);
Optional<Application> app = create(args);
if (app.isPresent()) {
try (AutoCloseable closeable = app.get().start()) {
MainWaiter.waitForExit();
}
}
} catch (ParseException exc) {
System.out.println(exc.getMessage());
Opt.HELP.handle("");
}
}

public static Optional<Application> create(String... args) throws CLIException, ParseException {
return createServer(args)
.map(Application::new);
}

public static Optional<Application> create(Properties configurationProperties) {
return createServer(configurationProperties)
.map(Application::new);
}

private Application(Server server) {
this.server = server;
}

private static void initLogging() {
System.out.println("log4j.configuration=" + System.getProperty("log4j.configuration"));
if (System.getProperty("log4j.configuration") == null) {
Expand All @@ -45,20 +66,12 @@ private static void initLogging() {
}
}

private static void start(Server resource) {
try (Server server = resource) {
server.start();

while (true) {
// Thread.onSpinWait();
}

} catch (IOException exc) {
throw new UncheckedIOException(exc);
}
public AutoCloseable start() throws IOException {
this.server.start();
return this.server;
}

static Optional<Server> createServer(String[] args) throws Exception {
static Optional<Server> createServer(String[] args) throws CLIException, ParseException {
CommandLineParser clParser = new DefaultParser();
CommandLine commandLine = clParser.parse(CLI_OPTIONS, args);

Expand All @@ -71,6 +84,12 @@ static Optional<Server> createServer(String[] args) throws Exception {
return Optional.of(new Server());
}

static Optional<Server> createServer(Properties configurationProperties) {
Configuration configuration = ConfigurationSupport.of(configurationProperties);
ServerConfigurationProvider.setConfiguration(configuration);
return Optional.of(new Server());
}


private static final Options CLI_OPTIONS;

Expand Down Expand Up @@ -128,16 +147,20 @@ private String getOptKey() {
return (this.opt != null) ? this.opt : this.option.getLongOpt();
}

boolean handle(CommandLine commandLine) throws Exception {
boolean handle(CommandLine commandLine) throws CLIException {
if (commandLine.hasOption(getOptKey())) {
String optValue = commandLine.getOptionValue(getOptKey());
return handle(optValue);
}
return true;
}

boolean handle(String value) throws Exception {
return this.handler.test(value);
boolean handle(String value) throws CLIException {
try {
return this.handler.test(value);
} catch (Exception e) {
throw new CLIException(e);
}
}
}

Expand All @@ -155,4 +178,18 @@ private static String getJarFileName() {
.orElse("parameters-standalone-app.jar");

}

public static class CLIException extends Exception {
public CLIException(String message) {
super(message);
}

public CLIException(String message, Throwable cause) {
super(message, cause);
}

public CLIException(Throwable cause) {
super(cause);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class FileStorageProvider implements StorageProvider {
private final Path storageDirectory;

public FileStorageProvider(Path storageDirectory) {
if (! Files.exists(storageDirectory)){
throw new IllegalStateException("Storage directory does not exist: " + storageDirectory.toAbsolutePath());
}
this.storageDirectory = storageDirectory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static GroupDataStore getDefaultDataStoreSupplier() {
private static GroupDataStore persistentStore(Path location) {
FileStorageProvider provider = new FileStorageProvider(location);
PersistedGroupDataStore store = new PersistedGroupDataStore(provider);
LOG.info("Using persistent data store at {}", location);
LOG.info("Using persistent data store at {}", location.toAbsolutePath());
return store;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,36 @@ private <Q> Q tryInternalize0(BackendGroup<Q> group, JSONObject query, BackendWi
}

@Override
public Object externalizeValueQuery(ValueQuery<?> query, ClientWireFormatterContext context) {
public JSONObject externalizeValueQuery(ValueQuery<?> query, ClientWireFormatterContext context) {
return new JSONObject()
.put(TYPE, TYPE_VALUE)
.put(PARAMETER, query.getParameter().getName());
}

@Override
public Object externalizeEntryQuery(EntryQuery query, ClientWireFormatterContext context) {
public JSONObject externalizeEntryQuery(EntryQuery query, ClientWireFormatterContext context) {
return new JSONObject()
.put(TYPE, TYPE_ENTRY);
}

@Override
public Object externalizeMappedQuery(MappedQuery<?, ?, ?> query, ClientWireFormatterContext context) {
public JSONObject externalizeMappedQuery(MappedQuery<?, ?, ?> query, ClientWireFormatterContext context) {
return new JSONObject()
.put(TYPE, TYPE_MAPPED)
.put(KEY, query.getKeyString())
.put(SUBQUERY, query.getSubQuery().externalize(context));
}

@Override
public Object externalizeRangedQuery(RangedQuery<?, ?, ?> query, ClientWireFormatterContext context) {
public JSONObject externalizeRangedQuery(RangedQuery<?, ?, ?> query, ClientWireFormatterContext context) {
return new JSONObject()
.put(TYPE, TYPE_RANGED)
.put(VALUE, query.getValueString())
.put("subquery", query.getSubQuery().externalize(context));
}

@Override
public <T> Object clientValueToWire(T value, ValueQuery<T> query, ClientWireFormatterContext context) {
public <T> String clientValueToWire(T value, ValueQuery<T> query, ClientWireFormatterContext context) {
return (value == null) ? null : query.getParameter().toString(value);
}

Expand All @@ -94,13 +94,13 @@ public <T> T wireToClientValue(Object result, ValueQuery<T> query, ClientWireFor
}

@Override
public Object clientEntryToWire(Entry entry, EntryQuery query, ClientWireFormatterContext context) {
return new JSONObject(entry.toMap());
public String clientEntryToWire(Entry entry, EntryQuery query, ClientWireFormatterContext context) {
return new JSONObject(entry.toMap()).toString();
}

@Override
public Entry wireToClientEntry(Object result, EntryQuery query, ClientWireFormatterContext context) {
return new DefaultEntry(((JSONObject) result).toMap());
return new DefaultEntry((new JSONObject((String) result)).toMap());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public boolean hasValue(Parameter<?> parameter) {
public Map<String, String> toMap() {
return this.values.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue())));
}

public String toString() {
return values.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public void testClientEntryToWire() {

Object wireEntry = formatter.clientEntryToWire(entry, entryQuery, context);

assertThat(wireEntry).isInstanceOf(JSONObject.class);
JSONObject jsonEntry = (JSONObject) wireEntry;
assertThat(wireEntry).isInstanceOf(String.class);
JSONObject jsonEntry = new JSONObject((String) wireEntry);

assertThat(jsonEntry.get("one")).isEqualTo("111");
assertThat(jsonEntry.get("two")).isEqualTo("22");
Expand All @@ -118,7 +118,7 @@ public void testWireToClientEntry() {
.put("1", "one")
.put("2", "zwei");

Entry clientEntry = formatter.wireToClientEntry(jsonEntry, entryQuery, context);
Entry clientEntry = formatter.wireToClientEntry(jsonEntry.toString(), entryQuery, context);

assertThat(clientEntry.getValue(parameter).get()).isEqualTo("one");
assertThat(clientEntry.getValue(parameter2).get()).isEqualTo("zwei");
Expand Down
Loading

0 comments on commit d8b5fd6

Please sign in to comment.