Add flag to disable context events

- EnableStateMachineFactory and EnableStateMachine now
  have contextEvents defaulting to true which can
  be used to disable spring app context events.
- Fixes #28
This commit is contained in:
Janne Valkealahti
2015-04-03 23:36:28 +01:00
parent e1be34cc92
commit cceec5500a
7 changed files with 263 additions and 26 deletions

View File

@@ -53,4 +53,12 @@ public @interface EnableStateMachine {
*/
String[] name() default {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE};
/**
* Defines if application context events for a state machine are
* enable or not. On default events are enabled.
*
* @return true, if events are enabled.
*/
boolean contextEvents() default true;
}

View File

@@ -41,7 +41,7 @@ import org.springframework.statemachine.config.configuration.StateMachineFactory
@EnableAnnotationConfiguration
@Import({StateMachineFactoryConfiguration.class,ObjectPostProcessorConfiguration.class})
public @interface EnableStateMachineFactory {
/**
* The name of bean, or if plural, aliases for bean created based on this
* annotation. If left unspecified bean name will be autogenerated.
@@ -50,5 +50,13 @@ public @interface EnableStateMachineFactory {
* @return the array if names or empty as default
*/
String[] name() default {StateMachineSystemConstants.DEFAULT_ID_STATEMACHINEFACTORY};
/**
* Defines if application context events for a state machine are
* enable or not. On default events are enabled.
*
* @return true, if events are enabled.
*/
boolean contextEvents() default true;
}

View File

