1. /*
  2. * @(#)UnresolvedPermissionCollection.java 1.12 03/01/23
  3. *
  4. * Copyright 2003 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.12 03/01/23
  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. // No need to synchronize because all adds are done sequentially
  57. // (by policy provider) before any getUnresolvedPermissions() calls.
  58. // If policy provider is multithreaded, it should lock the
  59. // collection to allow concurrent adds
  60. List v = (List) perms.get(up.getName());
  61. if (v == null) {
  62. v = new ArrayList();
  63. perms.put(up.getName(), v);
  64. }
  65. v.add(up);
  66. }
  67. /**
  68. * get any unresolved permissions of the same type as p,
  69. * and return the List containing them.
  70. */
  71. // No need to synchronize; list is only gotten for reading
  72. List getUnresolvedPermissions(Permission p) {
  73. return (List) perms.get(p.getClass().getName());
  74. }
  75. /**
  76. * always returns false for unresolved permissions
  77. *
  78. */
  79. public boolean implies(Permission permission)
  80. {
  81. return false;
  82. }
  83. /**
  84. * Returns an enumeration of all the UnresolvedPermission lists in the
  85. * container.
  86. *
  87. * @return an enumeration of all the UnresolvedPermission objects.
  88. */
  89. // No need to synchronize; lists won't change after initial adds
  90. // XXX A performance improvement might be to construct the global
  91. // list lazily as enumeration proceeds.
  92. public Enumeration elements() {
  93. List results = new ArrayList(); // where results are stored
  94. // Get iterator of Map values (which are lists of permissions)
  95. for (Iterator iter = perms.values().iterator(); iter.hasNext();) {
  96. results.addAll((List) iter.next());
  97. }
  98. return Collections.enumeration(results);
  99. }
  100. private static final long serialVersionUID = -7176153071733132400L;
  101. // Need to maintain serialization interoperability with earlier releases,
  102. // which had the serializable field:
  103. // private Hashtable permissions; // keyed on type
  104. /**
  105. * @serialField permissions java.util.Hashtable
  106. * A table of the UnresolvedPermissions keyed on type, value is Vector
  107. * of permissions
  108. */
  109. private static final ObjectStreamField[] serialPersistentFields = {
  110. new ObjectStreamField("permissions", Hashtable.class),
  111. };
  112. /**
  113. * @serialData Default field.
  114. */
  115. /*
  116. * Writes the contents of the perms field out as a Hashtable
  117. * in which the values are Vectors for
  118. * serialization compatibility with earlier releases.
  119. */
  120. private void writeObject(ObjectOutputStream out) throws IOException {
  121. // Don't call out.defaultWriteObject()
  122. // Copy perms into a Hashtable
  123. Hashtable permissions = new Hashtable(perms.size()*2);
  124. // Convert each entry (List) into a Vector
  125. Set set = perms.entrySet();
  126. for (Iterator iter = set.iterator(); iter.hasNext(); ) {
  127. Map.Entry e = (Map.Entry)iter.next();
  128. // Convert list into Vector
  129. List list = (List) e.getValue();
  130. Vector vec = new Vector(list.size());
  131. vec.addAll(list);
  132. // Add to Hashtable being serialized
  133. permissions.put(e.getKey(), vec);
  134. }
  135. // Write out serializable fields
  136. ObjectOutputStream.PutField pfields = out.putFields();
  137. pfields.put("permissions", permissions);
  138. out.writeFields();
  139. }
  140. /*
  141. * Reads in a Hashtable in which the values are Vectors of
  142. * UnresolvedPermissions and saves them in the perms field.
  143. */
  144. private void readObject(ObjectInputStream in) throws IOException,
  145. ClassNotFoundException {
  146. // Don't call defaultReadObject()
  147. // Read in serialized fields
  148. ObjectInputStream.GetField gfields = in.readFields();
  149. // Get permissions
  150. Hashtable permissions = (Hashtable)gfields.get("permissions", null);
  151. perms = new HashMap(permissions.size()*2);
  152. // Convert each entry (Vector) into a List
  153. Set set = permissions.entrySet();
  154. for (Iterator iter = set.iterator(); iter.hasNext(); ) {
  155. Map.Entry e = (Map.Entry)iter.next();
  156. // Convert Vector into ArrayList
  157. Vector vec = (Vector) e.getValue();
  158. List list = new ArrayList(vec.size());
  159. list.addAll(vec);
  160. // Add to Hashtable being serialized
  161. perms.put(e.getKey(), list);
  162. }
  163. }
  164. }