Skip to content

Commit

Permalink
Use exchange dispatch method for blocking handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Dec 5, 2018
1 parent 15422a0 commit f0f42d0
Showing 1 changed file with 42 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,11 @@ protected void generateRoutes()
.annotated(AnnotationSpec.builder(com.google.inject.name.Named.class).addMember("value", "$S", "registeredHandlerWrappers").build());

typeBuilder.addField(mapOfWrappers, "registeredHandlerWrappers", Modifier.PROTECTED, Modifier.FINAL);

// registeredHandlerWrappers.keySet().stream().forEach( k -> {
//
// typeBuilder.addField(HandlerWrapper.class, k + "HandlerWrapper", Modifier.PROTECTED, Modifier.FINAL);
//
// });


constructor.addParameter(this.controllerClass, className);
constructor.addParameter(annotatedMapOfWrappers, "registeredHandlerWrappers");

// registeredHandlerWrappers.keySet().stream().forEach( k -> {
//
// ParameterSpec p = ParameterSpec.builder(HandlerWrapper.class, k + "HandlerWrapper").addAnnotation(AnnotationSpec.builder(com.google.inject.name.Named.class).addMember("value", "$S", k).build()).build();
//
// constructor.addParameter(p);
//
// });

// String.format("%c%s%sHandler_%s", Character.toLowerCase(clazz.getSimpleName().charAt(0)), clazz.getSimpleName()
//.substring(1, clazz.getSimpleName().length()), StringUtils.capitalize(m.getName()), String.valueOf(nameIndex++));


constructor.addStatement("this.$N = $N", className, className);
constructor.addStatement("this.$N = $N", "registeredHandlerWrappers", "registeredHandlerWrappers");

Expand Down Expand Up @@ -311,7 +295,9 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
{
TypeHandler handler = TypeHandler.forType(p.getParameterizedType(), true);

if (handler.equals(TypeHandler.BeanListValueOfType) || handler.equals(TypeHandler.BeanListFromStringType) || handler.equals(TypeHandler.OptionalBeanListValueOfType)
if (handler.equals(TypeHandler.BeanListValueOfType)
|| handler.equals(TypeHandler.BeanListFromStringType)
|| handler.equals(TypeHandler.OptionalBeanListValueOfType)
|| handler.equals(TypeHandler.OptionalBeanListFromStringType))
{
parameterizedLiteralsNameMap.put(p.getParameterizedType(), HandlerGenerator.typeReferenceNameForParameterizedType(p.getParameterizedType()));
Expand Down Expand Up @@ -344,14 +330,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
{

}

/*
* if (t.getTypeName().matches("java\\.lang|java\\.nio|java\\.io|java\\.util"))
{
return false;
}
*/


if (t.getTypeName().contains("java.lang"))
{
return false;
Expand Down Expand Up @@ -569,7 +548,8 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))
for (Parameter p : m.getParameters())
{

if (p.getParameterizedType().equals(ServerRequest.class) || p.getParameterizedType().equals(HttpServerExchange.class)
if (p.getParameterizedType().equals(ServerRequest.class)
|| p.getParameterizedType().equals(HttpServerExchange.class)
|| p.getParameterizedType().equals(HttpHandler.class))
{
continue;
Expand Down Expand Up @@ -597,14 +577,24 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))

log.debug("parameterizedLiteralsNameMap: " + parameterizedLiteralsNameMap);

if(isBlocking)
{

methodBuilder.addCode(
"$L.dispatch( () -> ",
"exchange");

methodBuilder.beginControlFlow("", "");

methodBuilder.beginControlFlow("try");

}

Arrays.stream(m.getParameters()).forEachOrdered(p ->
{

Type type = p.getParameterizedType();




try
{

Expand All @@ -621,8 +611,7 @@ else if (p.getType().equals(HttpServerExchange.class))
}
else if (p.getType().equals(HttpHandler.class))
{
methodBuilder.addStatement("$T $L = this", HttpHandler.class, p.getName());
// methodBuilder.addCode("$L", "\n");
methodBuilder.addStatement("$T $L = this", HttpHandler.class, p.getName());
}
else
{
Expand Down Expand Up @@ -666,16 +655,13 @@ else if (handler.equals(TypeHandler.ValueOfType))
else if (handler.equals(TypeHandler.FromStringType))
{
handler = TypeHandler.HeaderFromStringType;
TypeHandler.addStatement(methodBuilder, p, handler);

}

TypeHandler.addStatement(methodBuilder, p, handler);
}
else
{
handler = TypeHandler.HeaderStringType;

TypeHandler.addStatement(methodBuilder, p, handler);

TypeHandler.addStatement(methodBuilder, p, handler);
}

}
Expand All @@ -684,8 +670,7 @@ else if (handler.equals(TypeHandler.FromStringType))
BeanParam beanParam = p.getAnnotation(BeanParam.class);

boolean isBeanParameter = beanParam != null;



TypeHandler t = TypeHandler.forType(type, isBeanParameter);

log.debug("beanParam handler: " + t);
Expand Down Expand Up @@ -727,8 +712,10 @@ else if (t.equals(TypeHandler.OptionalFromStringType) || t.equals(TypeHandler.Op

TypeHandler.addStatement(methodBuilder, p);
}
else if (t.equals(TypeHandler.QueryOptionalListFromStringType) || t.equals(TypeHandler.QueryOptionalListValueOfType)
|| t.equals(TypeHandler.QueryOptionalSetValueOfType) || t.equals(TypeHandler.QueryOptionalSetValueOfType))
else if (t.equals(TypeHandler.QueryOptionalListFromStringType)
|| t.equals(TypeHandler.QueryOptionalListValueOfType)
|| t.equals(TypeHandler.QueryOptionalSetValueOfType)
|| t.equals(TypeHandler.QueryOptionalSetValueOfType))
{
ParameterizedType pType = (ParameterizedType) type;

Expand Down Expand Up @@ -786,7 +773,7 @@ else if (t.equals(TypeHandler.OptionalBeanListFromStringType) || t.equals(TypeHa

String controllerMethodArgs = Arrays.stream(m.getParameters()).map(Parameter::getName).collect(Collectors.joining(","));

if (!m.getReturnType().toString().equals("void"))
if (!m.getReturnType().toString().equalsIgnoreCase("void"))
{
if (m.getReturnType().getTypeName().contains("java.util.concurrent.CompletionStage")
|| m.getReturnType().getTypeName().contains("java.util.concurrent.CompletableFuture"))
Expand Down Expand Up @@ -854,8 +841,7 @@ else if (producesContentType.contains(MediaType.TEXT_HTML))

}

handlerClassBuilder.addMethod(methodBuilder.build());


}
else
{
Expand All @@ -866,9 +852,18 @@ else if (producesContentType.contains(MediaType.TEXT_HTML))

methodBuilder.addCode("$L", "\n");

handlerClassBuilder.addMethod(methodBuilder.build());

}

if(isBlocking)
{
methodBuilder.endControlFlow("catch ($T e) { \n\texchange.putAttachment(io.sinistral.proteus.server.handlers.ServerDefaultResponseListener.EXCEPTION, e);\n\texchange.endExchange();\n}", Exception.class);
methodBuilder.endControlFlow(")", "");

}

handlerClassBuilder.addMethod(methodBuilder.build());


FieldSpec handlerField = FieldSpec.builder(httpHandlerClass, handlerName, Modifier.FINAL).initializer("$L", handlerClassBuilder.build()).build();

Expand Down

0 comments on commit f0f42d0

Please sign in to comment.