KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > persistenceunit > ClassFileTransformerAdapter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import java.security.ProtectionDomain JavaDoc;
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 JavaDoc {
39     
40     private static final Log logger = LogFactory.getLog(ClassFileTransformerAdapter.class);
41
42     private final ClassTransformer classTransformer;
43
44
45     public ClassFileTransformerAdapter(ClassTransformer classTransformer) {
46         Assert.notNull(classTransformer, "ClassTransformer must not be null");
47         this.classTransformer = classTransformer;
48     }
49
50
51     public byte[] transform(
52             ClassLoader JavaDoc loader, String JavaDoc className, Class JavaDoc<?> classBeingRedefined,
53             ProtectionDomain JavaDoc protectionDomain, byte[] classfileBuffer) {
54         
55         try {
56             byte[] transformed = this.classTransformer.transform(
57                     loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
58             if (transformed != null && logger.isDebugEnabled()) {
59                 logger.debug("Transformer of class [" + this.classTransformer.getClass().getName() +
60                         "] transformed class [" + className + "]; bytes in=" +
61                         classfileBuffer.length + "; bytes out=" + transformed.length);
62             }
63             return transformed;
64         }
65         catch (ClassCircularityError JavaDoc ex) {
66             logger.error("Error weaving class [" + className + "] with " +
67                     "transformer of class [" + this.classTransformer.getClass().getName() + "]", ex);
68             throw new IllegalStateException JavaDoc("Could not weave class [" + className + "]", ex);
69         }
70         catch (Throwable JavaDoc ex) {
71             if (logger.isWarnEnabled()) {
72                 logger.warn("Error weaving class [" + className + "] with " +
73                         "transformer of class [" + this.classTransformer.getClass().getName() + "]", ex);
74             }
75             // The exception will be ignored by the class loader, anyway...
76
throw new IllegalStateException JavaDoc("Could not weave class [" + className + "]", ex);
77         }
78     }
79
80
81     @Override JavaDoc
82     public String JavaDoc toString() {
83         return "Standard ClassFileTransformer wrapping JPA transformer: " + this.classTransformer;
84     }
85
86 }
87
Popular Tags