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.test.jpa;
18:
19: import java.util.LinkedList;
20: import java.util.List;
21:
22: import org.springframework.instrument.classloading.ResourceOverridingShadowingClassLoader;
23:
24: /**
25: * Subclass of ShadowingClassLoader that overrides attempts to
26: * locate <code>orm.xml</code>.
27: *
28: * <p>This class must <b>not</b> be an inner class of AbstractJpaTests
29: * to avoid it being loaded until first used.
30: *
31: * @author Rod Johnson
32: * @author Adrian Colyer
33: * @since 2.0
34: */
35: class OrmXmlOverridingShadowingClassLoader extends
36: ResourceOverridingShadowingClassLoader {
37:
38: /**
39: * Default location of the <code>orm.xml</code> file in the class path:
40: * "META-INF/orm.xml"
41: */
42: public static final String DEFAULT_ORM_XML_LOCATION = "META-INF/orm.xml";
43:
44: private final List<String> providerPrefixes = new LinkedList<String>();
45:
46: {
47: // Automatically exclude classes from these well-known persistence providers.
48: this .providerPrefixes.add("oracle.toplink.essentials");
49:
50: // Do NOT exclude Hibernate classes --
51: // this causes class casts due to use of CGLIB by Hibernate.
52: // Same goes for OpenJPA which will not enhance the domain classes.
53: }
54:
55: public OrmXmlOverridingShadowingClassLoader(ClassLoader loader,
56: String realOrmXmlLocation) {
57: super (loader);
58: override(DEFAULT_ORM_XML_LOCATION, realOrmXmlLocation);
59: }
60:
61: @Override
62: protected boolean isClassNameExcludedFromShadowing(String className) {
63: for (String providerPrefix : providerPrefixes) {
64: if (className.startsWith(providerPrefix)) {
65: return true;
66: }
67: }
68:
69: // Also do not shadow JUnit infrastructure.
70: if (className.startsWith("junit")) {
71: return true;
72: }
73:
74: return false;
75: }
76:
77: }
|