1 21 22 27 28 package javax.activation; 29 30 import java.security.*; 31 import java.net.*; 32 import java.io.*; 33 import java.util.*; 34 35 38 class SecuritySupport { 39 40 private SecuritySupport() { 41 } 43 44 public static ClassLoader getContextClassLoader() { 45 return (ClassLoader ) 46 AccessController.doPrivileged(new PrivilegedAction() { 47 public Object run() { 48 ClassLoader cl = null; 49 try { 50 cl = Thread.currentThread().getContextClassLoader(); 51 } catch (SecurityException ex) { } 52 return cl; 53 } 54 }); 55 } 56 57 public static InputStream getResourceAsStream(final Class c, 58 final String name) throws IOException { 59 try { 60 return (InputStream) 61 AccessController.doPrivileged(new PrivilegedExceptionAction() { 62 public Object run() throws IOException { 63 return c.getResourceAsStream(name); 64 } 65 }); 66 } catch (PrivilegedActionException e) { 67 throw (IOException)e.getException(); 68 } 69 } 70 71 public static URL[] getResources(final ClassLoader cl, final String name) { 72 return (URL[]) 73 AccessController.doPrivileged(new PrivilegedAction() { 74 public Object run() { 75 URL[] ret = null; 76 try { 77 List v = new ArrayList(); 78 Enumeration e = cl.getResources(name); 79 while (e != null && e.hasMoreElements()) { 80 URL url = (URL)e.nextElement(); 81 if (url != null) 82 v.add(url); 83 } 84 if (v.size() > 0) { 85 ret = new URL[v.size()]; 86 ret = (URL[])v.toArray(ret); 87 } 88 } catch (IOException ioex) { 89 } catch (SecurityException ex) { } 90 return ret; 91 } 92 }); 93 } 94 95 public static URL[] getSystemResources(final String name) { 96 return (URL[]) 97 AccessController.doPrivileged(new PrivilegedAction() { 98 public Object run() { 99 URL[] ret = null; 100 try { 101 List v = new ArrayList(); 102 Enumeration e = ClassLoader.getSystemResources(name); 103 while (e != null && e.hasMoreElements()) { 104 URL url = (URL)e.nextElement(); 105 if (url != null) 106 v.add(url); 107 } 108 if (v.size() > 0) { 109 ret = new URL[v.size()]; 110 ret = (URL[])v.toArray(ret); 111 } 112 } catch (IOException ioex) { 113 } catch (SecurityException ex) { } 114 return ret; 115 } 116 }); 117 } 118 119 public static InputStream openStream(final URL url) throws IOException { 120 try { 121 return (InputStream) 122 AccessController.doPrivileged(new PrivilegedExceptionAction() { 123 public Object run() throws IOException { 124 return url.openStream(); 125 } 126 }); 127 } catch (PrivilegedActionException e) { 128 throw (IOException)e.getException(); 129 } 130 } 131 } 132 | Popular Tags |