polishing

This commit is contained in:
Juergen Hoeller 2009-12-13 15:30:52 +00:00
parent 42c7be4590
commit 35354ad520
4 changed files with 53 additions and 49 deletions

View File

@ -473,7 +473,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
Class beanClass = predictBeanType(beanName, mbd, new Class[] {FactoryBean.class, typeToMatch}); Class beanClass = predictBeanType(beanName, mbd, FactoryBean.class, typeToMatch);
if (beanClass == null) { if (beanClass == null) {
return false; return false;
} }
@ -519,7 +519,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
Class beanClass = predictBeanType(beanName, mbd, null); Class beanClass = predictBeanType(beanName, mbd);
// Check bean class whether we're dealing with a FactoryBean. // Check bean class whether we're dealing with a FactoryBean.
if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) { if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
@ -1171,19 +1171,6 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
this.mergedBeanDefinitions.remove(beanName); this.mergedBeanDefinitions.remove(beanName);
} }
/**
* Resolve the bean class for the specified bean definition,
* resolving a bean class name into a Class reference (if necessary)
* and storing the resolved Class in the bean definition for further use.
* @param mbd the merged bean definition to determine the class for
* @param beanName the name of the bean (for error handling purposes)
* @return the resolved bean class (or <code>null</code> if none)
* @throws CannotLoadBeanClassException if we failed to load the class
*/
protected Class resolveBeanClass(RootBeanDefinition mbd, String beanName) {
return resolveBeanClass(mbd, beanName, null);
}
/** /**
* Resolve the bean class for the specified bean definition, * Resolve the bean class for the specified bean definition,
* resolving a bean class name into a Class reference (if necessary) * resolving a bean class name into a Class reference (if necessary)
@ -1195,7 +1182,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @return the resolved bean class (or <code>null</code> if none) * @return the resolved bean class (or <code>null</code> if none)
* @throws CannotLoadBeanClassException if we failed to load the class * @throws CannotLoadBeanClassException if we failed to load the class
*/ */
protected Class resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class[] typesToMatch) protected Class resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class... typesToMatch)
throws CannotLoadBeanClassException { throws CannotLoadBeanClassException {
try { try {
if (mbd.hasBeanClass()) { if (mbd.hasBeanClass()) {
@ -1224,8 +1211,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
} }
private Class doResolveBeanClass(final RootBeanDefinition mbd, final Class[] typesToMatch) private Class doResolveBeanClass(RootBeanDefinition mbd, Class... typesToMatch) throws ClassNotFoundException {
throws ClassNotFoundException {
if (typesToMatch != null) { if (typesToMatch != null) {
ClassLoader tempClassLoader = getTempClassLoader(); ClassLoader tempClassLoader = getTempClassLoader();
if (tempClassLoader != null) { if (tempClassLoader != null) {
@ -1274,7 +1260,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* (also signals that the returned <code>Class</code> will never be exposed to application code) * (also signals that the returned <code>Class</code> will never be exposed to application code)
* @return the type of the bean, or <code>null</code> if not predictable * @return the type of the bean, or <code>null</code> if not predictable
*/ */
protected Class predictBeanType(String beanName, RootBeanDefinition mbd, Class[] typesToMatch) { protected Class predictBeanType(String beanName, RootBeanDefinition mbd, Class... typesToMatch) {
if (mbd.getFactoryMethodName() != null) { if (mbd.getFactoryMethodName() != null) {
return null; return null;
} }
@ -1287,7 +1273,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @param mbd the corresponding bean definition * @param mbd the corresponding bean definition
*/ */
protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) { protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) {
Class beanClass = predictBeanType(beanName, mbd, new Class[] {FactoryBean.class}); Class beanClass = predictBeanType(beanName, mbd, FactoryBean.class);
return (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)); return (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2006-2009 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.instrument.classloading.glassfish; package org.springframework.instrument.classloading.glassfish;
import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.ClassFileTransformer;
@ -24,47 +25,55 @@ import java.lang.reflect.Method;
* encapsulate the classloader-specific methods (discovered and * encapsulate the classloader-specific methods (discovered and
* called through reflection) from the load-time weaver. * called through reflection) from the load-time weaver.
* *
* <p/> Supports GlassFish V1, V2 and V3 (currently in beta). * <p>Supports GlassFish V1, V2 and V3 (currently in beta).
* *
* @author Costin Leau * @author Costin Leau
* @since 3.0.0 * @since 3.0
*/ */
class GlassFishClassLoaderAdapter { class GlassFishClassLoaderAdapter {
static final String INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V2 = "com.sun.enterprise.loader.InstrumentableClassLoader"; static final String INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V2 = "com.sun.enterprise.loader.InstrumentableClassLoader";
static final String INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V3 = "org.glassfish.api.deployment.InstrumentableClassLoader"; static final String INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V3 = "org.glassfish.api.deployment.InstrumentableClassLoader";
private static final String CLASS_TRANSFORMER = "javax.persistence.spi.ClassTransformer"; private static final String CLASS_TRANSFORMER = "javax.persistence.spi.ClassTransformer";
private final ClassLoader classLoader; private final ClassLoader classLoader;
private final Method addTransformer; private final Method addTransformer;
private final Method copy; private final Method copy;
private final boolean glassFishV3; private final boolean glassFishV3;
public GlassFishClassLoaderAdapter(ClassLoader classLoader) { public GlassFishClassLoaderAdapter(ClassLoader classLoader) {
Class<?> instrumentableLoaderClass = null; Class<?> instrumentableLoaderClass;
boolean glassV3 = false; boolean glassV3 = false;
try { try {
// try the V1/V2 API first // try the V1/V2 API first
instrumentableLoaderClass = classLoader.loadClass(INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V2); instrumentableLoaderClass = classLoader.loadClass(INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V2);
} catch (ClassNotFoundException ex) { }
catch (ClassNotFoundException ex) {
// fall back to V3 // fall back to V3
try { try {
instrumentableLoaderClass = classLoader.loadClass(INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V3); instrumentableLoaderClass = classLoader.loadClass(INSTRUMENTABLE_CLASSLOADER_GLASSFISH_V3);
glassV3 = true; glassV3 = true;
} catch (ClassNotFoundException cnfe) { }
throw new IllegalStateException( catch (ClassNotFoundException cnfe) {
"Could not initialize GlassFish LoadTimeWeaver because GlassFish (V1, V2 or V3) API classes are not available", throw new IllegalStateException("Could not initialize GlassFish LoadTimeWeaver because " +
ex); "GlassFish (V1, V2 or V3) API classes are not available", ex);
} }
} }
try { try {
Class<?> classTransformerClass = (glassV3 ? ClassFileTransformer.class : classLoader Class<?> classTransformerClass =
.loadClass(CLASS_TRANSFORMER)); (glassV3 ? ClassFileTransformer.class : classLoader.loadClass(CLASS_TRANSFORMER));
addTransformer = instrumentableLoaderClass.getMethod("addTransformer", classTransformerClass); this.addTransformer = instrumentableLoaderClass.getMethod("addTransformer", classTransformerClass);
copy = instrumentableLoaderClass.getMethod("copy"); this.copy = instrumentableLoaderClass.getMethod("copy");
}
} catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException( throw new IllegalStateException(
"Could not initialize GlassFish LoadTimeWeaver because GlassFish API classes are not available", ex); "Could not initialize GlassFish LoadTimeWeaver because GlassFish API classes are not available", ex);
} }
@ -79,8 +88,8 @@ class GlassFishClassLoaderAdapter {
} }
if (clazzLoader == null) { if (clazzLoader == null) {
throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: " + "A [" throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: A [" +
+ instrumentableLoaderClass.getName() + "] implementation is required."); instrumentableLoaderClass.getName() + "] implementation is required.");
} }
this.classLoader = clazzLoader; this.classLoader = clazzLoader;
@ -89,10 +98,13 @@ class GlassFishClassLoaderAdapter {
public void addTransformer(ClassFileTransformer transformer) { public void addTransformer(ClassFileTransformer transformer) {
try { try {
addTransformer.invoke(classLoader, (glassFishV3 ? transformer : new ClassTransformerAdapter(transformer))); this.addTransformer.invoke(this.classLoader,
} catch (InvocationTargetException ex) { (this.glassFishV3 ? transformer : new ClassTransformerAdapter(transformer)));
}
catch (InvocationTargetException ex) {
throw new IllegalStateException("GlassFish addTransformer method threw exception ", ex.getCause()); throw new IllegalStateException("GlassFish addTransformer method threw exception ", ex.getCause());
} catch (Exception ex) { }
catch (Exception ex) {
throw new IllegalStateException("Could not invoke GlassFish addTransformer method", ex); throw new IllegalStateException("Could not invoke GlassFish addTransformer method", ex);
} }
} }
@ -103,11 +115,14 @@ class GlassFishClassLoaderAdapter {
public ClassLoader getThrowawayClassLoader() { public ClassLoader getThrowawayClassLoader() {
try { try {
return (ClassLoader) copy.invoke(classLoader, (Object[]) null); return (ClassLoader) this.copy.invoke(this.classLoader);
} catch (InvocationTargetException ex) { }
catch (InvocationTargetException ex) {
throw new IllegalStateException("GlassFish copy method threw exception ", ex.getCause()); throw new IllegalStateException("GlassFish copy method threw exception ", ex.getCause());
} catch (Exception ex) { }
catch (Exception ex) {
throw new IllegalStateException("Could not invoke GlassFish copy method", ex); throw new IllegalStateException("Could not invoke GlassFish copy method", ex);
} }
} }
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,10 +23,9 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
* {@link LoadTimeWeaver} implementation for GlassFish's * {@link LoadTimeWeaver} implementation for GlassFish's {@link InstrumentableClassLoader}.
* {@link InstrumentableClassLoader}.
* *
* <p/>Since Spring 3.0.0, GlassFish V3 is supported as well. * <p>As of Spring 3.0, GlassFish V3 is supported as well.
* *
* @author Costin Leau * @author Costin Leau
* @author Juergen Hoeller * @author Juergen Hoeller
@ -36,6 +35,7 @@ public class GlassFishLoadTimeWeaver implements LoadTimeWeaver {
private final GlassFishClassLoaderAdapter classLoader; private final GlassFishClassLoaderAdapter classLoader;
/** /**
* Creates a new instance of the <code>GlassFishLoadTimeWeaver</code> class * Creates a new instance of the <code>GlassFishLoadTimeWeaver</code> class
* using the default {@link ClassLoader}. * using the default {@link ClassLoader}.
@ -56,6 +56,7 @@ public class GlassFishLoadTimeWeaver implements LoadTimeWeaver {
this.classLoader = new GlassFishClassLoaderAdapter(classLoader); this.classLoader = new GlassFishClassLoaderAdapter(classLoader);
} }
public void addTransformer(ClassFileTransformer transformer) { public void addTransformer(ClassFileTransformer transformer) {
this.classLoader.addTransformer(transformer); this.classLoader.addTransformer(transformer);
} }
@ -67,4 +68,5 @@ public class GlassFishLoadTimeWeaver implements LoadTimeWeaver {
public ClassLoader getThrowawayClassLoader() { public ClassLoader getThrowawayClassLoader() {
return this.classLoader.getThrowawayClassLoader(); return this.classLoader.getThrowawayClassLoader();
} }
}
}

View File

@ -79,6 +79,7 @@ public abstract class JRubyScriptUtils {
Ruby ruby = initializeRuntime(); Ruby ruby = initializeRuntime();
Node scriptRootNode = ruby.parseEval(scriptSource, "", null, 0); Node scriptRootNode = ruby.parseEval(scriptSource, "", null, 0);
// keep using the deprecated runNormally variant for JRuby 1.1/1.2 compatibility...
IRubyObject rubyObject = ruby.runNormally(scriptRootNode, false); IRubyObject rubyObject = ruby.runNormally(scriptRootNode, false);
if (rubyObject instanceof RubyNil) { if (rubyObject instanceof RubyNil) {