KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > GuardedObject


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
8 package java.security;
9
10 /**
11  * A GuardedObject is an object that is used to protect access to
12  * another object.
13  *
14  * <p>A GuardedObject encapsulates a target object and a Guard object,
15  * such that access to the target object is possible
16  * only if the Guard object allows it.
17  * Once an object is encapsulated by a GuardedObject,
18  * access to that object is controlled by the <code>getObject</code>
19  * method, which invokes the
20  * <code>checkGuard</code> method on the Guard object that is
21  * guarding access. If access is not allowed,
22  * an exception is thrown.
23  *
24  * @see Guard
25  * @see Permission
26  *
27  * @version 1.15 03/12/19
28  * @author Roland Schemers
29  * @author Li Gong
30  */

31
32 public class GuardedObject implements java.io.Serializable JavaDoc {
33
34     private static final long serialVersionUID = -5240450096227834308L;
35
36     private Object JavaDoc object; // the object we are guarding
37
private Guard JavaDoc guard; // the guard
38

39     /**
40      * Constructs a GuardedObject using the specified object and guard.
41      * If the Guard object is null, then no restrictions will
42      * be placed on who can access the object.
43      *
44      * @param object the object to be guarded.
45      *
46      * @param guard the Guard object that guards access to the object.
47      */

48
49     public GuardedObject(Object JavaDoc object, Guard JavaDoc guard)
50     {
51         this.guard = guard;
52         this.object = object;
53     }
54
55     /**
56      * Retrieves the guarded object, or throws an exception if access
57      * to the guarded object is denied by the guard.
58      *
59      * @return the guarded object.
60      *
61      * @exception SecurityException if access to the guarded object is
62      * denied.
63      */

64     public Object JavaDoc getObject()
65         throws SecurityException JavaDoc
66     {
67         if (guard != null)
68             guard.checkGuard(object);
69
70         return object;
71     }
72
73     /**
74      * Writes this object out to a stream (i.e., serializes it).
75      * We check the guard if there is one.
76      */

77     private synchronized void writeObject(java.io.ObjectOutputStream JavaDoc oos)
78         throws java.io.IOException JavaDoc
79     {
80         if (guard != null)
81             guard.checkGuard(object);
82     
83     oos.defaultWriteObject();
84     }
85 }
86
Popular Tags