1 /*
2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.security;
27 
28 import java.io.IOException;
29 
30 /**
31  * A GuardedObject is an object that is used to protect access to
32  * another object.
33  *
34  * <p>A GuardedObject encapsulates a target object and a Guard object,
35  * such that access to the target object is possible
36  * only if the Guard object allows it.
37  * Once an object is encapsulated by a GuardedObject,
38  * access to that object is controlled by the {@code getObject}
39  * method, which invokes the
40  * {@code checkGuard} method on the Guard object that is
41  * guarding access. If access is not allowed,
42  * an exception is thrown.
43  *
44  * @see Guard
45  * @see Permission
46  *
47  * @author Roland Schemers
48  * @author Li Gong
49  * @since 1.2
50  */
51 
52 public class GuardedObject implements java.io.Serializable {
53 
54     @java.io.Serial
55     private static final long serialVersionUID = -5240450096227834308L;
56 
57     /**
58      * The object we are guarding.
59      */
60     @SuppressWarnings("serial") // Not statically typed as Serializable
61     private Object object;
62 
63     /**
64      * The guard object.
65      */
66     @SuppressWarnings("serial") // Not statically typed as Serializable
67     private Guard guard;
68 
69     /**
70      * Constructs a GuardedObject using the specified object and guard.
71      * If the Guard object is null, then no restrictions will
72      * be placed on who can access the object.
73      *
74      * @param object the object to be guarded.
75      *
76      * @param guard the Guard object that guards access to the object.
77      */
78 
GuardedObject(Object object, Guard guard)79     public GuardedObject(Object object, Guard guard)
80     {
81         this.guard = guard;
82         this.object = object;
83     }
84 
85     /**
86      * Retrieves the guarded object, or throws an exception if access
87      * to the guarded object is denied by the guard.
88      *
89      * @return the guarded object.
90      *
91      * @throws    SecurityException if access to the guarded object is
92      * denied.
93      */
getObject()94     public Object getObject()
95         throws SecurityException
96     {
97         if (guard != null)
98             guard.checkGuard(object);
99 
100         return object;
101     }
102 
103     /**
104      * Writes this object out to a stream (i.e., serializes it).
105      * We check the guard if there is one.
106      *
107      * @param  oos the {@code ObjectOutputStream} to which data is written
108      * @throws IOException if an I/O error occurs
109      */
110     @java.io.Serial
writeObject(java.io.ObjectOutputStream oos)111     private void writeObject(java.io.ObjectOutputStream oos)
112         throws IOException
113     {
114         if (guard != null)
115             guard.checkGuard(object);
116 
117         oos.defaultWriteObject();
118     }
119 }
120