Skip to content
Permalink
Browse files

Bugfixes and work on Bootstrapper

  • Loading branch information...
revetkn committed May 8, 2015
1 parent 24ba4b1 commit 0e077ec1ab9bc5d6cb53fe103ac9fb5b14055bf6
@@ -48,7 +48,7 @@ Coming soon
```
$ git clone https://github.com/soklet/soklet.git
$ cd soklet
$ mvn exec:java -Dexec.mainClass="com.soklet.bootstrap.Bootstrapper"
$ mvn -q exec:exec
```

## Example Code
@@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>{{mavenGroupId}}</groupId>
<artifactId>{{mavenArtifactId}}</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>{{appName}}</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<fork>true</fork>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.soklet</groupId>
<artifactId>soklet</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.soklet</groupId>
<artifactId>soklet-guice</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.soklet</groupId>
<artifactId>soklet-jetty</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
No changes.
No changes.
No changes.
@@ -0,0 +1,47 @@
package {{basePackage}}.web;

import static java.util.Objects.requireNonNull;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.mainlinedelivery.service.RestaurantService;
import com.soklet.web.annotation.GET;
import com.soklet.web.annotation.Resource;
import com.soklet.web.response.PageResponse;

/**
* @author {{authorName}}
*/
@Resource
@Singleton
public class ExampleResource {
private final ExampleService exampleService;

@Inject
public ExampleResource(ExampleService exampleService) {
this.exampleService = requireNonNull(exampleService);
}

@GET("/")
public PageResponse indexPage() {
return new PageResponse("index");
}

@GET("/examples")
public PageResponse examplesPage() {
return new PageResponse("example");
}

@GET("/examples/{exampleId}")
public PageResponse examplePage() {
return new PageResponse("example");
}

@POST("/api/examples")
public ApiResponse addExample(@RequestBody String requestBody) {
ExampleCommand exampleCommand =
exampleService.addExample(command);
return new ApiResponse("example");
}
}
14 pom.xml
@@ -67,6 +67,20 @@
<linksource>true</linksource>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>com.soklet.bootstrap.Bootstrapper</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>

@@ -21,6 +21,7 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
@@ -40,7 +41,7 @@
if (sourcePath.equals(destinationDirectory) && !Files.isDirectory(sourcePath))
destinationDirectory = sourcePath.getParent();

this.destinationDirectory = destinationDirectory;
this.destinationDirectory = destinationDirectory == null ? Paths.get(".") : destinationDirectory;
}

@Override
@@ -29,18 +29,33 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author <a href="http://revetkn.com">Mark Allen</a>
* @since 1.0.0
*/
public class Bootstrapper {
public static void main(String[] args) throws IOException {
new Bootstrapper().run();
private final Path bootstrapDirectory;

public Bootstrapper() {
this.bootstrapDirectory = Paths.get("bootstrap");

if (!Files.exists(bootstrapDirectory()))
throw new RuntimeException(format("Specified bootstrap directory %s does not exist", bootstrapDirectory()
.toAbsolutePath()));
if (!Files.isDirectory(bootstrapDirectory()))
throw new RuntimeException(format("Specified bootstrap directory %s is not a directory", bootstrapDirectory()
.toAbsolutePath()));
}

public void run() throws IOException {
out.println("*** Welcome to Soklet! ***");
out.println();
out.println("Welcome to Soklet!");
out.println("Enter '\\q' to quit at any time.");
out.println();

try (BufferedReader inputReader = new BufferedReader(new InputStreamReader(in))) {
String appName = valueFromInput(inputReader, "What's the name of your app? (e.g. ExampleApp)");
@@ -49,9 +64,14 @@ public void run() throws IOException {
valueFromInput(inputReader, "What's your app's base package name? (e.g. com.mycompany)",
new PackageNameInputValidator());

String mavenGroupId = basePackageName;
String mavenArtifactId = appName.toLowerCase().replace(" ", "-");

out.println(format("Application name is %s", appName));
out.println(format("Author name is %s", authorName));
out.println(format("Back package is %s", basePackageName));
out.println(format("Base package is %s", basePackageName));
out.println(format("Maven group ID is %s", mavenGroupId));
out.println(format("Maven artifact ID is %s", mavenArtifactId));

throw new UnsupportedOperationException();
}
@@ -72,11 +92,18 @@ protected String valueFromInput(BufferedReader reader, String prompt, InputValid
do {
out.print(format("%s\n-> ", prompt));
value = reader.readLine().trim();

if ("\\q".equals(value))
System.exit(0);
} while (!inputValidator.isValid(value));

return value;
}

protected Path bootstrapDirectory() {
return this.bootstrapDirectory;
}

protected static interface InputValidator {
boolean isValid(String input);
}
@@ -87,4 +114,8 @@ public boolean isValid(String input) {
return input.length() > 0 && input.matches("^([a-zA-Z_]{1}[a-zA-Z0-9_]*(\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)*)?$");
}
}

public static void main(String[] args) throws IOException {
new Bootstrapper().run();
}
}
@@ -34,6 +34,7 @@
import javax.servlet.http.HttpServletResponse;

import com.soklet.web.exception.ExceptionStatusMapper;
import com.soklet.web.exception.ResourceMethodExecutionException;
import com.soklet.web.response.writer.ApiResponseWriter;
import com.soklet.web.response.writer.BinaryResponseWriter;
import com.soklet.web.response.writer.PageResponseWriter;
@@ -73,6 +74,13 @@ public void handleResponse(HttpServletRequest httpServletRequest, HttpServletRes
requireNonNull(response);
requireNonNull(exception);

// Unwrap ResourceMethodExecutionExceptions to get at the real cause.
// TODO: maybe the signature for this method should take a Throwable instead of Exception
if (exception.isPresent() && exception.get() instanceof ResourceMethodExecutionException)
exception =
Optional.of(exception.get().getCause() instanceof Exception ? (Exception) exception.get().getCause()
: exception.get());

// Do nothing at all if this was an async response
if (response.isPresent() && response.get() instanceof AsyncResponse)
return;

0 comments on commit 0e077ec

Please sign in to comment.
You can’t perform that action at this time.