01: /*
02: * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package java.security;
27:
28: /**
29: * A GuardedObject is an object that is used to protect access to
30: * another object.
31: *
32: * <p>A GuardedObject encapsulates a target object and a Guard object,
33: * such that access to the target object is possible
34: * only if the Guard object allows it.
35: * Once an object is encapsulated by a GuardedObject,
36: * access to that object is controlled by the <code>getObject</code>
37: * method, which invokes the
38: * <code>checkGuard</code> method on the Guard object that is
39: * guarding access. If access is not allowed,
40: * an exception is thrown.
41: *
42: * @see Guard
43: * @see Permission
44: *
45: * @version 1.23 07/05/05
46: * @author Roland Schemers
47: * @author Li Gong
48: */
49:
50: public class GuardedObject implements java.io.Serializable {
51:
52: private static final long serialVersionUID = -5240450096227834308L;
53:
54: private Object object; // the object we are guarding
55: private Guard guard; // the guard
56:
57: /**
58: * Constructs a GuardedObject using the specified object and guard.
59: * If the Guard object is null, then no restrictions will
60: * be placed on who can access the object.
61: *
62: * @param object the object to be guarded.
63: *
64: * @param guard the Guard object that guards access to the object.
65: */
66:
67: public GuardedObject(Object object, Guard guard) {
68: this .guard = guard;
69: this .object = object;
70: }
71:
72: /**
73: * Retrieves the guarded object, or throws an exception if access
74: * to the guarded object is denied by the guard.
75: *
76: * @return the guarded object.
77: *
78: * @exception SecurityException if access to the guarded object is
79: * denied.
80: */
81: public Object getObject() throws SecurityException {
82: if (guard != null)
83: guard.checkGuard(object);
84:
85: return object;
86: }
87:
88: /**
89: * Writes this object out to a stream (i.e., serializes it).
90: * We check the guard if there is one.
91: */
92: private void writeObject(java.io.ObjectOutputStream oos)
93: throws java.io.IOException {
94: if (guard != null)
95: guard.checkGuard(object);
96:
97: oos.defaultWriteObject();
98: }
99: }
|