• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package java.security;
19 
20 import java.io.IOException;
21 import java.io.Serializable;
22 
23 /**
24  * {@code GuardedObject} controls access to an object, by checking all requests
25  * for the object with a {@code Guard}.
26  */
27 public class GuardedObject implements Serializable {
28 
29     private static final long serialVersionUID = -5240450096227834308L;
30 
31     private final Object object;
32 
33     private final Guard guard;
34 
35     /**
36      * Constructs a new instance of {@code GuardedObject} which protects access
37      * to the specified {@code Object} using the specified {@code Guard}.
38      *
39      * @param object
40      *            the {@code Object} to protect.
41      * @param guard
42      *            the {@code Guard} which protects the specified {@code Object},
43      *            maybe {@code null}.
44      */
GuardedObject(Object object, Guard guard)45     public GuardedObject(Object object, Guard guard) {
46         this.object = object;
47         this.guard = guard;
48     }
49 
50     /**
51      * Returns the guarded {@code Object} if the associated {@code Guard}
52      * permits access. If access is not granted, then a {@code
53      * SecurityException} is thrown.
54      *
55      * @return the guarded object.
56      * @exception SecurityException
57      *                if access is not granted to the guarded object.
58      */
getObject()59     public Object getObject() throws SecurityException {
60         if (guard != null) {
61             guard.checkGuard(object);
62         }
63         return object;
64     }
65 
66     /**
67      * Checks the guard (if there is one) before performing a default
68      * serialization.
69      */
writeObject(java.io.ObjectOutputStream out)70     private void writeObject(java.io.ObjectOutputStream out) throws IOException {
71         if (guard != null) {
72             guard.checkGuard(object);
73         }
74         out.defaultWriteObject();
75     }
76 }
77