KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > instrument > InstrumentingUnit


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.jpa.instrument;
21
22 import java.lang.instrument.ClassFileTransformer JavaDoc;
23 import java.lang.instrument.IllegalClassFormatException JavaDoc;
24 import java.lang.instrument.Instrumentation JavaDoc;
25 import java.security.ProtectionDomain JavaDoc;
26
27 import javax.persistence.spi.ClassTransformer;
28
29 import org.apache.cayenne.instrument.InstrumentUtil;
30 import org.apache.cayenne.jpa.JpaUnit;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * A unit that loads all transformers into a shared Instrumentation instance.
36  *
37  * @author Andrus Adamchik
38  */

39 public class InstrumentingUnit extends JpaUnit {
40
41     protected Log logger;
42
43     @Override JavaDoc
44     public void addTransformer(final ClassTransformer transformer) {
45
46         // wrap in a ClassFileTransformer
47
ClassFileTransformer JavaDoc transformerWrapper = new ClassFileTransformer JavaDoc() {
48
49             public byte[] transform(
50                     ClassLoader JavaDoc loader,
51                     String JavaDoc className,
52                     Class JavaDoc<?> classBeingRedefined,
53                     ProtectionDomain JavaDoc protectionDomain,
54                     byte[] classfileBuffer) throws IllegalClassFormatException JavaDoc {
55
56                 return transformer.transform(
57                         loader,
58                         className,
59                         classBeingRedefined,
60                         protectionDomain,
61                         classfileBuffer);
62             }
63         };
64
65         getLogger().info("*** Adding transformer: " + transformer);
66
67         Instrumentation JavaDoc i = InstrumentUtil.getInstrumentation();
68         if (i == null) {
69             throw new IllegalStateException JavaDoc("Attempt to add a transformer failed - "
70                     + "instrumentation is not initialized.");
71         }
72
73         i.addTransformer(transformerWrapper);
74     }
75
76     protected Log getLogger() {
77         if (logger == null) {
78             logger = LogFactory.getLog(getClass());
79         }
80
81         return logger;
82     }
83 }
84
Popular Tags