Skip to main content
summaryrefslogtreecommitdiffstats
blob: 93a536db0c310644708e77f6b80047716b51b045 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2003, 2009 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.equinox.p2.testserver;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import org.osgi.framework.Bundle;

/**
 * Utility class to execute common privileged code.
 */
public class SecureAction {
	// make sure we use the correct controlContext;
	private AccessControlContext controlContext;

	/**
	 * Constructs a new SecureAction object.  The constructed SecureAction object 
	 * uses the caller's AccessControlContext to perform security checks 
	 */
	public SecureAction() {
		// save the control context to be used.
		this.controlContext = AccessController.getContext();
	}

	/**
	 * Gets a resource from a bundle.
	 * @param bundle the bundle to get the resource from
	 * @param name The name of the resource
	 * @return The URL of the resource
	 */
	
	public URL getBundleResource(final Bundle bundle, final String name) {
		if (System.getSecurityManager() == null)
		     return bundle.getResource(name);
		return (URL) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return bundle.getResource(name);
			}
		}, controlContext);
	}
	
	public URLConnection openURL(final URL url) throws IOException {
		if (System.getSecurityManager() == null)
		    return open(url);
		try {	
			return (URLConnection) AccessController.doPrivileged(new PrivilegedExceptionAction() {
				public Object run() throws IOException {
					return open(url);
				}
			}, controlContext);  
		} catch(PrivilegedActionException ex) {
			throw (IOException) ex.getException();
		}
	}  
	
	URLConnection open(final URL url)throws IOException
	{
		URLConnection connection = url.openConnection();
        connection.connect(); /* establish connection to check permissions */
        return connection;
	}
	
	/**
	 * 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 System.getProperty(property, def);
		return (String) AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				return System.getProperty(property, def);
			}
		}, controlContext);
	}

}

Back to the top