Refactor concepts around uml model factory

- Fixes #226
This commit is contained in:
Janne Valkealahti
2016-05-17 09:11:14 +01:00
parent c8cc2b1ba4
commit ec21afc131
5 changed files with 230 additions and 73 deletions

View File

@@ -15,16 +15,15 @@
*/
package org.springframework.statemachine.config.model;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.guard.Guard;
import org.springframework.util.Assert;
/**
* Base implementation of a {@link StateMachineModelFactory} providing
@@ -43,20 +42,49 @@ import org.springframework.statemachine.guard.Guard;
public abstract class AbstractStateMachineModelFactory<S, E> implements StateMachineComponentResolver<S, E>, BeanFactoryAware, ResourceLoaderAware {
private BeanFactory beanFactory;
private ResourceLoader resourceLoader;
private final Map<String, Action<S, E>> registeredActions = new HashMap<>();
private final Map<String, Guard<S, E>> registeredGuards = new HashMap<>();
private ResourceLoader resourceLoader = new DefaultResourceLoader();
private StateMachineComponentResolver<S, E> stateMachineComponentResolver;
private final DefaultStateMachineComponentResolver<S, E> internalResolver = new DefaultStateMachineComponentResolver<S, E>();
/**
* Instantiates a new abstract state machine model factory.
*/
public AbstractStateMachineModelFactory() {
}
/**
* Instantiates a new abstract state machine model factory.
*
* @param resourceLoader the resource loader
* @param stateMachineComponentResolver the state machine component resolver
*/
public AbstractStateMachineModelFactory(ResourceLoader resourceLoader,
StateMachineComponentResolver<S, E> stateMachineComponentResolver) {
this.resourceLoader = resourceLoader;
this.stateMachineComponentResolver = stateMachineComponentResolver;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
internalResolver.setBeanFactory(beanFactory);
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
Assert.notNull(resourceLoader, "resourceLoader cannot be null");
this.resourceLoader = resourceLoader;
}
/**
* Sets the state machine component resolver.
*
* @param stateMachineComponentResolver the state machine component resolver
*/
public void setStateMachineComponentResolver(StateMachineComponentResolver<S, E> stateMachineComponentResolver) {
this.stateMachineComponentResolver = stateMachineComponentResolver;
}
/**
* Register {@link Action} into factory with a given id.
*
@@ -64,7 +92,7 @@ public abstract class AbstractStateMachineModelFactory<S, E> implements StateMac
* @param action the action
*/
public void registerAction(String id, Action<S, E> action) {
registeredActions.put(id, action);
internalResolver.registerAction(id, action);
}
/**
@@ -74,7 +102,50 @@ public abstract class AbstractStateMachineModelFactory<S, E> implements StateMac
* @param guard the guard
*/
public void registerGuard(String id, Guard<S, E> guard) {
registeredGuards.put(id, guard);
internalResolver.registerGuard(id, guard);
}
/**
* Gets the state machine component resolver.
*
* @return the state machine component resolver
*/
public StateMachineComponentResolver<S, E> getStateMachineComponentResolver() {
return stateMachineComponentResolver;
}
/**
* Resolve action.
*
* @param id the id
* @return the action
*/
public Action<S, E> resolveAction(String id) {
Action<S, E> a = internalResolver.resolveAction(id);
if (a == null && stateMachineComponentResolver != null) {
a = stateMachineComponentResolver.resolveAction(id);
}
if (a == null) {
throw new RuntimeException("Can't resolve action with id " + id + " either from registered actions nor beanfactory");
}
return a;
}
/**
* Resolve guard.
*
* @param id the id
* @return the guard
*/
public Guard<S, E> resolveGuard(String id) {
Guard<S, E> a = internalResolver.resolveGuard(id);
if (a == null && stateMachineComponentResolver != null) {
a = stateMachineComponentResolver.resolveGuard(id);
}
if (a == null) {
throw new RuntimeException("Can't resolve guard with id " + id + " either from registered guards nor beanfactory");
}
return a;
}
/**
@@ -94,42 +165,4 @@ public abstract class AbstractStateMachineModelFactory<S, E> implements StateMac
protected ResourceLoader getResourceLoader() {
return resourceLoader;
}
/**
* Resolve action.
*
* @param id the id
* @return the action
*/
@SuppressWarnings("unchecked")
public Action<S, E> resolveAction(String id) {
Action<S, E> a = null;
a = registeredActions.get(id);
if (a == null && beanFactory != null) {
a = beanFactory.getBean(id, Action.class);
}
if (a == null) {
throw new RuntimeException("Can't resolve action with id " + id + " either from registered actions nor beanfactory");
}
return a;
}
/**
* Resolve guard.
*
* @param id the id
* @return the guard
*/
@SuppressWarnings("unchecked")
public Guard<S, E> resolveGuard(String id) {
Guard<S, E> g = null;
g = registeredGuards.get(id);
if (g == null && beanFactory != null) {
g = beanFactory.getBean(id, Guard.class);
}
if (g == null) {
throw new RuntimeException("Can't resolve guard with id " + id + " either from registered guards nor beanfactory");
}
return g;
}
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright 2016 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.model;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.guard.Guard;
/**
* Default implementation of a {@link StateMachineComponentResolver} which resolves
* from a {@link BeanFactory} if given or from a manually registered actions and guards.
*
* @author Janne Valkealahti
*
* @param <S> the type of state
* @param <E> the type of event
*/
public class DefaultStateMachineComponentResolver<S, E> implements StateMachineComponentResolver<S, E> {
private BeanFactory beanFactory;
private final Map<String, Action<S, E>> registeredActions;
private final Map<String, Guard<S, E>> registeredGuards;
/**
* Instantiates a new default state machine component resolver.
*/
public DefaultStateMachineComponentResolver() {
this(null, null, null);
}
/**
* Instantiates a new default state machine component resolver.
*
* @param registeredActions the registered actions
* @param registeredGuards the registered guards
*/
public DefaultStateMachineComponentResolver(Map<String, Action<S, E>> registeredActions, Map<String, Guard<S, E>> registeredGuards) {
this(null, registeredActions, registeredGuards);
}
/**
* Instantiates a new default state machine component resolver.
*
* @param beanFactory the bean factory
* @param registeredActions the registered actions
* @param registeredGuards the registered guards
*/
public DefaultStateMachineComponentResolver(BeanFactory beanFactory, Map<String, Action<S, E>> registeredActions,
Map<String, Guard<S, E>> registeredGuards) {
this.beanFactory = beanFactory;
this.registeredActions = registeredActions != null ? registeredActions : new HashMap<String, Action<S, E>>();
this.registeredGuards = registeredGuards != null ? registeredGuards : new HashMap<String, Guard<S, E>>();
}
@SuppressWarnings("unchecked")
@Override
public Action<S, E> resolveAction(String id) {
Action<S, E> a = null;
a = registeredActions.get(id);
if (a == null && beanFactory != null) {
a = beanFactory.getBean(id, Action.class);
}
return a;
}
@SuppressWarnings("unchecked")
@Override
public Guard<S, E> resolveGuard(String id) {
Guard<S, E> g = null;
g = registeredGuards.get(id);
if (g == null && beanFactory != null) {
g = beanFactory.getBean(id, Guard.class);
}
return g;
}
/**
* Register {@link Action} with a given id.
*
* @param id the id
* @param action the action
*/
public void registerAction(String id, Action<S, E> action) {
registeredActions.put(id, action);
}
/**
* Register {@link Guard} with a given id.
*
* @param id the id
* @param guard the guard
*/
public void registerGuard(String id, Guard<S, E> guard) {
registeredGuards.put(id, guard);
}
/**
* Sets the bean factory.
*
* @param beanFactory the new bean factory
*/
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
}

View File

@@ -19,8 +19,7 @@ import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.guard.Guard;
/**
* Strategy interface for registering and resolving state machine
* components by their id's
* Strategy interface for resolving state machine components by their id's.
*
* @author Janne Valkealahti
*
@@ -29,22 +28,6 @@ import org.springframework.statemachine.guard.Guard;
*/
public interface StateMachineComponentResolver<S, E> {
/**
* Register {@link Action} into factory with a given id.
*
* @param id the id
* @param action the action
*/
void registerAction(String id, Action<S, E> action);
/**
* Register {@link Guard} into factory with a given id.
*
* @param id the id
* @param guard the guard
*/
void registerGuard(String id, Guard<S, E> guard);
/**
* Resolve action.
*