01: /*
02: * Copyright 2002-2006 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.orm.jpa.persistenceunit;
18:
19: import java.lang.instrument.ClassFileTransformer;
20: import java.security.ProtectionDomain;
21:
22: import javax.persistence.spi.ClassTransformer;
23:
24: import org.apache.commons.logging.Log;
25: import org.apache.commons.logging.LogFactory;
26:
27: import org.springframework.util.Assert;
28:
29: /**
30: * Simple adapter that implements the <code>java.lang.instrument.ClassFileTransformer</code>
31: * interface based on a JPA ClassTransformer which a JPA PersistenceProvider asks the
32: * PersistenceUnitInfo to install in the current runtime.
33: *
34: * @author Rod Johnson
35: * @since 2.0
36: * @see javax.persistence.spi.PersistenceUnitInfo#addTransformer(javax.persistence.spi.ClassTransformer)
37: */
38: class ClassFileTransformerAdapter implements ClassFileTransformer {
39:
40: private static final Log logger = LogFactory
41: .getLog(ClassFileTransformerAdapter.class);
42:
43: private final ClassTransformer classTransformer;
44:
45: public ClassFileTransformerAdapter(ClassTransformer classTransformer) {
46: Assert.notNull(classTransformer,
47: "ClassTransformer must not be null");
48: this .classTransformer = classTransformer;
49: }
50:
51: public byte[] transform(ClassLoader loader, String className,
52: Class<?> classBeingRedefined,
53: ProtectionDomain protectionDomain, byte[] classfileBuffer) {
54:
55: try {
56: byte[] transformed = this .classTransformer.transform(
57: loader, className, classBeingRedefined,
58: protectionDomain, classfileBuffer);
59: if (transformed != null && logger.isDebugEnabled()) {
60: logger.debug("Transformer of class ["
61: + this .classTransformer.getClass().getName()
62: + "] transformed class [" + className
63: + "]; bytes in=" + classfileBuffer.length
64: + "; bytes out=" + transformed.length);
65: }
66: return transformed;
67: } catch (ClassCircularityError ex) {
68: logger.error("Error weaving class [" + className
69: + "] with " + "transformer of class ["
70: + this .classTransformer.getClass().getName() + "]",
71: ex);
72: throw new IllegalStateException("Could not weave class ["
73: + className + "]", ex);
74: } catch (Throwable ex) {
75: if (logger.isWarnEnabled()) {
76: logger.warn("Error weaving class [" + className
77: + "] with " + "transformer of class ["
78: + this .classTransformer.getClass().getName()
79: + "]", ex);
80: }
81: // The exception will be ignored by the class loader, anyway...
82: throw new IllegalStateException("Could not weave class ["
83: + className + "]", ex);
84: }
85: }
86:
87: @Override
88: public String toString() {
89: return "Standard ClassFileTransformer wrapping JPA transformer: "
90: + this.classTransformer;
91: }
92:
93: }
|