1 11 12 package org.eclipse.osgi.framework.util; 13 14 import java.io.*; 15 import java.net.*; 16 import java.security.*; 17 import java.util.Properties ; 18 import java.util.zip.ZipFile ; 19 import org.eclipse.osgi.framework.internal.core.FrameworkProperties; 20 import org.osgi.framework.*; 21 import org.osgi.util.tracker.ServiceTracker; 22 23 27 public class SecureAction { 28 private AccessControlContext controlContext; 30 31 static final ClassLoader bootClassLoader = (ClassLoader ) AccessController.doPrivileged(new PrivilegedAction() { 33 public Object run() { 34 return new ClassLoader (null) { 35 }; 37 } 38 }); 39 40 45 SecureAction() { 46 this.controlContext = AccessController.getContext(); 48 } 49 50 58 public static PrivilegedAction createSecureAction() { 59 return new PrivilegedAction() { 60 public Object run() { 61 return new SecureAction(); 62 } 63 }; 64 } 65 66 72 public String getProperty(final String property) { 73 if (System.getSecurityManager() == null) 74 return FrameworkProperties.getProperty(property); 75 return (String ) AccessController.doPrivileged(new PrivilegedAction() { 76 public Object run() { 77 return FrameworkProperties.getProperty(property); 78 } 79 }, controlContext); 80 } 81 82 90 public String getProperty(final String property, final String def) { 91 if (System.getSecurityManager() == null) 92 return FrameworkProperties.getProperty(property, def); 93 return (String ) AccessController.doPrivileged(new PrivilegedAction() { 94 public Object run() { 95 return FrameworkProperties.getProperty(property, def); 96 } 97 }, controlContext); 98 } 99 100 105 public Properties getProperties() { 106 if (System.getSecurityManager() == null) 107 return FrameworkProperties.getProperties(); 108 return (Properties ) AccessController.doPrivileged(new PrivilegedAction() { 109 public Object run() { 110 return FrameworkProperties.getProperties(); 111 } 112 }, controlContext); 113 } 114 115 122 public FileInputStream getFileInputStream(final File file) throws FileNotFoundException { 123 if (System.getSecurityManager() == null) 124 return new FileInputStream(file); 125 try { 126 return (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() { 127 public Object run() throws FileNotFoundException { 128 return new FileInputStream(file); 129 } 130 }, controlContext); 131 } catch (PrivilegedActionException e) { 132 if (e.getException() instanceof FileNotFoundException) 133 throw (FileNotFoundException) e.getException(); 134 throw (RuntimeException ) e.getException(); 135 } 136 } 137 138 146 public FileOutputStream getFileOutputStream(final File file, final boolean append) throws FileNotFoundException { 147 if (System.getSecurityManager() == null) 148 return new FileOutputStream(file.getAbsolutePath(), append); 149 try { 150 return (FileOutputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() { 151 public Object run() throws FileNotFoundException { 152 return new FileOutputStream(file.getAbsolutePath(), append); 153 } 154 }, controlContext); 155 } catch (PrivilegedActionException e) { 156 if (e.getException() instanceof FileNotFoundException) 157 throw (FileNotFoundException) e.getException(); 158 throw (RuntimeException ) e.getException(); 159 } 160 } 161 162 168 public long length(final File file) { 169 if (System.getSecurityManager() == null) 170 return file.length(); 171 return ((Long ) AccessController.doPrivileged(new PrivilegedAction() { 172 public Object run() { 173 return new Long (file.length()); 174 } 175 }, controlContext)).longValue(); 176 } 177 178 184 public boolean exists(final File file) { 185 if (System.getSecurityManager() == null) 186 return file.exists(); 187 return ((Boolean ) AccessController.doPrivileged(new PrivilegedAction() { 188 public Object run() { 189 return file.exists() ? Boolean.TRUE : Boolean.FALSE; 190 } 191 }, controlContext)).booleanValue(); 192 } 193 194 200 public boolean isDirectory(final File file) { 201 if (System.getSecurityManager() == null) 202 return file.isDirectory(); 203 return ((Boolean ) AccessController.doPrivileged(new PrivilegedAction() { 204 public Object run() { 205 return file.isDirectory() ? Boolean.TRUE : Boolean.FALSE; 206 } 207 }, controlContext)).booleanValue(); 208 } 209 210 216 public long lastModified(final File file) { 217 if (System.getSecurityManager() == null) 218 return file.lastModified(); 219 return ((Long ) AccessController.doPrivileged(new PrivilegedAction() { 220 public Object run() { 221 return new Long (file.lastModified()); 222 } 223 }, controlContext)).longValue(); 224 } 225 226 232 public String [] list(final File file) { 233 if (System.getSecurityManager() == null) 234 return file.list(); 235 return (String []) AccessController.doPrivileged(new PrivilegedAction() { 236 public Object run() { 237 return file.list(); 238 } 239 }, controlContext); 240 } 241 242 249 public ZipFile getZipFile(final File file) throws IOException { 250 if (System.getSecurityManager() == null) 251 return new ZipFile (file); 252 try { 253 return (ZipFile ) AccessController.doPrivileged(new PrivilegedExceptionAction() { 254 public Object run() throws IOException { 255 return new ZipFile (file); 256 } 257 }, controlContext); 258 } catch (PrivilegedActionException e) { 259 if (e.getException() instanceof IOException) 260 throw (IOException) e.getException(); 261 throw (RuntimeException ) e.getException(); 262 } 263 } 264 265 276 public URL getURL(final String protocol, final String host, final int port, final String file, final URLStreamHandler handler) throws MalformedURLException { 277 if (System.getSecurityManager() == null) 278 return new URL(protocol, host, port, file, handler); 279 try { 280 return (URL) AccessController.doPrivileged(new PrivilegedExceptionAction() { 281 public Object run() throws MalformedURLException { 282 return new URL(protocol, host, port, file, handler); 283 } 284 }, controlContext); 285 } catch (PrivilegedActionException e) { 286 if (e.getException() instanceof MalformedURLException) 287 throw (MalformedURLException) e.getException(); 288 throw (RuntimeException ) e.getException(); 289 } 290 } 291 292 299 public Thread createThread(final Runnable target, final String name) { 300 if (System.getSecurityManager() == null) 301 return new Thread (target, name); 302 return (Thread ) AccessController.doPrivileged(new PrivilegedAction() { 303 public Object run() { 304 return new Thread (target, name); 305 } 306 }, controlContext); 307 } 308 309 316 public Object getService(final ServiceReference reference, final BundleContext context) { 317 if (System.getSecurityManager() == null) 318 return context.getService(reference); 319 return AccessController.doPrivileged(new PrivilegedAction() { 320 public Object run() { 321 return context.getService(reference); 322 } 323 }, controlContext); 324 } 325 326 333 public Class forName(final String name) throws ClassNotFoundException { 334 if (System.getSecurityManager() == null) 335 return Class.forName(name); 336 try { 337 return (Class ) AccessController.doPrivileged(new PrivilegedExceptionAction() { 338 public Object run() throws Exception { 339 return Class.forName(name); 340 } 341 }, controlContext); 342 } catch (PrivilegedActionException e) { 343 if (e.getException() instanceof ClassNotFoundException ) 344 throw (ClassNotFoundException ) e.getException(); 345 throw (RuntimeException ) e.getException(); 346 } 347 } 348 349 356 public Class loadSystemClass(final String name) throws ClassNotFoundException { 357 if (System.getSecurityManager() == null) { 358 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); 359 return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : bootClassLoader.loadClass(name); 360 } 361 try { 362 return (Class ) AccessController.doPrivileged(new PrivilegedExceptionAction() { 363 public Object run() throws Exception { 364 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); 365 return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : bootClassLoader.loadClass(name); 366 } 367 }, controlContext); 368 } catch (PrivilegedActionException e) { 369 if (e.getException() instanceof ClassNotFoundException ) 370 throw (ClassNotFoundException ) e.getException(); 371 throw (RuntimeException ) e.getException(); 372 } 373 } 374 375 379 public void open(final ServiceTracker tracker) { 380 if (System.getSecurityManager() == null) { 381 tracker.open(); 382 return; 383 } 384 AccessController.doPrivileged(new PrivilegedAction() { 385 public Object run() { 386 tracker.open(); 387 return null; 388 } 389 }, controlContext); 390 } 391 392 398 public void start(final Bundle bundle, final int options) throws BundleException { 399 if (System.getSecurityManager() == null) { 400 bundle.start(options); 401 return; 402 } 403 try { 404 AccessController.doPrivileged(new PrivilegedExceptionAction() { 405 public Object run() throws BundleException { 406 bundle.start(options); 407 return null; 408 } 409 }, controlContext); 410 return; 411 } catch (PrivilegedActionException e) { 412 if (e.getException() instanceof BundleException) 413 throw (BundleException) e.getException(); 414 throw (RuntimeException ) e.getException(); 415 } 416 } 417 418 423 public void start(final Bundle bundle) throws BundleException { 424 start(bundle, 0); 425 } 426 } 427 | Popular Tags |