Policy.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Policy.java --- Policy Manager Class
  2. Copyright (C) 1999 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. /**
  33. Policy is an abstract class for managing the system security
  34. policy for the Java application environment. It specifies
  35. which permissions are available for code from various
  36. sources. The security policy is represented through a
  37. subclass of Policy.
  38. Only one Policy is in effect at any time. ProtectionDomain
  39. initializes itself with information from this class on the
  40. set of permssions to grant.
  41. The location for the actual Policy could be anywhere in any
  42. form because it depends on the Policy implementation. The
  43. default system is in a flat ASCII file or it could be in a
  44. database.
  45. The current installed Policy can be accessed with getPolicy
  46. and changed with setPolicy if the code has the correct
  47. permissions.
  48. The refresh method causes the Policy class to refresh/reload
  49. its configuration. The method used to refresh depends on the
  50. Policy implementation.
  51. When a protection domain initializes its permissions it uses
  52. code like:
  53. <code>
  54. policy = Policy.getPolicy();
  55. permissionCollection perms = policy.getPermissions(MyCodeSource)
  56. </code>
  57. The protection domain passes the Policy handler a CodeSource
  58. object which contains the codebase URL and public key. The
  59. Policy implementation then returns the proper set of
  60. permissions for the CodeSource.
  61. The default Policy implementation can be changed by setting
  62. the "policy.provider" security provider in java.security
  63. to the correct Policy implementation class.
  64. @author Mark Benvenuto
  65. @since JDK 1.2
  66. */
  67. public abstract class Policy
  68. {
  69. // FIXME: The class name of the Policy provider should really be sourced
  70. // from the "java.security" configuration file. For now, just hard-code
  71. // a stub implementation.
  72. static private Policy currentPolicy = null;
  73. static
  74. {
  75. String pp = System.getProperty ("policy.provider");
  76. if (pp != null)
  77. try
  78. {
  79. currentPolicy = (Policy)Class.forName(pp).newInstance();
  80. }
  81. catch (Exception _)
  82. {
  83. currentPolicy = null;
  84. }
  85. if (currentPolicy == null)
  86. currentPolicy = new gnu.java.security.provider.DefaultPolicy();
  87. }
  88. /**
  89. Constructs a new Policy class.
  90. */
  91. public Policy()
  92. {
  93. }
  94. /**
  95. Gets the currently installed Policy handler. The value should
  96. not be cached as it can be changed by setPolicy. This
  97. function first calls <code>SecurityManager.checkPermission</code>
  98. with <code>SecurityPermission("getPolicy")</code> to check
  99. if the caller has Permission to get the current Policy.
  100. @return the current Policy
  101. @throws SecurityException if the security manager exists
  102. the caller does not have permission to
  103. <code>getPolicy</code>.
  104. */
  105. public static Policy getPolicy()
  106. {
  107. SecurityManager sm = System.getSecurityManager();
  108. if (sm != null)
  109. sm.checkPermission(new SecurityPermission("getPolicy"));
  110. return currentPolicy;
  111. }
  112. /**
  113. Sets the currently installed Policy handler. This
  114. function first calls <code>SecurityManager.checkPermission</code>
  115. with <code>SecurityPermission("setPolicy")</code> to check
  116. if the caller has Permission to get the current Policy.
  117. @param policy the new Policy to use
  118. @throws SecurityException if the security manager exists
  119. the caller does not have permission to
  120. <code>getPolicy</code>.
  121. */
  122. public static void setPolicy(Policy policy)
  123. {
  124. SecurityManager sm = System.getSecurityManager();
  125. if (sm != null)
  126. sm.checkPermission(new SecurityPermission("setPolicy"));
  127. currentPolicy = policy;
  128. }
  129. /**
  130. Evalutes the global policy and returns a set of Permissions
  131. allowed for the specified CodeSource.
  132. @param codesource The CodeSource to get Permission for
  133. @return a set of permissions for codesource specified by
  134. the current policy
  135. @throws SecurityException if the current thread does not
  136. have permission to call <code>getPermissions</code>
  137. */
  138. public abstract PermissionCollection getPermissions(CodeSource codesource);
  139. /**
  140. Refreshes and/or reloads the current Policy. The actual
  141. behavior of this method depends on the implementation.
  142. */
  143. public abstract void refresh();
  144. }