Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/GuardedObject.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627/**28* A GuardedObject is an object that is used to protect access to29* another object.30*31* <p>A GuardedObject encapsulates a target object and a Guard object,32* such that access to the target object is possible33* only if the Guard object allows it.34* Once an object is encapsulated by a GuardedObject,35* access to that object is controlled by the {@code getObject}36* method, which invokes the37* {@code checkGuard} method on the Guard object that is38* guarding access. If access is not allowed,39* an exception is thrown.40*41* @see Guard42* @see Permission43*44* @author Roland Schemers45* @author Li Gong46*/4748public class GuardedObject implements java.io.Serializable {4950private static final long serialVersionUID = -5240450096227834308L;5152private Object object; // the object we are guarding53private Guard guard; // the guard5455/**56* Constructs a GuardedObject using the specified object and guard.57* If the Guard object is null, then no restrictions will58* be placed on who can access the object.59*60* @param object the object to be guarded.61*62* @param guard the Guard object that guards access to the object.63*/6465public GuardedObject(Object object, Guard guard)66{67this.guard = guard;68this.object = object;69}7071/**72* Retrieves the guarded object, or throws an exception if access73* to the guarded object is denied by the guard.74*75* @return the guarded object.76*77* @exception SecurityException if access to the guarded object is78* denied.79*/80public Object getObject()81throws SecurityException82{83if (guard != null)84guard.checkGuard(object);8586return object;87}8889/**90* Writes this object out to a stream (i.e., serializes it).91* We check the guard if there is one.92*/93private void writeObject(java.io.ObjectOutputStream oos)94throws java.io.IOException95{96if (guard != null)97guard.checkGuard(object);9899oos.defaultWriteObject();100}101}102103104