+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * 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.argeo.slc.core.execution.xml;
import java.util.ArrayList;
import org.argeo.slc.SlcException;
import org.argeo.slc.core.execution.DefaultExecutionFlow;
+import org.argeo.slc.execution.ExecutionFlow;
+import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.RuntimeBeanReference;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
+import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
String spec = element.getAttribute("spec");
if (StringUtils.hasText(spec))
- builder.getRawBeanDefinition().getConstructorArgumentValues()
+ builder.getBeanDefinition().getConstructorArgumentValues()
.addGenericArgumentValue(new RuntimeBeanReference(spec));
String abstrac = element.getAttribute("abstract");
if (StringUtils.hasText(parent))
builder.setParentName(parent);
+ builder.getBeanDefinition().setDescription(
+ DomUtils.getChildElementValueByTagName(element, "description"));
+
List<Element> execElems = new ArrayList<Element>();
List<Element> argsElems = new ArrayList<Element>();
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
- if ("arg".equals(node.getLocalName()))
+ if (DomUtils.nodeNameEquals(node, "arg"))
argsElems.add((Element) node);
- else
+ else if (!DomUtils.nodeNameEquals(node, "description"))
execElems.add((Element) node);
}
}
if (argsElems.size() != 0) {
ManagedMap args = new ManagedMap(argsElems.size());
for (Element argElem : argsElems) {
- if (argElem.hasAttribute("value"))
- args.put(argElem.getAttribute("name"), argElem
- .getAttribute("value"));
- else if (argElem.hasAttribute("ref"))
- args.put(argElem.getAttribute("name"),
- new RuntimeBeanReference(argElem
- .getAttribute("ref")));
+ Object value = NamespaceUtils.parseValue(argElem,
+ parserContext, builder.getBeanDefinition(), null);
+ if (value != null)
+ args.put(argElem.getAttribute("name"), value);
else
throw new SlcException("No value defined.");
}
- builder.getRawBeanDefinition().getConstructorArgumentValues()
+ builder.getBeanDefinition().getConstructorArgumentValues()
.addGenericArgumentValue(args);
}
// Executables
if (execElems.size() != 0) {
ManagedList executables = new ManagedList(execElems.size());
- for (int i = 0; i < execElems.size(); i++) {
- Element child = execElems.get(i);
- String name = child.getLocalName();
- if ("bean".equals(name) || "ref".equals(name)) {
- Object target = parseBeanReference((Element) child,
- parserContext, builder);
- executables.add(target);
- } else if ("flow".equals(name)) {
- throw new SlcException(
- "Nested flows are not yet supported use a standard ref to another flow.");
- } else {
- throw new SlcException("Unsupported child '" + name + "'");
- }
+ for (Element child : execElems) {
+ // child validity check is performed in xsd
+ executables.add(NamespaceUtils.parseBeanOrReference(child,
+ parserContext, builder.getBeanDefinition()));
}
- builder.addPropertyValue("executables", executables);
+ if (executables.size() > 0)
+ builder.addPropertyValue("executables", executables);
}
}
+ @SuppressWarnings("unchecked")
@Override
- protected Class<DefaultExecutionFlow> getBeanClass(Element element) {
- return DefaultExecutionFlow.class;
+ protected Class<? extends ExecutionFlow> getBeanClass(Element element) {
+ String clss = element.getAttribute("class");
+ if (StringUtils.hasText(clss))
+ // TODO: check that it actually works
+ try {
+ return (Class<? extends ExecutionFlow>) getClass()
+ .getClassLoader().loadClass(clss);
+ } catch (ClassNotFoundException e) {
+ try {
+ return (Class<? extends ExecutionFlow>) Thread
+ .currentThread().getContextClassLoader().loadClass(
+ clss);
+ } catch (ClassNotFoundException e1) {
+ throw new SlcException("Cannot load class " + clss, e);
+ }
+ }
+ else
+ return DefaultExecutionFlow.class;
}
// parse nested bean definition
- private Object parseBeanReference(Element element,
- ParserContext parserContext, BeanDefinitionBuilder builder) {
- return parserContext.getDelegate().parsePropertySubElement(element,
- builder.getBeanDefinition());
+ // private Object parseBeanReference(Element element,
+ // ParserContext parserContext, BeanDefinitionBuilder builder) {
+ // return parserContext.getDelegate().parsePropertySubElement(element,
+ // builder.getBeanDefinition());
+ // }
+
+ @Override
+ protected String resolveId(Element element,
+ AbstractBeanDefinition definition, ParserContext parserContext)
+ throws BeanDefinitionStoreException {
+ String name = element.getAttribute("name");
+ if (StringUtils.hasText(name)) {
+ return name;
+ } else {
+ return super.resolveId(element, definition, parserContext);
+ }
}
protected boolean shouldGenerateIdAsFallback() {