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

Spring Framework example source code file (ClassFileTransformerAdapter.java)

This example Spring Framework source code file (ClassFileTransformerAdapter.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

classcircularityerror, classfiletransformer, classfiletransformer, classfiletransformeradapter, classloader, classtransformer, could, could, error, error, illegalstateexception, security, string, string, throwable

The Spring Framework ClassFileTransformerAdapter.java source code

/*
 * Copyright 2002-2006 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.orm.jpa.persistenceunit;

import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;

import javax.persistence.spi.ClassTransformer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;

/**
 * Simple adapter that implements the <code>java.lang.instrument.ClassFileTransformer
 * interface based on a JPA ClassTransformer which a JPA PersistenceProvider asks the
 * PersistenceUnitInfo to install in the current runtime.
 *
 * @author Rod Johnson
 * @since 2.0
 * @see javax.persistence.spi.PersistenceUnitInfo#addTransformer(javax.persistence.spi.ClassTransformer)
 */
class ClassFileTransformerAdapter implements ClassFileTransformer {
	
	private static final Log logger = LogFactory.getLog(ClassFileTransformerAdapter.class);

	private final ClassTransformer classTransformer;


	public ClassFileTransformerAdapter(ClassTransformer classTransformer) {
		Assert.notNull(classTransformer, "ClassTransformer must not be null");
		this.classTransformer = classTransformer;
	}


	public byte[] transform(
			ClassLoader loader, String className, Class<?> classBeingRedefined,
			ProtectionDomain protectionDomain, byte[] classfileBuffer) {
		
		try {
			byte[] transformed = this.classTransformer.transform(
					loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
			if (transformed != null && logger.isDebugEnabled()) {
				logger.debug("Transformer of class [" + this.classTransformer.getClass().getName() +
						"] transformed class [" + className + "]; bytes in=" +
						classfileBuffer.length + "; bytes out=" + transformed.length);
			}
			return transformed;
		}
		catch (ClassCircularityError ex) {
			logger.error("Error weaving class [" + className + "] with " +
					"transformer of class [" + this.classTransformer.getClass().getName() + "]", ex);
			throw new IllegalStateException("Could not weave class [" + className + "]", ex);
		}
		catch (Throwable ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Error weaving class [" + className + "] with " +
						"transformer of class [" + this.classTransformer.getClass().getName() + "]", ex);
			}
			// The exception will be ignored by the class loader, anyway...
			throw new IllegalStateException("Could not weave class [" + className + "]", ex);
		}
	}


	@Override
	public String toString() {
		return "Standard ClassFileTransformer wrapping JPA transformer: " + this.classTransformer;
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework ClassFileTransformerAdapter.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.