01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: /**
19: * @author Alexey V. Varlamov
20: * @version $Revision$
21: */package java.security;
22:
23: import java.io.IOException;
24: import java.io.Serializable;
25:
26: /**
27: * GuardedObject controls access to an object, by checking all requests for the
28: * object with a Guard.
29: *
30: */
31: public class GuardedObject implements Serializable {
32:
33: /**
34: * @com.intel.drl.spec_ref
35: */
36: private static final long serialVersionUID = -5240450096227834308L;
37:
38: /**
39: * @com.intel.drl.spec_ref
40: */
41: private final Object object;
42:
43: /**
44: * @com.intel.drl.spec_ref
45: */
46: private final Guard guard;
47:
48: /**
49: * Constructs a GuardedObject to protect access to the specified Object
50: * using the specified Guard.
51: *
52: * @param object
53: * the Object to guard
54: * @param guard
55: * the Guard
56: */
57: public GuardedObject(Object object, Guard guard) {
58: this .object = object;
59: this .guard = guard;
60: }
61:
62: /**
63: * Checks whether access should be granted to the object. If access is
64: * granted, this method returns the object. If it is not granted, then a
65: * <code>SecurityException</code> is thrown.
66: *
67: *
68: * @return the guarded object
69: *
70: * @exception java.lang.SecurityException
71: * If access is not granted to the object
72: */
73: public Object getObject() throws SecurityException {
74: if (guard != null) {
75: guard.checkGuard(object);
76: }
77: return object;
78: }
79:
80: /**
81: * Checks guard (if there is one) before performing a default serialization.
82: */
83: private void writeObject(java.io.ObjectOutputStream out)
84: throws IOException {
85: if (guard != null) {
86: guard.checkGuard(object);
87: }
88: out.defaultWriteObject();
89: }
90: }
|