Enhance config model with StateMachineListener

- Fixes #84
This commit is contained in:
Janne Valkealahti
2015-07-11 16:43:28 +01:00
parent 48bb6631f9
commit c28ace7945
6 changed files with 101 additions and 2 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.statemachine.config.builders.StateMachineTransitions;
import org.springframework.statemachine.config.builders.StateMachineTransitions.ChoiceData;
import org.springframework.statemachine.config.builders.StateMachineTransitions.TransitionData;
import org.springframework.statemachine.ensemble.DistributedStateMachine;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.region.Region;
import org.springframework.statemachine.state.ChoicePseudoState;
import org.springframework.statemachine.state.ChoicePseudoState.ChoiceStateData;
@@ -218,6 +219,10 @@ public abstract class AbstractStateMachineFactory<S, E> extends LifecycleObjectS
machine = distributedStateMachine;
}
for (StateMachineListener<S, E> listener : stateMachineConfigurationConfig.getStateMachineListeners()) {
machine.addStateListener(listener);
}
return delegateAutoStartup(machine);
}

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.statemachine.config.builders;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
@@ -26,6 +29,7 @@ import org.springframework.statemachine.config.configurers.DefaultConfigurationC
import org.springframework.statemachine.config.configurers.DefaultDistributedStateMachineConfigurer;
import org.springframework.statemachine.config.configurers.DistributedStateMachineConfigurer;
import org.springframework.statemachine.ensemble.StateMachineEnsemble;
import org.springframework.statemachine.listener.StateMachineListener;
/**
* {@link AnnotationBuilder} for {@link StateMachineStates}.
@@ -44,6 +48,7 @@ public class StateMachineConfigurationBuilder<S, E>
private TaskScheduler taskScheculer;
private boolean autoStart = false;
private StateMachineEnsemble<S, E> ensemble;
private final List<StateMachineListener<S, E>> listeners = new ArrayList<StateMachineListener<S, E>>();
/**
* Instantiates a new state machine configuration builder.
@@ -84,7 +89,7 @@ public class StateMachineConfigurationBuilder<S, E>
@Override
protected StateMachineConfigurationConfig<S, E> performBuild() throws Exception {
return new StateMachineConfigurationConfig<>(beanFactory, taskExecutor, taskScheculer, autoStart, ensemble);
return new StateMachineConfigurationConfig<>(beanFactory, taskExecutor, taskScheculer, autoStart, ensemble, listeners);
}
/**
@@ -132,4 +137,14 @@ public class StateMachineConfigurationBuilder<S, E>
this.autoStart = autoStart;
}
/**
* Sets the state machine listeners.
*
* @param listeners the listeners
*/
public void setStateMachineListeners(List<StateMachineListener<S, E>> listeners) {
this.listeners.clear();
this.listeners.addAll(listeners);
}
}

View File

@@ -15,10 +15,13 @@
*/
package org.springframework.statemachine.config.builders;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.statemachine.ensemble.StateMachineEnsemble;
import org.springframework.statemachine.listener.StateMachineListener;
/**
* Configuration object used to keep things together in {@link StateMachineConfigurationBuilder}.
@@ -35,6 +38,7 @@ public class StateMachineConfigurationConfig<S, E> {
private final TaskScheduler taskScheduler;
private final boolean autoStart;
private final StateMachineEnsemble<S, E> ensemble;
private final List<StateMachineListener<S, E>> listeners;
/**
* Instantiates a new state machine configuration config.
@@ -44,14 +48,17 @@ public class StateMachineConfigurationConfig<S, E> {
* @param taskScheduler the task scheduler
* @param autoStart the autostart flag
* @param ensemble the state machine ensemble
* @param listeners the state machine listeners
*/
public StateMachineConfigurationConfig(BeanFactory beanFactory, TaskExecutor taskExecutor,
TaskScheduler taskScheduler, boolean autoStart, StateMachineEnsemble<S, E> ensemble) {
TaskScheduler taskScheduler, boolean autoStart, StateMachineEnsemble<S, E> ensemble,
List<StateMachineListener<S, E>> listeners) {
this.beanFactory = beanFactory;
this.taskExecutor = taskExecutor;
this.taskScheduler = taskScheduler;
this.autoStart = autoStart;
this.ensemble = ensemble;
this.listeners = listeners;
}
/**
@@ -98,4 +105,14 @@ public class StateMachineConfigurationConfig<S, E> {
public boolean isAutoStart() {
return autoStart;
}
/**
* Gets the state machine listeners.
*
* @return the state machine listeners
*/
public List<StateMachineListener<S, E>> getStateMachineListeners() {
return listeners;
}
}

