1 11 package org.eclipse.osgi.framework.internal.core; 12 13 import java.io.IOException ; 14 import java.net.URL ; 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 import java.util.Enumeration ; 18 19 public class SystemPolicy implements IBuddyPolicy { 20 21 private static class ParentClassLoader extends ClassLoader { 22 protected ParentClassLoader() { 23 super(null); 24 } 25 } 26 27 public static final byte BOOT = 0; 28 public static final byte EXT = 1; 29 public static final byte APP = 2; 30 31 private static SystemPolicy[] instances = new SystemPolicy[3]; 32 33 private ClassLoader classLoader; 34 35 public static SystemPolicy getInstance(final byte type) { 36 if (instances[type] == null) { 37 instances[type] = new SystemPolicy(); 38 instances[type].classLoader = (ClassLoader ) AccessController.doPrivileged(new PrivilegedAction () { 39 public Object run() { 40 return createClassLoader(type); 41 } 42 }); 43 } 44 return instances[type]; 45 } 46 47 public SystemPolicy() { 48 } 50 51 public SystemPolicy(ClassLoader parent) { 52 classLoader = parent; 53 } 54 55 static ClassLoader createClassLoader(byte type) { 56 switch (type) { 57 case APP : 58 if (ClassLoader.getSystemClassLoader() != null) 59 return ClassLoader.getSystemClassLoader(); 60 return new ParentClassLoader(); 61 62 case BOOT : 63 return new ParentClassLoader(); 64 65 case EXT : 66 if (ClassLoader.getSystemClassLoader() != null) 67 return ClassLoader.getSystemClassLoader().getParent(); 68 return new ParentClassLoader(); 69 } 70 return null; 71 } 72 73 public Class loadClass(String name) { 74 try { 75 return classLoader.loadClass(name); 76 } catch (ClassNotFoundException e) { 77 return null; 78 } 79 } 80 81 public URL loadResource(String name) { 82 return classLoader.getResource(name); 83 } 84 85 public Enumeration loadResources(String name) { 86 try { 87 return classLoader.getResources(name); 88 } catch (IOException e) { 89 return null; 90 } 91 } 92 93 } 94 | Popular Tags |