1 16 17 package org.springframework.instrument.classloading; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.URL ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.springframework.util.Assert; 27 28 36 public class ResourceOverridingShadowingClassLoader extends ShadowingClassLoader { 37 38 private static final Enumeration <URL > EMPTY_URL_ENUMERATION = new Enumeration <URL >() { 39 public boolean hasMoreElements() { 40 return false; 41 } 42 public URL nextElement() { 43 throw new UnsupportedOperationException ("Should not be called. I am empty."); 44 } 45 }; 46 47 48 51 private Map <String , String > overrides = new HashMap <String , String >(); 52 53 54 59 public ResourceOverridingShadowingClassLoader(ClassLoader enclosingClassLoader) { 60 super(enclosingClassLoader); 61 } 62 63 64 70 public void override(String oldPath, String newPath) { 71 this.overrides.put(oldPath, newPath); 72 } 73 74 79 public void suppress(String oldPath) { 80 this.overrides.put(oldPath, null); 81 } 82 83 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 94 public URL getResource(String requestedPath) { 95 if (this.overrides.containsKey(requestedPath)) { 96 String 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 105 public InputStream getResourceAsStream(String requestedPath) { 106 if (this.overrides.containsKey(requestedPath)) { 107 String 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 116 public Enumeration <URL > getResources(String requestedPath) throws IOException { 117 if (this.overrides.containsKey(requestedPath)) { 118 String 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 |