Skip to content

Commit

Permalink
ResourceHandler x DynamicResourceHandler replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-ambrosio committed Nov 5, 2020
1 parent 015a50c commit 3fa9f63
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/vlingo/xoom/annotation/TypeRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public Stream<TypeMirror> subclassesOf(final Class superclass,
}

private boolean isSubclass(final Element typeElement, final Class superclass) {
final TypeMirror resourceHandler =
final TypeMirror type =
elements.getTypeElement(superclass.getCanonicalName())
.asType();

return ((TypeElement) typeElement).getSuperclass()
.equals(resourceHandler);
.equals(type);
}

public <T> TypeElement from(final T annotation, final Function<T, Class<?>> retriever) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package io.vlingo.xoom.annotation.initializer;

import io.vlingo.http.resource.ResourceHandler;
import io.vlingo.http.resource.DynamicResourceHandler;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -18,7 +18,7 @@
@Retention(RetentionPolicy.CLASS)
public @interface ResourceHandlers {

Class<? extends ResourceHandler>[] value() default {};
Class<? extends DynamicResourceHandler>[] value() default {};

String[] packages() default {""};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package io.vlingo.xoom.annotation.initializer.contentloader;

import io.vlingo.http.resource.DynamicResourceHandler;
import io.vlingo.http.resource.ResourceHandler;
import io.vlingo.xoom.annotation.initializer.ResourceHandlers;
import io.vlingo.xoom.codegen.template.TemplateStandard;
Expand Down Expand Up @@ -38,7 +39,7 @@ protected List<TypeElement> retrieveContentSource() {
}

if(isPackageBased(resourceHandlers)) {
return typeRetriever.subclassesOf(ResourceHandler.class, resourceHandlers.packages())
return typeRetriever.subclassesOf(DynamicResourceHandler.class, resourceHandlers.packages())
.map(this::toType).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.vlingo.xoom.codegen.template.TemplateData;
import io.vlingo.xoom.codegen.template.TemplateParameters;
import io.vlingo.xoom.codegen.template.TemplateStandard;
import io.vlingo.xoom.codegen.template.model.AggregateArgumentsFormat;
import io.vlingo.xoom.codegen.template.model.AggregateArgumentsFormat.MethodInvocation;
import io.vlingo.xoom.codegen.template.model.MethodScope;

import java.util.List;
Expand All @@ -22,7 +24,6 @@
import static io.vlingo.xoom.codegen.template.TemplateParameter.*;
import static io.vlingo.xoom.codegen.template.TemplateStandard.AGGREGATE_STATE;
import static io.vlingo.xoom.codegen.template.TemplateStandard.ENTITY_DATA;
import static io.vlingo.xoom.codegen.template.model.AggregateArgumentsFormat.DATA_BASED_METHOD_INVOCATION;

public class AutoDispatchHandlerEntryTemplateData extends TemplateData {

Expand All @@ -49,7 +50,7 @@ private AutoDispatchHandlerEntryTemplateData(final CodeGenerationParameter route
.and(AGGREGATE_PROTOCOL_VARIABLE, ClassFormatter.simpleNameToAttribute(aggregate.value))
.and(STATE_NAME, AGGREGATE_STATE.resolveClassname(aggregate.value))
.and(INDEX_NAME, AutoDispatchMappingValueFormatter.format(route.value))
.and(METHOD_INVOCATION_PARAMETERS, DATA_BASED_METHOD_INVOCATION.format(method, methodScope));
.and(METHOD_INVOCATION_PARAMETERS, resolveMethodInvocationParameters(method));
}

private CodeGenerationParameter findMethod(final CodeGenerationParameter aggregate,
Expand All @@ -59,6 +60,12 @@ private CodeGenerationParameter findMethod(final CodeGenerationParameter aggrega
.findFirst().get();
}

private String resolveMethodInvocationParameters(final CodeGenerationParameter method) {
final boolean factoryMethod = method.relatedParameterValueOf(Label.FACTORY_METHOD, Boolean::valueOf);
final MethodScope methodScope = factoryMethod ? MethodScope.STATIC : MethodScope.INSTANCE;
return new AggregateArgumentsFormat.MethodInvocation("$stage", "data").format(method, methodScope);
}

@Override
public TemplateParameters parameters() {
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public interface AggregateArgumentsFormat {

AggregateArgumentsFormat METHOD_INVOCATION = new MethodInvocation("stage");
AggregateArgumentsFormat SIGNATURE_DECLARATION = new SignatureDeclaration();
AggregateArgumentsFormat DATA_BASED_METHOD_INVOCATION = new MethodInvocation("$stage", "data");

default String format(final CodeGenerationParameter parameter) {
return format(parameter, MethodScope.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

import io.vlingo.http.Method;
import io.vlingo.xoom.codegen.parameter.CodeGenerationParameter;
import io.vlingo.xoom.codegen.template.model.AggregateArgumentsFormat;
import io.vlingo.xoom.codegen.template.model.AggregateDetail;
import io.vlingo.xoom.codegen.template.model.MethodScope;

import static io.vlingo.xoom.codegen.content.ClassFormatter.simpleNameToAttribute;
import static io.vlingo.xoom.codegen.parameter.Label.FACTORY_METHOD;
import static io.vlingo.xoom.codegen.parameter.Label.ROUTE_METHOD;
import static io.vlingo.xoom.codegen.template.TemplateStandard.ENTITY_DATA;
import static io.vlingo.xoom.codegen.template.model.AggregateArgumentsFormat.DATA_BASED_METHOD_INVOCATION;

public class DefaultHandlerInvocationResolver implements HandlerInvocationResolver {

Expand All @@ -39,11 +39,13 @@ public String resolveAdapterHandlerInvocation(final CodeGenerationParameter aggr
return String.format(ADAPTER_PATTERN, ENTITY_DATA.resolveClassname(aggregateParameter.value));
}

private String resolveCommandMethodInvocation(final CodeGenerationParameter aggregateParameter, final CodeGenerationParameter routeParameter) {
private String resolveCommandMethodInvocation(final CodeGenerationParameter aggregateParameter,
final CodeGenerationParameter routeParameter) {
final AggregateArgumentsFormat argumentsFormat = new AggregateArgumentsFormat.MethodInvocation("stage()", "data");
final CodeGenerationParameter method = AggregateDetail.methodWithName(aggregateParameter, routeParameter.value);
final Boolean factoryMethod = method.relatedParameterValueOf(FACTORY_METHOD, Boolean::valueOf);
final MethodScope scope = factoryMethod ? MethodScope.STATIC : MethodScope.INSTANCE;
final String methodInvocationParameters = DATA_BASED_METHOD_INVOCATION.format(method, scope);
final String methodInvocationParameters = argumentsFormat.format(method, scope);
final String invoker = factoryMethod ? aggregateParameter.value : simpleNameToAttribute(aggregateParameter.value);
return String.format(COMMAND_PATTERN, invoker, method.value, methodInvocationParameters);
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/resources/codegen/RestResource.ftl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ${packageName};

import io.vlingo.actors.Logger;
import io.vlingo.actors.Stage;
import io.vlingo.http.resource.Resource;
import io.vlingo.http.resource.ResourceHandler;
import io.vlingo.http.resource.DynamicResourceHandler;
import static io.vlingo.http.resource.ResourceBuilder.resource;

<#list imports as import>
Expand All @@ -17,24 +16,29 @@ import static io.vlingo.http.Response.Status.*;
import static io.vlingo.http.ResponseHeader.*;
import static io.vlingo.http.resource.ResourceBuilder.*;
<#if useAutoDispatch>
import io.vlingo.actors.Logger;
import io.vlingo.xoom.annotation.autodispatch.Handler;
</#if>

<#if useAutoDispatch>
public class ${resourceName} extends ResourceHandler implements ${autoDispatchMappingName} {
public class ${resourceName} extends DynamicResourceHandler implements ${autoDispatchMappingName} {
<#else>
public class ${resourceName} extends ResourceHandler {
public class ${resourceName} extends DynamicResourceHandler {
</#if>

<#if useAutoDispatch>
private final Stage $stage;
private final Logger $logger;
</#if>
<#if queries?has_content && !queries.empty>
private final ${queries.protocolName} $queries;
</#if>

public ${resourceName}(final Stage $stage) {
this.$stage = $stage;
this.$logger = $stage.world().defaultLogger();
public ${resourceName}(final Stage stage) {
super(stage);
<#if useAutoDispatch>
this.$stage = super.stage();
this.$logger = super.logger();
</#if>
<#if queries?has_content && !queries.empty>
this.$queries = ${storeProviderName}.instance().${queries.attributeName};
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
import io.vlingo.actors.Stage;
import io.vlingo.common.Completes;
import io.vlingo.http.Response;
import io.vlingo.http.resource.DynamicResourceHandler;
import io.vlingo.http.resource.Resource;
import io.vlingo.http.resource.ResourceHandler;

import static io.vlingo.http.Response.Status.Ok;
import static io.vlingo.http.resource.ResourceBuilder.get;
import static io.vlingo.http.resource.ResourceBuilder.resource;

public class FirstResource extends ResourceHandler {
public class FirstResource extends DynamicResourceHandler {

public FirstResource(final Stage stage) {
super(stage);
}

public Completes<Response> retrieveResource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import io.vlingo.actors.Stage;
import io.vlingo.common.Completes;
import io.vlingo.http.resource.DynamicResourceHandler;
import io.vlingo.http.resource.ObjectResponse;
import io.vlingo.http.resource.Resource;
import io.vlingo.http.resource.ResourceHandler;
Expand All @@ -17,9 +18,10 @@
import static io.vlingo.http.resource.ResourceBuilder.get;
import static io.vlingo.http.resource.ResourceBuilder.resource;

public class SecondResource extends ResourceHandler {
public class SecondResource extends DynamicResourceHandler {

public SecondResource(final Stage stage) {
super(stage);
}

public Completes<ObjectResponse<String>> retrieveResource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.Test;

import java.nio.file.Paths;
import java.util.List;

import static io.vlingo.xoom.codegen.parameter.Label.ROUTE_METHOD;
import static io.vlingo.xoom.codegen.parameter.Label.*;
Expand All @@ -45,10 +46,12 @@ public void testRestResourceGeneration() {

new RestResourceGenerationStep().process(context);

Assert.assertEquals(8, context.contents().size());
Assert.assertEquals("AuthorResource", context.contents().get(7).retrieveClassName());
Assert.assertTrue(context.contents().get(7).contains("class AuthorResource"));
Assert.assertTrue(context.contents().get(7).contains("package io.vlingo.xoomapp.resource;"));
final List<Content> contents = context.contents();
Assert.assertEquals(8, contents.size());
Assert.assertEquals("AuthorResource", contents.get(7).retrieveClassName());
Assert.assertTrue(contents.get(7).contains("class AuthorResource"));
Assert.assertTrue(contents.get(7).contains("Author.withName(stage(), data.name)"));
Assert.assertTrue(contents.get(7).contains("package io.vlingo.xoomapp.resource;"));
}

private CodeGenerationParameter authorAggregate() {
Expand Down

0 comments on commit 3fa9f63

Please sign in to comment.