1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/*******************************************************************************
* Copyright (c) 2003, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.framework.util;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.Properties;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;
/**
* Utility class to execute common privileged code.
* @since 3.1
*/
public class SecureAction {
// make sure we use the correct controlContext;
private AccessControlContext controlContext;
// This ClassLoader is used in loadSystemClass if System.getClassLoader() returns null
static final ClassLoader bootClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return new ClassLoader(Object.class.getClassLoader()) { /* boot class loader */};
}
});
/*
* Package privaet constructor a new SecureAction object.
* The constructed SecureAction object uses the caller's AccessControlContext
* to perform security checks
*/
SecureAction() {
// save the control context to be used.
this.controlContext = AccessController.getContext();
}
/**
* Creates a privileged action that can be used to construct a SecureAction object.
* The recommended way to construct a SecureAction object is the following: <p>
* <pre>
* SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction());
* </pre>
* @return a privileged action object that can be used to construct a SecureAction object.
*/
public static PrivilegedAction<SecureAction> createSecureAction() {
return new PrivilegedAction<SecureAction>() {
public SecureAction run() {
return new SecureAction();
}
};
}
/**
* Returns a system property. Same as calling
* System.getProperty(String).
* @param property the property key.
* @return the value of the property or null if it does not exist.
*/
public String getProperty(final String property) {
if (System.getSecurityManager() == null)
return FrameworkProperties.getProperty(property);
return AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return FrameworkProperties.getProperty(property);
}
}, controlContext);
}
/**
* Returns a system property. Same as calling
* System.getProperty(String,String).
* @param property the property key.
* @param def the default value if the property key does not exist.
* @return the value of the property or the def value if the property
* does not exist.
*/
public String getProperty(final String property, final String def) {
if (System.getSecurityManager() == null)
return FrameworkProperties.getProperty(property, def);
return AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return FrameworkProperties.getProperty(property, def);
}
}, controlContext);
}
/**
* Returns the system properties. Same as calling
* System.getProperties().
* @return the system properties.
*/
public Properties getProperties() {
if (System.getSecurityManager() == null)
return FrameworkProperties.getProperties();
return AccessController.doPrivileged(new PrivilegedAction<Properties>() {
public Properties run() {
return FrameworkProperties.getProperties();
}
}, controlContext);
}
/**
* Creates a FileInputStream from a File. Same as calling
* new FileInputStream(File).
* @param file the File to craete a FileInputStream from.
* @return The FileInputStream.
* @throws FileNotFoundException if the File does not exist.
*/
public FileInputStream getFileInputStream(final File file) throws FileNotFoundException {
if (System.getSecurityManager() == null)
return new FileInputStream(file);
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<FileInputStream>() {
public FileInputStream run() throws FileNotFoundException {
return new FileInputStream(file);
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof FileNotFoundException)
throw (FileNotFoundException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Creates a FileInputStream from a File. Same as calling
* new FileOutputStream(File,boolean).
* @param file the File to create a FileOutputStream from.
* @param append indicates if the OutputStream should append content.
* @return The FileOutputStream.
* @throws FileNotFoundException if the File does not exist.
*/
public FileOutputStream getFileOutputStream(final File file, final boolean append) throws FileNotFoundException {
if (System.getSecurityManager() == null)
return new FileOutputStream(file.getAbsolutePath(), append);
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<FileOutputStream>() {
public FileOutputStream run() throws FileNotFoundException {
return new FileOutputStream(file.getAbsolutePath(), append);
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof FileNotFoundException)
throw (FileNotFoundException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Returns the length of a file. Same as calling
* file.length().
* @param file a file object
* @return the length of a file.
*/
public long length(final File file) {
if (System.getSecurityManager() == null)
return file.length();
return AccessController.doPrivileged(new PrivilegedAction<Long>() {
public Long run() {
return new Long(file.length());
}
}, controlContext).longValue();
}
/**
* Returns the canonical path of a file. Same as calling
* file.getCanonicalPath().
* @param file a file object
* @return the canonical path of a file.
* @throws IOException on error
*/
public String getCanonicalPath(final File file) throws IOException {
if (System.getSecurityManager() == null)
return file.getCanonicalPath();
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
public String run() throws IOException {
return file.getCanonicalPath();
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof IOException)
throw (IOException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Returns the absolute file. Same as calling
* file.getAbsoluteFile().
* @param file a file object
* @return the absolute file.
*/
public File getAbsoluteFile(final File file) {
if (System.getSecurityManager() == null)
return file.getAbsoluteFile();
return AccessController.doPrivileged(new PrivilegedAction<File>() {
public File run() {
return file.getAbsoluteFile();
}
}, controlContext);
}
/**
* Returns the canonical file. Same as calling
* file.getCanonicalFile().
* @param file a file object
* @return the canonical file.
*/
public File getCanonicalFile(final File file) throws IOException {
if (System.getSecurityManager() == null)
return file.getCanonicalFile();
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {
public File run() throws IOException {
return file.getCanonicalFile();
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof IOException)
throw (IOException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Returns true if a file exists, otherwise false is returned. Same as calling
* file.exists().
* @param file a file object
* @return true if a file exists, otherwise false
*/
public boolean exists(final File file) {
if (System.getSecurityManager() == null)
return file.exists();
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
return file.exists() ? Boolean.TRUE : Boolean.FALSE;
}
}, controlContext).booleanValue();
}
/**
* Returns true if a file is a directory, otherwise false is returned. Same as calling
* file.isDirectory().
* @param file a file object
* @return true if a file is a directory, otherwise false
*/
public boolean isDirectory(final File file) {
if (System.getSecurityManager() == null)
return file.isDirectory();
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
return file.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
}
}, controlContext).booleanValue();
}
/**
* Returns a file's last modified stamp. Same as calling
* file.lastModified().
* @param file a file object
* @return a file's last modified stamp.
*/
public long lastModified(final File file) {
if (System.getSecurityManager() == null)
return file.lastModified();
return AccessController.doPrivileged(new PrivilegedAction<Long>() {
public Long run() {
return new Long(file.lastModified());
}
}, controlContext).longValue();
}
/**
* Returns a file's list. Same as calling
* file.list().
* @param file a file object
* @return a file's list.
*/
public String[] list(final File file) {
if (System.getSecurityManager() == null)
return file.list();
return AccessController.doPrivileged(new PrivilegedAction<String[]>() {
public String[] run() {
return file.list();
}
}, controlContext);
}
/**
* Returns a ZipFile. Same as calling
* new ZipFile(file)
* @param file the file to get a ZipFile for
* @return a ZipFile
* @throws IOException if an error occured
*/
public ZipFile getZipFile(final File file) throws IOException {
try {
if (System.getSecurityManager() == null)
return new ZipFile(file);
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<ZipFile>() {
public ZipFile run() throws IOException {
return new ZipFile(file);
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof IOException)
throw (IOException) e.getException();
throw (RuntimeException) e.getException();
}
} catch (ZipException e) {
ZipException zipNameException = new ZipException("Exception in opening zip file: " + file.getPath()); //$NON-NLS-1$
zipNameException.initCause(e);
throw zipNameException;
} catch (IOException e) {
IOException fileNameException = new IOException("Exception in opening zip file: " + file.getPath()); //$NON-NLS-1$
fileNameException.initCause(e);
throw fileNameException;
}
}
/**
* Gets a URL. Same a calling
* {@link URL#URL(java.lang.String, java.lang.String, int, java.lang.String, java.net.URLStreamHandler)}
* @param protocol the protocol
* @param host the host
* @param port the port
* @param file the file
* @param handler the URLStreamHandler
* @return a URL
* @throws MalformedURLException
*/
public URL getURL(final String protocol, final String host, final int port, final String file, final URLStreamHandler handler) throws MalformedURLException {
if (System.getSecurityManager() == null)
return new URL(protocol, host, port, file, handler);
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<URL>() {
public URL run() throws MalformedURLException {
return new URL(protocol, host, port, file, handler);
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof MalformedURLException)
throw (MalformedURLException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Creates a new Thread from a Runnable. Same as calling
* new Thread(target,name).setContextClassLoader(contextLoader).
* @param target the Runnable to create the Thread from.
* @param name The name of the Thread.
* @param contextLoader the context class loader for the thread
* @return The new Thread
*/
public Thread createThread(final Runnable target, final String name, final ClassLoader contextLoader) {
if (System.getSecurityManager() == null)
return createThread0(target, name, contextLoader);
return AccessController.doPrivileged(new PrivilegedAction<Thread>() {
public Thread run() {
return createThread0(target, name, contextLoader);
}
}, controlContext);
}
Thread createThread0(Runnable target, String name, ClassLoader contextLoader) {
Thread result = new Thread(target, name);
if (contextLoader != null)
result.setContextClassLoader(contextLoader);
return result;
}
/**
* Gets a service object. Same as calling
* context.getService(reference)
* @param reference the ServiceReference
* @param context the BundleContext
* @return a service object
*/
public <S> S getService(final ServiceReference<S> reference, final BundleContext context) {
if (System.getSecurityManager() == null)
return context.getService(reference);
return AccessController.doPrivileged(new PrivilegedAction<S>() {
public S run() {
return context.getService(reference);
}
}, controlContext);
}
/**
* Returns a Class. Same as calling
* Class.forName(name)
* @param name the name of the class.
* @return a Class
* @throws ClassNotFoundException
*/
public Class<?> forName(final String name) throws ClassNotFoundException {
if (System.getSecurityManager() == null)
return Class.forName(name);
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
public Class<?> run() throws Exception {
return Class.forName(name);
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof ClassNotFoundException)
throw (ClassNotFoundException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Returns a Class.
* Tries to load a class from the System ClassLoader or if that doesn't exist tries the boot ClassLoader
* @param name the name of the class.
* @return a Class
* @throws ClassNotFoundException
*/
public Class<?> loadSystemClass(final String name) throws ClassNotFoundException {
if (System.getSecurityManager() == null) {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : bootClassLoader.loadClass(name);
}
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
public Class<?> run() throws Exception {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : bootClassLoader.loadClass(name);
}
}, controlContext);
} catch (PrivilegedActionException e) {
if (e.getException() instanceof ClassNotFoundException)
throw (ClassNotFoundException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Opens a ServiceTracker. Same as calling tracker.open()
* @param tracker the ServiceTracker to open.
*/
public void open(final ServiceTracker<?, ?> tracker) {
if (System.getSecurityManager() == null) {
tracker.open();
return;
}
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
tracker.open();
return null;
}
}, controlContext);
}
/**
* Starts a bundle.
* @param bundle the bundle to start
* @param options the start options
* @throws BundleException
*/
public void start(final Bundle bundle, final int options) throws BundleException {
if (System.getSecurityManager() == null) {
bundle.start(options);
return;
}
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws BundleException {
bundle.start(options);
return null;
}
}, controlContext);
return;
} catch (PrivilegedActionException e) {
if (e.getException() instanceof BundleException)
throw (BundleException) e.getException();
throw (RuntimeException) e.getException();
}
}
/**
* Starts a bundle
* @param bundle
* @throws BundleException
*/
public void start(final Bundle bundle) throws BundleException {
start(bundle, 0);
}
}
|