Permissions.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Permissions.java -- a collection of permission collections
  2. Copyright (C) 1998, 2001, 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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.Enumeration;
  34. import java.util.Hashtable;
  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. // Package-private to avoid a trampoline.
  64. /**
  65. * This is the <code>Hashtable</code> that contains our collections.
  66. *
  67. * @serial maps Class to PermissionCollection
  68. */
  69. final Hashtable perms = new Hashtable();
  70. /**
  71. * This method initializes a new instance of <code>Permissions</code>.
  72. */
  73. public Permissions()
  74. {
  75. }
  76. /**
  77. * This method adds a new <code>Permission</code> to this collection. It
  78. * will be stored in a <code>PermissionCollection</code> of the appropriate
  79. * type, as determined by calling <code>newPermissionCollection</code> on
  80. * the specified permission (if an appropriate collection does not already
  81. * exist). If this object does not specify a particular type of collection,
  82. * a default collection, which stores in permissions in a hash table, will
  83. * be used.
  84. *
  85. * @param perm the <code>Permission</code> to add
  86. * @throws SecurityException if this collection is marked as read only
  87. */
  88. public void add(Permission perm)
  89. {
  90. if (isReadOnly())
  91. throw new SecurityException("PermissionCollection is read only");
  92. if (perm instanceof AllPermission)
  93. {
  94. if (allPermission == null)
  95. {
  96. allPermission = perm.newPermissionCollection();
  97. allPermission.add(perm);
  98. perms.put(perm.getClass(), allPermission);
  99. }
  100. }
  101. else
  102. {
  103. PermissionCollection pc
  104. = (PermissionCollection) perms.get(perm.getClass());
  105. if (pc == null)
  106. {
  107. pc = perm.newPermissionCollection();
  108. if (pc == null)
  109. pc = new PermissionsHash();
  110. perms.put(perm.getClass(), pc);
  111. }
  112. pc.add(perm);
  113. }
  114. }
  115. /**
  116. * This method tests whether or not the specified <code>Permission</code>
  117. * is implied by this <code>PermissionCollection</code>.
  118. *
  119. * @param perm the <code>Permission</code> to test
  120. * @return true if the specified permission is implied by this
  121. */
  122. public boolean implies(Permission perm)
  123. {
  124. if (allPermission != null)
  125. return true;
  126. PermissionCollection pc
  127. = (PermissionCollection) perms.get(perm.getClass());
  128. return pc == null ? false : pc.implies(perm);
  129. }
  130. /**
  131. * This method returns an <code>Enumeration</code> which contains a
  132. * list of all <code>Permission</code> objects contained in this
  133. * collection.
  134. *
  135. * @return an <code>Enumeration</code> of this collection's elements
  136. */
  137. public Enumeration<Permission> elements()
  138. {
  139. return new Enumeration()
  140. {
  141. Enumeration main_enum = perms.elements();
  142. Enumeration sub_enum;
  143. public boolean hasMoreElements()
  144. {
  145. if (sub_enum == null)
  146. {
  147. if (main_enum == null)
  148. return false;
  149. if (! main_enum.hasMoreElements())
  150. {
  151. main_enum = null;
  152. return false;
  153. }
  154. PermissionCollection pc =
  155. (PermissionCollection) main_enum.nextElement();
  156. sub_enum = pc.elements();
  157. }
  158. if (! sub_enum.hasMoreElements())
  159. {
  160. sub_enum = null;
  161. return hasMoreElements();
  162. }
  163. return true;
  164. }
  165. public Object nextElement()
  166. {
  167. if (! hasMoreElements())
  168. throw new NoSuchElementException();
  169. return sub_enum.nextElement();
  170. }
  171. };
  172. }
  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. private static final 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. // FIXME: Should this method be synchronized?
  209. public boolean implies(Permission perm)
  210. {
  211. Enumeration elements = elements();
  212. while (elements.hasMoreElements())
  213. {
  214. Permission p = (Permission)elements.nextElement();
  215. if (p.implies(perm))
  216. return true;
  217. }
  218. return false;
  219. }
  220. /**
  221. * Return the elements.
  222. *
  223. * @return the elements
  224. */
  225. public Enumeration elements()
  226. {
  227. return perms.elements();
  228. }
  229. } // class PermissionsHash
  230. } // class Permissions