View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
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
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.argeo.slc.core.execution.xml;
17  
18  import org.springframework.aop.scope.ScopedProxyUtils;
19  import org.springframework.beans.factory.config.BeanDefinitionHolder;
20  import org.springframework.beans.factory.parsing.BeanComponentDefinition;
21  import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
22  import org.springframework.beans.factory.xml.ParserContext;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.Node;
25  
26  /**
27   * Inspired by org.springframework.aop.config.ScopedProxyBeanDefinitionDecorator
28   */
29  public class ExecutionScopeDecorator implements BeanDefinitionDecorator {	
30  	private static final String PROXY_TARGET_CLASS = "proxy-target-class";	
31  	
32  	public BeanDefinitionHolder decorate(Node node,
33  			BeanDefinitionHolder definition, ParserContext parserContext) {
34  		
35  		definition.getBeanDefinition().setScope("execution");
36  		
37  		// Default: CGLib not used
38  		boolean proxyTargetClass = false;
39  		if (node instanceof Element) {
40  			Element ele = (Element) node;
41  			if (ele.hasAttribute(PROXY_TARGET_CLASS)) {
42  				proxyTargetClass = Boolean.valueOf(ele.getAttribute(PROXY_TARGET_CLASS)).booleanValue();
43  			}
44  		}
45  		
46  		// Register the original bean definition as it will be referenced by the scoped proxy and is relevant for tooling (validation, navigation).
47  		String targetBeanName = ScopedProxyUtils.getTargetBeanName(definition.getBeanName());
48  		parserContext.getReaderContext().fireComponentRegistered(new BeanComponentDefinition(definition.getBeanDefinition(), targetBeanName));
49  		
50  		return ScopedProxyUtils.createScopedProxy(definition, parserContext.getRegistry(), proxyTargetClass);		
51  	}
52  }