1 22 package org.objectweb.petals.classloader; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 44 public class SharedLibrariesClassLoader extends ClassLoader { 45 46 protected Map <String , PetalsClassLoader> slClassLoaders; 47 48 51 protected ClassLoader parent; 52 53 58 public SharedLibrariesClassLoader() throws IOException { 59 super(); 60 parent = getSystemClassLoader(); 61 slClassLoaders = Collections.synchronizedMap(new HashMap <String , PetalsClassLoader>()); 62 } 63 64 public void addSharedLibrariesClassLoader(String id, PetalsClassLoader cl) { 65 cl.setParentFirst(false); 66 slClassLoaders.put(id, cl); 67 } 68 69 public void removeSharedLibrariesClassLoader(String id) { 70 slClassLoaders.remove(id); 71 } 72 73 77 83 protected synchronized Class <?> loadClass(String className, boolean resolve, List <String > sharedLibrariesNameList) throws ClassNotFoundException { 84 Class <?> clazz = null; 85 clazz = findLoadedClass(className, sharedLibrariesNameList); 86 if (clazz == null) { 87 try { 89 clazz = findClass(className, sharedLibrariesNameList); 90 } 91 catch (ClassNotFoundException cnfe) { 92 clazz = parent.loadClass(className); 93 } 94 } 95 if (resolve) { 96 resolveClass(clazz); 97 } 98 return clazz; 99 } 100 101 106 protected synchronized URL getResource(String resourceName, List <String > sharedLibrariesNameList) { 107 URL url = null; 108 for (Iterator iter = sharedLibrariesNameList.iterator(); iter.hasNext() && url == null;) { 109 PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next()); 110 if (element != null) { 111 url = element.getResource(resourceName); 112 } 113 } 114 return url; 115 } 116 117 122 protected synchronized InputStream getResourceAsStream(String resourceName, List <String > sharedLibrariesNameList) { 123 InputStream inputStream = null; 124 for (Iterator iter = sharedLibrariesNameList.iterator(); iter.hasNext() && inputStream == null;) { 125 PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next()); 126 if (element != null) { 127 inputStream = element.getResourceAsStream(resourceName); 128 } 129 } 130 return inputStream; 131 } 132 133 141 protected Class <?> findClass(String className, List <String > sharedLibrariesNameList) throws ClassNotFoundException { 142 Class result = null; 143 if (sharedLibrariesNameList != null) { 144 for (Iterator iter = sharedLibrariesNameList.iterator(); iter.hasNext() && result == null;) { 145 PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next()); 146 if (element != null) { 147 try { 148 result = element.findClass(className); 149 } 150 catch (ClassNotFoundException e) { 151 } 154 } 155 } 156 } 157 if (result == null) { 158 throw new ClassNotFoundException (); 159 } 160 return result; 161 } 162 163 protected Class <?> findLoadedClass(String className, List <String > sharedLibrariesNameList) { 164 Class result = null; 165 if (sharedLibrariesNameList != null) { 166 for (Iterator iter = sharedLibrariesNameList.iterator(); iter.hasNext() && result == null;) { 167 PetalsClassLoader element = (PetalsClassLoader) slClassLoaders.get(iter.next()); 168 if (element != null) { 169 try { 170 result = element.findLoadedClazz(className); 171 } 172 catch (ClassNotFoundException e) { 173 } 176 } 177 } 178 } 179 return result; 180 } 181 182 } 183 | Popular Tags |