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.3 $
022: */package org.apache.harmony.rmi.server;
023:
024: import java.lang.ref.ReferenceQueue;
025: import java.lang.ref.WeakReference;
026:
027: import org.apache.harmony.rmi.common.RMILog;
028: import org.apache.harmony.rmi.internal.nls.Messages;
029:
030: /**
031: * Extension of WeakReference. It could contain strong reference to the object.
032: * Also hashCode() and equals() methods are overridden. This class is used for
033: * storing exported rmi objects.
034: *
035: * @author Mikhail A. Markov
036: * @version $Revision: 1.1.2.3 $
037: */
038: public class RMIReference extends WeakReference {
039:
040: // strong reference to the object this class refers to
041: private Object strongRef = null;
042:
043: // Hash code for the referenced object.
044: private int objHashCode;
045:
046: /**
047: * Constructs RMIReference from the given Object.
048: */
049: public RMIReference(Object obj) {
050: super (obj);
051: objHashCode = System.identityHashCode(obj);
052: }
053:
054: /**
055: * Constructs RMIReference from the given Object and ReferenceQueue.
056: */
057: public RMIReference(Object obj, ReferenceQueue queue) {
058: super (obj, queue);
059: objHashCode = System.identityHashCode(obj);
060: }
061:
062: /**
063: * If the given parameter is true then makes this reference strong;
064: * otherwise makes this reference weak.
065: *
066: * @param strong if true then this reference should be made strong
067: */
068: public synchronized void makeStrong(boolean strong) {
069: if (strong) {
070: if (strongRef == null) {
071: strongRef = get();
072:
073: if (DGCImpl.dgcLog.isLoggable(RMILog.VERBOSE)) {
074: // rmi.log.9F=Strongly referenced {0}
075: DGCImpl.dgcLog.log(RMILog.VERBOSE, Messages
076: .getString("rmi.log.9F", //$NON-NLS-1$
077: strongRef));
078: }
079: }
080: } else {
081: if (strongRef != null
082: && DGCImpl.dgcLog.isLoggable(RMILog.VERBOSE)) {
083: // rmi.log.10D=Weakly referenced {0}
084: DGCImpl.dgcLog.log(RMILog.VERBOSE, Messages.getString(
085: "rmi.log.10D", //$NON-NLS-1$
086: strongRef));
087: }
088: strongRef = null;
089: }
090: }
091:
092: /**
093: * Returns true if the given object is an instance of this class and refer
094: * to the same object as this reference and false otherwise.
095: *
096: * @param obj another object to be compared
097: *
098: * @return true if the given object is an instance of this class and refer
099: * to the same object as this reference and false otherwise
100: */
101: public boolean equals(Object obj) {
102: if (obj == this ) {
103: return true;
104: }
105:
106: if (!(obj instanceof RMIReference)) {
107: return false;
108: }
109: Object refObj = get();
110:
111: if (refObj == null) {
112: return false;
113: }
114: return refObj == ((WeakReference) obj).get();
115: }
116:
117: /**
118: * Returns hash code for the referenced object.
119: *
120: * @return hash code for the referenced object
121: */
122: public int hashCode() {
123: return objHashCode;
124: }
125: }
|