1. /*
  2. * @(#)ProtectionDomain.java 1.25 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. /**
  9. *
  10. * <p> This ProtectionDomain class encapulates the characteristics of
  11. * a domain, which encloses a set of classes whose instances
  12. * are granted the same set of permissions.
  13. *
  14. * <p>In addition to a set of permissions, a domain is comprised of a
  15. * CodeSource, which is a set of PublicKeys together with a codebase (in
  16. * the form of a URL). Thus, classes signed by the same keys and
  17. * from the same URL are placed in the same domain.
  18. * Classes that have the same permissions but are from different code
  19. * sources belong to different domains.
  20. *
  21. * <p> A class belongs to one and only one ProtectionDomain.
  22. *
  23. * @version 1.25, 11/29/01
  24. * @author Li Gong
  25. * @author Roland Schemers
  26. */
  27. public class ProtectionDomain {
  28. /* CodeSource */
  29. private CodeSource codesource ;
  30. /* the rights this protection domain is granted */
  31. private PermissionCollection permissions;
  32. /**
  33. * Creates a new ProtectionDomain with the given CodeSource and
  34. * Permissions. If the permissions object is not null, then
  35. * <code>setReadOnly()</code> will be called on the passed in
  36. * Permissions object.
  37. *
  38. * @param codesource the codesource associated with this domain
  39. * @param permissions the permissions granted to this domain
  40. */
  41. public ProtectionDomain(CodeSource codesource,
  42. PermissionCollection permissions) {
  43. this.codesource = codesource;
  44. if (permissions != null) {
  45. this.permissions = permissions;
  46. this.permissions.setReadOnly();
  47. }
  48. }
  49. /**
  50. * Returns the CodeSource of this domain.
  51. * @return the CodeSource of this domain.
  52. */
  53. public final CodeSource getCodeSource() {
  54. return this.codesource;
  55. }
  56. /**
  57. * Returns the permissions of this domain.
  58. * @return the permissions of this domain.
  59. */
  60. public final PermissionCollection getPermissions() {
  61. return this.permissions;
  62. }
  63. /**
  64. * Check and see if this ProtectionDomain implies the permissions
  65. * expressed in the Permission object.
  66. *
  67. * @param permission the Permission object to check.
  68. *
  69. * @return true if "permission" is a proper subset of a permission in
  70. * this ProtectionDomain, false if not.
  71. */
  72. public boolean implies(Permission permission) {
  73. if (permissions != null) {
  74. return permissions.implies(permission);
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Convert a ProtectionDomain to a String.
  81. */
  82. public String toString() {
  83. return "ProtectionDomain "+codesource+"\n"+permissions+"\n";
  84. }
  85. }