alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (GlassFishLoadTimeWeaver.java)

This example Spring Framework source code file (GlassFishLoadTimeWeaver.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

an, classloader, classloader, classloaders, classloaders, classtransformeradapter, glassfishloadtimeweaver, glassfishloadtimeweaver, illegalargumentexception, instrumentableclassloader, instrumentableclassloader, loadtimeweaver

The Spring Framework GlassFishLoadTimeWeaver.java source code

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.instrument.classloading.glassfish;

import java.lang.instrument.ClassFileTransformer;

import com.sun.enterprise.loader.InstrumentableClassLoader;

import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
 * {@link LoadTimeWeaver} implementation for GlassFish's
 * {@link InstrumentableClassLoader}.
 *
 * @author Costin Leau
 * @author Juergen Hoeller
 * @since 2.0.1
 * @see com.sun.enterprise.loader.InstrumentableClassLoader
 */
public class GlassFishLoadTimeWeaver implements LoadTimeWeaver {

	private final InstrumentableClassLoader classLoader;


	/**
	 * Create a new instance of the <code>GlassFishLoadTimeWeaver class
	 * using the default {@link ClassLoader}.
	 * @see #GlassFishLoadTimeWeaver(ClassLoader)
	 */
	public GlassFishLoadTimeWeaver() {
		this(ClassUtils.getDefaultClassLoader());
	}

	/**
	 * Create a new instance of the <code>GlassFishLoadTimeWeaver class.
	 * @param classLoader the specific {@link ClassLoader} to use; must not be <code>null
	 * @throws IllegalArgumentException if the supplied <code>classLoader is null;
	 * or if the supplied <code>classLoader is not an {@link InstrumentableClassLoader}
	 */
	public GlassFishLoadTimeWeaver(ClassLoader classLoader) {
		Assert.notNull(classLoader, "ClassLoader must not be null");
		InstrumentableClassLoader icl = determineClassLoader(classLoader);
		if (icl == null) {
			throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: " +
					"An [" + InstrumentableClassLoader.class.getName() + "] implementation is required.");
		}
		this.classLoader = icl;
	}

	/**
	 * Determine the GlassFish {@link InstrumentableClassLoader} for the given
	 * {@link ClassLoader}.
	 * @param classLoader the <code>ClassLoader to check
	 * @return the <code>InstrumentableClassLoader, or null if none found
	 */
	protected InstrumentableClassLoader determineClassLoader(ClassLoader classLoader) {
		// Detect transformation-aware ClassLoader by traversing the hierarchy
		// (as in GlassFish, Spring can be loaded by the WebappClassLoader).
		for (ClassLoader cl = classLoader; cl != null; cl = cl.getParent()) {
			if (cl instanceof InstrumentableClassLoader) {
				return (InstrumentableClassLoader) cl;
			}
		}
		return null;
	}


	public void addTransformer(ClassFileTransformer transformer) {
		this.classLoader.addTransformer(new ClassTransformerAdapter(transformer));
	}

	public ClassLoader getInstrumentableClassLoader() {
		return (ClassLoader) this.classLoader;
	}

	public ClassLoader getThrowawayClassLoader() {
		return this.classLoader.copy();
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework GlassFishLoadTimeWeaver.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.