KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > crypto > CryptoAllPermission


1 /*
2  * @(#)CryptoAllPermission.java 1.7 04/01/06
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.crypto;
9
10 import java.security.*;
11 import java.util.Enumeration;
12 import java.util.Vector;
13
14 /**
15  * The CryptoAllPermission is a permission that implies
16  * any other crypto permissions.
17  * <p>
18  *
19  * @see java.security.Permission
20  * @see java.security.AllPermission
21  *
22  * @version 1.7 01/06/04
23  * @author Sharon Liu
24  * @since 1.4
25  */

26
27 final class CryptoAllPermission extends CryptoPermission {
28
29     private static final long serialVersionUID = -5066513634293192112L;
30
31     // This class is similar to java.security.AllPermission.
32
static final String ALG_NAME = "CryptoAllPermission";
33     static final CryptoAllPermission INSTANCE =
34     new CryptoAllPermission();
35
36     private CryptoAllPermission() {
37         super(ALG_NAME);
38     }
39
40     /**
41      * Checks if the specified permission is implied by
42      * this object.
43      *
44      * @param p the permission to check against.
45      *
46      * @return true if the specified permission is an
47      * instance of CryptoPermission.
48      */

49     public boolean implies(Permission p) {
50      return (p instanceof CryptoPermission);
51     }
52
53     /**
54      * Checks two CryptoAllPermission objects for equality.
55      * Two CryptoAllPermission objects are always equal.
56      *
57      * @param obj the object to test for equality with this object.
58      *
59      * @return true if <i>obj</i> is a CryptoAllPermission object.
60      */

61     public boolean equals(Object obj) {
62     return (obj == INSTANCE);
63     }
64
65     /**
66      *
67      * Returns the hash code value for this object.
68      *
69      * @return a hash code value for this object.
70      */

71     public int hashCode() {
72     return 1;
73     }
74
75     /**
76      * Returns a new PermissionCollection object for storing
77      * CryptoAllPermission objects.
78      * <p>
79      *
80      * @return a new PermissionCollection object suitable for
81      * storing CryptoAllPermissions.
82      */

83     public PermissionCollection newPermissionCollection() {
84     return new CryptoAllPermissionCollection();
85     }
86 }
87
88 /**
89  * A CryptoAllPermissionCollection stores a collection
90  * of CryptoAllPermission permissions.
91  *
92  * @see java.security.Permission
93  * @see java.security.Permissions
94  * @see javax.crypto.CryptoPermission
95  *
96  * @version 1.7 01/06/04
97  *
98  * @author Sharon Liu
99  */

100 final class CryptoAllPermissionCollection extends PermissionCollection
101     implements java.io.Serializable
102 {
103
104     private static final long serialVersionUID = 7450076868380144072L;
105
106     // true if a CryptoAllPermission has been added
107
private boolean all_allowed;
108
109     /**
110      * Create an empty CryptoAllPermissions object.
111      */

112     CryptoAllPermissionCollection() {
113     all_allowed = false;
114     }
115
116     /**
117      * Adds a permission to the CryptoAllPermissions.
118      *
119      * @param permission the Permission object to add.
120      *
121      * @exception SecurityException - if this CryptoAllPermissionCollection
122      * object has been marked readonly
123      */

124     public void add(Permission permission)
125     {
126     if (isReadOnly())
127         throw new SecurityException("attempt to add a Permission to " +
128                     "a readonly PermissionCollection");
129
130         if (permission != CryptoAllPermission.INSTANCE)
131             return;
132
133     all_allowed = true;
134     }
135
136     /**
137      * Check and see if this set of permissions implies the permissions
138      * expressed in "permission".
139      *
140      * @param p the Permission object to compare
141      *
142      * @return true if the given permission is implied by this
143      * CryptoAllPermissionCollection.
144      */

145     public boolean implies(Permission permission)
146     {
147     if (!(permission instanceof CryptoPermission)) {
148         return false;
149     }
150     return all_allowed;
151     }
152
153     /**
154      * Returns an enumeration of all the CryptoAllPermission
155      * objects in the container.
156      *
157      * @return an enumeration of all the CryptoAllPermission objects.
158      */

159     public Enumeration elements() {
160     Vector v = new Vector(1);
161     if (all_allowed) v.add(CryptoAllPermission.INSTANCE);
162     return v.elements();
163     }
164 }
165
166
Popular Tags