View File

@@ -19,7 +19,9 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineConfigurer;
import org.springframework.statemachine.config.common.annotation.AnnotationConfigurerBuilder;
import org.springframework.statemachine.listener.StateMachineListener;
/**
* Base {@code ConfigConfigurer} interface for configuring generic config.
@@ -65,4 +67,14 @@ public interface ConfigurationConfigurer<S, E> extends
*/
ConfigurationConfigurer<S, E> autoStartup(boolean autoStartup);
/**
* Specify a {@link StateMachineListener} to be registered
* with a state machine. This method can be called multiple times
* to register multiple listeners.
*
* @param listener the listener to register
* @return the configuration configurer
*/
ConfigurationConfigurer<S, E> listener(StateMachineListener<S, E> listener);
}

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.statemachine.config.configurers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
@@ -22,6 +25,7 @@ import org.springframework.statemachine.config.builders.StateMachineConfiguratio
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfig;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.common.annotation.AnnotationConfigurerAdapter;
import org.springframework.statemachine.listener.StateMachineListener;
/**
* Default implementation of a {@link ConfigurationConfigurer}.
@@ -39,6 +43,7 @@ public class DefaultConfigurationConfigurer<S, E>
private TaskExecutor taskExecutor;
private TaskScheduler taskScheculer;
private boolean autoStart = false;
private final List<StateMachineListener<S, E>> listeners = new ArrayList<StateMachineListener<S, E>>();
@Override
public void configure(StateMachineConfigurationBuilder<S, E> builder) throws Exception {
@@ -46,6 +51,7 @@ public class DefaultConfigurationConfigurer<S, E>
builder.setTaskExecutor(taskExecutor);
builder.setTaskScheculer(taskScheculer);
builder.setAutoStart(autoStart);
builder.setStateMachineListeners(listeners);
}
@Override
@@ -72,4 +78,10 @@ public class DefaultConfigurationConfigurer<S, E>
return this;
}
@Override
public ConfigurationConfigurer<S, E> listener(StateMachineListener<S, E> listener) {
this.listeners.add(listener);
return this;
}
}

View File

@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -34,10 +35,12 @@ import org.springframework.core.task.TaskExecutor;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.ObjectStateMachine;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.TestUtils;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
/**
* Tests for state machine configuration.
@@ -122,6 +125,19 @@ public class ConfigurationTests extends AbstractStateMachineTests {
assertThat(machine.isRunning(), is(true));
}
@SuppressWarnings({ "unchecked" })
@Test
public void testRegisterListeners() throws Exception {
context.register(Config10.class);
context.refresh();
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
Object o1 = TestUtils.readField("stateListener", machine);
Object o2 = TestUtils.readField("listeners", o1);
Object o3 = TestUtils.readField("list", o2);
assertThat(((List<?>)o3).size(), is(2));
}
@Configuration
@EnableStateMachine
public static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@@ -464,4 +480,26 @@ public class ConfigurationTests extends AbstractStateMachineTests {
}
@Configuration
@EnableStateMachine
public static class Config10 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineConfigurationConfigurer<TestStates, TestEvents> config) throws Exception {
config
.withConfiguration()
.listener(new StateMachineListenerAdapter<TestStates, TestEvents>())
.listener(new StateMachineListenerAdapter<TestStates, TestEvents>());
}
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.S1)
.states(EnumSet.allOf(TestStates.class));
}
}
}