KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.PermissionCollection JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18
19 /**
20  * A simple Hashtable based collection of Permission objects.
21  * <p>
22  * The class' .implies method simply scans each permission
23  * individually and asks if the permission should be granted.
24  * No addition semantics is provided by the collection, so it is
25  * not possible to grant permissions whose "grantedness" is
26  * split across multiple stored Permissions.
27  * <p>
28  * Instances of this class can be used to store heterogeneous
29  * collections of permissions, as long as it is not necessary
30  * to remember when multiple occurances of .equal permissions
31  * are added.
32  *
33  */

34 class PermissionsHash extends PermissionCollection JavaDoc {
35     private static final long serialVersionUID = 3258408426341284153L;
36     /**
37      * A hashtable to store the elements of the collection.
38      */

39     Hashtable JavaDoc perms = new Hashtable JavaDoc(8);
40
41     /**
42      * Constructs a new instance of this class.
43      *
44      */

45     public PermissionsHash() {
46         super();
47     }
48
49     /**
50      * Adds the argument to the collection.
51      *
52      * @param perm java.security.Permission
53      * the permission to add to the collection.
54      * @exception IllegalStateException
55      * if the collection is read only.
56      */

57     public void add(Permission JavaDoc perm) {
58         if (isReadOnly()) {
59             throw new SecurityException JavaDoc();
60         }
61
62         perms.put(perm, perm);
63     }
64
65     /**
66      * Answers an enumeration of the permissions
67      * in the receiver.
68      *
69      * @return Enumeration
70      * the permissions in the receiver.
71      */

72     public Enumeration JavaDoc elements() {
73         return perms.keys();
74     }
75
76     /**
77      * Indicates whether the argument permission is implied
78      * by the permissions contained in the receiver.
79      *
80      * @return boolean
81      * <code>true</code> if the argument permission
82      * is implied by the permissions in the receiver,
83      * and <code>false</code> if it is not.
84      * @param perm java.security.Permission
85      * the permission to check
86      */

87     public boolean implies(Permission JavaDoc perm) {
88         Permission JavaDoc p = (Permission JavaDoc) perms.get(perm);
89
90         if ((p != null) && p.implies(perm)) {
91             return true;
92         }
93
94         Enumeration JavaDoc permsEnum = elements();
95
96         while (permsEnum.hasMoreElements()) {
97             if (((Permission JavaDoc) permsEnum.nextElement()).implies(perm)) {
98                 return true;
99             }
100         }
101
102         return false;
103     }
104 }
105
Popular Tags