Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2010 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.*;
Utility class to execute common privileged code.

Since:
3.1
 
 public class SecureAction {
 	// make sure we use the correct controlContext;
 
 	// This ClassLoader is used in loadSystemClass if System.getClassLoader() returns null
 	static final ClassLoader bootClassLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
 		public Object 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 
 	 */
 		// save the control context to be used.
 		this. = 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:

 SecureAction secureAction = (SecureAction) AccessController.doPrivileged(SecureAction.createSecureAction());
 

Returns:
a privileged action object that can be used to construct a SecureAction object.
 
 	public static PrivilegedAction createSecureAction() {
 		return new PrivilegedAction() {
 			public Object run() {
 				return new SecureAction();
 			}
 		};
 	}

Returns a system property. Same as calling System.getProperty(String).

Parameters:
property the property key.
Returns:
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 (String) AccessController.doPrivileged(new PrivilegedAction() {
 			public Object run() {
 				return FrameworkProperties.getProperty(property);
 			}
 	}

Returns a system property. Same as calling System.getProperty(String,String).

Parameters:
property the property key.
def the default value if the property key does not exist.
Returns:
the value of the property or the def value if the property does not exist.
 
 	public String getProperty(final String propertyfinal String def) {
 		if (System.getSecurityManager() == null)
 			return FrameworkProperties.getProperty(propertydef);
 		return (String) AccessController.doPrivileged(new PrivilegedAction() {
 			public Object run() {
 				return FrameworkProperties.getProperty(propertydef);
 			}
 	}

Returns the system properties. Same as calling System.getProperties().

Returns:
the system properties.
		if (System.getSecurityManager() == null)
			return FrameworkProperties.getProperties();
		return (Properties) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return FrameworkProperties.getProperties();
			}
	}

Creates a FileInputStream from a File. Same as calling new FileInputStream(File).

Parameters:
file the File to craete a FileInputStream from.
Returns:
The FileInputStream.
Throws:
java.io.FileNotFoundException if the File does not exist.
		if (System.getSecurityManager() == null)
			return new FileInputStream(file);
		try {
			return (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
				public Object run() throws FileNotFoundException {
					return new FileInputStream(file);
				}
catch (PrivilegedActionException e) {
			if (e.getException() instanceof FileNotFoundException)
		}
	}

Creates a FileInputStream from a File. Same as calling new FileOutputStream(File,boolean).

Parameters:
file the File to create a FileOutputStream from.
append indicates if the OutputStream should append content.
Returns:
The FileOutputStream.
Throws:
java.io.FileNotFoundException if the File does not exist.
	public FileOutputStream getFileOutputStream(final File filefinal boolean appendthrows FileNotFoundException {
		if (System.getSecurityManager() == null)
			return new FileOutputStream(file.getAbsolutePath(), append);
		try {
			return (FileOutputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
				public Object run() throws FileNotFoundException {
					return new FileOutputStream(file.getAbsolutePath(), append);
				}
catch (PrivilegedActionException e) {
			if (e.getException() instanceof FileNotFoundException)
		}
	}

Returns the length of a file. Same as calling file.length().

Parameters:
file a file object
Returns:
the length of a file.
	public long length(final File file) {
		if (System.getSecurityManager() == null)
			return file.length();
		return ((Long) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return new Long(file.length());
			}
	}

Returns true if a file exists, otherwise false is returned. Same as calling file.exists().

Parameters:
file a file object
Returns:
true if a file exists, otherwise false
	public boolean exists(final File file) {
		if (System.getSecurityManager() == null)
			return file.exists();
		return ((Boolean) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return file.exists() ? . : .;
			}
	}

Returns true if a file is a directory, otherwise false is returned. Same as calling file.isDirectory().

Parameters:
file a file object
Returns:
true if a file is a directory, otherwise false
	public boolean isDirectory(final File file) {
		if (System.getSecurityManager() == null)
			return file.isDirectory();
		return ((Boolean) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return file.isDirectory() ? . : .;
			}
	}

Returns a file's last modified stamp. Same as calling file.lastModified().

Parameters:
file a file object
Returns:
a file's last modified stamp.
	public long lastModified(final File file) {
		if (System.getSecurityManager() == null)
			return file.lastModified();
		return ((Long) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return new Long(file.lastModified());
			}
	}

Returns a file's list. Same as calling file.list().

Parameters:
file a file object
Returns:
a file's list.
	public String[] list(final File file) {
		if (System.getSecurityManager() == null)
			return file.list();
		return (String[]) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return file.list();
			}
	}

Returns a ZipFile. Same as calling new ZipFile(file)

Parameters:
file the file to get a ZipFile for
Returns:
a ZipFile
Throws:
java.io.IOException if an error occured
	public ZipFile getZipFile(final File filethrows IOException {
		try {
			if (System.getSecurityManager() == null)
				return new ZipFile(file);
			try {
				return (ZipFile) AccessController.doPrivileged(new PrivilegedExceptionAction() {
					public Object run() throws IOException {
						return new ZipFile(file);
					}
catch (PrivilegedActionException e) {
				if (e.getException() instanceof IOException)
			}
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 java.net.URL.(java.lang.String,java.lang.String,int,java.lang.String,java.net.URLStreamHandler)

Parameters:
protocol the protocol
host the host
port the port
file the file
handler the URLStreamHandler
Returns:
a URL
Throws:
java.net.MalformedURLException
	public URL getURL(final String protocolfinal String hostfinal int portfinal String filefinal URLStreamHandler handlerthrows MalformedURLException {
		if (System.getSecurityManager() == null)
			return new URL(protocolhostportfilehandler);
		try {
			return (URL) AccessController.doPrivileged(new PrivilegedExceptionAction() {
				public Object run() throws MalformedURLException {
					return new URL(protocolhostportfilehandler);
				}
catch (PrivilegedActionException e) {
			if (e.getException() instanceof MalformedURLException)
		}
	}

Creates a new Thread from a Runnable. Same as calling new Thread(target,name).setContextClassLoader(contextLoader).

Parameters:
target the Runnable to create the Thread from.
name The name of the Thread.
contextLoader the context class loader for the thread
Returns:
The new Thread
	public Thread createThread(final Runnable targetfinal String namefinal ClassLoader contextLoader) {
		if (System.getSecurityManager() == null)
			return createThread0(targetnamecontextLoader);
		return (Thread) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return createThread0(targetnamecontextLoader);
			}
	}
	Thread createThread0(Runnable targetString nameClassLoader contextLoader) {
		Thread result = new Thread(targetname);
		if (contextLoader != null)
			result.setContextClassLoader(contextLoader);
		return result;
	}

Gets a service object. Same as calling context.getService(reference)

Parameters:
reference the ServiceReference
context the BundleContext
Returns:
a service object
	public Object getService(final ServiceReference referencefinal BundleContext context) {
		if (System.getSecurityManager() == null)
			return context.getService(reference);
		return AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return context.getService(reference);
			}
	}

Returns a Class. Same as calling Class.forName(name)

Parameters:
name the name of the class.
Returns:
a Class
Throws:
java.lang.ClassNotFoundException
	public Class forName(final String namethrows ClassNotFoundException {
		if (System.getSecurityManager() == null)
			return Class.forName(name);
		try {
			return (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
				public Object run() throws Exception {
					return Class.forName(name);
				}
catch (PrivilegedActionException e) {
			if (e.getException() instanceof ClassNotFoundException)
		}
	}

Returns a Class. Tries to load a class from the System ClassLoader or if that doesn't exist tries the boot ClassLoader

Parameters:
name the name of the class.
Returns:
a Class
Throws:
java.lang.ClassNotFoundException
	public Class loadSystemClass(final String namethrows ClassNotFoundException {
		if (System.getSecurityManager() == null) {
			ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
			return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : .loadClass(name);
		}
		try {
			return (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
				public Object run() throws Exception {
					ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
					return (systemClassLoader != null) ? systemClassLoader.loadClass(name) : .loadClass(name);
				}
catch (PrivilegedActionException e) {
			if (e.getException() instanceof ClassNotFoundException)
		}
	}

Opens a ServiceTracker. Same as calling tracker.open()

Parameters:
tracker the ServiceTracker to open.
	public void open(final ServiceTracker tracker) {
		if (System.getSecurityManager() == null) {
			tracker.open();
			return;
		}
		AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				tracker.open();
				return null;
			}
	}

Starts a bundle.

Parameters:
bundle the bundle to start
options the start options
Throws:
org.osgi.framework.BundleException
	public void start(final Bundle bundlefinal int optionsthrows BundleException {
		if (System.getSecurityManager() == null) {
			bundle.start(options);
			return;
		}
		try {
			AccessController.doPrivileged(new PrivilegedExceptionAction() {
				public Object run() throws BundleException {
					bundle.start(options);
					return null;
				}
			return;
catch (PrivilegedActionException e) {
			if (e.getException() instanceof BundleException)
		}
	}

Starts a bundle

Parameters:
bundle
Throws:
org.osgi.framework.BundleException
	public void start(final Bundle bundlethrows BundleException {
		start(bundle, 0);
	}
New to GrepCode? Check out our FAQ X