Path: blob/master/src/java.rmi/share/classes/sun/rmi/transport/WeakRef.java
41154 views
/*1* Copyright (c) 1996, 2012, 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*/24package sun.rmi.transport;2526import java.lang.ref.*;27import sun.rmi.runtime.Log;2829/**30* WeakRef objects are used by the RMI runtime to hold potentially weak31* references to exported remote objects in the local object table.32*33* This class extends the functionality of java.lang.ref.WeakReference in34* several ways. The methods pin() and unpin() can be used to set35* whether the contained reference is strong or weak (it is weak upon36* construction). The hashCode() and equals() methods are overridden so37* that WeakRef objects hash and compare to each other according to the38* object identity of their referents.39*40* @author Ann Wollrath41* @author Peter Jones42*/43class WeakRef extends WeakReference<Object> {4445/** value of the referent's "identity" hash code */46private int hashValue;4748/** strong reference to the referent, for when this WeakRef is "pinned" */49private Object strongRef = null;5051/**52* Create a new WeakRef to the given object.53*/54public WeakRef(Object obj) {55super(obj);56setHashValue(obj); // cache object's "identity" hash code57}5859/**60* Create a new WeakRef to the given object, registered with a queue.61*/62public WeakRef(Object obj, ReferenceQueue<Object> q) {63super(obj, q);64setHashValue(obj); // cache object's "identity" hash code65}6667/**68* Pin the contained reference (make this a strong reference).69*/70public synchronized void pin() {71if (strongRef == null) {72strongRef = get();7374if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {75DGCImpl.dgcLog.log(Log.VERBOSE,76"strongRef = " + strongRef);77}78}79}8081/**82* Unpin the contained reference (make this a weak reference).83*/84public synchronized void unpin() {85if (strongRef != null) {86if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {87DGCImpl.dgcLog.log(Log.VERBOSE,88"strongRef = " + strongRef);89}9091strongRef = null;92}93}9495/*96* Cache referent's "identity" hash code (so that we still have the97* value after the referent gets cleared).98*99* We cannot use the value from the object's hashCode() method, since100* if the object is of a remote class not extended from RemoteObject101* and it is trying to implement hashCode() and equals() so that it102* can be compared to stub objects, its own hash code could not have103* been initialized yet (see bugid 4102938). Also, object table keys104* based on server objects are indeed matched on object identity, so105* this is the correct hash technique regardless.106*/107private void setHashValue(Object obj) {108if (obj != null) {109hashValue = System.identityHashCode(obj);110} else {111hashValue = 0;112}113}114115/**116* Always return the "identity" hash code of the original referent.117*/118public int hashCode() {119return hashValue;120}121122/**123* Return true if "obj" is this identical WeakRef object, or, if the124* contained reference has not been cleared, if "obj" is another WeakRef125* object with the identical non-null referent. Otherwise, return false.126*/127public boolean equals(Object obj) {128if (obj instanceof WeakRef) {129if (obj == this)130return true;131132Object referent = get();133return (referent != null) && (referent == ((WeakRef) obj).get());134} else {135return false;136}137}138}139140141