2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.core
.execution
.xml
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import org
.argeo
.slc
.SlcException
;
22 import org
.argeo
.slc
.core
.execution
.DefaultExecutionFlow
;
23 import org
.argeo
.slc
.execution
.ExecutionFlow
;
24 import org
.springframework
.beans
.factory
.BeanDefinitionStoreException
;
25 import org
.springframework
.beans
.factory
.config
.RuntimeBeanReference
;
26 import org
.springframework
.beans
.factory
.support
.AbstractBeanDefinition
;
27 import org
.springframework
.beans
.factory
.support
.BeanDefinitionBuilder
;
28 import org
.springframework
.beans
.factory
.support
.ManagedList
;
29 import org
.springframework
.beans
.factory
.support
.ManagedMap
;
30 import org
.springframework
.beans
.factory
.xml
.AbstractSingleBeanDefinitionParser
;
31 import org
.springframework
.beans
.factory
.xml
.ParserContext
;
32 import org
.springframework
.util
.StringUtils
;
33 import org
.springframework
.util
.xml
.DomUtils
;
34 import org
.w3c
.dom
.Element
;
35 import org
.w3c
.dom
.Node
;
36 import org
.w3c
.dom
.NodeList
;
38 public class FlowBeanDefinitionParser
extends
39 AbstractSingleBeanDefinitionParser
{
40 // private Log log = LogFactory.getLog(FlowBeanDefinitionParser.class);
42 @SuppressWarnings("unchecked")
44 protected void doParse(Element element
, ParserContext parserContext
,
45 BeanDefinitionBuilder builder
) {
46 String path
= element
.getAttribute("path");
47 if (StringUtils
.hasText(path
))
48 builder
.addPropertyValue("path", path
);
50 String spec
= element
.getAttribute("spec");
51 if (StringUtils
.hasText(spec
))
52 builder
.getBeanDefinition().getConstructorArgumentValues()
53 .addGenericArgumentValue(new RuntimeBeanReference(spec
));
55 String abstrac
= element
.getAttribute("abstract");
56 if (StringUtils
.hasText(abstrac
))
57 builder
.setAbstract(Boolean
.parseBoolean(abstrac
));
59 String parent
= element
.getAttribute("parent");
60 if (StringUtils
.hasText(parent
))
61 builder
.setParentName(parent
);
63 builder
.getBeanDefinition().setDescription(
64 DomUtils
.getChildElementValueByTagName(element
, "description"));
66 List
<Element
> execElems
= new ArrayList
<Element
>();
67 List
<Element
> argsElems
= new ArrayList
<Element
>();
68 NodeList nodeList
= element
.getChildNodes();
69 for (int i
= 0; i
< nodeList
.getLength(); i
++) {
70 Node node
= nodeList
.item(i
);
71 if (node
instanceof Element
) {
72 if (DomUtils
.nodeNameEquals(node
, "arg"))
73 argsElems
.add((Element
) node
);
74 else if (!DomUtils
.nodeNameEquals(node
, "description"))
75 execElems
.add((Element
) node
);
80 if (argsElems
.size() != 0) {
81 ManagedMap args
= new ManagedMap(argsElems
.size());
82 for (Element argElem
: argsElems
) {
83 Object value
= NamespaceUtils
.parseValue(argElem
,
84 parserContext
, builder
.getBeanDefinition(), null);
86 args
.put(argElem
.getAttribute("name"), value
);
88 throw new SlcException("No value defined.");
90 builder
.getBeanDefinition().getConstructorArgumentValues()
91 .addGenericArgumentValue(args
);
95 if (execElems
.size() != 0) {
96 ManagedList executables
= new ManagedList(execElems
.size());
97 for (Element child
: execElems
) {
98 // child validity check is performed in xsd
99 executables
.add(NamespaceUtils
.parseBeanOrReference(child
,
100 parserContext
, builder
.getBeanDefinition()));
102 if (executables
.size() > 0)
103 builder
.addPropertyValue("executables", executables
);
107 @SuppressWarnings("unchecked")
109 protected Class
<?
extends ExecutionFlow
> getBeanClass(Element element
) {
110 String clss
= element
.getAttribute("class");
111 if (StringUtils
.hasText(clss
))
112 // TODO: check that it actually works
114 return (Class
<?
extends ExecutionFlow
>) getClass()
115 .getClassLoader().loadClass(clss
);
116 } catch (ClassNotFoundException e
) {
118 return (Class
<?
extends ExecutionFlow
>) Thread
119 .currentThread().getContextClassLoader().loadClass(
121 } catch (ClassNotFoundException e1
) {
122 throw new SlcException("Cannot load class " + clss
, e
);
126 return DefaultExecutionFlow
.class;
129 // parse nested bean definition
130 // private Object parseBeanReference(Element element,
131 // ParserContext parserContext, BeanDefinitionBuilder builder) {
132 // return parserContext.getDelegate().parsePropertySubElement(element,
133 // builder.getBeanDefinition());
137 protected String
resolveId(Element element
,
138 AbstractBeanDefinition definition
, ParserContext parserContext
)
139 throws BeanDefinitionStoreException
{
140 String name
= element
.getAttribute("name");
141 if (StringUtils
.hasText(name
)) {
144 return super.resolveId(element
, definition
, parserContext
);
148 protected boolean shouldGenerateIdAsFallback() {