KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > test > jpa > OrmXmlOverridingShadowingClassLoader


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.test.jpa;
18
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
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 ResourceOverridingShadowingClassLoader {
36     
37     /**
38      * Default location of the <code>orm.xml</code> file in the class path:
39      * "META-INF/orm.xml"
40      */

41     public static final String JavaDoc DEFAULT_ORM_XML_LOCATION = "META-INF/orm.xml";
42
43
44     private final List JavaDoc<String JavaDoc> providerPrefixes = new LinkedList JavaDoc<String JavaDoc>();
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
56     public OrmXmlOverridingShadowingClassLoader(ClassLoader JavaDoc loader, String JavaDoc realOrmXmlLocation) {
57         super(loader);
58         override(DEFAULT_ORM_XML_LOCATION, realOrmXmlLocation);
59     }
60     
61     @Override JavaDoc
62     protected boolean isClassNameExcludedFromShadowing(String JavaDoc className) {
63         for (String JavaDoc 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 }
78
Popular Tags