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.context.config;
018:
019: import org.w3c.dom.Element;
020:
021: import org.springframework.beans.factory.config.BeanDefinition;
022: import org.springframework.beans.factory.support.AbstractBeanDefinition;
023: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
024: import org.springframework.beans.factory.support.RootBeanDefinition;
025: import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
026: import org.springframework.beans.factory.xml.ParserContext;
027: import org.springframework.context.ConfigurableApplicationContext;
028: import org.springframework.util.ClassUtils;
029:
030: /**
031: * Parser for the <context:load-time-weaver/> element.
032: *
033: * @author Juergen Hoeller
034: * @since 2.5
035: */
036: class LoadTimeWeaverBeanDefinitionParser extends
037: AbstractSingleBeanDefinitionParser {
038:
039: private static final String WEAVER_CLASS_ATTRIBUTE = "weaver-class";
040:
041: private static final String ASPECTJ_WEAVING_ATTRIBUTE = "aspectj-weaving";
042:
043: private static final String ASPECTJ_AOP_XML_RESOURCE = "META-INF/aop.xml";
044:
045: private static final String DEFAULT_LOAD_TIME_WEAVER_CLASS_NAME = "org.springframework.context.weaving.DefaultContextLoadTimeWeaver";
046:
047: private static final String ASPECTJ_WEAVING_ENABLER_CLASS_NAME = "org.springframework.context.weaving.AspectJWeavingEnabler";
048:
049: protected String getBeanClassName(Element element) {
050: if (element.hasAttribute(WEAVER_CLASS_ATTRIBUTE)) {
051: return element.getAttribute(WEAVER_CLASS_ATTRIBUTE);
052: }
053: return DEFAULT_LOAD_TIME_WEAVER_CLASS_NAME;
054: }
055:
056: protected String resolveId(Element element,
057: AbstractBeanDefinition definition,
058: ParserContext parserContext) {
059: return ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME;
060: }
061:
062: protected void doParse(Element element,
063: ParserContext parserContext, BeanDefinitionBuilder builder) {
064: builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
065:
066: if (isAspectJWeavingEnabled(element
067: .getAttribute(ASPECTJ_WEAVING_ATTRIBUTE), parserContext)) {
068: RootBeanDefinition weavingEnablerDef = new RootBeanDefinition();
069: weavingEnablerDef
070: .setBeanClassName(ASPECTJ_WEAVING_ENABLER_CLASS_NAME);
071: parserContext.getReaderContext().registerWithGeneratedName(
072: weavingEnablerDef);
073:
074: if (isBeanConfigurerAspectEnabled(parserContext
075: .getReaderContext().getBeanClassLoader())) {
076: new SpringConfiguredBeanDefinitionParser().parse(
077: element, parserContext);
078: }
079: }
080: }
081:
082: protected boolean isAspectJWeavingEnabled(String value,
083: ParserContext parserContext) {
084: if ("on".equals(value)) {
085: return true;
086: } else if ("off".equals(value)) {
087: return false;
088: } else {
089: // Determine default...
090: ClassLoader cl = parserContext.getReaderContext()
091: .getResourceLoader().getClassLoader();
092: return (cl.getResource(ASPECTJ_AOP_XML_RESOURCE) != null);
093: }
094: }
095:
096: protected boolean isBeanConfigurerAspectEnabled(
097: ClassLoader beanClassLoader) {
098: return ClassUtils
099: .isPresent(
100: SpringConfiguredBeanDefinitionParser.BEAN_CONFIGURER_ASPECT_CLASS_NAME,
101: beanClassLoader);
102: }
103:
104: }
|