ProtectionDomain.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* ProtectionDomain.java -- A security domain
  2. Copyright (C) 1998, 2003, 2004 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.classpath.SystemProperties;
  33. import gnu.java.lang.CPStringBuilder;
  34. import java.util.Enumeration;
  35. /**
  36. * This class represents a group of classes, along with their granted
  37. * permissions. The classes are identified by a {@link CodeSource}. Thus, any
  38. * class loaded from the specified {@link CodeSource} is treated as part of
  39. * this domain. The set of permissions is represented by an instance of
  40. * {@link PermissionCollection}.
  41. *
  42. * <p>Every class in the system will belong to one and only one
  43. * <code>ProtectionDomain</code>.</p>
  44. *
  45. * @author Aaron M. Renn (arenn@urbanophile.com)
  46. * @version 0.0
  47. */
  48. public class ProtectionDomain
  49. {
  50. /** This is the <code>CodeSource</code> for this protection domain. */
  51. private CodeSource code_source;
  52. /** This is the set of permissions granted to this domain. */
  53. private PermissionCollection perms;
  54. /** The {@link ClassLoader} associated with this domain. */
  55. private ClassLoader classloader;
  56. /** The array of Principals associated with this domain.. */
  57. private Principal[] principals;
  58. /** Post 1.4 the policy may be refreshed! use false for pre 1.4. */
  59. private boolean staticBinding;
  60. /** True if this protection domain has all permissions */
  61. private boolean hasAllPermissions;
  62. /**
  63. * Initializes a new instance of <code>ProtectionDomain</code> representing
  64. * the specified {@link CodeSource} and set of permissions. No permissions
  65. * can be added later to the {@link PermissionCollection} and this contructor
  66. * will call the <code>setReadOnly</code> method on the specified set of
  67. * permissions.
  68. *
  69. * @param codesource
  70. * The {@link CodeSource} for this domain.
  71. * @param permissions
  72. * The set of permissions for this domain.
  73. * @see PermissionCollection#setReadOnly()
  74. */
  75. public ProtectionDomain(CodeSource codesource, PermissionCollection permissions)
  76. {
  77. this(codesource, permissions, null, null, true);
  78. }
  79. /**
  80. * This method initializes a new instance of <code>ProtectionDomain</code>
  81. * given its {@link CodeSource}, granted permissions, associated
  82. * {@link ClassLoader} and {@link Principal}s.
  83. *
  84. * <p>Similar to the previous constructor, if the designated set of
  85. * permissions is not <code>null</code>, the <code>setReadOnly</code> method
  86. * is called on that set.</p>
  87. *
  88. * @param codesource
  89. * The {@link CodeSource} for this domain.
  90. * @param permissions
  91. * The permission set for this domain.
  92. * @param classloader
  93. * the ClassLoader associated with this domain.
  94. * @param principals
  95. * the array of {@link Principal}s associated with this domain.
  96. * @since 1.4
  97. * @see PermissionCollection#setReadOnly()
  98. */
  99. public ProtectionDomain(CodeSource codesource,
  100. PermissionCollection permissions,
  101. ClassLoader classloader, Principal[] principals)
  102. {
  103. this(codesource, permissions, classloader, principals, false);
  104. }
  105. private ProtectionDomain(CodeSource codesource,
  106. PermissionCollection permissions,
  107. ClassLoader classloader, Principal[] principals,
  108. boolean staticBinding)
  109. {
  110. super();
  111. code_source = codesource;
  112. if (permissions != null)
  113. {
  114. perms = permissions;
  115. perms.setReadOnly();
  116. /* Check if this protection domain has all permissions */
  117. Enumeration<Permission> e = permissions.elements();
  118. while (e.hasMoreElements())
  119. {
  120. if (e.nextElement() instanceof AllPermission)
  121. hasAllPermissions = true;
  122. }
  123. }
  124. this.classloader = classloader;
  125. this.principals =
  126. (principals != null ? (Principal[]) principals.clone() : new Principal[0]);
  127. this.staticBinding = staticBinding;
  128. }
  129. /**
  130. * Returns the {@link CodeSource} of this domain.
  131. *
  132. * @return the {@link CodeSource} of this domain.
  133. * @since 1.2
  134. */
  135. public final CodeSource getCodeSource()
  136. {
  137. return code_source;
  138. }
  139. /**
  140. * Returns the {@link ClassLoader} of this domain.
  141. *
  142. * @return the {@link ClassLoader} of this domain.
  143. * @since 1.4
  144. */
  145. public final ClassLoader getClassLoader()
  146. {
  147. return this.classloader;
  148. }
  149. /**
  150. * Returns a clone of the {@link Principal}s of this domain.
  151. *
  152. * @return a clone of the {@link Principal}s of this domain.
  153. * @since 1.4
  154. */
  155. public final Principal[] getPrincipals()
  156. {
  157. return (Principal[]) principals.clone();
  158. }
  159. /**
  160. * Returns the {@link PermissionCollection} of this domain.
  161. *
  162. * @return The {@link PermissionCollection} of this domain.
  163. */
  164. public final PermissionCollection getPermissions()
  165. {
  166. return perms;
  167. }
  168. /**
  169. * Tests whether or not the specified {@link Permission} is implied by the
  170. * set of permissions granted to this domain.
  171. *
  172. * @param permission
  173. * the {@link Permission} to test.
  174. * @return <code>true</code> if the specified {@link Permission} is implied
  175. * for this domain, <code>false</code> otherwise.
  176. */
  177. public boolean implies(Permission permission)
  178. {
  179. if (hasAllPermissions)
  180. return true;
  181. if (staticBinding)
  182. return (perms == null ? false : perms.implies(permission));
  183. // Else dynamically bound. Do we have it?
  184. // NOTE: this will force loading of Policy.currentPolicy
  185. return Policy.getCurrentPolicy().implies(this, permission);
  186. }
  187. /**
  188. * Returns a string representation of this object. It will include the
  189. * {@link CodeSource} and set of permissions associated with this domain.
  190. *
  191. * @return A string representation of this object.
  192. */
  193. public String toString()
  194. {
  195. String linesep = SystemProperties.getProperty("line.separator");
  196. CPStringBuilder sb = new CPStringBuilder("ProtectionDomain (").append(linesep);
  197. if (code_source == null)
  198. sb.append("CodeSource:null");
  199. else
  200. sb.append(code_source);
  201. sb.append(linesep);
  202. if (classloader == null)
  203. sb.append("ClassLoader:null");
  204. else
  205. sb.append(classloader);
  206. sb.append(linesep);
  207. sb.append("Principals:");
  208. if (principals != null && principals.length > 0)
  209. {
  210. sb.append("[");
  211. Principal pal;
  212. for (int i = 0; i < principals.length; i++)
  213. {
  214. pal = principals[i];
  215. sb.append("'").append(pal.getName())
  216. .append("' of type ").append(pal.getClass().getName());
  217. if (i < principals.length-1)
  218. sb.append(", ");
  219. }
  220. sb.append("]");
  221. }
  222. else
  223. sb.append("none");
  224. sb.append(linesep);
  225. if (!staticBinding) // include all but dont force loading Policy.currentPolicy
  226. if (Policy.isLoaded())
  227. try
  228. {
  229. sb.append(Policy.getPolicy().getPermissions(this));
  230. }
  231. catch (SecurityException e)
  232. {
  233. // We are not allowed access to the policy.
  234. sb.append(perms);
  235. }
  236. else // fallback on this one's permissions
  237. sb.append(perms);
  238. else
  239. sb.append(perms);
  240. return sb.append(linesep).append(")").append(linesep).toString();
  241. }
  242. }