@@ -66,6 +66,8 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
private final StateMachineStates<S, E> stateMachineStates;
private Boolean contextEvents;
/**
* Instantiates a new enum state machine factory.
*
@@ -140,7 +142,7 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
Collection<StateData<S, E>> stateDatas = popSameParents(stateStack);
Collection<TransitionData<S, E>> transitionsData = getTransitionData(iterator.hasNext(), stateDatas);
machine = buildMachine(machineMap, stateMap, stateDatas, transitionsData, getBeanFactory());
machine = buildMachine(machineMap, stateMap, stateDatas, transitionsData, getBeanFactory(), contextEvents);
// TODO: last part in if feels a bit hack
// (!peek.isInitial() && !machineMap.containsKey(peek.getParent()))
if (peek.isInitial() || (!peek.isInitial() && !machineMap.containsKey(peek.getParent()))) {
@@ -168,7 +170,7 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
}
if (initials == 1) {
machine = buildMachine(machineMap, stateMap, stateDatas, transitionsData, getBeanFactory());
machine = buildMachine(machineMap, stateMap, stateDatas, transitionsData, getBeanFactory(), contextEvents);
}
}
@@ -183,13 +185,21 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
RegionState<S, E> rstate = new RegionState<S, E>(null, regions);
Collection<State<S, E>> states = new ArrayList<State<S, E>>();
states.add(rstate);
machine = new EnumStateMachine<S, E>(states, null, rstate, null);
EnumStateMachine<S, E> m = new EnumStateMachine<S, E>(states, null, rstate, null);
if (contextEvents != null) {
m.setContextEventsEnabled(contextEvents);
}
machine = m;
}
}
return machine;
}
public void setContextEventsEnabled(Boolean contextEvents) {
this.contextEvents = contextEvents;
}
private Collection<TransitionData<S, E>> getTransitionData(boolean roots, Collection<StateData<S, E>> stateDatas) {
if (roots) {
return resolveTransitionData(stateMachineTransitions.getTransitions(), stateDatas);
@@ -257,7 +267,7 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
private static <S extends Enum<S>, E extends Enum<E>> StateMachine<S, E> buildMachine(
Map<Object, StateMachine<S, E>> machineMap, Map<S, State<S, E>> stateMap, Collection<StateData<S, E>> stateDatas,
Collection<TransitionData<S, E>> transitionsData, BeanFactory beanFactory) {
Collection<TransitionData<S, E>> transitionsData, BeanFactory beanFactory, Boolean contextEvents) {
State<S, E> state = null;
State<S, E> initialState = null;
Action<S, E> initialAction = null;
@@ -339,6 +349,9 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
EnumStateMachine<S, E> machine = new EnumStateMachine<S, E>(states, transitions, initialState,
initialTransition, endState, null, null);
if (contextEvents != null) {
machine.setContextEventsEnabled(contextEvents);
}
machine.afterPropertiesSet();
if (beanFactory != null) {
machine.setBeanFactory(beanFactory);

View File

@@ -21,7 +21,9 @@ import java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachine;
@@ -45,9 +47,14 @@ public class StateMachineConfiguration<S extends Enum<S>, E extends Enum<E>> ext
Class<? extends Annotation> namedAnnotation) throws Exception {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
.rootBeanDefinition(StateMachineDelegatingFactoryBean2.class);
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(
EnableStateMachine.class.getName(), false));
Boolean contextEvents = attributes.getBoolean("contextEvents");
beanDefinitionBuilder.addConstructorArgValue(builder);
beanDefinitionBuilder.addConstructorArgValue(StateMachine.class);
beanDefinitionBuilder.addConstructorArgValue(importingClassMetadata.getClassName());
beanDefinitionBuilder.addConstructorArgValue(contextEvents);
return beanDefinitionBuilder.getBeanDefinition();
}
@@ -62,11 +69,13 @@ public class StateMachineConfiguration<S extends Enum<S>, E extends Enum<E>> ext
extends BeanDelegatingFactoryBean<StateMachine<S, E>,StateMachineConfigBuilder<S, E>,StateMachineConfig<S, E>> {
private String clazzName;
private Boolean contextEvents;
public StateMachineDelegatingFactoryBean2(StateMachineConfigBuilder<S, E> builder, Class<StateMachine<S, E>> clazz,
String clazzName) {
String clazzName, Boolean contextEvents) {
super(builder, clazz);
this.clazzName = clazzName;
this.contextEvents = contextEvents;
}
@Override
@@ -83,6 +92,7 @@ public class StateMachineConfiguration<S extends Enum<S>, E extends Enum<E>> ext
EnumStateMachineFactory<S, E> stateMachineFactory = new EnumStateMachineFactory<S, E>(
stateMachineTransitions, stateMachineStates);
stateMachineFactory.setBeanFactory(getBeanFactory());
stateMachineFactory.setContextEventsEnabled(contextEvents);
setObject(stateMachineFactory.getStateMachine());
}

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2015 the original author or authors.
*
* 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.springframework.statemachine.config.configuration;
import java.lang.annotation.Annotation;
@@ -12,7 +27,9 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineFactory;
@@ -35,7 +52,11 @@ public class StateMachineFactoryConfiguration<S extends Enum<S>, E extends Enum<
Class<? extends Annotation> namedAnnotation) throws Exception {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
.rootBeanDefinition(StateMachineFactoryDelegatingFactoryBean.class);
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(
EnableStateMachineFactory.class.getName(), false));
Boolean contextEvents = attributes.getBoolean("contextEvents");
beanDefinitionBuilder.addConstructorArgValue(builder);
beanDefinitionBuilder.addConstructorArgValue(contextEvents);
return beanDefinitionBuilder.getBeanDefinition();
}
@@ -58,9 +79,12 @@ public class StateMachineFactoryConfiguration<S extends Enum<S>, E extends Enum<
private StateMachineFactory<S, E> stateMachineFactory;
private Boolean contextEvents;
@SuppressWarnings("unused")
public StateMachineFactoryDelegatingFactoryBean(StateMachineConfigBuilder<S, E> builder) {
public StateMachineFactoryDelegatingFactoryBean(StateMachineConfigBuilder<S, E> builder, Boolean contextEvents) {
this.builder = builder;
this.contextEvents = contextEvents;
}
@Override
@@ -88,6 +112,7 @@ public class StateMachineFactoryConfiguration<S extends Enum<S>, E extends Enum<
StateMachineStates<S, E> stateMachineStates = stateMachineConfig.getStates();
EnumStateMachineFactory<S,E> enumStateMachineFactory = new EnumStateMachineFactory<S, E>(stateMachineTransitions, stateMachineStates);
enumStateMachineFactory.setBeanFactory(beanFactory);
enumStateMachineFactory.setContextEventsEnabled(contextEvents);
this.stateMachineFactory = enumStateMachineFactory;
}

View File

@@ -105,6 +105,8 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
private final Map<Trigger<S, E>, Transition<S,E>> triggerToTransitionMap = new HashMap<Trigger<S,E>, Transition<S,E>>();
private boolean contextEventsEnabled = true;
/**
* Instantiates a new abstract state machine.
*
@@ -270,6 +272,17 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
return buf.toString();
}
/**
* Set if context application events are enabled. Events
* are enabled by default. Set this to false if you don't
* want state machine to send application context events.
*
* @param contextEventsEnabled the enabled flag
*/
public void setContextEventsEnabled(boolean contextEventsEnabled) {
this.contextEventsEnabled = contextEventsEnabled;
}
protected boolean acceptEvent(Message<E> event) {
boolean accepted = currentState.sendEvent(event);
@@ -692,49 +705,61 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
private void notifyStateChanged(State<S,E> source, State<S,E> target) {
stateListener.stateChanged(source, target);
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishStateChanged(this, source, target);
if (contextEventsEnabled) {
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishStateChanged(this, source, target);
}
}
}
private void notifyStateEntered(State<S,E> state) {
stateListener.stateEntered(state);
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishStateEntered(this, state);
if (contextEventsEnabled) {
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishStateEntered(this, state);
}
}
}
private void notifyStateExited(State<S,E> state) {
stateListener.stateExited(state);
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishStateExited(this, state);
if (contextEventsEnabled) {
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishStateExited(this, state);
}
}
}
private void notifyTransitionStart(Transition<S,E> transition) {
stateListener.transitionStarted(transition);
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishTransitionStart(this, transition);
if (contextEventsEnabled) {
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishTransitionStart(this, transition);
}
}
}
private void notifyTransition(Transition<S,E> transition) {
stateListener.transition(transition);
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishTransitionEnd(this, transition);
if (contextEventsEnabled) {
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishTransitionEnd(this, transition);
}
}
}
private void notifyTransitionEnd(Transition<S,E> transition) {
stateListener.transitionEnded(transition);
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishTransition(this, transition);
if (contextEventsEnabled) {
StateMachineEventPublisher eventPublisher = getStateMachineEventPublisher();
if (eventPublisher != null) {
eventPublisher.publishTransition(this, transition);
}
}
}

