2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.core
.execution
.xml
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
22 import org
.argeo
.slc
.SlcException
;
23 import org
.argeo
.slc
.core
.execution
.DefaultExecutionFlow
;
24 import org
.argeo
.slc
.execution
.ExecutionFlow
;
25 import org
.springframework
.beans
.factory
.BeanDefinitionStoreException
;
26 import org
.springframework
.beans
.factory
.config
.RuntimeBeanReference
;
27 import org
.springframework
.beans
.factory
.support
.AbstractBeanDefinition
;
28 import org
.springframework
.beans
.factory
.support
.BeanDefinitionBuilder
;
29 import org
.springframework
.beans
.factory
.support
.ManagedList
;
30 import org
.springframework
.beans
.factory
.support
.ManagedMap
;
31 import org
.springframework
.beans
.factory
.xml
.AbstractSingleBeanDefinitionParser
;
32 import org
.springframework
.beans
.factory
.xml
.ParserContext
;
33 import org
.springframework
.util
.StringUtils
;
34 import org
.springframework
.util
.xml
.DomUtils
;
35 import org
.w3c
.dom
.Element
;
36 import org
.w3c
.dom
.Node
;
37 import org
.w3c
.dom
.NodeList
;
39 public class FlowBeanDefinitionParser
extends
40 AbstractSingleBeanDefinitionParser
{
41 // private Log log = LogFactory.getLog(FlowBeanDefinitionParser.class);
43 @SuppressWarnings("unchecked")
45 protected void doParse(Element element
, ParserContext parserContext
,
46 BeanDefinitionBuilder builder
) {
47 String path
= element
.getAttribute("path");
48 if (StringUtils
.hasText(path
))
49 builder
.addPropertyValue("path", path
);
51 String spec
= element
.getAttribute("spec");
52 if (StringUtils
.hasText(spec
))
53 builder
.getBeanDefinition().getConstructorArgumentValues()
54 .addGenericArgumentValue(new RuntimeBeanReference(spec
));
56 String abstrac
= element
.getAttribute("abstract");
57 if (StringUtils
.hasText(abstrac
))
58 builder
.setAbstract(Boolean
.parseBoolean(abstrac
));
60 String parent
= element
.getAttribute("parent");
61 if (StringUtils
.hasText(parent
))
62 builder
.setParentName(parent
);
64 builder
.getBeanDefinition().setDescription(
65 DomUtils
.getChildElementValueByTagName(element
, "description"));
67 List
<Element
> execElems
= new ArrayList
<Element
>();
68 List
<Element
> argsElems
= new ArrayList
<Element
>();
69 NodeList nodeList
= element
.getChildNodes();
70 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
71 Node node
= nodeList
.item(i
);
72 if (node
instanceof Element
) {
73 if (DomUtils
.nodeNameEquals(node
, "arg"))
74 argsElems
.add((Element
) node
);
75 else if (!DomUtils
.nodeNameEquals(node
, "description"))
76 execElems
.add((Element
) node
);
81 if (argsElems
.size() != 0) {
82 ManagedMap args
= new ManagedMap(argsElems
.size());
83 for (Element argElem
: argsElems
) {
84 Object value
= NamespaceUtils
.parseValue(argElem
,
85 parserContext
, builder
.getBeanDefinition(), null);
87 args
.put(argElem
.getAttribute("name"), value
);
89 throw new SlcException("No value defined.");
91 builder
.getBeanDefinition().getConstructorArgumentValues()
92 .addGenericArgumentValue(args
);
96 if (execElems
.size() != 0) {
97 ManagedList executables
= new ManagedList(execElems
.size());
98 for (Element child
: execElems
) {
99 // child validity check is performed in xsd
100 executables
.add(NamespaceUtils
.parseBeanOrReference(child
,
101 parserContext
, builder
.getBeanDefinition()));
103 if (executables
.size() > 0)
104 builder
.addPropertyValue("executables", executables
);
108 @SuppressWarnings("unchecked")
110 protected Class
<?
extends ExecutionFlow
> getBeanClass(Element element
) {
111 String clss
= element
.getAttribute("class");
112 if (StringUtils
.hasText(clss
))
113 // TODO: check that it actually works
115 return (Class
<?
extends ExecutionFlow
>) getClass()
116 .getClassLoader().loadClass(clss
);
117 } catch (ClassNotFoundException e
) {
119 return (Class
<?
extends ExecutionFlow
>) Thread
120 .currentThread().getContextClassLoader().loadClass(
122 } catch (ClassNotFoundException e1
) {
123 throw new SlcException("Cannot load class " + clss
, e
);
127 return DefaultExecutionFlow
.class;
130 // parse nested bean definition
131 // private Object parseBeanReference(Element element,
132 // ParserContext parserContext, BeanDefinitionBuilder builder) {
133 // return parserContext.getDelegate().parsePropertySubElement(element,
134 // builder.getBeanDefinition());
138 protected String
resolveId(Element element
,
139 AbstractBeanDefinition definition
, ParserContext parserContext
)
140 throws BeanDefinitionStoreException
{
141 String name
= element
.getAttribute("name");
142 if (StringUtils
.hasText(name
)) {
145 return super.resolveId(element
, definition
, parserContext
);
149 protected boolean shouldGenerateIdAsFallback() {