1 /* 2 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.rmi.transport; 26 27 import java.lang.ref.ReferenceQueue; 28 import java.rmi.NoSuchObjectException; 29 import java.rmi.Remote; 30 import java.rmi.dgc.VMID; 31 import java.rmi.server.ExportException; 32 import java.rmi.server.ObjID; 33 import java.security.AccessController; 34 import java.security.PrivilegedAction; 35 import java.util.HashMap; 36 import java.util.Map; 37 import sun.misc.GC; 38 import sun.rmi.runtime.Log; 39 import sun.rmi.runtime.NewThreadAction; 40 import sun.security.action.GetLongAction; 41 42 /** 43 * Object table shared by all implementors of the Transport interface. 44 * This table maps object ids to remote object targets in this address 45 * space. 46 * 47 * @author Ann Wollrath 48 * @author Peter Jones 49 */ 50 public final class ObjectTable { 51 52 /** maximum interval between complete garbage collections of local heap */ 53 private final static long gcInterval = // default 1 hour 54 AccessController.doPrivileged( 55 new GetLongAction("sun.rmi.dgc.server.gcInterval", 3600000)); 56 57 /** 58 * lock guarding objTable and implTable. 59 * Holders MAY acquire a Target instance's lock or keepAliveLock. 60 */ 61 private static final Object tableLock = new Object(); 62 63 /** tables mapping to Target, keyed from ObjectEndpoint and impl object */ 64 private static final Map<ObjectEndpoint,Target> objTable = 65 new HashMap<ObjectEndpoint,Target>(); 66 private static final Map<WeakRef,Target> implTable = 67 new HashMap<WeakRef,Target>(); 68 69 /** 70 * lock guarding keepAliveCount, reaper, and gcLatencyRequest. 71 * Holders may NOT acquire a Target instance's lock or tableLock. 72 */ 73 private static final Object keepAliveLock = new Object(); 74 75 /** count of non-permanent objects in table or still processing calls */ 76 private static int keepAliveCount = 0; 77 78 /** thread to collect unreferenced objects from table */ 79 private static Thread reaper = null; 80 81 /** queue notified when weak refs in the table are cleared */ 82 static final ReferenceQueue reapQueue = new ReferenceQueue(); 83 84 /** handle for GC latency request (for future cancellation) */ 85 private static GC.LatencyRequest gcLatencyRequest = null; 86 87 /* 88 * Disallow anyone from creating one of these. 89 */ 90 private ObjectTable() {} 91 92 /** 93 * Returns the target associated with the object id. 94 */ 95 static Target getTarget(ObjectEndpoint oe) { 96 synchronized (tableLock) { 97 return objTable.get(oe); 98 } 99 } 100 101 /** 102 * Returns the target associated with the remote object 103 */ 104 public static Target getTarget(Remote impl) { 105 synchronized (tableLock) { 106 return implTable.get(new WeakRef(impl)); 107 } 108 } 109 110 /** 111 * Returns the stub for the remote object <b>obj</b> passed 112 * as a parameter. This operation is only valid <i>after</i> 113 * the object has been exported. 114 * 115 * @return the stub for the remote object, <b>obj</b>. 116 * @exception NoSuchObjectException if the stub for the 117 * remote object could not be found. 118 */ 119 public static Remote getStub(Remote impl) 120 throws NoSuchObjectException 121 { 122 Target target = getTarget(impl); 123 if (target == null) { 124 throw new NoSuchObjectException("object not exported"); 125 } else { 126 return target.getStub(); 127 } 128 } 129 130 /** 131 * Remove the remote object, obj, from the RMI runtime. If 132 * successful, the object can no longer accept incoming RMI calls. 133 * If the force parameter is true, the object is forcibly unexported 134 * even if there are pending calls to the remote object or the 135 * remote object still has calls in progress. If the force 136 * parameter is false, the object is only unexported if there are 137 * no pending or in progress calls to the object. 138 * 139 * @param obj the remote object to be unexported 140 * @param force if true, unexports the object even if there are 141 * pending or in-progress calls; if false, only unexports the object 142 * if there are no pending or in-progress calls 143 * @return true if operation is successful, false otherwise 144 * @exception NoSuchObjectException if the remote object is not 145 * currently exported 146 */ 147 public static boolean unexportObject(Remote obj, boolean force) 148 throws java.rmi.NoSuchObjectException 149 { 150 synchronized (tableLock) { 151 Target target = getTarget(obj); 152 if (target == null) { 153 throw new NoSuchObjectException("object not exported"); 154 } else { 155 if (target.unexport(force)) { 156 removeTarget(target); 157 return true; 158 } else { 159 return false; 160 } 161 } 162 } 163 } 164 165 /** 166 * Add target to object table. If it is not a permanent entry, then 167 * make sure that reaper thread is running to remove collected entries 168 * and keep VM alive. 169 */ 170 static void putTarget(Target target) throws ExportException { 171 ObjectEndpoint oe = target.getObjectEndpoint(); 172 WeakRef weakImpl = target.getWeakImpl(); 173 174 if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) { 175 DGCImpl.dgcLog.log(Log.VERBOSE, "add object " + oe); 176 } 177 178 synchronized (tableLock) { 179 if (target.getImpl() != null) { 180 if (objTable.containsKey(oe)) { 181 throw new ExportException( 182 "internal error: ObjID already in use"); 183 } else if (implTable.containsKey(weakImpl)) { 184 throw new ExportException("object already exported"); 185 } 186 187 objTable.put(oe, target); 188 implTable.put(weakImpl, target); 189 190 if (!target.isPermanent()) { 191 incrementKeepAliveCount(); 192 } 193 } 194 } 195 } 196 197 /** 198 * Remove target from object table. 199 * 200 * NOTE: This method must only be invoked while synchronized on 201 * the "tableLock" object, because it does not do so itself. 202 */ 203 private static void removeTarget(Target target) { 204 // assert Thread.holdsLock(tableLock); 205 206 ObjectEndpoint oe = target.getObjectEndpoint(); 207 WeakRef weakImpl = target.getWeakImpl(); 208 209 if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) { 210 DGCImpl.dgcLog.log(Log.VERBOSE, "remove object " + oe); 211 } 212 213 objTable.remove(oe); 214 implTable.remove(weakImpl); 215 216 target.markRemoved(); // handles decrementing keep-alive count 217 } 218 219 /** 220 * Process client VM signalling reference for given ObjID: forward to 221 * correspoding Target entry. If ObjID is not found in table, 222 * no action is taken. 223 */ 224 static void referenced(ObjID id, long sequenceNum, VMID vmid) { 225 synchronized (tableLock) { 226 ObjectEndpoint oe = 227 new ObjectEndpoint(id, Transport.currentTransport()); 228 Target target = objTable.get(oe); 229 if (target != null) { 230 target.referenced(sequenceNum, vmid); 231 } 232 } 233 } 234 235 /** 236 * Process client VM dropping reference for given ObjID: forward to 237 * correspoding Target entry. If ObjID is not found in table, 238 * no action is taken. 239 */ 240 static void unreferenced(ObjID id, long sequenceNum, VMID vmid, 241 boolean strong) 242 { 243 synchronized (tableLock) { 244 ObjectEndpoint oe = 245 new ObjectEndpoint(id, Transport.currentTransport()); 246 Target target = objTable.get(oe); 247 if (target != null) 248 target.unreferenced(sequenceNum, vmid, strong); 249 } 250 } 251 252 /** 253 * Increments the "keep-alive count". 254 * 255 * The "keep-alive count" is the number of non-permanent remote objects 256 * that are either in the object table or still have calls in progress. 257 * Therefore, this method should be invoked exactly once for every 258 * non-permanent remote object exported (a remote object must be 259 * exported before it can have any calls in progress). 260 * 261 * The VM is "kept alive" while the keep-alive count is greater than 262 * zero; this is accomplished by keeping a non-daemon thread running. 263 * 264 * Because non-permanent objects are those that can be garbage 265 * collected while exported, and thus those for which the "reaper" 266 * thread operates, the reaper thread also serves as the non-daemon 267 * VM keep-alive thread; a new reaper thread is created if necessary. 268 */ 269 static void incrementKeepAliveCount() { 270 synchronized (keepAliveLock) { 271 keepAliveCount++; 272 273 if (reaper == null) { 274 reaper = AccessController.doPrivileged( 275 new NewThreadAction(new Reaper(), "Reaper", false)); 276 reaper.start(); 277 } 278 279 /* 280 * While there are non-"permanent" objects in the object table, 281 * request a maximum latency for inspecting the entire heap 282 * from the local garbage collector, to place an upper bound 283 * on the time to discover remote objects that have become 284 * unreachable (and thus can be removed from the table). 285 */ 286 if (gcLatencyRequest == null) { 287 gcLatencyRequest = GC.requestLatency(gcInterval); 288 } 289 } 290 } 291 292 /** 293 * Decrements the "keep-alive count". 294 * 295 * The "keep-alive count" is the number of non-permanent remote objects 296 * that are either in the object table or still have calls in progress. 297 * Therefore, this method should be invoked exactly once for every 298 * previously-exported non-permanent remote object that both has been 299 * removed from the object table and has no calls still in progress. 300 * 301 * If the keep-alive count is decremented to zero, then the current 302 * reaper thread is terminated to cease keeping the VM alive (and 303 * because there are no more non-permanent remote objects to reap). 304 */ 305 static void decrementKeepAliveCount() { 306 synchronized (keepAliveLock) { 307 keepAliveCount--; 308 309 if (keepAliveCount == 0) { 310 if (!(reaper != null)) { throw new AssertionError(); } 311 AccessController.doPrivileged(new PrivilegedAction<Void>() { 312 public Void run() { 313 reaper.interrupt(); 314 return null; 315 } 316 }); 317 reaper = null; 318 319 /* 320 * If there are no longer any non-permanent objects in the 321 * object table, we are no longer concerned with the latency 322 * of local garbage collection here. 323 */ 324 gcLatencyRequest.cancel(); 325 gcLatencyRequest = null; 326 } 327 } 328 } 329 330 /** 331 * The Reaper thread waits for notifications that weak references in the 332 * object table have been cleared. When it receives a notification, it 333 * removes the corresponding entry from the table. 334 * 335 * Since the Reaper is created as a non-daemon thread, it also serves 336 * to keep the VM from exiting while there are objects in the table 337 * (other than permanent entries that should neither be reaped nor 338 * keep the VM alive). 339 */ 340 private static class Reaper implements Runnable { 341 342 public void run() { 343 try { 344 do { 345 // wait for next cleared weak reference 346 WeakRef weakImpl = (WeakRef) reapQueue.remove(); 347 348 synchronized (tableLock) { 349 Target target = implTable.get(weakImpl); 350 if (target != null) { 351 if (!target.isEmpty()) { 352 throw new Error( 353 "object with known references collected"); 354 } else if (target.isPermanent()) { 355 throw new Error("permanent object collected"); 356 } 357 removeTarget(target); 358 } 359 } 360 } while (!Thread.interrupted()); 361 } catch (InterruptedException e) { 362 // pass away if interrupted 363 } 364 } 365 } 366 }