View File

@@ -0,0 +1,148 @@
/*
* Copyright 2015 the original author or authors.
*
* 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.springframework.statemachine.event;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.EnumStateMachine;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
/**
* State machine context events related tests.
*
* @author Janne Valkealahti
*
*/
public class ContextEventTests extends AbstractStateMachineTests {
@Override
protected AnnotationConfigApplicationContext buildContext() {
return new AnnotationConfigApplicationContext();
}
@SuppressWarnings("unchecked")
@Test
public void contextEventsEnabled() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config.class, Config1.class);
context.refresh();
EnumStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
machine.start();
machine.sendEvent(TestEvents.E1);
StateMachineApplicationEventListener listener = context.getBean(StateMachineApplicationEventListener.class);
listener.latch.await(1, TimeUnit.SECONDS);
assertThat(listener.count, greaterThan(1));
}
@SuppressWarnings("unchecked")
@Test
public void contextEventsDisabled() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config.class, Config2.class);
context.refresh();
EnumStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
machine.start();
machine.sendEvent(TestEvents.E1);
StateMachineApplicationEventListener listener = context.getBean(StateMachineApplicationEventListener.class);
listener.latch.await(1, TimeUnit.SECONDS);
assertThat(listener.count, is(0));
}
@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.S1)
.state(TestStates.S1)
.state(TestStates.S2);
}
@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
transitions
.withExternal()
.source(TestStates.S1)
.target(TestStates.S2)
.event(TestEvents.E1);
}
}
@Configuration
@EnableStateMachine(contextEvents = false)
static class Config2 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.S1)
.state(TestStates.S1)
.state(TestStates.S2);
}
@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
transitions
.withExternal()
.source(TestStates.S1)
.target(TestStates.S2)
.event(TestEvents.E1);
}
}
@Configuration
static class Config {
@Bean
public StateMachineApplicationEventListener stateMachineApplicationEventListener() {
return new StateMachineApplicationEventListener();
}
}
static class StateMachineApplicationEventListener implements ApplicationListener<StateMachineEvent> {
CountDownLatch latch = new CountDownLatch(1);
int count = 0;
@Override
public void onApplicationEvent(StateMachineEvent event) {
count++;
latch.countDown();
}
}
}