Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4474a5296904fbe4aa996cdc00dcad5283f214bf (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
170
171
172
173
/*******************************************************************************
 * Copyright (c) 2003, 2004 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.internal.core;

import java.lang.reflect.Constructor;
import java.security.Permission;
import java.security.PermissionCollection;
import org.eclipse.osgi.framework.debug.Debug;

/**
 * Holds permissions which are of an unknown type when a
 * policy file is read
 *
 */
final class UnresolvedPermission extends Permission {
	private static final long serialVersionUID = 3546358422783079475L;
	/**
	 * The type of permission this will be
	 */
	private String type;
	/**
	 * the action string
	 */
	private String actions;
	/**
	 * name of the permission
	 */
	private String name;

	private static Class[] constructorArgs;

	static {
		Class string = String.class;
		constructorArgs = new Class[] {string, string};
	}

	/**
	 * Constructs a new instance of this class with its
	 * type, name, and certificates set to the arguments
	 * by definition, actions are ignored
	 *
	 * @param 	type the type class of permission object
	 * @param	name the name of the permission
	 * @param	actions the actions
	 */
	UnresolvedPermission(String type, String name, String actions) {
		super(type);
		this.name = name;
		this.type = type;
		this.actions = actions;
	}

	/**
	 * Compares the argument to the receiver, and answers true
	 * if they represent the <em>same</em> object using a class
	 * specific comparison. In this case, the receiver and the
	 * object must have the same class, permission name,
	 * actions, and certificates
	 *
	 * @param		obj		the object to compare with this object
	 * @return		<code>true</code>
	 *					if the object is the same as this object
	 *				<code>false</code>
	 *					if it is different from this object
	 */
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof UnresolvedPermission)) {
			return false;
		}

		UnresolvedPermission perm = (UnresolvedPermission) obj;

		return type.equals(perm.type) && name.equals(perm.name) && actions.equals(perm.actions);
	}

	/**
	 * Indicates whether the argument permission is implied
	 * by the receiver. UnresolvedPermission objects imply
	 * nothing because nothing is known about them yet.
	 *
	 * @return		boolean always replies false
	 * @param		p java.security.Permission
	 *					the permission to check
	 */
	public boolean implies(Permission p) {
		return false;
	}

	/**
	 * Answers a new PermissionCollection for holding permissions
	 * of this class. Answer null if any permission collection can
	 * be used.
	 *
	 * @return		a new PermissionCollection or null
	 *
	 */
	public PermissionCollection newPermissionCollection() {
		return new UnresolvedPermissionCollection();
	}

	/**
	 * Answers the actions associated with the receiver.
	 * Since UnresolvedPermission objects have no actions, answer
	 * the empty string.
	 *
	 * @return		String
	 *					the actions associated with the receiver.
	 */
	public String getActions() {
		return ""; //$NON-NLS-1$
	}

	/**
	 * Answers an integer hash code for the receiver. Any two
	 * objects which answer <code>true</code> when passed to
	 * <code>equals</code> must answer the same value for this
	 * method.
	 *
	 * @return		int
	 *					the receiver's hash
	 *
	 */
	public int hashCode() {
		return toString().hashCode();
	}

	/**
	 * Answers a string containing a concise, human-readable
	 * description of the receiver.
	 *
	 * @return		String
	 *					a printable representation for the receiver.
	 */
	public String toString() {
		return "(unresolved " + type + " " + name + " " + actions + ")";  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}

	Permission resolve(Class clazz) {
		if (clazz.getName().equals(type)) {
			try {
				Constructor constructor = clazz.getConstructor(constructorArgs);

				Permission permission = (Permission) constructor.newInstance(new Object[] {name, actions});

				if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
					Debug.println("Resolved " + this); //$NON-NLS-1$
				}

				return permission;
			} catch (Exception e) {
				/* Ignore any error trying to resolve the permission */
				if (Debug.DEBUG && Debug.DEBUG_SECURITY) {
					Debug.println("Exception trying to resolve permission"); //$NON-NLS-1$
					Debug.printStackTrace(e);
				}
			}
		}

		return null;
	}
}

Back to the top