Path: blob/master/src/java.base/share/classes/java/security/GuardedObject.java
41152 views
/*1* Copyright (c) 1997, 2021, 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;2627import java.io.IOException;2829/**30* A GuardedObject is an object that is used to protect access to31* another object.32*33* <p>A GuardedObject encapsulates a target object and a Guard object,34* such that access to the target object is possible35* only if the Guard object allows it.36* Once an object is encapsulated by a GuardedObject,37* access to that object is controlled by the {@code getObject}38* method, which invokes the39* {@code checkGuard} method on the Guard object that is40* guarding access. If access is not allowed,41* an exception is thrown.42*43* @see Guard44* @see Permission45*46* @author Roland Schemers47* @author Li Gong48* @since 1.249*/5051public class GuardedObject implements java.io.Serializable {5253@java.io.Serial54private static final long serialVersionUID = -5240450096227834308L;5556/**57* The object we are guarding.58*/59@SuppressWarnings("serial") // Not statically typed as Serializable60private Object object;6162/**63* The guard object.64*/65@SuppressWarnings("serial") // Not statically typed as Serializable66private Guard guard;6768/**69* Constructs a GuardedObject using the specified object and guard.70* If the Guard object is null, then no restrictions will71* be placed on who can access the object.72*73* @param object the object to be guarded.74*75* @param guard the Guard object that guards access to the object.76*/7778public GuardedObject(Object object, Guard guard)79{80this.guard = guard;81this.object = object;82}8384/**85* Retrieves the guarded object, or throws an exception if access86* to the guarded object is denied by the guard.87*88* @return the guarded object.89*90* @throws SecurityException if access to the guarded object is91* denied.92*/93public Object getObject()94throws SecurityException95{96if (guard != null)97guard.checkGuard(object);9899return object;100}101102/**103* Writes this object out to a stream (i.e., serializes it).104* We check the guard if there is one.105*106* @param oos the {@code ObjectOutputStream} to which data is written107* @throws IOException if an I/O error occurs108*/109@java.io.Serial110private void writeObject(java.io.ObjectOutputStream oos)111throws IOException112{113if (guard != null)114guard.checkGuard(object);115116oos.defaultWriteObject();117}118}119120121