001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.config;
018:
019: import org.w3c.dom.Element;
020:
021: import org.springframework.beans.factory.config.BeanDefinition;
022: import org.springframework.beans.factory.parsing.BeanComponentDefinition;
023: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
024: import org.springframework.beans.factory.xml.ParserContext;
025:
026: /**
027: * Utility class for handling registration of auto-proxy creators used internally
028: * by the '<code>aop</code>' namespace tags.
029: *
030: * <p>Only a single auto-proxy creator can be registered and multiple tags may wish
031: * to register different concrete implementations. As such this class delegates to
032: * {@link AopConfigUtils} which wraps a simple escalation protocol. Therefore
033: * classes may request a particular auto-proxy creator and know that class,
034: * <code>or a subclass thereof</code>, will eventually be resident in the
035: * application context.
036: *
037: * @author Rob Harrop
038: * @author Juergen Hoeller
039: * @author Mark Fisher
040: * @since 2.0
041: * @see AopConfigUtils
042: */
043: public abstract class AopNamespaceUtils {
044:
045: /**
046: * The <code>proxy-target-class</code> attribute as found on AOP-related XML tags.
047: */
048: public static final String PROXY_TARGET_CLASS_ATTRIBUTE = "proxy-target-class";
049:
050: public static void registerAutoProxyCreatorIfNecessary(
051: ParserContext parserContext, Element sourceElement) {
052:
053: BeanDefinition beanDefinition = AopConfigUtils
054: .registerAutoProxyCreatorIfNecessary(parserContext
055: .getRegistry(), parserContext
056: .extractSource(sourceElement));
057: useClassProxyingIfNecessary(parserContext.getRegistry(),
058: sourceElement);
059: registerComponentIfNecessary(beanDefinition, parserContext);
060: }
061:
062: public static void registerAspectJAutoProxyCreatorIfNecessary(
063: ParserContext parserContext, Element sourceElement) {
064:
065: BeanDefinition beanDefinition = AopConfigUtils
066: .registerAspectJAutoProxyCreatorIfNecessary(
067: parserContext.getRegistry(), parserContext
068: .extractSource(sourceElement));
069: useClassProxyingIfNecessary(parserContext.getRegistry(),
070: sourceElement);
071: registerComponentIfNecessary(beanDefinition, parserContext);
072: }
073:
074: public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary(
075: ParserContext parserContext, Element sourceElement) {
076:
077: BeanDefinition beanDefinition = AopConfigUtils
078: .registerAspectJAnnotationAutoProxyCreatorIfNecessary(
079: parserContext.getRegistry(), parserContext
080: .extractSource(sourceElement));
081: useClassProxyingIfNecessary(parserContext.getRegistry(),
082: sourceElement);
083: registerComponentIfNecessary(beanDefinition, parserContext);
084: }
085:
086: private static void useClassProxyingIfNecessary(
087: BeanDefinitionRegistry registry, Element sourceElement) {
088: if (sourceElement != null) {
089: boolean proxyTargetClass = Boolean
090: .valueOf(
091: sourceElement
092: .getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE))
093: .booleanValue();
094: if (proxyTargetClass) {
095: AopConfigUtils
096: .forceAutoProxyCreatorToUseClassProxying(registry);
097: }
098: }
099: }
100:
101: private static void registerComponentIfNecessary(
102: BeanDefinition beanDefinition, ParserContext parserContext) {
103: if (beanDefinition != null) {
104: BeanComponentDefinition componentDefinition = new BeanComponentDefinition(
105: beanDefinition,
106: AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
107: parserContext.registerComponent(componentDefinition);
108: }
109: }
110:
111: }
|