1. /*
  2. * @(#)GuardedObject.java 1.10 01/11/29
  3. *
  4. * Copyright 2002 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.10 01/11/29
  26. * @author Roland Schemers
  27. * @author Li Gong
  28. */
  29. public class GuardedObject implements java.io.Serializable {
  30. private Object object; // the object we are guarding
  31. private Guard guard; // the guard
  32. /**
  33. * Constructs a GuardedObject using the specified object and guard.
  34. * If the Guard object is null, then no restrictions will
  35. * be placed on who can access the object.
  36. *
  37. * @param object the object to be guarded.
  38. *
  39. * @param guard the Guard object that guards access to the object.
  40. */
  41. public GuardedObject(Object object, Guard guard)
  42. {
  43. this.guard = guard;
  44. this.object = object;
  45. }
  46. /**
  47. * Retrieves the guarded object, or throws an exception if access
  48. * to the guarded object is denied by the guard.
  49. *
  50. * @return the guarded object.
  51. *
  52. * @exception SecurityException if access to the guarded object is
  53. * denied.
  54. */
  55. public Object getObject()
  56. throws SecurityException
  57. {
  58. if (guard != null)
  59. guard.checkGuard(object);
  60. return object;
  61. }
  62. /**
  63. * Writes this object out to a stream (i.e., serializes it).
  64. * We check the guard if there is one.
  65. */
  66. private synchronized void writeObject(java.io.ObjectOutputStream oos)
  67. throws java.io.IOException
  68. {
  69. if (guard != null)
  70. guard.checkGuard(object);
  71. oos.defaultWriteObject();
  72. }
  73. }