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.10.4.3 $
022: */package java.rmi.server;
023:
024: import java.io.Serializable;
025: import java.io.IOException;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.rmi.MarshalException;
029: import java.rmi.NoSuchObjectException;
030: import java.rmi.Remote;
031: import java.rmi.UnmarshalException;
032:
033: import org.apache.harmony.rmi.common.RMIUtil;
034: import org.apache.harmony.rmi.internal.nls.Messages;
035: import org.apache.harmony.rmi.server.ExportManager;
036:
037: /**
038: * @com.intel.drl.spec_ref
039: *
040: * @author Mikhail A. Markov
041: * @version $Revision: 1.10.4.3 $
042: */
043: public abstract class RemoteObject implements Remote, Serializable {
044:
045: private static final long serialVersionUID = -3215090123894869218L;
046:
047: /** @com.intel.drl.spec_ref */
048: protected transient RemoteRef ref;
049:
050: /**
051: * @com.intel.drl.spec_ref
052: */
053: protected RemoteObject() {
054: ref = null;
055: }
056:
057: /**
058: * @com.intel.drl.spec_ref
059: */
060: protected RemoteObject(RemoteRef ref) {
061: this .ref = ref;
062: }
063:
064: /**
065: * @com.intel.drl.spec_ref
066: */
067: public RemoteRef getRef() {
068: return ref;
069: }
070:
071: /**
072: * @com.intel.drl.spec_ref
073: */
074: public String toString() {
075: String clName = RMIUtil.getShortName(getClass());
076: return (ref == null) ? clName : clName
077: + "[" + ref.remoteToString() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
078: }
079:
080: /**
081: * @com.intel.drl.spec_ref
082: */
083: public boolean equals(Object obj) {
084: if (!(obj instanceof RemoteObject)) {
085: return (obj == null) ? false : obj.equals(this );
086: }
087:
088: if (ref != null) {
089: return ref.remoteEquals(((RemoteObject) obj).ref);
090: }
091: return (this == obj);
092: }
093:
094: /**
095: * @com.intel.drl.spec_ref
096: */
097: public int hashCode() {
098: return (ref != null) ? ref.remoteHashCode() : super .hashCode();
099: }
100:
101: /**
102: * @com.intel.drl.spec_ref
103: */
104: public static Remote toStub(Remote obj)
105: throws NoSuchObjectException {
106: if (obj instanceof RemoteStub) {
107: return (RemoteStub) obj;
108: }
109: return ExportManager.getStub(obj);
110: }
111:
112: /**
113: * @com.intel.drl.spec_ref
114: */
115: private void writeObject(ObjectOutputStream out) throws IOException {
116: if (ref == null) {
117: // rmi.17=Invalid remote object: RemoteRef = null
118: throw new MarshalException(Messages.getString("rmi.17")); //$NON-NLS-1$
119: }
120: String refType = ref.getRefClass(out);
121:
122: if (refType != null && refType.length() != 0) {
123: out.writeUTF(refType);
124: ref.writeExternal(out);
125: } else {
126: out.writeUTF(""); //$NON-NLS-1$
127: out.writeObject(ref);
128: }
129: }
130:
131: /**
132: * @com.intel.drl.spec_ref
133: */
134: private void readObject(ObjectInputStream in) throws IOException,
135: ClassNotFoundException {
136: String refName = null;
137:
138: try {
139: refName = in.readUTF();
140:
141: if (refName.length() == 0) {
142: // read RemoteRef object
143: ref = (RemoteRef) in.readObject();
144: return;
145: }
146:
147: // well-known RemoteRef types
148: // TODO: the following line is a temporary solution. Line after
149: // that should be uncommented later.
150: String refClName = "org.apache.harmony.rmi.remoteref." + refName; //$NON-NLS-1$
151: //String refClName = RemoteRef.packagePrefix + "." + refName;
152: ref = ((RemoteRef) Class.forName(refClName).newInstance());
153: ref.readExternal(in);
154: } catch (Exception ex) {
155: // rmi.18=Unable to create RemoteRef instance
156: throw new UnmarshalException(
157: Messages.getString("rmi.18"), ex);//$NON-NLS-1$
158: }
159: }
160: }
|