UnresolvedPermission.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* UnresolvedPermission.java -- Placeholder for unresolved permissions
  2. Copyright (C) 1998, 2001, 2002, 2004, 2005 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. // All uses of Certificate in this file refer to the one in the listed
  33. // package, not this one.
  34. import java.security.cert.Certificate;
  35. import java.util.Arrays;
  36. import java.util.Enumeration;
  37. import java.util.Hashtable;
  38. import java.util.NoSuchElementException;
  39. import java.util.Vector;
  40. /**
  41. * This class is used to hold instances of all permissions that cannot
  42. * be resolved to available permission classes when the security
  43. * <code>Policy</code> object is instantiated. This may happen when the
  44. * necessary security class has not yet been downloaded from the network.
  45. *
  46. * <p>Instances of this class are re-resolved when
  47. * <code>AccessController</code> check is done. At that time, a scan is
  48. * made of all existing <code>UnresolvedPermission</code> objects and they
  49. * are converted to objects of the appropriate permission type if the class
  50. * for that type is then available.
  51. *
  52. * @author Aaron M. Renn (arenn@urbanophile.com)
  53. * @see Permission
  54. * @see Permissions
  55. * @see PermissionCollection
  56. * @see Policy
  57. * @since 1.1
  58. * @status updated to 1.4
  59. */
  60. public final class UnresolvedPermission extends Permission
  61. {
  62. /**
  63. * Compatible with JDK 1.1+.
  64. */
  65. private static final long serialVersionUID = -4821973115467008846L;
  66. /**
  67. * The list of actions associated with this permission object.
  68. *
  69. * @serial the permission actions
  70. */
  71. private final String actions;
  72. /**
  73. * The list of <code>Certificates</code> associated with this object.
  74. */
  75. private final transient Certificate[] certs;
  76. /**
  77. * The name of the class this object should be resolved to.
  78. *
  79. * @serial the fully-qualified classname of the resolved type
  80. */
  81. // Package visible for use by UnresolvedPermissionCollection.
  82. final String type;
  83. /**
  84. * The name of the permission.
  85. *
  86. * @serial the permission name
  87. */
  88. private final String name;
  89. /**
  90. * Create a new instance with all the information necessary to resolve it
  91. * to an instance of the proper class at a future time.
  92. *
  93. * @param type the fully-qualified name of the class of this permission
  94. * @param name the name of this permission
  95. * @param actions the action list for this permission
  96. * @param certs the list of certificates that sign this permission
  97. */
  98. public UnresolvedPermission(String type, String name, String actions,
  99. Certificate[] certs)
  100. {
  101. super(name);
  102. this.name = name;
  103. this.type = type;
  104. this.actions = actions;
  105. this.certs = certs;
  106. }
  107. /**
  108. * This method returns <code>false</code> always to indicate that this
  109. * permission does not imply the specified permission. An
  110. * <code>UnresolvedPermission</code> never grants any permissions.
  111. *
  112. * @param perm the <code>Permission</code> object to test
  113. * @return false; until a permission is resolved, it implies nothing
  114. */
  115. public boolean implies(Permission perm)
  116. {
  117. return false;
  118. }
  119. /**
  120. * This method tests this permission for equality against the specified
  121. * <code>Object</code>. This will be true if and only if the following
  122. * conditions are met:<ul>
  123. * <li>The specified <code>Object</code> is an UnresolvedPermission</li>
  124. * <li>The specified permission has the same type (i.e., desired class name)
  125. * as this permission.</li>
  126. * <li>The specified permission has the same name as this one.</li>
  127. * <li>The specified permissoin has the same action list as this one.</li>
  128. * <li>The specified permission has the same certificate list as this
  129. * one.</li>
  130. * </ul>
  131. *
  132. * @param obj the <code>Object</code> to test for equality
  133. * @return true if the specified object is equal to this one
  134. */
  135. public boolean equals(Object obj)
  136. {
  137. if (! (obj instanceof UnresolvedPermission))
  138. return (false);
  139. UnresolvedPermission up = (UnresolvedPermission) obj;
  140. return up.name.equals(name) && up.actions.equals(actions)
  141. && up.type.equals(type) && Arrays.equals(up.certs, certs);
  142. }
  143. /**
  144. * Returns a hash code value for this object. Following the lead of
  145. * Permission, this returns the hashcode of the permission name.
  146. *
  147. * @return A hash value
  148. */
  149. public int hashCode()
  150. {
  151. return name.hashCode();
  152. }
  153. /**
  154. * This method returns the list of actions associated with this
  155. * permission.
  156. *
  157. * @return the action list
  158. */
  159. public String getActions()
  160. {
  161. return actions;
  162. }
  163. /**
  164. * This method returns a <code>String</code> representation of this
  165. * class. The format is: '(unresolved "ClassName "name" "actions")'
  166. *
  167. * @return <code>String</code> representation of this object
  168. */
  169. public String toString()
  170. {
  171. return "(unresolved " + type + ' ' + name + ' ' + actions + ')';
  172. }
  173. /**
  174. * This class returns a <code>PermissionCollection</code> object that can
  175. * be used to store instances of <code>UnresolvedPermission</code>.
  176. *
  177. * @return a new <code>PermissionCollection</code>
  178. */
  179. public PermissionCollection newPermissionCollection()
  180. {
  181. return new UnresolvedPermissionCollection();
  182. }
  183. /**
  184. * Return the name of the class of the unresolved permission.
  185. * @since 1.5
  186. */
  187. public String getUnresolvedType()
  188. {
  189. return type;
  190. }
  191. /**
  192. * Return the name of the unresolved permission.
  193. * @since 1.5
  194. */
  195. public String getUnresolvedName()
  196. {
  197. return name;
  198. }
  199. /**
  200. * Return the actions of the unresolved permission, or null
  201. * if there are no actions.
  202. * @since 1.5
  203. */
  204. public String getUnresolvedActions()
  205. {
  206. return actions;
  207. }
  208. /**
  209. * Return the certificates of the unresolved permission.
  210. * If there are no certificates, null is returned. Otherwise,
  211. * a new array is returned.
  212. * @since 1.5
  213. */
  214. public Certificate[] getUnresolvedCerts()
  215. {
  216. if (certs == null)
  217. return null;
  218. return (Certificate[]) certs.clone();
  219. }
  220. } // class UnresolvedPermission
  221. /**
  222. * Implements the permission collection for unresolved permissions, and
  223. * obeys serialization of JDK.
  224. *
  225. * @author Eric Blake (ebb9@email.byu.edu)
  226. */
  227. class UnresolvedPermissionCollection extends PermissionCollection
  228. {
  229. /**
  230. * Compatible with JDK 1.1+.
  231. */
  232. private static final long serialVersionUID = -7176153071733132400L;
  233. // Package-private to avoid a trampoline.
  234. /**
  235. * Hashtable where we store permissions.
  236. *
  237. * @serial map of typename to a Vector of permissions (you'd think Sun
  238. * would document this better!)
  239. */
  240. final Hashtable permissions = new Hashtable();
  241. /**
  242. * Add a permission.
  243. *
  244. * @param perm the permission to add
  245. * @throws IllegalArgumentException if perm is not an UnresolvedPermission
  246. * @throws SecurityException if the collection is read-only
  247. */
  248. public void add(Permission perm)
  249. {
  250. if (isReadOnly())
  251. throw new SecurityException();
  252. if (! (perm instanceof UnresolvedPermission))
  253. throw new IllegalArgumentException();
  254. UnresolvedPermission up = (UnresolvedPermission) perm;
  255. Vector v = (Vector) permissions.get(up.type);
  256. if (v == null)
  257. {
  258. v = new Vector();
  259. permissions.put(up.type, v);
  260. }
  261. v.add(up);
  262. }
  263. /**
  264. * Returns true if perm is implied by the collection.
  265. *
  266. * @param perm the permission to check
  267. * @return false; unresolved permissions imply nothing
  268. */
  269. public boolean implies(Permission perm)
  270. {
  271. return false;
  272. }
  273. /**
  274. * Return the elements.
  275. *
  276. * @return the elements
  277. */
  278. public Enumeration elements()
  279. {
  280. return new Enumeration()
  281. {
  282. Enumeration main_enum = permissions.elements();
  283. Enumeration sub_enum;
  284. public boolean hasMoreElements()
  285. {
  286. if (sub_enum == null)
  287. {
  288. if (main_enum == null)
  289. return false;
  290. if (! main_enum.hasMoreElements())
  291. {
  292. main_enum = null;
  293. return false;
  294. }
  295. Vector v = (Vector) main_enum.nextElement();
  296. sub_enum = v.elements();
  297. }
  298. if (! sub_enum.hasMoreElements())
  299. {
  300. sub_enum = null;
  301. return hasMoreElements();
  302. }
  303. return true;
  304. }
  305. public Object nextElement()
  306. {
  307. if (! hasMoreElements())
  308. throw new NoSuchElementException();
  309. return sub_enum.nextElement();
  310. }
  311. };
  312. }
  313. } // class UnresolvedPermissionCollection