1 7 8 package javax.xml.xpath; 9 10 import java.net.URL ; 11 import java.security.*; 12 import java.net.*; 13 import java.io.*; 14 import java.util.*; 15 16 23 class SecuritySupport { 24 25 26 ClassLoader getContextClassLoader() { 27 return (ClassLoader ) 28 AccessController.doPrivileged(new PrivilegedAction() { 29 public Object run() { 30 ClassLoader cl = null; 31 try { 32 cl = Thread.currentThread().getContextClassLoader(); 33 } catch (SecurityException ex) { } 34 return cl; 35 } 36 }); 37 } 38 39 String getSystemProperty(final String propName) { 40 return (String ) 41 AccessController.doPrivileged(new PrivilegedAction() { 42 public Object run() { 43 return System.getProperty(propName); 44 } 45 }); 46 } 47 48 FileInputStream getFileInputStream(final File file) 49 throws FileNotFoundException 50 { 51 try { 52 return (FileInputStream) 53 AccessController.doPrivileged(new PrivilegedExceptionAction() { 54 public Object run() throws FileNotFoundException { 55 return new FileInputStream(file); 56 } 57 }); 58 } catch (PrivilegedActionException e) { 59 throw (FileNotFoundException)e.getException(); 60 } 61 } 62 63 InputStream getURLInputStream(final URL url) 64 throws IOException 65 { 66 try { 67 return (InputStream) 68 AccessController.doPrivileged(new PrivilegedExceptionAction() { 69 public Object run() throws IOException { 70 return url.openStream(); 71 } 72 }); 73 } catch (PrivilegedActionException e) { 74 throw (IOException)e.getException(); 75 } 76 } 77 78 URL getResourceAsURL(final ClassLoader cl, 79 final String name) 80 { 81 return (URL ) 82 AccessController.doPrivileged(new PrivilegedAction() { 83 public Object run() { 84 URL url; 85 if (cl == null) { 86 url = ClassLoader.getSystemResource(name); 87 } else { 88 url = cl.getResource(name); 89 } 90 return url; 91 } 92 }); 93 } 94 95 Enumeration getResources(final ClassLoader cl, 96 final String name) throws IOException 97 { 98 try{ 99 return (Enumeration) 100 AccessController.doPrivileged(new PrivilegedExceptionAction() { 101 public Object run() throws IOException{ 102 Enumeration enumeration; 103 if (cl == null) { 104 enumeration = ClassLoader.getSystemResources(name); 105 } else { 106 enumeration = cl.getResources(name); 107 } 108 return enumeration; 109 } 110 }); 111 }catch(PrivilegedActionException e){ 112 throw (IOException)e.getException(); 113 } 114 } 115 116 InputStream getResourceAsStream(final ClassLoader cl, 117 final String name) 118 { 119 return (InputStream) 120 AccessController.doPrivileged(new PrivilegedAction() { 121 public Object run() { 122 InputStream ris; 123 if (cl == null) { 124 ris = ClassLoader.getSystemResourceAsStream(name); 125 } else { 126 ris = cl.getResourceAsStream(name); 127 } 128 return ris; 129 } 130 }); 131 } 132 133 boolean doesFileExist(final File f) { 134 return ((Boolean ) 135 AccessController.doPrivileged(new PrivilegedAction() { 136 public Object run() { 137 return new Boolean (f.exists()); 138 } 139 })).booleanValue(); 140 } 141 142 } 143 | Popular Tags |