001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov
021: * @version $Revision: 1.1.2.2 $
022: */package org.apache.harmony.rmi.server;
023:
024: import java.util.Hashtable;
025: import java.rmi.server.ObjID;
026:
027: /**
028: * Defines structure for storing RMIObjects and methods for finding them by
029: * different separate keys, which are unique inside the table: ObjID and
030: * RMIReference to the object's implementation.
031: *
032: * @author Mikhail A. Markov
033: * @version $Revision: 1.1.2.2 $
034: */
035: final class RMIObjectTable {
036:
037: // Table where key is ObjID.
038: private Hashtable idTable = new Hashtable();
039:
040: // Table where key is RMIReference to the impl.
041: private Hashtable refTable = new Hashtable();
042:
043: /*
044: * Object using for synchronization, because we should change
045: * 2 tables simultaneously.
046: */
047: private class TablesLock {
048: }
049:
050: private Object tablesLock = new TablesLock();
051:
052: /**
053: * Adds specified info to the table if there are no elements with the
054: * same ObjID or RMIReference to the impl there.
055: *
056: * @param info RMIObjectInfo to be added to the table
057: *
058: * @return true if the table did not contain the given info and the info
059: * was successfully added to the table and false otherwise
060: *
061: * @throws NullPointerException if info is null or info.id is null
062: */
063: public boolean add(RMIObjectInfo info) {
064: synchronized (tablesLock) {
065: if (!idTable.containsKey(info.id)) {
066: idTable.put(info.id, info);
067: refTable.put(info.ref, info);
068: return true;
069: }
070: }
071: return false;
072: }
073:
074: /**
075: * Returns true if the table contains element with specified RMIReference
076: * and false otherwise.
077: *
078: * @param ref RMIReference to the remote object implementation to be used
079: * as a key
080: *
081: * @return true if the table contains element with specified ref and false
082: * otherwise
083: */
084: public boolean containsByRef(RMIReference ref) {
085: synchronized (tablesLock) {
086: return refTable.containsKey(ref);
087: }
088: }
089:
090: /**
091: * Returns true if the table contains element with specified id and false
092: * otherwise.
093: *
094: * @param id ObjID to be used as a key
095: *
096: * @return true if the table contains element with specified id and false
097: * otherwise
098: */
099: public boolean containsById(ObjID id) {
100: synchronized (tablesLock) {
101: return idTable.containsKey(id);
102: }
103: }
104:
105: /**
106: * Returns true if the table contains element which is equal to the info
107: * specified.
108: *
109: * @param info RMIObjectInfo to be used as a key
110: *
111: * @return true if the table contains element which is equal to the info
112: * specified
113: */
114: public boolean contains(RMIObjectInfo info) {
115: if (info != null && info.id != null) {
116: synchronized (tablesLock) {
117: return containsById(info.id);
118: }
119: }
120: return false;
121: }
122:
123: /**
124: * Finds and returns RMIObjectInfo found by specified reference to remote
125: * object implementation or null if record has not been found.
126: *
127: * @param ref RMIReference to the remote object implementation to be used
128: * as a key
129: *
130: * @return RMIObjectInfo if found in the table or null if matching record
131: * has not been found
132: */
133: public RMIObjectInfo getByRef(RMIReference ref) {
134: synchronized (tablesLock) {
135: return (RMIObjectInfo) refTable.get(ref);
136: }
137: }
138:
139: /**
140: * Finds and returns RMIObjectInfo found by specified ObjID or null
141: * if record has not been found.
142: *
143: * @param id ObjID to use as a key
144: *
145: * @return RMIObjectInfo if found in the table or null if matching record
146: * has not been found
147: */
148: public RMIObjectInfo getById(ObjID id) {
149: synchronized (tablesLock) {
150: return (RMIObjectInfo) idTable.get(id);
151: }
152: }
153:
154: /**
155: * Removes RMIObjectInfo found by specified reference to remote object
156: * implementation from the table.
157: *
158: * @param ref RMIReference to the remote object implementation to be used
159: * as a key
160: *
161: * @return RMIObjectInfo if found in the table or null if matching record
162: * has not been found
163: */
164: public RMIObjectInfo removeByRef(RMIReference ref) {
165: RMIObjectInfo info;
166:
167: synchronized (tablesLock) {
168: info = (RMIObjectInfo) refTable.remove(ref);
169:
170: if (info != null) {
171: idTable.remove(info.id);
172: }
173: }
174: return info;
175: }
176:
177: /**
178: * Removes RMIObjectInfo found by specified ObjID from the table.
179: *
180: * @param id ObjID to use as a key
181: *
182: * @return RMIObjectInfo if found in the table or null if matching record
183: * has not been found
184: */
185: public RMIObjectInfo removeById(ObjID id) {
186: RMIObjectInfo info;
187:
188: synchronized (tablesLock) {
189: info = (RMIObjectInfo) idTable.remove(id);
190:
191: if (info != null) {
192: refTable.remove(info.ref);
193: }
194: }
195: return info;
196: }
197:
198: /**
199: * Returns true if this table contains no records and false otherwise.
200: *
201: * @return true if this table contains no records and false otherwise
202: */
203: public boolean isEmpty() {
204: synchronized (tablesLock) {
205: return idTable.isEmpty();
206: }
207: }
208: }
|