Skip to content

Commit 3fad0fa

Browse files
jhoellerunknown
authored and
unknown
committed
Introduced TomcatLoadTimeWeaver for Tomcat's new InstrumentableClassLoader interface
At the same time, dropped GlassFish 1/2 support from GlassFishLoadTimeWeaver and redesigned it to be as analogous to TomcatLoadTimeWeaver as possible. Issue: SPR-10788
1 parent 888e3c7 commit 3fad0fa

File tree

7 files changed

+198
-210
lines changed

7 files changed

+198
-210
lines changed

spring-context/src/main/java/org/springframework/context/weaving/DefaultContextLoadTimeWeaver.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver;
3030
import org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver;
3131
import org.springframework.instrument.classloading.jboss.JBossLoadTimeWeaver;
32+
import org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver;
3233
import org.springframework.instrument.classloading.weblogic.WebLogicLoadTimeWeaver;
3334
import org.springframework.instrument.classloading.websphere.WebSphereLoadTimeWeaver;
3435

@@ -40,13 +41,11 @@
4041
* "{@code loadTimeWeaver}"; the most convenient way to achieve this is
4142
* Spring's {@code <context:load-time-weaver>} XML tag.
4243
*
43-
* <p>This class implements a runtime environment check for obtaining
44-
* the appropriate weaver implementation: As of Spring 3.1, it detects
45-
* Oracle WebLogic 10, GlassFish 3, JBoss AS 5, 6 and 7, IBM WebSphere 7 and 8,
44+
* <p>This class implements a runtime environment check for obtaining the
45+
* appropriate weaver implementation: As of Spring 4.0, it detects Oracle WebLogic 10,
46+
* GlassFish 3, Tomcat 6, 7 and 8, JBoss AS 5, 6 and 7, IBM WebSphere 7 and 8,
4647
* {@link InstrumentationSavingAgent Spring's VM agent}, and any {@link ClassLoader}
47-
* supported by Spring's {@link ReflectiveLoadTimeWeaver} (for example the
48-
* {@link org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader}
49-
* for Tomcat 6 and 7).
48+
* supported by Spring's {@link ReflectiveLoadTimeWeaver}.
5049
*
5150
* @author Juergen Hoeller
5251
* @author Ramnivas Laddad
@@ -111,9 +110,12 @@ protected LoadTimeWeaver createServerSpecificLoadTimeWeaver(ClassLoader classLoa
111110
if (name.startsWith("weblogic")) {
112111
return new WebLogicLoadTimeWeaver(classLoader);
113112
}
114-
else if (name.startsWith("com.sun.enterprise") || name.startsWith("org.glassfish")) {
113+
else if (name.startsWith("org.glassfish")) {
115114
return new GlassFishLoadTimeWeaver(classLoader);
116115
}
116+
else if (name.startsWith("org.apache.catalina")) {
117+
return new TomcatLoadTimeWeaver(classLoader);
118+
}
117119
else if (name.startsWith("org.jboss")) {
118120
return new JBossLoadTimeWeaver(classLoader);
119121
}

spring-context/src/main/java/org/springframework/instrument/classloading/glassfish/ClassTransformerAdapter.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

spring-context/src/main/java/org/springframework/instrument/classloading/glassfish/GlassFishClassLoaderAdapter.java

Lines changed: 0 additions & 128 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package org.springframework.instrument.classloading.glassfish;
1818

1919
import java.lang.instrument.ClassFileTransformer;
20+
import java.lang.reflect.InvocationTargetException;
21+
import java.lang.reflect.Method;
2022

2123
import org.springframework.instrument.classloading.LoadTimeWeaver;
2224
import org.springframework.util.Assert;
@@ -26,52 +28,95 @@
2628
* {@link LoadTimeWeaver} implementation for GlassFish's
2729
* {@link org.glassfish.api.deployment.InstrumentableClassLoader InstrumentableClassLoader}.
2830
*
29-
* <p>As of Spring 3.0, GlassFish V3 is supported as well.
31+
* <p>As of Spring 4.0, this weaver supports GlassFish V3 and V4.
3032
*
3133
* @author Costin Leau
3234
* @author Juergen Hoeller
3335
* @since 2.0.1
3436
*/
3537
public class GlassFishLoadTimeWeaver implements LoadTimeWeaver {
3638

37-
private final GlassFishClassLoaderAdapter classLoader;
39+
private static final String INSTRUMENTABLE_LOADER_CLASS_NAME = "org.glassfish.api.deployment.InstrumentableClassLoader";
40+
41+
42+
private final ClassLoader classLoader;
43+
44+
private final Method addTransformerMethod;
45+
46+
private final Method copyMethod;
3847

3948

40-
/**
41-
* Creates a new instance of the {@code GlassFishLoadTimeWeaver} class
42-
* using the default {@link ClassLoader}.
43-
* @see #GlassFishLoadTimeWeaver(ClassLoader)
44-
*/
4549
public GlassFishLoadTimeWeaver() {
4650
this(ClassUtils.getDefaultClassLoader());
4751
}
4852

49-
/**
50-
* Creates a new instance of the {@code GlassFishLoadTimeWeaver} class.
51-
* @param classLoader the specific {@link ClassLoader} to use; must not be {@code null}
52-
* @throws IllegalArgumentException if the supplied {@code classLoader} is {@code null};
53-
* or if the supplied {@code classLoader} is not an
54-
* {@link org.glassfish.api.deployment.InstrumentableClassLoader InstrumentableClassLoader}
55-
*/
5653
public GlassFishLoadTimeWeaver(ClassLoader classLoader) {
5754
Assert.notNull(classLoader, "ClassLoader must not be null");
58-
this.classLoader = new GlassFishClassLoaderAdapter(classLoader);
55+
56+
Class<?> instrumentableLoaderClass;
57+
try {
58+
instrumentableLoaderClass = classLoader.loadClass(INSTRUMENTABLE_LOADER_CLASS_NAME);
59+
}
60+
catch (ClassNotFoundException ex) {
61+
throw new IllegalStateException(
62+
"Could not initialize GlassFishLoadTimeWeaver because GlassFish API classes are not available", ex);
63+
}
64+
try {
65+
this.addTransformerMethod = instrumentableLoaderClass.getMethod("addTransformer", ClassFileTransformer.class);
66+
this.copyMethod = instrumentableLoaderClass.getMethod("copy");
67+
}
68+
catch (Exception ex) {
69+
throw new IllegalStateException(
70+
"Could not initialize GlassFishLoadTimeWeaver because GlassFish API classes are not available", ex);
71+
}
72+
73+
ClassLoader clazzLoader = null;
74+
// Detect transformation-aware ClassLoader by traversing the hierarchy
75+
// (as in GlassFish, Spring can be loaded by the WebappClassLoader).
76+
for (ClassLoader cl = classLoader; cl != null && clazzLoader == null; cl = cl.getParent()) {
77+
if (instrumentableLoaderClass.isInstance(cl)) {
78+
clazzLoader = cl;
79+
}
80+
}
81+
82+
if (clazzLoader == null) {
83+
throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: A [" +
84+
instrumentableLoaderClass.getName() + "] implementation is required.");
85+
}
86+
87+
this.classLoader = clazzLoader;
5988
}
6089

6190

6291
@Override
6392
public void addTransformer(ClassFileTransformer transformer) {
64-
this.classLoader.addTransformer(transformer);
93+
try {
94+
this.addTransformerMethod.invoke(this.classLoader, transformer);
95+
}
96+
catch (InvocationTargetException ex) {
97+
throw new IllegalStateException("GlassFish addTransformer method threw exception", ex.getCause());
98+
}
99+
catch (Exception ex) {
100+
throw new IllegalStateException("Could not invoke GlassFish addTransformer method", ex);
101+
}
65102
}
66103

67104
@Override
68105
public ClassLoader getInstrumentableClassLoader() {
69-
return this.classLoader.getClassLoader();
106+
return this.classLoader;
70107
}
71108

72109
@Override
73110
public ClassLoader getThrowawayClassLoader() {
74-
return this.classLoader.getThrowawayClassLoader();
111+
try {
112+
return (ClassLoader) this.copyMethod.invoke(this.classLoader);
113+
}
114+
catch (InvocationTargetException ex) {
115+
throw new IllegalStateException("GlassFish copy method threw exception", ex.getCause());
116+
}
117+
catch (Exception ex) {
118+
throw new IllegalStateException("Could not invoke GlassFish copy method", ex);
119+
}
75120
}
76121

77122
}

spring-context/src/main/java/org/springframework/instrument/classloading/glassfish/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
*
4-
* Support for class instrumentation on GlassFish / Sun Application Server.
4+
* Support for class instrumentation on GlassFish.
55
*
66
*/
77
package org.springframework.instrument.classloading.glassfish;

0 commit comments

Comments
 (0)