KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > BundleCombinedPermissions


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.core;
13
14 import java.security.Permission JavaDoc;
15 import java.util.*;
16
17 /**
18  * A combination of two BundlePermissionCollection classes.
19  *
20  */

21 final class BundleCombinedPermissions extends BundlePermissionCollection {
22     private static final long serialVersionUID = 4049357526208360496L;
23     // Note that this forces the Enumeration inner class to be loaded as soon as possible (see bug 119069)
24
private static final Enumeration EMPTY_ENUMERATION = new Enumeration() {
25         public boolean hasMoreElements() {
26             return false;
27         }
28         public Object JavaDoc nextElement() {
29             throw new NoSuchElementException();
30         }
31     };
32     private BundlePermissionCollection assigned;
33     private BundlePermissionCollection implied;
34     private ConditionalPermissions conditional;
35     private ConditionalPermissionSet restrictedPermissions;
36     private boolean isDefault;
37
38     /**
39      * Create a permission combiner class.
40      *
41      * @param implied The permissions a bundle always has.
42      */

43     BundleCombinedPermissions(BundlePermissionCollection implied) {
44         this.implied = implied;
45
46         setReadOnly(); /* this doesn't really mean anything */
47     }
48
49     /**
50      * Assign the administrator defined permissions.
51      *
52      * @param assigned The permissions assigned by the administrator.
53      * @param isDefault If true, the assigned permissions are the default permissions.
54      */

55     void setAssignedPermissions(BundlePermissionCollection assigned, boolean isDefault) {
56         this.assigned = assigned;
57         this.isDefault = isDefault;
58     }
59
60     /**
61      * Assign the conditional permissions
62      *
63      * @param conditional The conditional permissions assigned by the administrator
64      */

65     void setConditionalPermissions(ConditionalPermissions conditional) {
66         this.conditional = conditional;
67     }
68
69     void checkConditionalPermissionInfo(ConditionalPermissionInfoImpl cpi) {
70         if (conditional != null) {
71             conditional.checkConditionalPermissionInfo(cpi);
72         }
73     }
74
75     void unresolvePermissions() {
76         if (assigned != null)
77             assigned.unresolvePermissions();
78         if (implied != null)
79             implied.unresolvePermissions();
80         if (conditional != null)
81             conditional.unresolvePermissions();
82         if (restrictedPermissions != null)
83             restrictedPermissions.unresolvePermissions();
84     }
85
86     /**
87      * Adds the argument to the collection.
88      *
89      * @param permission java.security.Permission
90      * the permission to add to the collection.
91      * @exception SecurityException
92      * if the collection is read only.
93      */

94     public void add(Permission JavaDoc permission) {
95         throw new SecurityException JavaDoc();
96     }
97
98     /**
99      * Answers an enumeration of the permissions
100      * in the receiver.
101      *
102      * @return Enumeration
103      * the permissions in the receiver.
104      */

105     public Enumeration elements() {
106         // TODO return an empty enumeration for now;
107
// It does not seem possible to do this properly with multiple exports and conditional permissions.
108
// When looking to fix this be sure the Enumeration class is loaded as soon as possible (see bug 119069)
109
return EMPTY_ENUMERATION;
110     }
111
112     /**
113      * Indicates whether the argument permission is implied
114      * by the permissions contained in the receiver.
115      *
116      * @return boolean
117      * <code>true</code> if the argument permission
118      * is implied by the permissions in the receiver,
119      * and <code>false</code> if it is not.
120      * @param permission java.security.Permission
121      * the permission to check
122      */

123     public boolean implies(Permission JavaDoc permission) {
124         if ((implied != null) && implied.implies(permission))
125             return true;
126
127         /* We must be allowed by the restricted permissions to have any hope of
128          * passing the check */

129         if ((restrictedPermissions != null) && !restrictedPermissions.implies(permission)) {
130             return false;
131         }
132
133         /* If we aren't using the default permissions, then the assigned
134          * permission are the exact permissions the bundle has. */

135         if (!isDefault && assigned != null)
136             return assigned.implies(permission);
137         if (conditional != null) {
138             boolean conditionalImplies = conditional.implies(permission);
139             if (!conditional.isEmpty()) {
140                 return conditionalImplies;
141             }
142         }
143         /* If there aren't any conditional permissions that apply, we use
144          * the default. */

145         return assigned.implies(permission);
146     }
147
148     /**
149      * Sets the restricted Permissions of the Bundle. This set of Permissions limit the
150      * Permissions available to the Bundle.
151      *
152      * @param restrictedPermissions the maximum set of permissions allowed to the Bundle
153      * irrespective of the actual permissions assigned to it.
154      */

155     public void setRestrictedPermissions(ConditionalPermissionSet restrictedPermissions) {
156         this.restrictedPermissions = restrictedPermissions;
157     }
158 }
159
Popular Tags