Skip to content

Commit

Permalink
Make controller compilation synchronous.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Sep 28, 2023
1 parent ffb84ea commit 638ef73
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 193 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Migrate to Jakarta.**


[ffb84eaddbbc01e](https://github.com/noboomu/proteus/commit/ffb84eaddbbc01e) Joshua Bauer *2023-09-27 18:41:05*

**Switch to SourceBuddy from OpenHFT for dynamic complilation. Breaks < JDK 17 compatability however.**


Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ Inspired by [Play](http://playframework.com), [Jooby](http://jooby.org), and [li

Dependencies
----------
* [JDK 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
* [JDK 17](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
* [Maven 3](http://maven.apache.org/)


Expand Down
18 changes: 7 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-text.version>1.9</commons-text.version>
<guava.version>32.1.2-jre</guava.version>
<guice.version>5.1.0</guice.version>
<guice.version>6.0.0</guice.version>
<jackson.version>2.15.2</jackson.version>
<jakarta-ws-rs.version>3.1.0</jakarta-ws-rs.version>
<jansi.version>1.18</jansi.version>
Expand Down Expand Up @@ -114,11 +114,11 @@
<version>${micrometer-core.version}</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>com.javax0.sourcebuddy</groupId>-->
<!-- <artifactId>SourceBuddy</artifactId>-->
<!-- <version>2.3.0</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.javax0.sourcebuddy</groupId>-->
<!-- <artifactId>SourceBuddy</artifactId>-->
<!-- <version>2.3.0</version>-->
<!-- </dependency>-->

<dependency>
<groupId>com.sun.xml.ws</groupId>
Expand Down Expand Up @@ -153,11 +153,6 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.sinistral</groupId>
<artifactId>proteus-swagger</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>
</dependencyManagement>
Expand Down Expand Up @@ -253,6 +248,7 @@
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<dependencies>
Expand Down
22 changes: 16 additions & 6 deletions proteus-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<groupId>io.sinistral</groupId>
<version>0.7.0-SNAPSHOT</version>
</parent>
<properties>
<jakarta.activation-api.version>2.1.2</jakarta.activation-api.version>
</properties>

<modelVersion>4.0.0</modelVersion>
<artifactId>proteus-core</artifactId>
Expand Down Expand Up @@ -192,6 +195,19 @@ Proteus Changelog.
<version>${jakarta-ws-rs.version}</version>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${jakarta-validation-api.version}</version>
</dependency>


<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>${jakarta.activation-api.version}</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>net.openhft</groupId>-->
<!-- <artifactId>compiler</artifactId>-->
Expand Down Expand Up @@ -244,12 +260,6 @@ Proteus Changelog.
<version>${typesafe-config.version}</version>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${jakarta-validation-api.version}</version>
</dependency>


<dependency>
<groupId>org.yaml</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,15 @@ public void buildServer() {

final Instant compilationStartTime = Instant.now();

ExecutorService handlerCompilationExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

CountDownLatch countDownLatch = new CountDownLatch(registeredControllers.size());

CopyOnWriteArrayList<Class<? extends Supplier<RoutingHandler>>> routerClasses = new CopyOnWriteArrayList<>();

ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
List<Class<? extends Supplier<RoutingHandler>>> routerClasses = new ArrayList<>();
//
// ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

log.info("Compiling route handlers...");

for (Class<?> controllerClass : registeredControllers) {

handlerCompilationExecutor.submit(() -> {


try {

Expand All @@ -311,15 +307,10 @@ public void buildServer() {

log.debug("Compiling {}...", controllerClass);

log.debug("Generating {}...", controllerClass);

final String source = generator.generateClassSource();

log.debug("Generated {}...", controllerClass);


lock.writeLock().lock();

try {

final var compiled = Compiler.java().from(source).compile();
Expand All @@ -340,22 +331,14 @@ public void buildServer() {
}


lock.writeLock().unlock();

} catch (Exception e) {
log.error("Failed to compile", e);
}

countDownLatch.countDown();
});

}

try {
countDownLatch.await();
} catch (Exception e) {
log.error("Failed waiting for handlers to generate", e);
}

log.debug("Compilation completed in {}", DurationFormatUtils.formatDurationHMS(Duration.between(compilationStartTime, Instant.now()).toMillis()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.undertow.server.handlers.Cookie;
import io.undertow.server.handlers.ExceptionHandler;
import io.undertow.util.*;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,7 +32,6 @@
/**
* @author jbauer
* Base server response. Friendlier interface to underlying exchange.
* @TODO extend jakarta.ws.rs.core.Response
*/

public class ServerResponse<T>
Expand All @@ -57,7 +55,7 @@ public class ServerResponse<T>
protected int status = StatusCodes.OK;
protected final HeaderMap headers = new HeaderMap();
protected final Map<String, Cookie> cookies = new HashMap<>();
protected String contentType = jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
protected String contentType = MediaType.APPLICATION_JSON.contentType();
protected T entity;
protected Throwable throwable;
// protected Class<? extends JsonContext> jsonContext;
Expand Down Expand Up @@ -175,14 +173,7 @@ public void setStatus(int status)
this.status = status;
}

/**
* @param status
* the status to set
*/
public void setStatus(Response.Status status)
{
this.status = status.getStatusCode();
}


public ServerResponse<T> body(ByteBuffer body)
{
Expand Down Expand Up @@ -266,11 +257,7 @@ public ServerResponse<T> throwable(Throwable throwable)
return this;
}

public ServerResponse<T> status(Response.Status status)
{
this.status = status.getStatusCode();
return this;
}


public ServerResponse<T> status(int status)
{
Expand Down Expand Up @@ -301,11 +288,11 @@ protected void setContentType(String contentType)
{
this.contentType = contentType;

if (this.contentType.contains(jakarta.ws.rs.core.MediaType.APPLICATION_JSON)) {
if (this.contentType.contains(MediaType.APPLICATION_JSON.contentType())) {
if (!this.preprocessed) {
this.processJson = true;
}
} else if (this.contentType.contains(jakarta.ws.rs.core.MediaType.APPLICATION_XML)) {
} else if (this.contentType.contains(MediaType.APPLICATION_XML.contentType())) {
if (!this.preprocessed) {
this.processXml = true;
}
Expand All @@ -319,12 +306,6 @@ public ServerResponse<T> contentType(String contentType)
}


public ServerResponse<T> contentType(jakarta.ws.rs.core.MediaType mediaType)
{
this.setContentType(mediaType.toString());
return this;
}

public ServerResponse<T> contentType(MediaType mediaType)
{
this.setContentType(mediaType.contentType());
Expand All @@ -336,19 +317,19 @@ public ServerResponse<T> applicationJson()
if (!this.preprocessed) {
this.processJson = true;
}
this.contentType = jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
this.contentType = MediaType.APPLICATION_JSON.contentType();
return this;
}

public ServerResponse<T> textHtml()
{
this.contentType = jakarta.ws.rs.core.MediaType.TEXT_HTML;
this.contentType = MediaType.TEXT_HTML_UTF8.contentType();
return this;
}

public ServerResponse<T> applicationOctetStream()
{
this.contentType = jakarta.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
this.contentType = MediaType.APPLICATION_OCTET_STREAM.contentType();
return this;
}

Expand All @@ -357,13 +338,13 @@ public ServerResponse<T> applicationXml()
if (!this.preprocessed) {
this.processXml = true;
}
this.contentType = jakarta.ws.rs.core.MediaType.APPLICATION_XML;
this.contentType = MediaType.APPLICATION_XML.contentType();
return this;
}

public ServerResponse<T> textPlain()
{
this.contentType = jakarta.ws.rs.core.MediaType.TEXT_PLAIN;
this.contentType = MediaType.TEXT_PLAIN_UTF8.contentType();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
package io.sinistral.proteus.server.exceptions;

import jakarta.ws.rs.core.Response.Status;
import io.undertow.util.StatusCodes;

/**
* @author jbauer
Expand All @@ -16,7 +16,7 @@ public class ServerException extends RuntimeException
*/
private static final long serialVersionUID = 8360356916374374408L;

private Integer status = Status.BAD_REQUEST.getStatusCode();
private Integer status = StatusCodes.BAD_REQUEST;

public ServerException(int status)
{
Expand All @@ -25,13 +25,6 @@ public ServerException(int status)
this.status = status;
}

public ServerException(Status status)
{
super();

this.status = status.getStatusCode();
}

/**
* @param message
*/
Expand All @@ -42,15 +35,6 @@ public ServerException(String message, int status)
this.status = status;
}

/**
* @param message
*/
public ServerException(String message, Status status)
{
super(message);

this.status = status.getStatusCode();
}

/**
* @param cause
Expand All @@ -62,15 +46,6 @@ public ServerException(Throwable cause, int status)
this.status = status;
}

/**
* @param cause
*/
public ServerException(Throwable cause, Status status)
{
super(cause);

this.status = status.getStatusCode();
}

public ServerException(String message, Throwable cause, int status)
{
Expand All @@ -79,13 +54,6 @@ public ServerException(String message, Throwable cause, int status)
this.status = status;
}

public ServerException(String message, Throwable cause, Status status)
{
super(message, cause);

this.status = status.getStatusCode();
}

/**
* @return the status
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down
Loading

0 comments on commit 638ef73

Please sign in to comment.