Permission.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Permission.java -- The superclass for all permission objects
  2. Copyright (C) 1998, 2001, 2002, 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.lang.CPStringBuilder;
  33. import java.io.Serializable;
  34. /**
  35. * This class is the abstract superclass of all classes that implement
  36. * the concept of a permission. A permission consists of a permission name
  37. * and optionally a list of actions that relate to the permission. The
  38. * actual meaning of the name of the permission is defined only in the
  39. * context of a subclass. It may name a resource to which access permissions
  40. * are granted (for example, the name of a file) or it might represent
  41. * something else entirely. Similarly, the action list only has meaning
  42. * within the context of a subclass. Some permission names may have no
  43. * actions associated with them. That is, you either have the permission
  44. * or you don't.
  45. *
  46. * <p>The most important method in this class is <code>implies</code>. This
  47. * checks whether if one has this permission, then the specified
  48. * permission is also implied. As a conceptual example, consider the
  49. * permissions "Read All Files" and "Read File foo". The permission
  50. * "Read All Files" implies that the caller has permission to read the
  51. * file foo.
  52. *
  53. * <p><code>Permission</code>'s must be immutable - do not change their
  54. * state after creation.
  55. *
  56. * @author Aaron M. Renn (arenn@urbanophile.com)
  57. * @see Permissions
  58. * @see PermissionCollection
  59. * @since 1.1
  60. * @status updated to 1.4
  61. */
  62. public abstract class Permission implements Guard, Serializable
  63. {
  64. /**
  65. * Compatible with JDK 1.1+.
  66. */
  67. private static final long serialVersionUID = -5636570222231596674L;
  68. /**
  69. * This is the name assigned to this permission object.
  70. *
  71. * @serial the name of the permission
  72. */
  73. private String name;
  74. /**
  75. * Create an instance with the specified name.
  76. *
  77. * @param name the permission name
  78. */
  79. public Permission(String name)
  80. {
  81. this.name = name;
  82. }
  83. /**
  84. * This method implements the <code>Guard</code> interface for this class.
  85. * It calls the <code>checkPermission</code> method in
  86. * <code>SecurityManager</code> with this <code>Permission</code> as its
  87. * argument. This method returns silently if the security check succeeds
  88. * or throws an exception if it fails.
  89. *
  90. * @param obj the <code>Object</code> being guarded - ignored by this class
  91. * @throws SecurityException if the security check fails
  92. * @see GuardedObject
  93. * @see SecurityManager#checkPermission(Permission)
  94. */
  95. public void checkGuard(Object obj)
  96. {
  97. SecurityManager sm = System.getSecurityManager();
  98. if (sm != null)
  99. sm.checkPermission(this);
  100. }
  101. /**
  102. * This method tests whether this <code>Permission</code> implies that the
  103. * specified <code>Permission</code> is also granted.
  104. *
  105. * @param perm the <code>Permission</code> to test against
  106. * @return true if perm is implied by this
  107. */
  108. public abstract boolean implies(Permission perm);
  109. /**
  110. * Check to see if this object equals obj. Use <code>implies</code>, rather
  111. * than <code>equals</code>, when making access control decisions.
  112. *
  113. * @param obj the object to compare to
  114. */
  115. public abstract boolean equals(Object obj);
  116. /**
  117. * This method returns a hash code for this <code>Permission</code>. It
  118. * must satisfy the contract of <code>Object.hashCode</code>: it must be
  119. * the same for all objects that equals considers to be the same.
  120. *
  121. * @return a hash value
  122. */
  123. public abstract int hashCode();
  124. /**
  125. * Get the name of this <code>Permission</code>.
  126. *
  127. * @return the name
  128. */
  129. public final String getName()
  130. {
  131. return name;
  132. }
  133. /**
  134. * This method returns the list of actions for this <code>Permission</code>
  135. * as a <code>String</code>. The string should be in canonical order, for
  136. * example, both <code>new FilePermission(f, "write,read")</code> and
  137. * <code>new FilePermission(f, "read,write")</code> have the action list
  138. * "read,write".
  139. *
  140. * @return the action list for this <code>Permission</code>
  141. */
  142. public abstract String getActions();
  143. /**
  144. * This method returns an empty <code>PermissionCollection</code> object
  145. * that can store permissions of this type, or <code>null</code> if no
  146. * such collection is defined. Subclasses must override this to provide
  147. * an appropriate collection when one is needed to accurately calculate
  148. * <code>implies</code>.
  149. *
  150. * @return a new <code>PermissionCollection</code>
  151. */
  152. public PermissionCollection newPermissionCollection()
  153. {
  154. return null;
  155. }
  156. /**
  157. * This method returns a <code>String</code> representation of this
  158. * <code>Permission</code> object. This is in the format:
  159. * <code>'(' + getClass().getName() + ' ' + getName() + ' ' + getActions
  160. * + ')'</code>.
  161. *
  162. * @return this object as a <code>String</code>
  163. */
  164. public String toString()
  165. {
  166. CPStringBuilder string = new CPStringBuilder();
  167. string = string.append('(');
  168. string = string.append(getClass().getName());
  169. string = string.append(' ');
  170. string = string.append(getName());
  171. if (!(getActions().equals("")))
  172. {
  173. string = string.append(' ');
  174. string = string.append(getActions());
  175. }
  176. string = string.append(')');
  177. return string.toString();
  178. }
  179. } // class Permission