KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > instrument > classloading > ResourceOverridingShadowingClassLoader


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.instrument.classloading;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.springframework.util.Assert;
27
28 /**
29  * Subclass of ShadowingClassLoader that overrides attempts to
30  * locate certain files.
31  *
32  * @author Rod Johnson
33  * @author Adrian Colyer
34  * @since 2.0
35  */

36 public class ResourceOverridingShadowingClassLoader extends ShadowingClassLoader {
37     
38     private static final Enumeration JavaDoc<URL JavaDoc> EMPTY_URL_ENUMERATION = new Enumeration JavaDoc<URL JavaDoc>() {
39         public boolean hasMoreElements() {
40             return false;
41         }
42         public URL JavaDoc nextElement() {
43             throw new UnsupportedOperationException JavaDoc("Should not be called. I am empty.");
44         }
45     };
46     
47
48     /**
49      * Key is asked for value: value is actual value
50      */

51     private Map JavaDoc<String JavaDoc, String JavaDoc> overrides = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
52
53
54     /**
55      * Create a new ResourceOverridingShadowingClassLoader,
56      * decorating the given ClassLoader.
57      * @param enclosingClassLoader the ClassLoader to decorate
58      */

59     public ResourceOverridingShadowingClassLoader(ClassLoader JavaDoc enclosingClassLoader) {
60         super(enclosingClassLoader);
61     }
62     
63
64     /**
65      * Return the resource (if any) at the new path
66      * on an attempt to locate a resource at the old path.
67      * @param oldPath the path requested
68      * @param newPath the actual path to be looked up
69      */

70     public void override(String JavaDoc oldPath, String JavaDoc newPath) {
71         this.overrides.put(oldPath, newPath);
72     }
73     
74     /**
75      * Ensure that a resource with the given path is not found.
76      * @param oldPath the path of the resource to hide even if
77      * it exists in the parent ClassLoader
78      */

79     public void suppress(String JavaDoc oldPath) {
80         this.overrides.put(oldPath, null);
81     }
82
83     /**
84      * Copy all overrides from the given ClassLoader.
85      * @param other the other ClassLoader to copy from
86      */

87     public void copyOverrides(ResourceOverridingShadowingClassLoader other) {
88         Assert.notNull(other, "Other ClassLoader must not be null");
89         this.overrides.putAll(other.overrides);
90     }
91
92
93     @Override JavaDoc
94     public URL JavaDoc getResource(String JavaDoc requestedPath) {
95         if (this.overrides.containsKey(requestedPath)) {
96             String JavaDoc overriddenPath = this.overrides.get(requestedPath);
97             return (overriddenPath != null ? super.getResource(overriddenPath) : null);
98         }
99         else {
100             return super.getResource(requestedPath);
101         }
102     }
103
104     @Override JavaDoc
105     public InputStream JavaDoc getResourceAsStream(String JavaDoc requestedPath) {
106         if (this.overrides.containsKey(requestedPath)) {
107             String JavaDoc overriddenPath = this.overrides.get(requestedPath);
108             return (overriddenPath != null ? super.getResourceAsStream(overriddenPath) : null);
109         }
110         else {
111             return super.getResourceAsStream(requestedPath);
112         }
113     }
114     
115     @Override JavaDoc
116     public Enumeration JavaDoc<URL JavaDoc> getResources(String JavaDoc requestedPath) throws IOException JavaDoc {
117         if (this.overrides.containsKey(requestedPath)) {
118             String JavaDoc overriddenLocation = this.overrides.get(requestedPath);
119             return (overriddenLocation != null ?
120                     super.getResources(overriddenLocation) : EMPTY_URL_ENUMERATION);
121         }
122         else {
123             return super.getResources(requestedPath);
124         }
125     }
126
127 }
128
Popular Tags