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.11.4.3 $
022: */package java.rmi.server;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.rmi.NoSuchObjectException;
027: import java.rmi.Remote;
028: import java.rmi.RemoteException;
029:
030: import org.apache.harmony.rmi.internal.nls.Messages;
031: import org.apache.harmony.rmi.remoteref.UnicastServerRef;
032: import org.apache.harmony.rmi.remoteref.UnicastServerRef2;
033: import org.apache.harmony.rmi.server.ExportManager;
034:
035: /**
036: * @com.intel.drl.spec_ref
037: *
038: * @author Mikhail A. Markov
039: * @version $Revision: 1.11.4.3 $
040: */
041: public class UnicastRemoteObject extends RemoteServer {
042:
043: private static final long serialVersionUID = 4974527148936298033L;
044: private int port;
045: private RMIClientSocketFactory csf = null;
046: private RMIServerSocketFactory ssf = null;
047:
048: /**
049: * @com.intel.drl.spec_ref
050: */
051: protected UnicastRemoteObject(int port, RMIClientSocketFactory csf,
052: RMIServerSocketFactory ssf) throws RemoteException {
053: this .csf = csf;
054: this .ssf = ssf;
055: this .port = port;
056: exportObject(this , port, csf, ssf);
057: }
058:
059: /**
060: * @com.intel.drl.spec_ref
061: */
062: protected UnicastRemoteObject(int port) throws RemoteException {
063: this .port = port;
064: exportObject(this , port);
065: }
066:
067: /**
068: * @com.intel.drl.spec_ref
069: */
070: protected UnicastRemoteObject() throws RemoteException {
071: this (0);
072: }
073:
074: /**
075: * @com.intel.drl.spec_ref
076: */
077: public Object clone() throws CloneNotSupportedException {
078: try {
079: UnicastRemoteObject clonedObj = (UnicastRemoteObject) super
080: .clone();
081: clonedObj.export();
082: return clonedObj;
083: } catch (RemoteException re) {
084: // rmi.1A=Unable to clone the object
085: throw new ServerCloneException(
086: Messages.getString("rmi.1A"), re); //$NON-NLS-1$
087: }
088: }
089:
090: /**
091: * @com.intel.drl.spec_ref
092: */
093: public static Remote exportObject(Remote obj, int port,
094: RMIClientSocketFactory csf, RMIServerSocketFactory ssf)
095: throws RemoteException {
096: return exportObject(obj, port, csf, ssf, true);
097: }
098:
099: /**
100: * @com.intel.drl.spec_ref
101: */
102: public static RemoteStub exportObject(Remote obj)
103: throws RemoteException {
104: return (RemoteStub) exportObject(obj, 0, null, null, false);
105: }
106:
107: /**
108: * @com.intel.drl.spec_ref
109: */
110: public static Remote exportObject(Remote obj, int port)
111: throws RemoteException {
112: return exportObject(obj, port, null, null, true);
113: }
114:
115: /*
116: * Exports the given remote object.
117: *
118: * @param obj Remote object to be exported.
119: * @param port Port to export object on.
120: * @param csf Client-side socket factory
121: * @param ssf Server-side socket factory
122: * @param useProxyStubs If true then Proxy stubs will be generated if stub
123: * class could not be found in classpath and codebase; if false Proxy
124: * stubs will not be tried (this is needed for exportObject(Remote)
125: * method because it returns RemoteStub class (but Proxy class could
126: * not be casted to it)
127: *
128: * @return stub for exported object
129: *
130: * @throws RemoteException if any error occurred while exporting object
131: */
132: private static Remote exportObject(Remote obj, int port,
133: RMIClientSocketFactory csf, RMIServerSocketFactory ssf,
134: boolean useProxyStubs) throws RemoteException {
135: UnicastServerRef sref;
136:
137: if (csf != null || ssf != null) {
138: sref = new UnicastServerRef2(port, csf, ssf);
139: } else {
140: sref = new UnicastServerRef(port, csf, ssf);
141: }
142: Remote stub = ExportManager.exportObject(obj, sref,
143: useProxyStubs);
144:
145: if (obj instanceof UnicastRemoteObject) {
146: ((UnicastRemoteObject) obj).ref = sref;
147: }
148: return stub;
149: }
150:
151: /**
152: * @com.intel.drl.spec_ref
153: */
154: public static boolean unexportObject(Remote obj, boolean force)
155: throws NoSuchObjectException {
156: return ExportManager.unexportObject(obj, force);
157: }
158:
159: private void readObject(ObjectInputStream in) throws IOException,
160: ClassNotFoundException {
161: in.defaultReadObject();
162: export();
163: }
164:
165: /*
166: * Exports this UnicastRemoteObject from pre-initialized fields. This method
167: * is used by readObject() and clone() methods.
168: */
169: private void export() throws RemoteException {
170: if (csf != null || ssf != null) {
171: exportObject(this, port, csf, ssf);
172: } else {
173: exportObject(this, port);
174: }
175: }
176: }
|