1. /*
  2. * @(#)UnresolvedPermissionCollection.java 1.14 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.util.*;
  9. import java.io.ObjectStreamField;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.IOException;
  13. /**
  14. * A UnresolvedPermissionCollection stores a collection
  15. * of UnresolvedPermission permissions.
  16. *
  17. * @see java.security.Permission
  18. * @see java.security.Permissions
  19. * @see java.security.UnresolvedPermission
  20. *
  21. * @version 1.14 03/12/19
  22. *
  23. * @author Roland Schemers
  24. *
  25. * @serial include
  26. */
  27. final class UnresolvedPermissionCollection
  28. extends PermissionCollection
  29. implements java.io.Serializable
  30. {
  31. /**
  32. * Key is permission type, value is a list of the UnresolvedPermissions
  33. * of the same type.
  34. * Not serialized; see serialization section at end of class.
  35. */
  36. private transient Map perms;
  37. /**
  38. * Create an empty UnresolvedPermissionCollection object.
  39. *
  40. */
  41. public UnresolvedPermissionCollection() {
  42. perms = new HashMap(11);
  43. }
  44. /**
  45. * Adds a permission to this UnresolvedPermissionCollection.
  46. * The key for the hash is the unresolved permission's type (class) name.
  47. *
  48. * @param permission the Permission object to add.
  49. */
  50. public void add(Permission permission)
  51. {
  52. if (! (permission instanceof UnresolvedPermission))
  53. throw new IllegalArgumentException("invalid permission: "+
  54. permission);
  55. UnresolvedPermission up = (UnresolvedPermission) permission;
  56. List v;
  57. synchronized (this) {
  58. v = (List) perms.get(up.getName());
  59. if (v == null) {
  60. v = new ArrayList();
  61. perms.put(up.getName(), v);
  62. }
  63. }
  64. synchronized (v) {
  65. v.add(up);
  66. }
  67. }
  68. /**
  69. * get any unresolved permissions of the same type as p,
  70. * and return the List containing them.
  71. */
  72. List getUnresolvedPermissions(Permission p) {
  73. synchronized (this) {
  74. return (List) perms.get(p.getClass().getName());
  75. }
  76. }
  77. /**
  78. * always returns false for unresolved permissions
  79. *
  80. */
  81. public boolean implies(Permission permission)
  82. {
  83. return false;
  84. }
  85. /**
  86. * Returns an enumeration of all the UnresolvedPermission lists in the
  87. * container.
  88. *
  89. * @return an enumeration of all the UnresolvedPermission objects.
  90. */
  91. public Enumeration elements() {
  92. List results = new ArrayList(); // where results are stored
  93. // Get iterator of Map values (which are lists of permissions)
  94. synchronized (this) {
  95. for (Iterator iter = perms.values().iterator(); iter.hasNext();) {
  96. List l = (List) iter.next();
  97. synchronized (l) {
  98. results.addAll(l);
  99. }
  100. }
  101. }
  102. return Collections.enumeration(results);
  103. }
  104. private static final long serialVersionUID = -7176153071733132400L;
  105. // Need to maintain serialization interoperability with earlier releases,
  106. // which had the serializable field:
  107. // private Hashtable permissions; // keyed on type
  108. /**
  109. * @serialField permissions java.util.Hashtable
  110. * A table of the UnresolvedPermissions keyed on type, value is Vector
  111. * of permissions
  112. */
  113. private static final ObjectStreamField[] serialPersistentFields = {
  114. new ObjectStreamField("permissions", Hashtable.class),
  115. };
  116. /**
  117. * @serialData Default field.
  118. */
  119. /*
  120. * Writes the contents of the perms field out as a Hashtable
  121. * in which the values are Vectors for
  122. * serialization compatibility with earlier releases.
  123. */
  124. private void writeObject(ObjectOutputStream out) throws IOException {
  125. // Don't call out.defaultWriteObject()
  126. // Copy perms into a Hashtable
  127. Hashtable permissions = new Hashtable(perms.size()*2);
  128. // Convert each entry (List) into a Vector
  129. synchronized (this) {
  130. Set set = perms.entrySet();
  131. for (Iterator iter = set.iterator(); iter.hasNext(); ) {
  132. Map.Entry e = (Map.Entry)iter.next();
  133. // Convert list into Vector
  134. List list = (List) e.getValue();
  135. Vector vec = new Vector(list.size());
  136. synchronized (list) {
  137. vec.addAll(list);
  138. }
  139. // Add to Hashtable being serialized
  140. permissions.put(e.getKey(), vec);
  141. }
  142. }
  143. // Write out serializable fields
  144. ObjectOutputStream.PutField pfields = out.putFields();
  145. pfields.put("permissions", permissions);
  146. out.writeFields();
  147. }
  148. /*
  149. * Reads in a Hashtable in which the values are Vectors of
  150. * UnresolvedPermissions and saves them in the perms field.
  151. */
  152. private void readObject(ObjectInputStream in) throws IOException,
  153. ClassNotFoundException {
  154. // Don't call defaultReadObject()
  155. // Read in serialized fields
  156. ObjectInputStream.GetField gfields = in.readFields();
  157. // Get permissions
  158. Hashtable permissions = (Hashtable)gfields.get("permissions", null);
  159. perms = new HashMap(permissions.size()*2);
  160. // Convert each entry (Vector) into a List
  161. Set set = permissions.entrySet();
  162. for (Iterator iter = set.iterator(); iter.hasNext(); ) {
  163. Map.Entry e = (Map.Entry)iter.next();
  164. // Convert Vector into ArrayList
  165. Vector vec = (Vector) e.getValue();
  166. List list = new ArrayList(vec.size());
  167. list.addAll(vec);
  168. // Add to Hashtable being serialized
  169. perms.put(e.getKey(), list);
  170. }
  171. }
  172. }