diff --git a/docs/src/reference/asciidoc/sm.adoc b/docs/src/reference/asciidoc/sm.adoc index 8e548244..9b3adceb 100644 --- a/docs/src/reference/asciidoc/sm.adoc +++ b/docs/src/reference/asciidoc/sm.adoc @@ -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] ---- diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/model/AbstractStateMachineModelFactory.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/model/AbstractStateMachineModelFactory.java index 6f61982f..4c9a1503 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/model/AbstractStateMachineModelFactory.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/config/model/AbstractStateMachineModelFactory.java @@ -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 the type of state * @param the type of event */ -public abstract class AbstractStateMachineModelFactory implements StateMachineComponentResolver, BeanFactoryAware { +public abstract class AbstractStateMachineModelFactory implements StateMachineComponentResolver, BeanFactoryAware, ResourceLoaderAware { private BeanFactory beanFactory; + private ResourceLoader resourceLoader; private final Map> registeredActions = new HashMap<>(); private final Map> registeredGuards = new HashMap<>(); @@ -49,6 +52,11 @@ public abstract class AbstractStateMachineModelFactory 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 implements StateMac return beanFactory; } + /** + * Gets the resource loader. + * + * @return the resource loader + */ + protected ResourceLoader getResourceLoader() { + return resourceLoader; + } + /** * Resolve action. * diff --git a/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlStateMachineModelFactory.java b/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlStateMachineModelFactory.java index 932cf4e7..354e75d9 100644 --- a/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlStateMachineModelFactory.java +++ b/spring-statemachine-uml/src/main/java/org/springframework/statemachine/uml/UmlStateMachineModelFactory.java @@ -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 implements StateMachineModelFactory { - 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 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 configurationData = new ConfigurationData<>(); return new DefaultStateMachineModel(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); + } + } } diff --git a/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java b/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java index 00e49b5f..a4ffc7c2 100644 --- a/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java +++ b/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/UmlStateMachineModelFactoryTests.java @@ -608,8 +608,7 @@ public class UmlStateMachineModelFactoryTests extends AbstractUmlTests { @Bean public StateMachineModelFactory 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 diff --git a/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/docs/DocsUmlSampleTests1.java b/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/docs/DocsUmlSampleTests1.java index e8e00623..fea7bcbe 100644 --- a/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/docs/DocsUmlSampleTests1.java +++ b/spring-statemachine-uml/src/test/java/org/springframework/statemachine/uml/docs/DocsUmlSampleTests1.java @@ -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 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[]