Skip to content

Commit

Permalink
Merge pull request #349 from viadeo/prepare_1.1.0_spring
Browse files Browse the repository at this point in the history
Prepare 1.2.0 spring
  • Loading branch information
mglcel committed Oct 26, 2015
2 parents 9081ab8 + 096c5b8 commit ad938ef
Show file tree
Hide file tree
Showing 160 changed files with 869 additions and 331 deletions.
6 changes: 6 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

spring refacto :
- add test AuthroizationInterceptorUTest for Spring (get the original one and put it into kasper-spring)
- add Sagas tests without Spring




urgent: full refactoring of the test domains of kasper-documentation

Expand Down
12 changes: 6 additions & 6 deletions kasper-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ dependencies {
compile project(':kasper-api')
compile project(':kasper-common')

testCompile project(':kasper-test')

// Third party dependencies
compile libraries.REFLECTIONS
compile libraries.SPRING_CONTEXT
compile libraries.JACKSON_CORE
compile libraries.JACKSON_DATABIND
compile libraries.SPRING_AMQP
compile libraries.SPRING_TX
compile libraries.SPRING_CORE
compile libraries.SPRING_CONTEXT
compile libraries.AXON_CORE
compile libraries.QUARTZ_SCHEDULER
compile libraries.QUARTZ_JOBS
Expand All @@ -35,10 +32,13 @@ dependencies {
compile libraries.JERSEY_CLIENT
compile libraries.HYSTRIX

testCompile libraries.SPRING_TEST
// WARNING: only used for AxonFramework CommandGateway
compile libraries.SPRING_BEANS

testCompile libraries.LOGBACK
testCompile libraries.JUNIT
testCompile libraries.MOCKITO
testCompile libraries.HAMCREST
testCompile libraries.JCACHE_EHCACHE
testCompile libraries.HIBERNATE_VALIDATOR
testCompile libraries.AWAITILITY
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.viadeo.kasper.core.component.event.saga;

import com.google.common.collect.Lists;
import com.viadeo.kasper.core.component.event.saga.factory.DefaultSagaFactoryProvider;
import com.viadeo.kasper.core.component.event.saga.factory.SagaFactoryProvider;
import com.viadeo.kasper.core.component.event.saga.repository.InMemorySagaRepository;
import com.viadeo.kasper.core.component.event.saga.repository.SagaRepository;
import com.viadeo.kasper.core.component.event.saga.step.StepChecker;
import com.viadeo.kasper.core.component.event.saga.step.StepProcessor;
import com.viadeo.kasper.core.component.event.saga.step.StepResolver;
import com.viadeo.kasper.core.component.event.saga.step.Steps;
import com.viadeo.kasper.core.component.event.saga.step.facet.FacetApplierRegistry;

import java.util.List;

public class DefaultSagaManager extends SagaManager {

public DefaultSagaManager(
final SagaFactoryProvider sagaFactoryProvider,
final SagaRepository repository,
final StepProcessor operationProcessor
) {
super(sagaFactoryProvider, repository, operationProcessor);
}

public static DefaultSagaManager build() {
final SagaFactoryProvider sagaFactoryProvider = new DefaultSagaFactoryProvider();
final FacetApplierRegistry facetApplierRegistry = new FacetApplierRegistry();
final List<StepResolver> stepResolvers = Lists.newArrayList((StepResolver)
new Steps.StartStepResolver(facetApplierRegistry),
new Steps.BasicStepResolver(facetApplierRegistry),
new Steps.EndStepResolver(facetApplierRegistry)
);

final StepChecker stepChecker = new Steps.Checker();
final StepProcessor operationProcessor = new StepProcessor(
stepChecker,
stepResolvers.toArray(new StepResolver[stepResolvers.size()])
);
final SagaRepository repository = new InMemorySagaRepository(sagaFactoryProvider);

return new DefaultSagaManager(sagaFactoryProvider, repository, operationProcessor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public Optional<SagaExecutor> get(final Class<? extends Saga> sagaClass) {
return Optional.fromNullable(descriptors.get(sagaClass));
}

public StepProcessor getStepProcessor() {
return this.operationProcessor;
}

@VisibleForTesting
public void clear() {
descriptors.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@

import com.viadeo.kasper.core.component.event.saga.Saga;
import com.viadeo.kasper.core.component.event.saga.exception.SagaInstantiationException;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;

/**
* Default implementation of a SagaFactory using @link org.springframework.context.ApplicationContext
* Default implementation of a SagaFactory
*/
public class DefaultSagaFactory implements SagaFactory {

private final AutowireCapableBeanFactory beanFactory;

public DefaultSagaFactory(final ApplicationContext applicationContext) {
beanFactory = applicationContext.getAutowireCapableBeanFactory();
}

@Override
public <SAGA extends Saga> SAGA create(final Object identifier, final Class<SAGA> sagaClass) {
try {
return (SAGA) beanFactory.autowire(sagaClass, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, Boolean.TRUE);
} catch (final UnsatisfiedDependencyException e) {
throw new SagaInstantiationException(String.format("Error instantiating saga of '%s'", sagaClass.getName()), e);
return sagaClass.newInstance();
} catch (final InstantiationException | IllegalAccessException e) {
throw new SagaInstantiationException(String.format(
"Error instantiating saga of '%s'",
sagaClass.getName()
), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.viadeo.kasper.core.component.event.saga.Saga;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;

public class DefaultSagaFactoryProvider implements SagaFactoryProvider {

Expand All @@ -36,51 +26,20 @@ public static void clearCache() {
CACHE.clear();
}

private final ApplicationContext applicationContext;

public DefaultSagaFactoryProvider(final ApplicationContext applicationContext) {
this.applicationContext = checkNotNull(applicationContext);
}

@Override
public Optional<SagaFactory> get(final Class<? extends Saga> sagaClass) {
return Optional.fromNullable(CACHE.get(sagaClass));
}

@Override
public SagaFactory getOrCreate(final Saga saga) {
Optional<SagaFactory> optionalSagaFactory = get(saga.getClass());
final Optional<SagaFactory> optionalSagaFactory = get(saga.getClass());

if (optionalSagaFactory.isPresent()) {
return optionalSagaFactory.get();
}

final Set<Class<?>> parameterTypes = Sets.newHashSet();

for (final Constructor<?> constructor : saga.getClass().getConstructors()) {
parameterTypes.addAll(Arrays.asList(constructor.getParameterTypes()));
}

final GenericApplicationContext sagaContext = new GenericApplicationContext();
sagaContext.setParent(applicationContext);

final ConfigurableListableBeanFactory beanFactory = sagaContext.getBeanFactory();

for (final Field field : saga.getClass().getDeclaredFields()) {
if (parameterTypes.contains(field.getType()) && applicationContext.getBeanNamesForType(field.getType()).length == 0) {
field.setAccessible(Boolean.TRUE);
try {
beanFactory.registerSingleton(field.getName(), field.get(saga));
} catch (IllegalAccessException e) {
LOGGER.warn("Failed to enrich the context with '{}'", field.getName(), e);
}
}
}

sagaContext.refresh();

SagaFactory sagaFactory = new DefaultSagaFactory(sagaContext);

final SagaFactory sagaFactory = new DefaultSagaFactory();
CACHE.put(saga.getClass(), sagaFactory);

return sagaFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* Provide a <code>SagaFactory</code> for a given <code>Saga</code>.
*/
public interface SagaFactoryProvider {

SagaFactory getOrCreate(final Saga saga);

Optional<SagaFactory> get(final Class<? extends Saga> sagaClass);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
Expand All @@ -30,7 +28,7 @@
/**
* MethodInvocationScheduler implementation that delegates scheduling and triggering to a Quartz Scheduler.
*/
public class MethodInvocationScheduler implements com.viadeo.kasper.core.component.event.saga.step.Scheduler, SmartLifecycle {
public class MethodInvocationScheduler implements com.viadeo.kasper.core.component.event.saga.step.Scheduler {

private static final Logger LOGGER = LoggerFactory.getLogger(MethodInvocationScheduler.class);

Expand Down Expand Up @@ -69,22 +67,27 @@ public class MethodInvocationScheduler implements com.viadeo.kasper.core.compone

private final ObjectMapper mapper;
private final Scheduler scheduler;
private final ApplicationContext applicationContext;
private final String groupIdentifier;
private final SagaManager sagaManager;

private boolean initialized;

// ------------------------------------------------------------------------

public MethodInvocationScheduler(final ObjectMapper mapper, final Scheduler scheduler, final ApplicationContext applicationContext) {
this(mapper, scheduler, applicationContext, DEFAULT_GROUP_NAME);
public MethodInvocationScheduler(final ObjectMapper mapper, final Scheduler scheduler, final SagaManager sagaManager) {
this(mapper, scheduler, DEFAULT_GROUP_NAME, sagaManager);
}

public MethodInvocationScheduler(final ObjectMapper mapper, final Scheduler scheduler, final ApplicationContext applicationContext, final String groupIdentifier) {
public MethodInvocationScheduler(
final ObjectMapper mapper,
final Scheduler scheduler,
final String groupIdentifier,
final SagaManager sagaManager
) {
this.mapper = mapper;
this.scheduler = checkNotNull(scheduler);
this.applicationContext = checkNotNull(applicationContext);
this.groupIdentifier = checkNotNull(groupIdentifier);
this.sagaManager = checkNotNull(sagaManager);
}

// ------------------------------------------------------------------------
Expand All @@ -93,7 +96,7 @@ public MethodInvocationScheduler(final ObjectMapper mapper, final Scheduler sche
public void initialize() {
try {
this.scheduler.getContext().put(OBJECT_MAPPER_KEY, mapper);
this.scheduler.getContext().put(SAGA_MANAGER_KEY, applicationContext.getBean(SagaManager.class));
this.scheduler.getContext().put(SAGA_MANAGER_KEY, sagaManager);
this.scheduler.start();
initialized = true;
} catch (SchedulerException e) {
Expand Down Expand Up @@ -223,39 +226,6 @@ protected String buildJobIdentifier(final Class<? extends Saga> sagaClass, final
return JOB_NAME_PREFIX + "_" + sagaClass.getName() + "_" + methodName + "_" + identifier.toString();
}

@Override
public void start() {
this.initialize();
}

@Override
public void stop() {
try {
this.shutdown();
} catch (SchedulerException e) {
LOGGER.error("Failed to shutdown the scheduler", e);
}
}

@Override
public boolean isRunning() {
return isInitialized();
}

@Override
public boolean isAutoStartup() {
return false;
}

@Override
public void stop(Runnable callback) {
stop();
}

@Override
public int getPhase() {
return Integer.MAX_VALUE - 1;
}

// ------------------------------------------------------------------------

Expand Down
Loading

0 comments on commit ad938ef

Please sign in to comment.