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 javax.persistence.spi.ClassTransformer;
20:
21: import org.springframework.instrument.classloading.LoadTimeWeaver;
22: import org.springframework.instrument.classloading.SimpleThrowawayClassLoader;
23: import org.springframework.util.ClassUtils;
24:
25: /**
26: * Subclass of MutablePersistenceUnitInfo that adds instrumentation
27: * hooks based on Spring's LoadTimeWeaver abstraction.
28: *
29: * <p>This class is restricted to package visibility, in contrast
30: * to its superclass.
31: *
32: * @author Rod Johnson
33: * @author Juergen Hoeller
34: * @author Costin Leau
35: * @since 2.0
36: * @see #setLoadTimeWeaver
37: * @see PersistenceUnitManager
38: */
39: class SpringPersistenceUnitInfo extends MutablePersistenceUnitInfo {
40:
41: private LoadTimeWeaver loadTimeWeaver;
42:
43: /**
44: * Set the LoadTimeWeaver SPI strategy interface used by Spring
45: * to add instrumentation to the current class loader.
46: */
47: public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
48: this .loadTimeWeaver = loadTimeWeaver;
49: }
50:
51: public ClassLoader getClassLoader() {
52: if (this .loadTimeWeaver != null) {
53: return this .loadTimeWeaver.getInstrumentableClassLoader();
54: } else {
55: return ClassUtils.getDefaultClassLoader();
56: }
57: }
58:
59: /**
60: * Method called by PersistenceProvider to add instrumentation to
61: * the current environment.
62: */
63: public void addTransformer(ClassTransformer classTransformer) {
64: if (this .loadTimeWeaver == null) {
65: throw new IllegalStateException(
66: "Cannot apply class transformer without LoadTimeWeaver specified");
67: }
68: this .loadTimeWeaver
69: .addTransformer(new ClassFileTransformerAdapter(
70: classTransformer));
71: }
72:
73: public ClassLoader getNewTempClassLoader() {
74: if (this .loadTimeWeaver != null) {
75: return this .loadTimeWeaver.getThrowawayClassLoader();
76: } else {
77: return new SimpleThrowawayClassLoader(ClassUtils
78: .getDefaultClassLoader());
79: }
80: }
81:
82: }
|