01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.context.weaving;
18:
19: import java.lang.instrument.ClassFileTransformer;
20: import java.lang.instrument.IllegalClassFormatException;
21: import java.security.ProtectionDomain;
22:
23: import org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter;
24:
25: import org.springframework.beans.BeansException;
26: import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
27: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
28: import org.springframework.core.Ordered;
29: import org.springframework.instrument.InstrumentationSavingAgent;
30: import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
31: import org.springframework.instrument.classloading.LoadTimeWeaver;
32:
33: /**
34: * Post-processor that registers AspectJ's
35: * {@link org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter}
36: * with the Spring application context's default
37: * {@link org.springframework.instrument.classloading.LoadTimeWeaver}.
38: *
39: * @author Juergen Hoeller
40: * @author Ramnivas Laddad
41: * @since 2.5
42: */
43: public class AspectJWeavingEnabler implements BeanFactoryPostProcessor,
44: LoadTimeWeaverAware, Ordered {
45:
46: private LoadTimeWeaver loadTimeWeaver;
47:
48: public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
49: this .loadTimeWeaver = loadTimeWeaver;
50: }
51:
52: public int getOrder() {
53: return HIGHEST_PRECEDENCE;
54: }
55:
56: public void postProcessBeanFactory(
57: ConfigurableListableBeanFactory beanFactory)
58: throws BeansException {
59: LoadTimeWeaver weaverToUse = this .loadTimeWeaver;
60: if (weaverToUse == null
61: && InstrumentationSavingAgent.getInstrumentation() != null) {
62: weaverToUse = new InstrumentationLoadTimeWeaver();
63: }
64: weaverToUse
65: .addTransformer(new AspectJClassBypassingClassFileTransformerDecorator(
66: new ClassPreProcessorAgentAdapter()));
67: }
68:
69: /*
70: * Potentially temporary way to avoid processing AspectJ classes and avoiding the LinkageError
71: * while doing so. OC4J and Tomcat (in Glassfish) definitely need bypasing such classes.
72: * TODO: Investigate further to see why AspectJ itself isn't doing so.
73: */
74: private static class AspectJClassBypassingClassFileTransformerDecorator
75: implements ClassFileTransformer {
76: private ClassFileTransformer delegate;
77:
78: public AspectJClassBypassingClassFileTransformerDecorator(
79: ClassFileTransformer delegate) {
80: this .delegate = delegate;
81: }
82:
83: @Override
84: public byte[] transform(ClassLoader loader, String className,
85: Class<?> classBeingRedefined,
86: ProtectionDomain protectionDomain,
87: byte[] classfileBuffer)
88: throws IllegalClassFormatException {
89: if (className.startsWith("org.aspectj")
90: || className.startsWith("org/aspectj")) {
91: return classfileBuffer;
92: }
93: return delegate.transform(loader, className,
94: classBeingRedefined, protectionDomain,
95: classfileBuffer);
96: }
97: }
98: }
|