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