Skip to content

Commit

Permalink
Merge pull request #65 from SentryMan/internals
Browse files Browse the repository at this point in the history
Refactor Internals
  • Loading branch information
SentryMan authored Nov 23, 2024
2 parents 5cf708a + e214aa7 commit 24a153e
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build/
*.prefs
*.classpath
*.prefs
*.factorypath
bin/
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[![Discord](https://img.shields.io/discord/1074074312421683250?color=%237289da&label=discord)](https://discord.gg/Qcqf9R27BR)
[![Build](https://github.com/avaje/avaje-jex/actions/workflows/build.yml/badge.svg)](https://github.com/avaje/avaje-jex/actions/workflows/build.yml)
[![Maven Central](https://img.shields.io/maven-central/v/io.avaje/avaje-jex-parent.svg?label=Maven%20Central)](https://mvnrepository.com/artifact/io.avaje/avaje-jex-parent)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/avaje/avaje-jex/blob/master/LICENSE)
[![JDK EA](https://github.com/avaje/avaje-jex/actions/workflows/jdk-ea.yml/badge.svg)](https://github.com/avaje/avaje-jex/actions/workflows/jdk-ea.yml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/avaje/avaje-jex/blob/master/LICENSE)
[![Maven Central](https://img.shields.io/maven-central/v/io.avaje/avaje-jex.svg?label=Maven%20Central)](https://mvnrepository.com/artifact/io.avaje/avaje-jex)
[![javadoc](https://javadoc.io/badge2/io.avaje/avaje-jex/javadoc.svg?color=purple)](https://javadoc.io/doc/io.avaje/avaje-jex)

# avaje-jex

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import freemarker.template.TemplateException;
import freemarker.template.Version;
import io.avaje.jex.Context;
import io.avaje.jex.TemplateRender;
import io.avaje.jex.spi.TemplateRender;
import io.avaje.spi.ServiceProvider;

@ServiceProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.MustacheFactory;
import io.avaje.jex.Context;
import io.avaje.jex.TemplateRender;
import io.avaje.jex.spi.TemplateRender;
import io.avaje.spi.ServiceProvider;

import java.io.IOException;
Expand Down
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/DJex.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ public Jex jsonService(JsonService jsonService) {
}

@Override
public Jex plugin(Plugin plugin) {
public Jex plugin(JexPlugin plugin) {
plugin.apply(this);
return this;
}

@Override
public Jex configureWith(BeanScope beanScope) {
lifecycle.onShutdown(beanScope::close);
for (Plugin plugin : beanScope.list(Plugin.class)) {
for (JexPlugin plugin : beanScope.list(JexPlugin.class)) {
plugin.apply(this);
}
for (ErrorHandling.Service service : beanScope.list(ErrorHandling.Service.class)) {
Expand Down
1 change: 1 addition & 0 deletions avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.net.ssl.SSLContext;

import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

class DJexConfig implements JexConfig {

Expand Down
3 changes: 2 additions & 1 deletion avaje-jex/src/main/java/io/avaje/jex/Jex.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.avaje.inject.BeanScope;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

import java.util.Collection;
import java.util.function.Consumer;
Expand Down Expand Up @@ -97,7 +98,7 @@ static Jex create() {
/**
* Add Plugin functionality.
*/
Jex plugin(Plugin plugin);
Jex plugin(JexPlugin plugin);

/**
* Configure given the dependency injection scope from <em>avaje-inject</em>.
Expand Down
1 change: 1 addition & 0 deletions avaje-jex/src/main/java/io/avaje/jex/JexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.net.ssl.SSLContext;

import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

/**
* Jex configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* A plugin that can register things like routes, exception handlers etc.
*/
public interface Plugin {
public interface JexPlugin {

/**
* Register the plugin features with jex.
Expand Down
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/core/HealthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import io.avaje.jex.AppLifecycle;
import io.avaje.jex.Context;
import io.avaje.jex.Jex;
import io.avaje.jex.Plugin;
import io.avaje.jex.JexPlugin;

/**
* Health plugin with liveness and readiness support based on
* the application lifecycle support.
*/
public class HealthPlugin implements Plugin {
public class HealthPlugin implements JexPlugin {

private AppLifecycle lifecycle;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.avaje.jex.core;

import io.avaje.jex.Context;
import io.avaje.jex.TemplateRender;
import io.avaje.jex.spi.TemplateRender;

import java.util.HashMap;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.Optional;
import java.util.ServiceLoader;

import io.avaje.jex.TemplateRender;
import io.avaje.jex.spi.JexExtension;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

/** Core implementation of SpiServiceManager provided to specific implementations like jetty etc. */
class CoreServiceLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.avaje.jex.spi.HeaderKeys;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.SpiContext;
import io.avaje.jex.spi.SpiServiceManager;
import io.avaje.jex.spi.TemplateRender;

import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
Expand All @@ -20,7 +20,7 @@
/**
* Core implementation of SpiServiceManager provided to specific implementations like jetty etc.
*/
public class CoreServiceManager implements SpiServiceManager {
public final class CoreServiceManager implements SpiServiceManager {

private static final System.Logger log = AppLog.getLogger("io.avaje.jex");
public static final String UTF_8 = "UTF-8";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.avaje.jex.spi;
package io.avaje.jex.core.internal;

import io.avaje.jex.Context;
import io.avaje.jex.Routing;
import io.avaje.jex.jdk.CtxServiceManager;
import io.avaje.jex.spi.SpiContext;

import java.util.Iterator;
import java.util.List;
Expand All @@ -11,7 +13,7 @@
/**
* Core service methods available to Context implementations.
*/
public interface SpiServiceManager {
public sealed interface SpiServiceManager permits CoreServiceManager, CtxServiceManager {

/**
* Read and return the type from json request content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.util.Collections;
import java.util.Map;

/**
* Throwing an uncaught {@code HttpResponseException} will interrupt http processing and set the
* status code and response body with the given message
*/
public class HttpResponseException extends RuntimeException {

private final int status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
package io.avaje.jex.spi;
package io.avaje.jex.jdk;

import io.avaje.jex.Context;
import io.avaje.jex.Routing;
import io.avaje.jex.core.internal.SpiServiceManager;
import io.avaje.jex.spi.SpiContext;

import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
* Provides a delegating proxy to a SpiServiceManager.
* <p>
* Can be used by specific implementations like Jetty and JDK Http Server to add core functionality
* to provide to the specific context implementation.
*/
public abstract class ProxyServiceManager implements SpiServiceManager {
public final class CtxServiceManager implements SpiServiceManager {

protected final SpiServiceManager delegate;
private final String scheme;
private final String contextPath;

protected ProxyServiceManager(SpiServiceManager delegate) {
private final SpiServiceManager delegate;

CtxServiceManager(SpiServiceManager delegate, String scheme, String contextPath) {
this.delegate = delegate;
this.scheme = scheme;
this.contextPath = contextPath;
}

OutputStream createOutputStream(JdkContext jdkContext) {
return new BufferedOutStream(jdkContext);
}

String scheme() {
return scheme;
}

public String url() {
return scheme + "://";
}

public String contextPath() {
return contextPath;
}

@Override
Expand Down
10 changes: 4 additions & 6 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JdkContext implements Context, SpiContext {
private static final int SC_MOVED_TEMPORARILY = 302;
private static final String SET_COOKIE = "Set-Cookie";
private static final String COOKIE = "Cookie";
private final ServiceManager mgr;
private final CtxServiceManager mgr;
private final String path;
private final Map<String, String> pathParams;
private final Set<Role> roles;
Expand All @@ -52,7 +52,7 @@ class JdkContext implements Context, SpiContext {
private String characterEncoding;

JdkContext(
ServiceManager mgr,
CtxServiceManager mgr,
HttpExchange exchange,
String path,
Map<String, String> pathParams,
Expand All @@ -65,7 +65,7 @@ class JdkContext implements Context, SpiContext {
}

/** Create when no route matched. */
JdkContext(ServiceManager mgr, HttpExchange exchange, String path, Set<Role> roles) {
JdkContext(CtxServiceManager mgr, HttpExchange exchange, String path, Set<Role> roles) {
this.mgr = mgr;
this.roles = roles;
this.exchange = exchange;
Expand Down Expand Up @@ -365,10 +365,8 @@ public Context write(byte[] bytes) {
@Override
public Context write(InputStream is) {

try (is; var os = exchange.getResponseBody()) {
exchange.sendResponseHeaders(statusCode(), 0);
try (is; var os = outputStream()) {
is.transferTo(os);
os.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
10 changes: 7 additions & 3 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,41 @@
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.*;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;

import io.avaje.applog.AppLog;
import io.avaje.jex.AppLifecycle;
import io.avaje.jex.Jex;
import io.avaje.jex.core.internal.SpiServiceManager;
import io.avaje.jex.routes.SpiRoutes;
import io.avaje.jex.spi.SpiServiceManager;

public class JdkServerStart {

private static final System.Logger log = AppLog.getLogger("io.avaje.jex");

public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceManager) {
final ServiceManager manager = new ServiceManager(serviceManager, "http", "");

try {
final HttpServer server;

var port = new InetSocketAddress(jex.config().port());
final var sslContext = jex.config().sslContext();

final String scheme;
if (sslContext != null) {
var httpsServer = HttpsServer.create(port, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
server = httpsServer;
scheme = "https";
} else {
scheme = "http";
server = HttpServer.create(port, 0);
}

final var manager = new CtxServiceManager(serviceManager, scheme, "");

var handler = new BaseHandler(routes);
var context = server.createContext("/", handler);
context.getFilters().add(new RoutingFilter(routes, manager));
Expand Down
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/RoutingFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
final class RoutingFilter extends Filter {

private final SpiRoutes routes;
private final ServiceManager mgr;
private final CtxServiceManager mgr;

RoutingFilter(SpiRoutes routes, ServiceManager mgr) {
RoutingFilter(SpiRoutes routes, CtxServiceManager mgr) {
this.mgr = mgr;
this.routes = routes;
}
Expand Down
34 changes: 0 additions & 34 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/ServiceManager.java

This file was deleted.

1 change: 0 additions & 1 deletion avaje-jex/src/main/java/io/avaje/jex/spi/JexExtension.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.avaje.jex.spi;

import io.avaje.jex.TemplateRender;
import io.avaje.spi.Service;

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.avaje.jex;
package io.avaje.jex.spi;

import java.util.Map;

import io.avaje.jex.spi.JexExtension;
import io.avaje.jex.Context;

/**
* Template rendering typically of html.
Expand Down

0 comments on commit 24a153e

Please sign in to comment.