Support resource loader in UmlStateMachineModelFactory

- Fixes #200
This commit is contained in:
Janne Valkealahti
2016-04-17 16:10:44 +01:00
parent 519d3e9029
commit 9bf9d6cdfb
5 changed files with 49 additions and 11 deletions

View File

@@ -1761,7 +1761,8 @@ After uml file is in place in your project, it can be imported into
configuration using `StateMachineModelConfigurer` where
`StateMachineModelFactory` is associated with a model.
`UmlStateMachineModelFactory` is a special factory which knows how to
process _Eclipse Papyrus_ generated uml structure.
process _Eclipse Papyrus_ generated uml structure. Source uml file can
either be given as a Spring `Resource` or a normal location string.
[source,java,indent=0]
----

View File

@@ -21,6 +21,8 @@ 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.ResourceLoader;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.guard.Guard;
@@ -38,9 +40,10 @@ import org.springframework.statemachine.guard.Guard;
* @param <S> the type of state
* @param <E> the type of event
*/
public abstract class AbstractStateMachineModelFactory<S, E> implements StateMachineComponentResolver<S, E>, BeanFactoryAware {
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<>();
@@ -49,6 +52,11 @@ public abstract class AbstractStateMachineModelFactory<S, E> implements StateMac
this.beanFactory = beanFactory;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
/**
* Register {@link Action} into factory with a given id.
*
@@ -78,6 +86,15 @@ public abstract class AbstractStateMachineModelFactory<S, E> implements StateMac
return beanFactory;
}
/**
* Gets the resource loader.
*
* @return the resource loader
*/
protected ResourceLoader getResourceLoader() {
return resourceLoader;
}
/**
* Resolve action.
*

View File

@@ -18,7 +18,9 @@ package org.springframework.statemachine.uml;
import java.io.IOException;
import org.eclipse.uml2.uml.Model;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.statemachine.config.model.AbstractStateMachineModelFactory;
import org.springframework.statemachine.config.model.ConfigurationData;
import org.springframework.statemachine.config.model.DefaultStateMachineModel;
@@ -36,7 +38,8 @@ import org.springframework.util.Assert;
public class UmlStateMachineModelFactory extends AbstractStateMachineModelFactory<String, String>
implements StateMachineModelFactory<String, String> {
private final Resource resource;
private Resource resource;
private String location;
/**
* Instantiates a new uml state machine model factory.
@@ -48,17 +51,38 @@ public class UmlStateMachineModelFactory extends AbstractStateMachineModelFactor
this.resource = resource;
}
/**
* Instantiates a new uml state machine model factory.
*
* @param location the resource location
*/
public UmlStateMachineModelFactory(String location) {
this.location = location;
}
@Override
public StateMachineModel<String, String> build() {
Model model = null;
try {
model = UmlUtils.getModel(resource.getURI().getPath());
model = UmlUtils.getModel(resolveResource().getURI().getPath());
} catch (IOException e) {
throw new IllegalArgumentException("Cannot build build model from resource " + resource, e);
throw new IllegalArgumentException("Cannot build build model from resource " + resource + " or location " + location, e);
}
UmlModelParser parser = new UmlModelParser(model, this);
DataHolder dataHolder = parser.parseModel();
ConfigurationData<String, String> configurationData = new ConfigurationData<>();
return new DefaultStateMachineModel<String, String>(configurationData, dataHolder.getStatesData(), dataHolder.getTransitionsData());
}
private Resource resolveResource() {
if (resource != null) {
return resource;
} else {
ResourceLoader loader = getResourceLoader();
if (loader == null) {
loader = new DefaultResourceLoader();
}
return loader.getResource(location);
}
}
}

View File

@@ -608,8 +608,7 @@ public class UmlStateMachineModelFactoryTests extends AbstractUmlTests {
@Bean
public StateMachineModelFactory<String, String> modelFactory() {
Resource model = new ClassPathResource("org/springframework/statemachine/uml/simple-actions.uml");
return new UmlStateMachineModelFactory(model);
return new UmlStateMachineModelFactory("classpath:org/springframework/statemachine/uml/simple-actions.uml");
}
@Bean

View File

@@ -17,8 +17,6 @@ package org.springframework.statemachine.uml.docs;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineModelConfigurer;
@@ -41,8 +39,7 @@ public class DocsUmlSampleTests1 {
@Bean
public StateMachineModelFactory<String, String> modelFactory() {
Resource model = new ClassPathResource("org/springframework/statemachine/uml/docs/simple-machine.uml");
return new UmlStateMachineModelFactory(model);
return new UmlStateMachineModelFactory("classpath:org/springframework/statemachine/uml/docs/simple-machine.uml");
}
}
// end::snippetA[]