-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AxonConfigurer created default ResourceInjector that injects componen…
…ts from Configuration The Spring configuration default to a SpringResourceInjector instead, which autowires Saga instances.
- Loading branch information
Showing
9 changed files
with
209 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/src/main/java/org/axonframework/config/ConfigurationResourceInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
package org.axonframework.config; | |||
|
|||
import org.axonframework.eventhandling.saga.AbstractResourceInjector; | |||
|
|||
import java.util.Optional; | |||
|
|||
/** | |||
* ResourceInjector implementation that injects resources defined in the Axon Configuration. | |||
*/ | |||
public class ConfigurationResourceInjector extends AbstractResourceInjector { | |||
|
|||
private final Configuration configuration; | |||
|
|||
/** | |||
* Initializes the ResourceInjector to inject the resources found in the given {@code configuration}. | |||
* | |||
* @param configuration the Configuration to find injectable resources in | |||
*/ | |||
public ConfigurationResourceInjector(Configuration configuration) { | |||
this.configuration = configuration; | |||
} | |||
|
|||
@Override | |||
protected <R> Optional<R> findResource(Class<R> requiredType) { | |||
return Optional.ofNullable(configuration.getComponent(requiredType)); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
core/src/main/java/org/axonframework/eventhandling/saga/AbstractResourceInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,84 @@ | |||
/* | |||
* Copyright (c) 2010-2016. Axon Framework | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
|
|||
package org.axonframework.eventhandling.saga; | |||
|
|||
import org.axonframework.common.ReflectionUtils; | |||
import org.axonframework.common.annotation.AnnotationUtils; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
|
|||
import java.lang.reflect.Field; | |||
import java.lang.reflect.InvocationTargetException; | |||
import java.lang.reflect.Method; | |||
import java.util.Optional; | |||
|
|||
import static org.axonframework.common.ReflectionUtils.fieldsOf; | |||
import static org.axonframework.common.ReflectionUtils.methodsOf; | |||
|
|||
public abstract class AbstractResourceInjector implements ResourceInjector { | |||
|
|||
private static final Logger logger = LoggerFactory.getLogger(AbstractResourceInjector.class); | |||
|
|||
private static final String FULLY_QUALIFIED_CLASS_NAME_INJECT = "javax.inject.Inject"; | |||
|
|||
@Override | |||
public void injectResources(Object saga) { | |||
injectFieldResources(saga); | |||
injectMethodResources(saga); | |||
} | |||
|
|||
private void injectFieldResources(Object saga) { | |||
fieldsOf(saga.getClass()).forEach( | |||
field -> Optional.ofNullable(AnnotationUtils.findAnnotation(field, FULLY_QUALIFIED_CLASS_NAME_INJECT)) | |||
.ifPresent(annotatedFields -> { | |||
Class<?> requiredType = field.getType(); | |||
findResource(requiredType).ifPresent(resource -> injectFieldResource(saga, field, resource)); | |||
})); | |||
} | |||
|
|||
protected abstract <R> Optional<R> findResource(Class<R> requiredType); | |||
|
|||
private void injectFieldResource(Object saga, Field injectField, Object resource) { | |||
try { | |||
ReflectionUtils.ensureAccessible(injectField); | |||
injectField.set(saga, resource); | |||
} catch (IllegalAccessException e) { | |||
logger.warn("Unable to inject resource. Exception while setting field: ", e); | |||
} | |||
} | |||
|
|||
private void injectMethodResources(Object saga) { | |||
methodsOf(saga.getClass()).forEach( | |||
method -> Optional.ofNullable(AnnotationUtils.findAnnotation(method, FULLY_QUALIFIED_CLASS_NAME_INJECT)) | |||
.ifPresent(annotatedMethods -> { | |||
Class<?> requiredType = method.getParameterTypes()[0]; | |||
findResource(requiredType).ifPresent(resource -> injectMethodResource(saga, method, resource)); | |||
})); | |||
} | |||
|
|||
private void injectMethodResource(Object saga, Method injectMethod, Object resource) { | |||
try { | |||
ReflectionUtils.ensureAccessible(injectMethod); | |||
injectMethod.invoke(saga, resource); | |||
} catch (IllegalAccessException e) { | |||
logger.warn("Unable to inject resource. Exception while invoking setter: ", e); | |||
} catch (InvocationTargetException e) { | |||
logger.warn("Unable to inject resource. Exception while invoking setter: ", e.getCause()); | |||
} | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
core/src/test/java/org/axonframework/config/ConfigurationResourceInjectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,48 @@ | |||
package org.axonframework.config; | |||
|
|||
import org.axonframework.commandhandling.CommandBus; | |||
import org.axonframework.commandhandling.gateway.CommandGateway; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
|
|||
import javax.inject.Inject; | |||
|
|||
import static org.junit.Assert.*; | |||
|
|||
public class ConfigurationResourceInjectorTest { | |||
|
|||
private Configuration configuration; | |||
private ConfigurationResourceInjector testSubject; | |||
|
|||
@Before | |||
public void setUp() throws Exception { | |||
configuration = DefaultConfigurer.defaultConfiguration().buildConfiguration(); | |||
testSubject = new ConfigurationResourceInjector(configuration); | |||
} | |||
|
|||
@Test | |||
public void testInjectorHasResource() throws Exception { | |||
Saga saga = new Saga(); | |||
testSubject.injectResources(saga); | |||
|
|||
assertSame(configuration.commandBus(), saga.commandBus); | |||
assertSame(configuration.commandGateway(), saga.commandGateway); | |||
assertNull(saga.inexistent); | |||
} | |||
|
|||
public static class Saga { | |||
|
|||
@Inject | |||
private CommandBus commandBus; | |||
|
|||
@Inject | |||
private String inexistent; | |||
|
|||
private CommandGateway commandGateway; | |||
|
|||
@Inject | |||
public void setCommandGateway(CommandGateway commandGateway) { | |||
this.commandGateway = commandGateway; | |||
} | |||
} | |||
} |
Oops, something went wrong.