01: /*
02: * Copyright 2002-2007 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.Assert;
24:
25: /**
26: * Subclass of {@link MutablePersistenceUnitInfo} that adds instrumentation hooks based on
27: * Spring's {@link org.springframework.instrument.classloading.LoadTimeWeaver} abstraction.
28: *
29: * <p>This class is restricted to package visibility, in contrast to its superclass.
30: *
31: * @author Rod Johnson
32: * @author Juergen Hoeller
33: * @author Costin Leau
34: * @since 2.0
35: * @see PersistenceUnitManager
36: */
37: class SpringPersistenceUnitInfo extends MutablePersistenceUnitInfo {
38:
39: private LoadTimeWeaver loadTimeWeaver;
40:
41: private ClassLoader classLoader;
42:
43: /**
44: * Initialize this PersistenceUnitInfo with the LoadTimeWeaver SPI interface
45: * used by Spring to add instrumentation to the current class loader.
46: */
47: public void init(LoadTimeWeaver loadTimeWeaver) {
48: Assert.notNull(loadTimeWeaver,
49: "LoadTimeWeaver must not be null");
50: this .loadTimeWeaver = loadTimeWeaver;
51: this .classLoader = loadTimeWeaver
52: .getInstrumentableClassLoader();
53: }
54:
55: /**
56: * Initialize this PersistenceUnitInfo with the current class loader
57: * (instead of with a LoadTimeWeaver).
58: */
59: public void init(ClassLoader classLoader) {
60: Assert.notNull(classLoader, "ClassLoader must not be null");
61: this .classLoader = classLoader;
62: }
63:
64: /**
65: * This implementation returns the LoadTimeWeaver's instrumentable ClassLoader,
66: * if specified.
67: */
68: public ClassLoader getClassLoader() {
69: return this .classLoader;
70: }
71:
72: /**
73: * This implementation delegates to the LoadTimeWeaver, if specified.
74: */
75: public void addTransformer(ClassTransformer classTransformer) {
76: if (this .loadTimeWeaver == null) {
77: throw new IllegalStateException(
78: "Cannot apply class transformer without LoadTimeWeaver specified");
79: }
80: this .loadTimeWeaver
81: .addTransformer(new ClassFileTransformerAdapter(
82: classTransformer));
83: }
84:
85: /**
86: * This implementation delegates to the LoadTimeWeaver, if specified.
87: */
88: public ClassLoader getNewTempClassLoader() {
89: if (this .loadTimeWeaver != null) {
90: return this .loadTimeWeaver.getThrowawayClassLoader();
91: } else {
92: return new SimpleThrowawayClassLoader(this.classLoader);
93: }
94: }
95:
96: }
|