Skip to content

Commit

Permalink
Extract spring related code into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Sep 23, 2024
1 parent 209fc41 commit 3147229
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 128 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ Advantages:

## Setup

* Add Maven dependency:
* Add Maven dependencies:
```xml
<dependency>
<groupId>com.namics.oss.magnolia</groupId>
<artifactId>magnolia-powernode</artifactId>
<version>2.1.0</version>
</dependency>
<!-- only if used with blossom -->
<dependency>
<groupId>com.namics.oss.magnolia</groupId>
<artifactId>magnolia-powernode-spring</artifactId>
<version>2.1.0</version>
</dependency>
```

* Add binding for ZoneId provider
Expand Down Expand Up @@ -52,7 +58,7 @@ Advantages:
</components>
```

* Import Spring Configuration:
* Import Spring Configuration (only if used with blossom):
```java
@Configuration
@Import({PowerNodeConfiguration.class})
Expand All @@ -61,10 +67,10 @@ Advantages:
}
```

* If needed, add the DelegatingPowerNodeArgumentResolver in the `BlossomServletConfiguration` as follows:
```java
@Override
protected void addArgumentResolvers(final List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(delegatingPowerNodeArgumentResolverFactory.create(new BlossomHandlerMethodArgumentResolver()));
}
```
* If needed, add the DelegatingPowerNodeArgumentResolver in the `BlossomServletConfiguration` as follows:
```java
@Override
protected void addArgumentResolvers(final List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(delegatingPowerNodeArgumentResolverFactory.create(new BlossomHandlerMethodArgumentResolver()));
}
```
17 changes: 7 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<module>powernode-generator</module>
<module>powernode-base</module>
<module>powernode</module>
<module>powernode-spring</module>
</modules>

<properties>
Expand Down Expand Up @@ -82,6 +83,11 @@
<artifactId>magnolia-powernode-generator</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.namics.oss.magnolia</groupId>
<artifactId>magnolia-powernode</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down Expand Up @@ -114,16 +120,7 @@
<artifactId>magnolia-rendering</artifactId>
<!-- required to implement PowerNodeArgumentResolver -->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
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;
import java.util.Collection;
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<ClassGenerator> classGenerators = Set.of(
new PowerNodeClassGenerator(),
new PowerNodeDecoratorClassGenerator(),
new DelegatingPowerNodeArgumentResolverFactoryClassGenerator(),
new PowerNodeConfigurationClassGenerator(),
new PowerNodeServiceClassGenerator()
);

Expand Down
38 changes: 38 additions & 0 deletions powernode-spring/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.namics.oss.magnolia</groupId>
<artifactId>magnolia-powernode-parent</artifactId>
<version>2.1.1-SNAPSHOT</version>
</parent>

<artifactId>magnolia-powernode-spring</artifactId>

<properties>
<maven.deploy.skip>false</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>com.namics.oss.magnolia</groupId>
<artifactId>magnolia-powernode-base</artifactId>
</dependency>
<dependency>
<groupId>com.namics.oss.magnolia</groupId>
<artifactId>magnolia-powernode</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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<N extends AbstractPowerNode<N>> implements HandlerMethodArgumentResolver {
private final AbstractPowerNodeDecorator<N> decorator;
private final Class<N> powerNodeClass;
public class DelegatingPowerNodeArgumentResolver implements HandlerMethodArgumentResolver {
private final PowerNodeDecorator decorator;
private final HandlerMethodArgumentResolver delegate;

public DelegatingPowerNodeArgumentResolver(
final AbstractPowerNodeDecorator<N> decorator,
final Class<N> powerNodeClass,
final PowerNodeDecorator decorator,
final HandlerMethodArgumentResolver delegate
) {
this.decorator = decorator;
this.powerNodeClass = powerNodeClass;
this.delegate = delegate;
}

Expand All @@ -51,7 +53,7 @@ public Object resolveArgument(
}

private Optional<MethodParameter> nodeMethodParam(final MethodParameter methodParameter) {
if(methodParameter.getParameterType().isAssignableFrom(powerNodeClass)) {
if(methodParameter.getParameterType().isAssignableFrom(PowerNode.class)) {
return Optional.of(
new MethodParameter(methodParameter) {
@Override
Expand All @@ -64,20 +66,17 @@ public Class<?> getParameterType() {
return Optional.empty();
}

public static abstract class AbstractDelegatingPowerNodeArgumentResolverFactory<N extends AbstractPowerNode<N>> {
private final AbstractPowerNodeDecorator<N> decorator;
private final Class<N> powerNodeClass;
@Component
public static class Factory {
private final PowerNodeDecorator decorator;

public AbstractDelegatingPowerNodeArgumentResolverFactory(
final AbstractPowerNodeDecorator<N> decorator,
final Class<N> powerNodeClass
) {
@Inject
public Factory(final PowerNodeDecorator decorator) {
this.decorator = decorator;
this.powerNodeClass = powerNodeClass;
}

public DelegatingPowerNodeArgumentResolver<N> create(final HandlerMethodArgumentResolver delegate) {
return new DelegatingPowerNodeArgumentResolver<>(decorator, powerNodeClass, delegate);
public DelegatingPowerNodeArgumentResolver create(final HandlerMethodArgumentResolver delegate) {
return new DelegatingPowerNodeArgumentResolver(decorator, delegate);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 3147229

Please sign in to comment.