Permissions.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Permissions.java -- a collection of permission collections
  2. Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. import java.io.Serializable;
  33. import java.util.Hashtable;
  34. import java.util.Enumeration;
  35. import java.util.NoSuchElementException;
  36. /**
  37. * This class is a heterogeneous collection of permissions. It is
  38. * organized as a collection of <code>PermissionCollection</code>'s stored
  39. * in a hashtable. Each individual <code>PermissionCollection</code>
  40. * contains permissions of a single type. If a specific type of
  41. * <code>Permission</code> does not provide a collection type to use
  42. * via its <code>newPermissionCollection</code> method, then a default
  43. * collection type which stores its permissions in a hash table will be
  44. * used.
  45. *
  46. * @author Aaron M. Renn <arenn@urbanophile.com>
  47. * @author Eric Blake <ebb9@email.byu.edu>
  48. * @since 1.1
  49. */
  50. public final class Permissions extends PermissionCollection
  51. implements Serializable
  52. {
  53. /**
  54. * Compatible with JDK 1.1+.
  55. */
  56. private static final long serialVersionUID = 4858622370623524688L;
  57. /**
  58. * Holds instances of <code>AllPermission</code>.
  59. *
  60. * @serial the permission collection for AllPermission
  61. */
  62. private PermissionCollection allPermission;
  63. /**
  64. * This is the <code>Hashtable</code> that contains our collections.
  65. *
  66. * @serial maps Class to PermissionCollection
  67. */
  68. private final Hashtable perms = new Hashtable();
  69. /**
  70. * This method initializes a new instance of <code>Permissions</code>.
  71. */
  72. public Permissions()
  73. {
  74. }
  75. /**
  76. * This method adds a new <code>Permission</code> to this collection. It
  77. * will be stored in a <code>PermissionCollection</code> of the appropriate
  78. * type, as determined by calling <code>newPermissionCollection</code> on
  79. * the specified permission (if an appropriate collection does not already
  80. * exist). If this object does not specify a particular type of collection,
  81. * a default collection, which stores in permissions in a hash table, will
  82. * be used.
  83. *
  84. * @param perm the <code>Permission</code> to add
  85. * @throws SecurityException if this collection is marked as read only
  86. */
  87. public void add(Permission perm)
  88. {
  89. if (isReadOnly())
  90. throw new SecurityException("PermissionCollection is read only");
  91. if (perm instanceof AllPermission)
  92. {
  93. if (allPermission == null)
  94. {
  95. allPermission = perm.newPermissionCollection();
  96. allPermission.add(perm);
  97. perms.put(perm.getClass(), allPermission);
  98. }
  99. }
  100. else
  101. {
  102. PermissionCollection pc
  103. = (PermissionCollection) perms.get(perm.getClass());
  104. if (pc == null)
  105. {
  106. pc = perm.newPermissionCollection();
  107. if (pc == null)
  108. pc = new PermissionsHash();
  109. perms.put(perm.getClass(), pc);
  110. }
  111. pc.add(perm);
  112. }
  113. }
  114. /**
  115. * This method tests whether or not the specified <code>Permission</code>
  116. * is implied by this <code>PermissionCollection</code>.
  117. *
  118. * @param perm the <code>Permission</code> to test
  119. * @return true if the specified permission is implied by this
  120. */
  121. public boolean implies(Permission perm)
  122. {
  123. if (allPermission != null)
  124. return true;
  125. PermissionCollection pc
  126. = (PermissionCollection) perms.get(perm.getClass());
  127. return pc == null ? false : pc.implies(perm);
  128. }
  129. /**
  130. * This method returns an <code>Enumeration</code> which contains a
  131. * list of all <code>Permission</code> objects contained in this
  132. * collection.
  133. *
  134. * @return an <code>Enumeration</code> of this collection's elements
  135. */
  136. public Enumeration elements()
  137. {
  138. return new Enumeration()
  139. {
  140. Enumeration main_enum = perms.elements();
  141. Enumeration sub_enum;
  142. public boolean hasMoreElements()
  143. {
  144. if (sub_enum == null)
  145. {
  146. if (main_enum == null)
  147. return false;
  148. if (! main_enum.hasMoreElements())
  149. {
  150. main_enum = null;
  151. return false;
  152. }
  153. PermissionCollection pc =
  154. (PermissionCollection) main_enum.nextElement();
  155. sub_enum = pc.elements();
  156. }
  157. if (! sub_enum.hasMoreElements())
  158. {
  159. sub_enum = null;
  160. return hasMoreElements();
  161. }
  162. return true;
  163. }
  164. public Object nextElement()
  165. {
  166. if (! hasMoreElements())
  167. throw new NoSuchElementException();
  168. return sub_enum.nextElement();
  169. }
  170. };
  171. }
  172. } // class Permissions
  173. /**
  174. * Implements the permission collection for all permissions without one of
  175. * their own, and obeys serialization of JDK.
  176. *
  177. * @author Eric Blake <ebb9@email.byu.edu>
  178. */
  179. class PermissionsHash extends PermissionCollection
  180. {
  181. /**
  182. * Compatible with JDK 1.1+.
  183. */
  184. private static final long serialVersionUID = -8491988220802933440L;
  185. /**
  186. * Hashtable where we store permissions.
  187. *
  188. * @serial the stored permissions, both as key and value
  189. */
  190. private final Hashtable perms = new Hashtable();
  191. /**
  192. * Add a permission. We don't need to check for read-only, as this
  193. * collection is never exposed outside of Permissions, which has already
  194. * done that check.
  195. *
  196. * @param perm the permission to add
  197. */
  198. public void add(Permission perm)
  199. {
  200. perms.put(perm, perm);
  201. }
  202. /**
  203. * Returns true if perm is in the collection.
  204. *
  205. * @param perm the permission to check
  206. * @return true if it is implied
  207. */
  208. public boolean implies(Permission perm)
  209. {
  210. return perms.get(perm) != null;
  211. }
  212. /**
  213. * Return the elements.
  214. *
  215. * @return the elements
  216. */
  217. public Enumeration elements()
  218. {
  219. return perms.elements();
  220. }
  221. } // class Permissions