1 16 17 package org.apache.xml.serialize; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.InputStream ; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.security.PrivilegedActionException ; 27 import java.security.PrivilegedExceptionAction ; 28 29 35 final class SecuritySupport { 36 37 private static final SecuritySupport securitySupport = new SecuritySupport(); 38 39 42 static SecuritySupport getInstance() { 43 return securitySupport; 44 } 45 46 ClassLoader getContextClassLoader() { 47 return (ClassLoader ) 48 AccessController.doPrivileged(new PrivilegedAction () { 49 public Object run() { 50 ClassLoader cl = null; 51 try { 52 cl = Thread.currentThread().getContextClassLoader(); 53 } catch (SecurityException ex) { } 54 return cl; 55 } 56 }); 57 } 58 59 ClassLoader getSystemClassLoader() { 60 return (ClassLoader ) 61 AccessController.doPrivileged(new PrivilegedAction () { 62 public Object run() { 63 ClassLoader cl = null; 64 try { 65 cl = ClassLoader.getSystemClassLoader(); 66 } catch (SecurityException ex) {} 67 return cl; 68 } 69 }); 70 } 71 72 ClassLoader getParentClassLoader(final ClassLoader cl) { 73 return (ClassLoader ) 74 AccessController.doPrivileged(new PrivilegedAction () { 75 public Object run() { 76 ClassLoader parent = null; 77 try { 78 parent = cl.getParent(); 79 } catch (SecurityException ex) {} 80 81 return (parent == cl) ? null : parent; 84 } 85 }); 86 } 87 88 String getSystemProperty(final String propName) { 89 return (String ) 90 AccessController.doPrivileged(new PrivilegedAction () { 91 public Object run() { 92 return System.getProperty(propName); 93 } 94 }); 95 } 96 97 FileInputStream getFileInputStream(final File file) 98 throws FileNotFoundException 99 { 100 try { 101 return (FileInputStream ) 102 AccessController.doPrivileged(new PrivilegedExceptionAction () { 103 public Object run() throws FileNotFoundException { 104 return new FileInputStream (file); 105 } 106 }); 107 } catch (PrivilegedActionException e) { 108 throw (FileNotFoundException )e.getException(); 109 } 110 } 111 112 InputStream getResourceAsStream(final ClassLoader cl, 113 final String name) 114 { 115 return (InputStream ) 116 AccessController.doPrivileged(new PrivilegedAction () { 117 public Object run() { 118 InputStream ris; 119 if (cl == null) { 120 ris = ClassLoader.getSystemResourceAsStream(name); 121 } else { 122 ris = cl.getResourceAsStream(name); 123 } 124 return ris; 125 } 126 }); 127 } 128 129 boolean getFileExists(final File f) { 130 return ((Boolean ) 131 AccessController.doPrivileged(new PrivilegedAction () { 132 public Object run() { 133 return new Boolean (f.exists()); 134 } 135 })).booleanValue(); 136 } 137 138 long getLastModified(final File f) { 139 return ((Long ) 140 AccessController.doPrivileged(new PrivilegedAction () { 141 public Object run() { 142 return new Long (f.lastModified()); 143 } 144 })).longValue(); 145 } 146 147 private SecuritySupport () {} 148 } 149 | Popular Tags |