From 3147229a8b97a3ed701225de93645fdf2954a6bc Mon Sep 17 00:00:00 2001 From: eschleb Date: Mon, 23 Sep 2024 10:09:04 +0200 Subject: [PATCH] Extract spring related code into separate module --- README.md | 24 ++++++---- pom.xml | 17 +++---- ...ArgumentResolverFactoryClassGenerator.java | 36 -------------- .../PowerNodeConfigurationClassGenerator.java | 48 ------------------- .../powernode/generator/SourceGenerator.java | 11 ++--- powernode-spring/pom.xml | 38 +++++++++++++++ .../DelegatingPowerNodeArgumentResolver.java | 35 +++++++------- .../configuration/PowerNodeConfiguration.java | 17 ++++++- 8 files changed, 98 insertions(+), 128 deletions(-) delete mode 100644 powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/DelegatingPowerNodeArgumentResolverFactoryClassGenerator.java delete mode 100644 powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/PowerNodeConfigurationClassGenerator.java create mode 100644 powernode-spring/pom.xml rename {powernode-base => powernode-spring}/src/main/java/com/merkle/oss/magnolia/powernode/DelegatingPowerNodeArgumentResolver.java (63%) rename powernode-base/src/main/java/com/merkle/oss/magnolia/powernode/configuration/BasePowerNodeConfiguration.java => powernode-spring/src/main/java/com/merkle/oss/magnolia/powernode/configuration/PowerNodeConfiguration.java (61%) diff --git a/README.md b/README.md index 1e460a7..7c6982c 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,19 @@ Advantages: ## Setup -* Add Maven dependency: +* Add Maven dependencies: ```xml com.namics.oss.magnolia magnolia-powernode 2.1.0 + + + com.namics.oss.magnolia + magnolia-powernode-spring + 2.1.0 + ``` * Add binding for ZoneId provider @@ -52,7 +58,7 @@ Advantages: ``` -* Import Spring Configuration: +* Import Spring Configuration (only if used with blossom): ```java @Configuration @Import({PowerNodeConfiguration.class}) @@ -61,10 +67,10 @@ Advantages: } ``` -* If needed, add the DelegatingPowerNodeArgumentResolver in the `BlossomServletConfiguration` as follows: - ```java - @Override - protected void addArgumentResolvers(final List argumentResolvers) { - argumentResolvers.add(delegatingPowerNodeArgumentResolverFactory.create(new BlossomHandlerMethodArgumentResolver())); - } - ``` \ No newline at end of file + * If needed, add the DelegatingPowerNodeArgumentResolver in the `BlossomServletConfiguration` as follows: + ```java + @Override + protected void addArgumentResolvers(final List argumentResolvers) { + argumentResolvers.add(delegatingPowerNodeArgumentResolverFactory.create(new BlossomHandlerMethodArgumentResolver())); + } + ``` \ No newline at end of file diff --git a/pom.xml b/pom.xml index 96f04da..6ae1ec7 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ powernode-generator powernode-base powernode + powernode-spring @@ -82,6 +83,11 @@ magnolia-powernode-generator ${project.version} + + com.namics.oss.magnolia + magnolia-powernode + ${project.version} + com.google.code.findbugs jsr305 @@ -114,16 +120,7 @@ magnolia-rendering - - org.springframework - spring-web - ${spring.version} - - - org.springframework - spring-context - ${spring.version} - + javax.servlet diff --git a/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/DelegatingPowerNodeArgumentResolverFactoryClassGenerator.java b/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/DelegatingPowerNodeArgumentResolverFactoryClassGenerator.java deleted file mode 100644 index b763d68..0000000 --- a/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/DelegatingPowerNodeArgumentResolverFactoryClassGenerator.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.merkle.oss.magnolia.powernode.generator; - -import com.merkle.oss.magnolia.powernode.DelegatingPowerNodeArgumentResolver.AbstractDelegatingPowerNodeArgumentResolverFactory; -import com.squareup.javapoet.*; -import org.springframework.stereotype.Component; - -import javax.inject.Inject; -import javax.lang.model.element.Modifier; -import java.util.List; - -public class DelegatingPowerNodeArgumentResolverFactoryClassGenerator implements ClassGenerator { - public static final ClassName CLASS_NAME = ClassName.get(AbstractDelegatingPowerNodeArgumentResolverFactory.class.getPackageName(), "DelegatingPowerNodeArgumentResolverFactory"); - - @Override - public JavaFile generate(final AnnotationSpec... additionalClassAnnotations) { - final MethodSpec constructor = MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addAnnotation(Inject.class) - .addParameter(PowerNodeDecoratorClassGenerator.CLASS_NAME, "decorator", Modifier.FINAL) - .addStatement("super(decorator, $T.class)", PowerNodeClassGenerator.CLASS_NAME) - .build(); - - final TypeSpec type = TypeSpec.classBuilder(CLASS_NAME) - .addAnnotations(List.of(additionalClassAnnotations)) - .addAnnotation(Component.class) - .addModifiers(Modifier.PUBLIC, Modifier.FINAL) - .superclass(ParameterizedTypeName.get(ClassName.get(AbstractDelegatingPowerNodeArgumentResolverFactory.class), PowerNodeClassGenerator.CLASS_NAME)) - .addMethod(constructor) - .build(); - - return JavaFile.builder(CLASS_NAME.packageName(), type) - .skipJavaLangImports(true) - .indent("\t") - .build(); - } -} diff --git a/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/PowerNodeConfigurationClassGenerator.java b/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/PowerNodeConfigurationClassGenerator.java deleted file mode 100644 index f416b5e..0000000 --- a/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/PowerNodeConfigurationClassGenerator.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.merkle.oss.magnolia.powernode.generator; - -import com.merkle.oss.magnolia.powernode.configuration.BasePowerNodeConfiguration; -import com.squareup.javapoet.*; -import info.magnolia.objectfactory.Components; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.Scope; - -import javax.lang.model.element.Modifier; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -public class PowerNodeConfigurationClassGenerator implements ClassGenerator { - public static final ClassName CLASS_NAME = ClassName.get(BasePowerNodeConfiguration.class.getPackageName(), "PowerNodeConfiguration"); - private static final Set GUICE_DELEGATE_BEANS = Set.of( - PowerNodeDecoratorClassGenerator.CLASS_NAME, - PowerNodeServiceClassGenerator.CLASS_NAME - ); - - @Override - public JavaFile generate(final AnnotationSpec... additionalClassAnnotations) { - final TypeSpec type = TypeSpec.classBuilder(CLASS_NAME) - .addAnnotation(Configuration.class) - .addAnnotation(AnnotationSpec.builder(Import.class).addMember("value", "{$T.class}", BasePowerNodeConfiguration.class).build()) - .addAnnotations(List.of(additionalClassAnnotations)) - .addModifiers(Modifier.PUBLIC) - .addMethods(GUICE_DELEGATE_BEANS.stream().map(this::guiceBean).collect(Collectors.toList())) - .build(); - return JavaFile.builder(CLASS_NAME.packageName(), type) - .skipJavaLangImports(true) - .indent("\t") - .build(); - } - - private MethodSpec guiceBean(final ClassName className) { - return MethodSpec.methodBuilder(className.simpleName()+"_binding") - .addAnnotation(Bean.class) - .addAnnotation(AnnotationSpec.builder(Scope.class).addMember("value", "$S", ConfigurableBeanFactory.SCOPE_PROTOTYPE).build()) - .addModifiers(Modifier.PUBLIC) - .returns(className) - .addStatement("return $T.getComponent($T.class)", Components.class, className) - .build(); - } -} diff --git a/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/SourceGenerator.java b/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/SourceGenerator.java index 95dd119..744a9d7 100644 --- a/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/SourceGenerator.java +++ b/powernode-generator/src/main/java/com/merkle/oss/magnolia/powernode/generator/SourceGenerator.java @@ -1,9 +1,5 @@ package com.merkle.oss.magnolia.powernode.generator; -import com.squareup.javapoet.AnnotationSpec; -import com.squareup.javapoet.JavaFile; - -import javax.annotation.processing.Generated; import java.io.IOException; import java.nio.file.Path; import java.time.LocalDateTime; @@ -11,12 +7,15 @@ import java.util.Set; import java.util.stream.Collectors; +import javax.annotation.processing.Generated; + +import com.squareup.javapoet.AnnotationSpec; +import com.squareup.javapoet.JavaFile; + public class SourceGenerator { private final Set classGenerators = Set.of( new PowerNodeClassGenerator(), new PowerNodeDecoratorClassGenerator(), - new DelegatingPowerNodeArgumentResolverFactoryClassGenerator(), - new PowerNodeConfigurationClassGenerator(), new PowerNodeServiceClassGenerator() ); diff --git a/powernode-spring/pom.xml b/powernode-spring/pom.xml new file mode 100644 index 0000000..b25e73e --- /dev/null +++ b/powernode-spring/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.namics.oss.magnolia + magnolia-powernode-parent + 2.1.1-SNAPSHOT + + + magnolia-powernode-spring + + + false + + + + + com.namics.oss.magnolia + magnolia-powernode-base + + + com.namics.oss.magnolia + magnolia-powernode + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + \ No newline at end of file diff --git a/powernode-base/src/main/java/com/merkle/oss/magnolia/powernode/DelegatingPowerNodeArgumentResolver.java b/powernode-spring/src/main/java/com/merkle/oss/magnolia/powernode/DelegatingPowerNodeArgumentResolver.java similarity index 63% rename from powernode-base/src/main/java/com/merkle/oss/magnolia/powernode/DelegatingPowerNodeArgumentResolver.java rename to powernode-spring/src/main/java/com/merkle/oss/magnolia/powernode/DelegatingPowerNodeArgumentResolver.java index 13360e7..ef977c9 100644 --- a/powernode-base/src/main/java/com/merkle/oss/magnolia/powernode/DelegatingPowerNodeArgumentResolver.java +++ b/powernode-spring/src/main/java/com/merkle/oss/magnolia/powernode/DelegatingPowerNodeArgumentResolver.java @@ -1,30 +1,32 @@ package com.merkle.oss.magnolia.powernode; import org.springframework.core.MethodParameter; +import org.springframework.stereotype.Component; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import javax.annotation.Nullable; +import javax.inject.Inject; import javax.jcr.Node; import java.util.Optional; +import com.merkle.oss.magnolia.powernode.PowerNode; +import com.merkle.oss.magnolia.powernode.PowerNodeDecorator; + /** - * Resolves arguments with type {@link AbstractPowerNode} by resolving type {@link Node} and wrapping it. + * Resolves arguments with type {@link PowerNode} by resolving type {@link Node} and wrapping it. */ -public class DelegatingPowerNodeArgumentResolver> implements HandlerMethodArgumentResolver { - private final AbstractPowerNodeDecorator decorator; - private final Class powerNodeClass; +public class DelegatingPowerNodeArgumentResolver implements HandlerMethodArgumentResolver { + private final PowerNodeDecorator decorator; private final HandlerMethodArgumentResolver delegate; public DelegatingPowerNodeArgumentResolver( - final AbstractPowerNodeDecorator decorator, - final Class powerNodeClass, + final PowerNodeDecorator decorator, final HandlerMethodArgumentResolver delegate ) { this.decorator = decorator; - this.powerNodeClass = powerNodeClass; this.delegate = delegate; } @@ -51,7 +53,7 @@ public Object resolveArgument( } private Optional nodeMethodParam(final MethodParameter methodParameter) { - if(methodParameter.getParameterType().isAssignableFrom(powerNodeClass)) { + if(methodParameter.getParameterType().isAssignableFrom(PowerNode.class)) { return Optional.of( new MethodParameter(methodParameter) { @Override @@ -64,20 +66,17 @@ public Class getParameterType() { return Optional.empty(); } - public static abstract class AbstractDelegatingPowerNodeArgumentResolverFactory> { - private final AbstractPowerNodeDecorator decorator; - private final Class powerNodeClass; + @Component + public static class Factory { + private final PowerNodeDecorator decorator; - public AbstractDelegatingPowerNodeArgumentResolverFactory( - final AbstractPowerNodeDecorator decorator, - final Class powerNodeClass - ) { + @Inject + public Factory(final PowerNodeDecorator decorator) { this.decorator = decorator; - this.powerNodeClass = powerNodeClass; } - public DelegatingPowerNodeArgumentResolver create(final HandlerMethodArgumentResolver delegate) { - return new DelegatingPowerNodeArgumentResolver<>(decorator, powerNodeClass, delegate); + public DelegatingPowerNodeArgumentResolver create(final HandlerMethodArgumentResolver delegate) { + return new DelegatingPowerNodeArgumentResolver(decorator, delegate); } } } diff --git a/powernode-base/src/main/java/com/merkle/oss/magnolia/powernode/configuration/BasePowerNodeConfiguration.java b/powernode-spring/src/main/java/com/merkle/oss/magnolia/powernode/configuration/PowerNodeConfiguration.java similarity index 61% rename from powernode-base/src/main/java/com/merkle/oss/magnolia/powernode/configuration/BasePowerNodeConfiguration.java rename to powernode-spring/src/main/java/com/merkle/oss/magnolia/powernode/configuration/PowerNodeConfiguration.java index a6cec41..cda4912 100644 --- a/powernode-base/src/main/java/com/merkle/oss/magnolia/powernode/configuration/BasePowerNodeConfiguration.java +++ b/powernode-spring/src/main/java/com/merkle/oss/magnolia/powernode/configuration/PowerNodeConfiguration.java @@ -1,6 +1,9 @@ package com.merkle.oss.magnolia.powernode.configuration; import com.merkle.oss.magnolia.powernode.NodeService; +import com.merkle.oss.magnolia.powernode.PowerNodeDecorator; +import com.merkle.oss.magnolia.powernode.PowerNodeService; + import info.magnolia.objectfactory.Components; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Bean; @@ -18,7 +21,19 @@ @ComponentScan.Filter(Service.class) } ) -public class BasePowerNodeConfiguration { +public class PowerNodeConfiguration { + + @Bean + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public PowerNodeDecorator PowerNodeDecorator_binding() { + return Components.getComponent(PowerNodeDecorator.class); + } + + @Bean + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public PowerNodeService PowerNodeService_binding() { + return Components.getComponent(PowerNodeService.class); + } @Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)