home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / GuardedObject.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  2.3 KB  |  81 lines

  1. /*
  2.  * @(#)GuardedObject.java    1.5 98/03/18
  3.  *
  4.  * Copyright 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. /**
  18.  * A GuardedObject is an object that is used to protect access to
  19.  * another object. 
  20.  * 
  21.  * <p>A GuardedObject encapsulates a target object and a Guard object,
  22.  * such that access to the target object is possible 
  23.  * only if the Guard object allows it. 
  24.  * Once an object is encapsulated by a GuardedObject,
  25.  * access to that object is controlled by the <code>getObject</code> 
  26.  * method, which invokes the 
  27.  * <code>checkGuard</code> method on the Guard object that is 
  28.  * guarding access. If access is not allowed,
  29.  * an exception is thrown.
  30.  * 
  31.  * @see Guard
  32.  * @see Permission
  33.  *
  34.  * @version 1.5 98/03/18
  35.  * @author Roland Schemers
  36.  * @author Li Gong
  37.  */
  38.  
  39. public class GuardedObject implements java.io.Serializable {
  40.  
  41.     /** use serialVersionUID from JDK 1.2 for interoperability */
  42.     private static final long serialVersionUID = -5240450096227834308L;
  43.  
  44.     private Object object; // the object we are guarding
  45.     private Guard guard;   // the guard
  46.  
  47.     /**
  48.      * Constructs a GuardedObject using the specified object and guard. 
  49.      * If the Guard object is null, then no restrictions will 
  50.      * be placed on who can access the object.
  51.      * 
  52.      * @param object the object to be guarded.
  53.      *
  54.      * @param guard the Guard object that guards access to the object.
  55.      */
  56.  
  57.     public GuardedObject(Object object, Guard guard)
  58.     {
  59.         this.guard = guard;
  60.         this.object = object;
  61.     }
  62.  
  63.     /**
  64.      * Retrieves the guarded object, or throws an exception if access
  65.      * to the guarded object is denied by the guard.
  66.      * 
  67.      * @return the guarded object.
  68.      * 
  69.      * @exception SecurityException if access to the guarded object is 
  70.      * denied.
  71.      */
  72.     public Object getObject() 
  73.         throws SecurityException
  74.     {
  75.         if (guard != null)
  76.             guard.checkGuard(object);
  77.  
  78.         return object;
  79.     }
  80. }
  81.