Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 352290757036a49ed124d69fe40c10a8d3345c8b (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
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
/*******************************************************************************
 * Copyright (c) 1997, 2018 by ProSyst Software GmbH
 * http://www.prosyst.com
 * 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:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.util.security;

import java.security.*;
import org.eclipse.equinox.internal.util.pool.ObjectCreator;
import org.eclipse.equinox.internal.util.pool.ObjectPool;

/**
 * A simple wrapper for executing privileged actions.
 * 
 * @author Valentin Valchev
 * @author Pavlin Dobrev
 * @version 1.0
 */

public final class PrivilegedRunner implements ObjectCreator {

	private static ObjectPool POOL;

	static {
		try {
			POOL = new ObjectPool(new PrivilegedRunner(), 5, 10);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* prevent instantiations */
	private PrivilegedRunner() {
	}

	/**
	 * Same as the longer doPrivileged method, but fills in the first parameter
	 * only. All other parameters are set to <code>null</code>.
	 * 
	 * @param context
	 *            the access context
	 * @param dispatcher
	 *            the dispatcher which should be called
	 * @param type
	 *            the type of the action - used in the dispatcher
	 * @param arg1
	 *            a parameter received by the dispatcher
	 * @see #doPrivileged(Object, PrivilegedDispatcher, int, Object)
	 * @return the object returned from the execution
	 * @throws Exception
	 *             if the dispatcher fails
	 */
	public static final Object doPrivileged(Object context, PrivilegedDispatcher dispatcher, int type, Object arg1) throws Exception {
		return doPrivileged(context, dispatcher, type, arg1, null, null, null);
	}

	/**
	 * Performs a privileged action. The method calls the dispatcher inside the
	 * privileged call passing it the same parameters that were passed to this
	 * method.
	 * 
	 * @param context
	 *            the access context
	 * @param dispatcher
	 *            the dispatcher which should be called
	 * @param type
	 *            the type of the action - used in the dispatcher
	 * @param arg1
	 *            a parameter received by the dispatcher
	 * @param arg2
	 *            a parameter received by the dispatcher
	 * @param arg3
	 *            a parameter received by the dispatcher
	 * @param arg4
	 *            a parameter received by the dispatcher
	 * @return the object returned from the execution
	 * @throws Exception
	 *             if the dispatcher fails
	 */
	public static final Object doPrivileged(Object context, PrivilegedDispatcher dispatcher, int type, Object arg1, Object arg2, Object arg3, Object arg4) throws Exception {
		/* init runner */
		PA runner = (PA) POOL.getObject();
		runner.dispatcher = dispatcher;
		runner.type = type;
		runner.arg1 = arg1;
		runner.arg2 = arg2;
		runner.arg3 = arg3;
		runner.arg4 = arg4;

		try {
			if (System.getSecurityManager() != null) {
				/*
				 * if security manager is set - then privileged execution is
				 * started
				 */
				if (context != null) {
					return AccessController.doPrivileged(runner, (AccessControlContext) context);
				}
				return AccessController.doPrivileged(runner);
			}
			/* if no security manager is set - simply run the action */
			return runner.run();
		} catch (PrivilegedActionException e) {
			throw e.getException();
		} finally {
			runner.recycle();
			POOL.releaseObject(runner);
		}
	}

	@Override
	public Object getInstance() throws Exception {
		return new PA();
	}

	/**
	 * This dispatcher is the handler that is called within the privileged call.
	 * It should dispatch and perform the requested actions depending on the
	 * action type and using the given job parameters.
	 * 
	 * @author Valentin Valchev
	 * @version $Revision: 1.1 $
	 */
	public static interface PrivilegedDispatcher {

		/**
		 * @param type
		 *            the type of the action
		 * @param arg1
		 *            parameter 1 - depends on the action type
		 * @param arg2
		 *            parameter 2 - depends on the action type
		 * @param arg3
		 *            parameter 3 - depends on the action type
		 * @param arg4
		 *            parameter 4 - depends on the action type
		 * @return an object which should be returned from the
		 *         PrivilegedAction.run() method
		 * @throws Exception
		 *             on error
		 */
		Object dispatchPrivileged(int type, Object arg1, Object arg2, Object arg3, Object arg4) throws Exception;
	}

	static class PA implements PrivilegedExceptionAction<Object> {

		int type;
		Object arg1, arg2, arg3, arg4;
		PrivilegedDispatcher dispatcher;

		void recycle() {
			dispatcher = null;
			type = -1;
			arg1 = arg2 = arg3 = arg4 = null;
		}

		@Override
		public Object run() throws Exception {
			return dispatcher.dispatchPrivileged(type, arg1, arg2, arg3, arg4);
		}
	}

}

Back to the top