1. /*
  2. * @(#)GuardedObject.java 1.15 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. /**
  9. * A GuardedObject is an object that is used to protect access to
  10. * another object.
  11. *
  12. * <p>A GuardedObject encapsulates a target object and a Guard object,
  13. * such that access to the target object is possible
  14. * only if the Guard object allows it.
  15. * Once an object is encapsulated by a GuardedObject,
  16. * access to that object is controlled by the <code>getObject</code>
  17. * method, which invokes the
  18. * <code>checkGuard</code> method on the Guard object that is
  19. * guarding access. If access is not allowed,
  20. * an exception is thrown.
  21. *
  22. * @see Guard
  23. * @see Permission
  24. *
  25. * @version 1.15 03/12/19
  26. * @author Roland Schemers
  27. * @author Li Gong
  28. */
  29. public class GuardedObject implements java.io.Serializable {
  30. private static final long serialVersionUID = -5240450096227834308L;
  31. private Object object; // the object we are guarding
  32. private Guard guard; // the guard
  33. /**
  34. * Constructs a GuardedObject using the specified object and guard.
  35. * If the Guard object is null, then no restrictions will
  36. * be placed on who can access the object.
  37. *
  38. * @param object the object to be guarded.
  39. *
  40. * @param guard the Guard object that guards access to the object.
  41. */
  42. public GuardedObject(Object object, Guard guard)
  43. {
  44. this.guard = guard;
  45. this.object = object;
  46. }
  47. /**
  48. * Retrieves the guarded object, or throws an exception if access
  49. * to the guarded object is denied by the guard.
  50. *
  51. * @return the guarded object.
  52. *
  53. * @exception SecurityException if access to the guarded object is
  54. * denied.
  55. */
  56. public Object getObject()
  57. throws SecurityException
  58. {
  59. if (guard != null)
  60. guard.checkGuard(object);
  61. return object;
  62. }
  63. /**
  64. * Writes this object out to a stream (i.e., serializes it).
  65. * We check the guard if there is one.
  66. */
  67. private synchronized void writeObject(java.io.ObjectOutputStream oos)
  68. throws java.io.IOException
  69. {
  70. if (guard != null)
  71. guard.checkGuard(object);
  72. oos.defaultWriteObject();
  73. }
  74. }