AllPermission.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* AllPermission.java -- Permission to do anything
  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 gnu.java.util.EmptyEnumeration;
  33. import java.util.Collections;
  34. import java.util.Enumeration;
  35. /**
  36. * This class is a permission that implies all other permissions. Granting
  37. * this permission effectively grants all others. Extreme caution should
  38. * be exercised in granting this permission.
  39. *
  40. * @author Aaron M. Renn (arenn@urbanophile.com)
  41. * @author Eric Blake (ebb9@email.byu.edu)
  42. * @see AccessController
  43. * @see Permissions
  44. * @see SecurityManager
  45. * @since 1.1
  46. * @status updated to 1.4
  47. */
  48. public final class AllPermission extends Permission
  49. {
  50. /**
  51. * Compatible with JDK 1.1+.
  52. */
  53. private static final long serialVersionUID = -2916474571451318075L;
  54. /**
  55. * Create a new AllPermission object.
  56. */
  57. public AllPermission()
  58. {
  59. super("*");
  60. }
  61. /**
  62. * Create a new AllPermission object. The parameters are ignored, as all
  63. * permission implies ALL PERMISSION.
  64. *
  65. * @param name ignored
  66. * @param actions ignored
  67. */
  68. public AllPermission(String name, String actions)
  69. {
  70. super("*");
  71. }
  72. /**
  73. * This method always returns <code>true</code> to indicate that this
  74. * permission always implies that any other permission is also granted.
  75. *
  76. * @param perm ignored
  77. * @return true, the permission is implied
  78. */
  79. public boolean implies(Permission perm)
  80. {
  81. return true;
  82. }
  83. /**
  84. * Checks an object for equality. All AllPermissions are equal.
  85. *
  86. * @param obj the <code>Object</code> to test for equality
  87. */
  88. public boolean equals(Object obj)
  89. {
  90. return obj instanceof AllPermission;
  91. }
  92. /**
  93. * This method returns a hash code for this object. This returns 1.
  94. *
  95. * @return a hash value for this object
  96. */
  97. public int hashCode()
  98. {
  99. return 1;
  100. }
  101. /**
  102. * This method returns the list of actions associated with this object.
  103. * This will always be the empty string ("") for this class.
  104. *
  105. * @return the action list
  106. */
  107. public String getActions()
  108. {
  109. return "";
  110. }
  111. /**
  112. * Returns a PermissionCollection which can hold AllPermission.
  113. *
  114. * @return a permission collection
  115. */
  116. public PermissionCollection newPermissionCollection()
  117. {
  118. return new AllPermissionCollection();
  119. }
  120. /**
  121. * Implements AllPermission.newPermissionCollection, and obeys serialization
  122. * of JDK.
  123. *
  124. * @author Eric Blake (ebb9@email.byu.edu)
  125. */
  126. private static final class AllPermissionCollection extends PermissionCollection
  127. {
  128. /**
  129. * Compatible with JDK 1.1+.
  130. */
  131. private static final long serialVersionUID = -4023755556366636806L;
  132. /**
  133. * Whether an AllPermission has been added to the collection.
  134. *
  135. * @serial if all permission is in the collection yet
  136. */
  137. private boolean all_allowed;
  138. /**
  139. * Add an AllPermission.
  140. *
  141. * @param perm the permission to add
  142. * @throws IllegalArgumentException if perm is not an AllPermission
  143. * @throws SecurityException if the collection is read-only
  144. */
  145. public void add(Permission perm)
  146. {
  147. if (isReadOnly())
  148. throw new SecurityException();
  149. if (! (perm instanceof AllPermission))
  150. throw new IllegalArgumentException();
  151. all_allowed = true;
  152. }
  153. /**
  154. * Returns true if this collection implies a permission.
  155. *
  156. * @param perm the permission to check
  157. * @return true if this collection contains an AllPermission
  158. */
  159. public boolean implies(Permission perm)
  160. {
  161. return all_allowed;
  162. }
  163. /**
  164. * Returns an enumeration of the elements in the collection.
  165. *
  166. * @return the elements in the collection
  167. */
  168. public Enumeration elements()
  169. {
  170. return all_allowed
  171. ? Collections.enumeration(Collections.singleton(new AllPermission()))
  172. : EmptyEnumeration.getInstance();
  173. }
  174. } // class AllPermissionCollection
  175. } // class AllPermission