feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 1996, 2001, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.rmi.RemoteException;
public interface Channel {
/**
* Generates a new connection to the endpoint of the address space
* for which this is a channel.
*/
public Connection newConnection() throws RemoteException;
/**
* Returns the endpoint of the address space for which this is a
* channel.
*/
public Endpoint getEndpoint();
/**
* Free the connection generated by this channel.
* @param c The connection
* @param reuse If true, the connection is in a state in which it
* can be reused for another method call.
*/
public void free(Connection conn, boolean reuse) throws RemoteException;
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.*;
public interface Connection {
/**
* Gets the input stream for this connection.
*/
public InputStream getInputStream() throws IOException;
/*
* Release the input stream for this connection.
*/
public void releaseInputStream() throws IOException;
/**
* Gets the output stream for this connection
*/
public OutputStream getOutputStream() throws IOException;
/*
* Release the output stream for this connection.
*/
public void releaseOutputStream() throws IOException;
/**
* Return true if channel can be used for multiple operations.
*/
public boolean isReusable();
/**
* Close connection.
*/
public void close() throws IOException;
/**
* Returns the channel for this connection.
*/
public Channel getChannel();
}

View File

@@ -0,0 +1,166 @@
/*
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.*;
import java.util.*;
import java.rmi.RemoteException;
import java.rmi.server.UID;
import sun.rmi.server.MarshalInputStream;
import sun.rmi.runtime.Log;
/**
* Special stream to keep track of refs being unmarshaled so that
* refs can be ref-counted locally.
*
* @author Ann Wollrath
*/
class ConnectionInputStream extends MarshalInputStream {
/** indicates whether ack is required for DGC */
private boolean dgcAckNeeded = false;
/** Hashtable mapping Endpoints to lists of LiveRefs to register */
private Map<Endpoint, List<LiveRef>> incomingRefTable = new HashMap<>(5);
/** identifier for gc ack*/
private UID ackID;
/**
* Constructs a marshal input stream using the underlying
* stream "in".
*/
ConnectionInputStream(InputStream in) throws IOException {
super(in);
}
void readID() throws IOException {
ackID = UID.read((DataInput) this);
}
/**
* Save reference in order to send "dirty" call after all args/returns
* have been unmarshaled. Save in hashtable incomingRefTable. This
* table is keyed on endpoints, and holds objects of type
* IncomingRefTableEntry.
*/
void saveRef(LiveRef ref) {
Endpoint ep = ref.getEndpoint();
// check whether endpoint is already in the hashtable
List<LiveRef> refList = incomingRefTable.get(ep);
if (refList == null) {
refList = new ArrayList<LiveRef>();
incomingRefTable.put(ep, refList);
}
// add ref to list of refs for endpoint ep
refList.add(ref);
}
/**
* Discard the saved incoming refs so there is nothing to register
* when {@code registerRefs} is called.
*/
void discardRefs() {
incomingRefTable.clear();
}
/**
* Add references to DGC table (and possibly send dirty call).
* RegisterRefs now calls DGCClient.referenced on all
* refs with the same endpoint at once to achieve batching of
* calls to the DGC
*/
void registerRefs() throws IOException {
if (!incomingRefTable.isEmpty()) {
for (Map.Entry<Endpoint, List<LiveRef>> entry :
incomingRefTable.entrySet()) {
DGCClient.registerRefs(entry.getKey(), entry.getValue());
}
}
}
/**
* Indicate that an ack is required to the distributed
* collector.
*/
void setAckNeeded() {
dgcAckNeeded = true;
}
/**
* Done with input stream for remote call. Send DGC ack if necessary.
* Allow sending of ack to fail without flagging an error.
*/
void done(Connection c) {
/*
* WARNING: The connection c may have already been freed. It
* is only be safe to use c to obtain c's channel.
*/
if (dgcAckNeeded) {
Connection conn = null;
Channel ch = null;
boolean reuse = true;
DGCImpl.dgcLog.log(Log.VERBOSE, "send ack");
try {
ch = c.getChannel();
conn = ch.newConnection();
DataOutputStream out =
new DataOutputStream(conn.getOutputStream());
out.writeByte(TransportConstants.DGCAck);
if (ackID == null) {
ackID = new UID();
}
ackID.write((DataOutput) out);
conn.releaseOutputStream();
/*
* Fix for 4221173: if this connection is on top of an
* HttpSendSocket, the DGCAck won't actually get sent until a
* read operation is attempted on the socket. Calling
* available() is the most innocuous way of triggering the
* write.
*/
conn.getInputStream().available();
conn.releaseInputStream();
} catch (RemoteException e) {
reuse = false;
} catch (IOException e) {
reuse = false;
}
try {
if (conn != null)
ch.free(conn, reuse);
} catch (RemoteException e){
// eat exception
}
}
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.IOException;
import java.rmi.server.UID;
import sun.rmi.server.MarshalOutputStream;
/**
* Special stream to keep track of refs being marshaled as return
* results to determine whether a special ack needs to be sent
* to the distributed collector.
*
* @author Ann Wollrath
*/
class ConnectionOutputStream extends MarshalOutputStream {
/** connection associated with ConnectionOutputStream */
private final Connection conn;
/** indicates whether output stream is used to marshal results */
private final boolean resultStream;
/** identifier for gc ack*/
private final UID ackID;
/** to store refs to returned remote object until DGC ack is received */
private DGCAckHandler dgcAckHandler = null;
/**
* Constructs an marshal output stream using the underlying
* stream associated with the connection, the parameter c.
* @param c is the Connection object associated with the
* ConnectionOutputStream object being constructed
* @param resultStream indicates whether this stream is used
* to marshal return results
*/
ConnectionOutputStream(Connection conn, boolean resultStream)
throws IOException
{
super(conn.getOutputStream());
this.conn = conn;
this.resultStream = resultStream;
ackID = resultStream ? new UID() : null;
}
void writeID() throws IOException {
assert resultStream;
ackID.write(this);
}
/**
* Returns true if this output stream is used to marshal return
* results; otherwise returns false.
*/
boolean isResultStream() {
return resultStream;
}
/**
* Saves a reference to the specified object in this stream's
* DGCAckHandler.
**/
void saveObject(Object obj) {
// should always be accessed from same thread
if (dgcAckHandler == null) {
dgcAckHandler = new DGCAckHandler(ackID);
}
dgcAckHandler.add(obj);
}
/**
* Returns this stream's DGCAckHandler, or null if it doesn't have
* one (saveObject was not invoked). This method should only be
* invoked after all objects have been written to the stream,
* because future objects written may yet cause a DGCAckHandler to
* be created (by invoking saveObject).
**/
DGCAckHandler getDGCAckHandler() {
return dgcAckHandler;
}
void done() {
if (dgcAckHandler != null) {
dgcAckHandler.startTimer();
}
}
}

View File

@@ -0,0 +1,151 @@
/*
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.rmi.server.UID;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import sun.rmi.runtime.RuntimeUtil;
import sun.security.action.GetLongAction;
/**
* Holds strong references to a set of remote objects, or live remote
* references to remote objects, after they have been marshalled (as
* remote references) as parts of the arguments or the result of a
* remote invocation. The purpose is to prevent remote objects or
* live remote references that might otherwise be determined to be
* unreachable in this VM from being locally garbage collected before
* the receiver has had an opportunity to register the unmarshalled
* remote references for DGC.
*
* The references are held strongly until an acknowledgment has been
* received that the receiver has had an opportunity to process the
* remote references or until a timeout has expired. For remote
* references sent as parts of the arguments of a remote invocation,
* the acknowledgment is the beginning of the response indicating
* completion of the remote invocation. For remote references sent as
* parts of the result of a remote invocation, a UID is included as
* part of the result, and the acknowledgment is a transport-level
* "DGCAck" message containing that UID.
*
* @author Ann Wollrath
* @author Peter Jones
**/
public class DGCAckHandler {
/** timeout for holding references without receiving an acknowledgment */
private static final long dgcAckTimeout = // default 5 minutes
AccessController.doPrivileged(
new GetLongAction("sun.rmi.dgc.ackTimeout", 300000));
/** thread pool for scheduling delayed tasks */
private static final ScheduledExecutorService scheduler =
AccessController.doPrivileged(
new RuntimeUtil.GetInstanceAction()).getScheduler();
/** table mapping ack ID to handler */
private static final Map<UID,DGCAckHandler> idTable =
Collections.synchronizedMap(new HashMap<UID,DGCAckHandler>());
private final UID id;
private List<Object> objList = new ArrayList<>(); // null if released
private Future<?> task = null;
/**
* Creates a new DGCAckHandler, associated with the specified UID
* if the argument is not null.
*
* References added to this DGCAckHandler will be held strongly
* until its "release" method is invoked or (after the
* "startTimer" method has been invoked) the timeout has expired.
* If the argument is not null, then invoking the static
* "received" method with the specified UID is equivalent to
* invoking this instance's "release" method.
**/
DGCAckHandler(UID id) {
this.id = id;
if (id != null) {
assert !idTable.containsKey(id);
idTable.put(id, this);
}
}
/**
* Adds the specified reference to this DGCAckHandler.
**/
synchronized void add(Object obj) {
if (objList != null) {
objList.add(obj);
}
}
/**
* Starts the timer for this DGCAckHandler. After the timeout has
* expired, the references are released even if the acknowledgment
* has not been received.
**/
synchronized void startTimer() {
if (objList != null && task == null) {
task = scheduler.schedule(new Runnable() {
public void run() {
if (id != null) {
idTable.remove(id);
}
release();
}
}, dgcAckTimeout, TimeUnit.MILLISECONDS);
}
}
/**
* Releases the references held by this DGCAckHandler.
**/
synchronized void release() {
if (task != null) {
task.cancel(false);
task = null;
}
objList = null;
}
/**
* Causes the DGCAckHandler associated with the specified UID to
* release its references.
**/
public static void received(UID id) {
DGCAckHandler h = idTable.remove(id);
if (h != null) {
h.release();
}
}
}

View File

@@ -0,0 +1,841 @@
/*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.InvalidClassException;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.net.SocketPermission;
import java.rmi.UnmarshalException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.rmi.ConnectException;
import java.rmi.RemoteException;
import java.rmi.dgc.DGC;
import java.rmi.dgc.Lease;
import java.rmi.dgc.VMID;
import java.rmi.server.ObjID;
import sun.misc.GC;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.server.UnicastRef;
import sun.rmi.server.Util;
import sun.security.action.GetLongAction;
import java.security.AccessControlContext;
import java.security.Permissions;
import java.security.ProtectionDomain;
/**
* DGCClient implements the client-side of the RMI distributed garbage
* collection system.
*
* The external interface to DGCClient is the "registerRefs" method.
* When a LiveRef to a remote object enters the VM, it needs to be
* registered with the DGCClient to participate in distributed garbage
* collection.
*
* When the first LiveRef to a particular remote object is registered,
* a "dirty" call is made to the server-side distributed garbage
* collector for the remote object, which returns a lease guaranteeing
* that the server-side DGC will not collect the remote object for a
* certain period of time. While LiveRef instances to remote objects
* on a particular server exist, the DGCClient periodically sends more
* "dirty" calls to renew its lease.
*
* The DGCClient tracks the local reachability of registered LiveRef
* instances (using phantom references). When the LiveRef instance
* for a particular remote object becomes garbage collected locally,
* a "clean" call is made to the server-side distributed garbage
* collector, indicating that the server no longer needs to keep the
* remote object alive for this client.
*
* @see java.rmi.dgc.DGC, sun.rmi.transport.DGCImpl
*
* @author Ann Wollrath
* @author Peter Jones
*/
final class DGCClient {
/** next sequence number for DGC calls (access synchronized on class) */
private static long nextSequenceNum = Long.MIN_VALUE;
/** unique identifier for this VM as a client of DGC */
private static VMID vmid = new VMID();
/** lease duration to request (usually ignored by server) */
private static final long leaseValue = // default 10 minutes
AccessController.doPrivileged(
new GetLongAction("java.rmi.dgc.leaseValue",
600000)).longValue();
/** maximum interval between retries of failed clean calls */
private static final long cleanInterval = // default 3 minutes
AccessController.doPrivileged(
new GetLongAction("sun.rmi.dgc.cleanInterval",
180000)).longValue();
/** maximum interval between complete garbage collections of local heap */
private static final long gcInterval = // default 1 hour
AccessController.doPrivileged(
new GetLongAction("sun.rmi.dgc.client.gcInterval",
3600000)).longValue();
/** minimum retry count for dirty calls that fail */
private static final int dirtyFailureRetries = 5;
/** retry count for clean calls that fail with ConnectException */
private static final int cleanFailureRetries = 5;
/** constant empty ObjID array for lease renewal optimization */
private static final ObjID[] emptyObjIDArray = new ObjID[0];
/** ObjID for server-side DGC object */
private static final ObjID dgcID = new ObjID(ObjID.DGC_ID);
/**
* An AccessControlContext with only socket permissions,
* suitable for an RMIClientSocketFactory.
*/
private static final AccessControlContext SOCKET_ACC;
static {
Permissions perms = new Permissions();
perms.add(new SocketPermission("*", "connect,resolve"));
ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
SOCKET_ACC = new AccessControlContext(pd);
}
/*
* Disallow anyone from creating one of these.
*/
private DGCClient() {}
/**
* Register the LiveRef instances in the supplied list to participate
* in distributed garbage collection.
*
* All of the LiveRefs in the list must be for remote objects at the
* given endpoint.
*/
static void registerRefs(Endpoint ep, List<LiveRef> refs) {
/*
* Look up the given endpoint and register the refs with it.
* The retrieved entry may get removed from the global endpoint
* table before EndpointEntry.registerRefs() is able to acquire
* its lock; in this event, it returns false, and we loop and
* try again.
*/
EndpointEntry epEntry;
do {
epEntry = EndpointEntry.lookup(ep);
} while (!epEntry.registerRefs(refs));
}
/**
* Get the next sequence number to be used for a dirty or clean
* operation from this VM. This method should only be called while
* synchronized on the EndpointEntry whose data structures the
* operation affects.
*/
private static synchronized long getNextSequenceNum() {
return nextSequenceNum++;
}
/**
* Given the length of a lease and the time that it was granted,
* compute the absolute time at which it should be renewed, giving
* room for reasonable computational and communication delays.
*/
private static long computeRenewTime(long grantTime, long duration) {
/*
* REMIND: This algorithm should be more sophisticated, waiting
* a longer fraction of the lease duration for longer leases.
*/
return grantTime + (duration / 2);
}
/**
* EndpointEntry encapsulates the client-side DGC information specific
* to a particular Endpoint. Of most significance is the table that
* maps LiveRef value to RefEntry objects and the renew/clean thread
* that handles asynchronous client-side DGC operations.
*/
private static class EndpointEntry {
/** the endpoint that this entry is for */
private Endpoint endpoint;
/** synthesized reference to the remote server-side DGC */
private DGC dgc;
/** table of refs held for endpoint: maps LiveRef to RefEntry */
private Map<LiveRef, RefEntry> refTable = new HashMap<>(5);
/** set of RefEntry instances from last (failed) dirty call */
private Set<RefEntry> invalidRefs = new HashSet<>(5);
/** true if this entry has been removed from the global table */
private boolean removed = false;
/** absolute time to renew current lease to this endpoint */
private long renewTime = Long.MAX_VALUE;
/** absolute time current lease to this endpoint will expire */
private long expirationTime = Long.MIN_VALUE;
/** count of recent dirty calls that have failed */
private int dirtyFailures = 0;
/** absolute time of first recent failed dirty call */
private long dirtyFailureStartTime;
/** (average) elapsed time for recent failed dirty calls */
private long dirtyFailureDuration;
/** renew/clean thread for handling lease renewals and clean calls */
private Thread renewCleanThread;
/** true if renew/clean thread may be interrupted */
private boolean interruptible = false;
/** reference queue for phantom references */
private ReferenceQueue<LiveRef> refQueue = new ReferenceQueue<>();
/** set of clean calls that need to be made */
private Set<CleanRequest> pendingCleans = new HashSet<>(5);
/** global endpoint table: maps Endpoint to EndpointEntry */
private static Map<Endpoint,EndpointEntry> endpointTable = new HashMap<>(5);
/** handle for GC latency request (for future cancellation) */
private static GC.LatencyRequest gcLatencyRequest = null;
/**
* Look up the EndpointEntry for the given Endpoint. An entry is
* created if one does not already exist.
*/
public static EndpointEntry lookup(Endpoint ep) {
synchronized (endpointTable) {
EndpointEntry entry = endpointTable.get(ep);
if (entry == null) {
entry = new EndpointEntry(ep);
endpointTable.put(ep, entry);
/*
* While we are tracking live remote references registered
* in this VM, request a maximum latency for inspecting the
* entire heap from the local garbage collector, to place
* an upper bound on the time to discover remote references
* that have become unreachable (see bugid 4171278).
*/
if (gcLatencyRequest == null) {
gcLatencyRequest = GC.requestLatency(gcInterval);
}
}
return entry;
}
}
private EndpointEntry(final Endpoint endpoint) {
this.endpoint = endpoint;
try {
LiveRef dgcRef = new LiveRef(dgcID, endpoint, false);
dgc = (DGC) Util.createProxy(DGCImpl.class,
new UnicastRef(dgcRef), true);
} catch (RemoteException e) {
throw new Error("internal error creating DGC stub");
}
renewCleanThread = AccessController.doPrivileged(
new NewThreadAction(new RenewCleanThread(),
"RenewClean-" + endpoint, true));
renewCleanThread.start();
}
/**
* Register the LiveRef instances in the supplied list to participate
* in distributed garbage collection.
*
* This method returns false if this entry was removed from the
* global endpoint table (because it was empty) before these refs
* could be registered. In that case, a new EndpointEntry needs
* to be looked up.
*
* This method must NOT be called while synchronized on this entry.
*/
public boolean registerRefs(List<LiveRef> refs) {
assert !Thread.holdsLock(this);
Set<RefEntry> refsToDirty = null; // entries for refs needing dirty
long sequenceNum; // sequence number for dirty call
synchronized (this) {
if (removed) {
return false;
}
Iterator<LiveRef> iter = refs.iterator();
while (iter.hasNext()) {
LiveRef ref = iter.next();
assert ref.getEndpoint().equals(endpoint);
RefEntry refEntry = refTable.get(ref);
if (refEntry == null) {
LiveRef refClone = (LiveRef) ref.clone();
refEntry = new RefEntry(refClone);
refTable.put(refClone, refEntry);
if (refsToDirty == null) {
refsToDirty = new HashSet<>(5);
}
refsToDirty.add(refEntry);
}
refEntry.addInstanceToRefSet(ref);
}
if (refsToDirty == null) {
return true;
}
refsToDirty.addAll(invalidRefs);
invalidRefs.clear();
sequenceNum = getNextSequenceNum();
}
makeDirtyCall(refsToDirty, sequenceNum);
return true;
}
/**
* Remove the given RefEntry from the ref table. If that makes
* the ref table empty, remove this entry from the global endpoint
* table.
*
* This method must ONLY be called while synchronized on this entry.
*/
private void removeRefEntry(RefEntry refEntry) {
assert Thread.holdsLock(this);
assert !removed;
assert refTable.containsKey(refEntry.getRef());
refTable.remove(refEntry.getRef());
invalidRefs.remove(refEntry);
if (refTable.isEmpty()) {
synchronized (endpointTable) {
endpointTable.remove(endpoint);
Transport transport = endpoint.getOutboundTransport();
transport.free(endpoint);
/*
* If there are no longer any live remote references
* registered, we are no longer concerned with the
* latency of local garbage collection here.
*/
if (endpointTable.isEmpty()) {
assert gcLatencyRequest != null;
gcLatencyRequest.cancel();
gcLatencyRequest = null;
}
removed = true;
}
}
}
/**
* Make a DGC dirty call to this entry's endpoint, for the ObjIDs
* corresponding to the given set of refs and with the given
* sequence number.
*
* This method must NOT be called while synchronized on this entry.
*/
private void makeDirtyCall(Set<RefEntry> refEntries, long sequenceNum) {
assert !Thread.holdsLock(this);
ObjID[] ids;
if (refEntries != null) {
ids = createObjIDArray(refEntries);
} else {
ids = emptyObjIDArray;
}
long startTime = System.currentTimeMillis();
try {
Lease lease =
dgc.dirty(ids, sequenceNum, new Lease(vmid, leaseValue));
long duration = lease.getValue();
long newRenewTime = computeRenewTime(startTime, duration);
long newExpirationTime = startTime + duration;
synchronized (this) {
dirtyFailures = 0;
setRenewTime(newRenewTime);
expirationTime = newExpirationTime;
}
} catch (Exception e) {
long endTime = System.currentTimeMillis();
synchronized (this) {
dirtyFailures++;
if (e instanceof UnmarshalException
&& e.getCause() instanceof InvalidClassException) {
DGCImpl.dgcLog.log(Log.BRIEF, "InvalidClassException exception in DGC dirty call", e);
return; // protocol error, do not register these refs
}
if (dirtyFailures == 1) {
/*
* If this was the first recent failed dirty call,
* reschedule another one immediately, in case there
* was just a transient network problem, and remember
* the start time and duration of this attempt for
* future calculations of the delays between retries.
*/
dirtyFailureStartTime = startTime;
dirtyFailureDuration = endTime - startTime;
setRenewTime(endTime);
} else {
/*
* For each successive failed dirty call, wait for a
* (binary) exponentially increasing delay before
* retrying, to avoid network congestion.
*/
int n = dirtyFailures - 2;
if (n == 0) {
/*
* Calculate the initial retry delay from the
* average time elapsed for each of the first
* two failed dirty calls. The result must be
* at least 1000ms, to prevent a tight loop.
*/
dirtyFailureDuration =
Math.max((dirtyFailureDuration +
(endTime - startTime)) >> 1, 1000);
}
long newRenewTime =
endTime + (dirtyFailureDuration << n);
/*
* Continue if the last known held lease has not
* expired, or else at least a fixed number of times,
* or at least until we've tried for a fixed amount
* of time (the default lease value we request).
*/
if (newRenewTime < expirationTime ||
dirtyFailures < dirtyFailureRetries ||
newRenewTime < dirtyFailureStartTime + leaseValue)
{
setRenewTime(newRenewTime);
} else {
/*
* Give up: postpone lease renewals until next
* ref is registered for this endpoint.
*/
setRenewTime(Long.MAX_VALUE);
}
}
if (refEntries != null) {
/*
* Add all of these refs to the set of refs for this
* endpoint that may be invalid (this VM may not be in
* the server's referenced set), so that we will
* attempt to explicitly dirty them again in the
* future.
*/
invalidRefs.addAll(refEntries);
/*
* Record that a dirty call has failed for all of these
* refs, so that clean calls for them in the future
* will be strong.
*/
Iterator<RefEntry> iter = refEntries.iterator();
while (iter.hasNext()) {
RefEntry refEntry = iter.next();
refEntry.markDirtyFailed();
}
}
/*
* If the last known held lease will have expired before
* the next renewal, all refs might be invalid.
*/
if (renewTime >= expirationTime) {
invalidRefs.addAll(refTable.values());
}
}
}
}
/**
* Set the absolute time at which the lease for this entry should
* be renewed.
*
* This method must ONLY be called while synchronized on this entry.
*/
private void setRenewTime(long newRenewTime) {
assert Thread.holdsLock(this);
if (newRenewTime < renewTime) {
renewTime = newRenewTime;
if (interruptible) {
AccessController.doPrivileged(
new PrivilegedAction<Void>() {
public Void run() {
renewCleanThread.interrupt();
return null;
}
});
}
} else {
renewTime = newRenewTime;
}
}
/**
* RenewCleanThread handles the asynchronous client-side DGC activity
* for this entry: renewing the leases and making clean calls.
*/
private class RenewCleanThread implements Runnable {
public void run() {
do {
long timeToWait;
RefEntry.PhantomLiveRef phantom = null;
boolean needRenewal = false;
Set<RefEntry> refsToDirty = null;
long sequenceNum = Long.MIN_VALUE;
synchronized (EndpointEntry.this) {
/*
* Calculate time to block (waiting for phantom
* reference notifications). It is the time until the
* lease renewal should be done, bounded on the low
* end by 1 ms so that the reference queue will always
* get processed, and if there are pending clean
* requests (remaining because some clean calls
* failed), bounded on the high end by the maximum
* clean call retry interval.
*/
long timeUntilRenew =
renewTime - System.currentTimeMillis();
timeToWait = Math.max(timeUntilRenew, 1);
if (!pendingCleans.isEmpty()) {
timeToWait = Math.min(timeToWait, cleanInterval);
}
/*
* Set flag indicating that it is OK to interrupt this
* thread now, such as if a earlier lease renewal time
* is set, because we are only going to be blocking
* and can deal with interrupts.
*/
interruptible = true;
}
try {
/*
* Wait for the duration calculated above for any of
* our phantom references to be enqueued.
*/
phantom = (RefEntry.PhantomLiveRef)
refQueue.remove(timeToWait);
} catch (InterruptedException e) {
}
synchronized (EndpointEntry.this) {
/*
* Set flag indicating that it is NOT OK to interrupt
* this thread now, because we may be undertaking I/O
* operations that should not be interrupted (and we
* will not be blocking arbitrarily).
*/
interruptible = false;
Thread.interrupted(); // clear interrupted state
/*
* If there was a phantom reference enqueued, process
* it and all the rest on the queue, generating
* clean requests as necessary.
*/
if (phantom != null) {
processPhantomRefs(phantom);
}
/*
* Check if it is time to renew this entry's lease.
*/
long currentTime = System.currentTimeMillis();
if (currentTime > renewTime) {
needRenewal = true;
if (!invalidRefs.isEmpty()) {
refsToDirty = invalidRefs;
invalidRefs = new HashSet<>(5);
}
sequenceNum = getNextSequenceNum();
}
}
boolean needRenewal_ = needRenewal;
Set<RefEntry> refsToDirty_ = refsToDirty;
long sequenceNum_ = sequenceNum;
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
if (needRenewal_) {
makeDirtyCall(refsToDirty_, sequenceNum_);
}
if (!pendingCleans.isEmpty()) {
makeCleanCalls();
}
return null;
}}, SOCKET_ACC);
} while (!removed || !pendingCleans.isEmpty());
}
}
/**
* Process the notification of the given phantom reference and any
* others that are on this entry's reference queue. Each phantom
* reference is removed from its RefEntry's ref set. All ref
* entries that have no more registered instances are collected
* into up to two batched clean call requests: one for refs
* requiring a "strong" clean call, and one for the rest.
*
* This method must ONLY be called while synchronized on this entry.
*/
private void processPhantomRefs(RefEntry.PhantomLiveRef phantom) {
assert Thread.holdsLock(this);
Set<RefEntry> strongCleans = null;
Set<RefEntry> normalCleans = null;
do {
RefEntry refEntry = phantom.getRefEntry();
refEntry.removeInstanceFromRefSet(phantom);
if (refEntry.isRefSetEmpty()) {
if (refEntry.hasDirtyFailed()) {
if (strongCleans == null) {
strongCleans = new HashSet<>(5);
}
strongCleans.add(refEntry);
} else {
if (normalCleans == null) {
normalCleans = new HashSet<>(5);
}
normalCleans.add(refEntry);
}
removeRefEntry(refEntry);
}
} while ((phantom =
(RefEntry.PhantomLiveRef) refQueue.poll()) != null);
if (strongCleans != null) {
pendingCleans.add(
new CleanRequest(createObjIDArray(strongCleans),
getNextSequenceNum(), true));
}
if (normalCleans != null) {
pendingCleans.add(
new CleanRequest(createObjIDArray(normalCleans),
getNextSequenceNum(), false));
}
}
/**
* CleanRequest holds the data for the parameters of a clean call
* that needs to be made.
*/
private static class CleanRequest {
final ObjID[] objIDs;
final long sequenceNum;
final boolean strong;
/** how many times this request has failed */
int failures = 0;
CleanRequest(ObjID[] objIDs, long sequenceNum, boolean strong) {
this.objIDs = objIDs;
this.sequenceNum = sequenceNum;
this.strong = strong;
}
}
/**
* Make all of the clean calls described by the clean requests in
* this entry's set of "pending cleans". Clean requests for clean
* calls that succeed are removed from the "pending cleans" set.
*
* This method must NOT be called while synchronized on this entry.
*/
private void makeCleanCalls() {
assert !Thread.holdsLock(this);
Iterator<CleanRequest> iter = pendingCleans.iterator();
while (iter.hasNext()) {
CleanRequest request = iter.next();
try {
dgc.clean(request.objIDs, request.sequenceNum, vmid,
request.strong);
iter.remove();
} catch (Exception e) {
/*
* Many types of exceptions here could have been
* caused by a transient failure, so try again a
* few times, but not forever.
*/
if (++request.failures >= cleanFailureRetries) {
iter.remove();
}
}
}
}
/**
* Create an array of ObjIDs (needed for the DGC remote calls)
* from the ids in the given set of refs.
*/
private static ObjID[] createObjIDArray(Set<RefEntry> refEntries) {
ObjID[] ids = new ObjID[refEntries.size()];
Iterator<RefEntry> iter = refEntries.iterator();
for (int i = 0; i < ids.length; i++) {
ids[i] = iter.next().getRef().getObjID();
}
return ids;
}
/**
* RefEntry encapsulates the client-side DGC information specific
* to a particular LiveRef value. In particular, it contains a
* set of phantom references to all of the instances of the LiveRef
* value registered in the system (but not garbage collected
* locally).
*/
private class RefEntry {
/** LiveRef value for this entry (not a registered instance) */
private LiveRef ref;
/** set of phantom references to registered instances */
private Set<PhantomLiveRef> refSet = new HashSet<>(5);
/** true if a dirty call containing this ref has failed */
private boolean dirtyFailed = false;
public RefEntry(LiveRef ref) {
this.ref = ref;
}
/**
* Return the LiveRef value for this entry (not a registered
* instance).
*/
public LiveRef getRef() {
return ref;
}
/**
* Add a LiveRef to the set of registered instances for this entry.
*
* This method must ONLY be invoked while synchronized on this
* RefEntry's EndpointEntry.
*/
public void addInstanceToRefSet(LiveRef ref) {
assert Thread.holdsLock(EndpointEntry.this);
assert ref.equals(this.ref);
/*
* Only keep a phantom reference to the registered instance,
* so that it can be garbage collected normally (and we can be
* notified when that happens).
*/
refSet.add(new PhantomLiveRef(ref));
}
/**
* Remove a PhantomLiveRef from the set of registered instances.
*
* This method must ONLY be invoked while synchronized on this
* RefEntry's EndpointEntry.
*/
public void removeInstanceFromRefSet(PhantomLiveRef phantom) {
assert Thread.holdsLock(EndpointEntry.this);
assert refSet.contains(phantom);
refSet.remove(phantom);
}
/**
* Return true if there are no registered LiveRef instances for
* this entry still reachable in this VM.
*
* This method must ONLY be invoked while synchronized on this
* RefEntry's EndpointEntry.
*/
public boolean isRefSetEmpty() {
assert Thread.holdsLock(EndpointEntry.this);
return refSet.size() == 0;
}
/**
* Record that a dirty call that explicitly contained this
* entry's ref has failed.
*
* This method must ONLY be invoked while synchronized on this
* RefEntry's EndpointEntry.
*/
public void markDirtyFailed() {
assert Thread.holdsLock(EndpointEntry.this);
dirtyFailed = true;
}
/**
* Return true if a dirty call that explicitly contained this
* entry's ref has failed (and therefore a clean call for this
* ref needs to be marked "strong").
*
* This method must ONLY be invoked while synchronized on this
* RefEntry's EndpointEntry.
*/
public boolean hasDirtyFailed() {
assert Thread.holdsLock(EndpointEntry.this);
return dirtyFailed;
}
/**
* PhantomLiveRef is a PhantomReference to a LiveRef instance,
* used to detect when the LiveRef becomes permanently
* unreachable in this VM.
*/
private class PhantomLiveRef extends PhantomReference<LiveRef> {
public PhantomLiveRef(LiveRef ref) {
super(ref, EndpointEntry.this.refQueue);
}
public RefEntry getRefEntry() {
return RefEntry.this;
}
}
}
}
}

View File

@@ -0,0 +1,448 @@
/*
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.net.SocketPermission;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.dgc.DGC;
import java.rmi.dgc.Lease;
import java.rmi.dgc.VMID;
import java.rmi.server.LogStream;
import java.rmi.server.ObjID;
import java.rmi.server.RemoteServer;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.UID;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.security.Security;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import sun.misc.ObjectInputFilter;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.RuntimeUtil;
import sun.rmi.server.UnicastRef;
import sun.rmi.server.UnicastServerRef;
import sun.rmi.server.Util;
import sun.security.action.GetLongAction;
import sun.security.action.GetPropertyAction;
/**
* This class implements the guts of the server-side distributed GC
* algorithm
*
* @author Ann Wollrath
*/
@SuppressWarnings("deprecation")
final class DGCImpl implements DGC {
/* dgc system log */
static final Log dgcLog = Log.getLog("sun.rmi.dgc", "dgc",
LogStream.parseLevel(AccessController.doPrivileged(
new GetPropertyAction("sun.rmi.dgc.logLevel"))));
/** lease duration to grant to clients */
private static final long leaseValue = // default 10 minutes
AccessController.doPrivileged(
new GetLongAction("java.rmi.dgc.leaseValue", 600000));
/** lease check interval; default is half of lease grant duration */
private static final long leaseCheckInterval =
AccessController.doPrivileged(
new GetLongAction("sun.rmi.dgc.checkInterval", leaseValue / 2));
/** thread pool for scheduling delayed tasks */
private static final ScheduledExecutorService scheduler =
AccessController.doPrivileged(
new RuntimeUtil.GetInstanceAction()).getScheduler();
/** remote implementation of DGC interface for this VM */
private static DGCImpl dgc;
/** table that maps VMID to LeaseInfo */
private Map<VMID,LeaseInfo> leaseTable = new HashMap<>();
/** checks for lease expiration */
private Future<?> checker = null;
/**
* Return the remote implementation of the DGC interface for
* this VM.
*/
static DGCImpl getDGCImpl() {
return dgc;
}
/**
* Property name of the DGC serial filter to augment
* the built-in list of allowed types.
* Setting the property in the {@code lib/security/java.security} file
* will enable the augmented filter.
*/
private static final String DGC_FILTER_PROPNAME = "sun.rmi.transport.dgcFilter";
/** Registry max depth of remote invocations. **/
private static int DGC_MAX_DEPTH = 5;
/** Registry maximum array size in remote invocations. **/
private static int DGC_MAX_ARRAY_SIZE = 10000;
/**
* The dgcFilter created from the value of the {@code "sun.rmi.transport.dgcFilter"}
* property.
*/
private static final ObjectInputFilter dgcFilter =
AccessController.doPrivileged((PrivilegedAction<ObjectInputFilter>)DGCImpl::initDgcFilter);
/**
* Initialize the dgcFilter from the security properties or system property; if any
* @return an ObjectInputFilter, or null
*/
private static ObjectInputFilter initDgcFilter() {
ObjectInputFilter filter = null;
String props = System.getProperty(DGC_FILTER_PROPNAME);
if (props == null) {
props = Security.getProperty(DGC_FILTER_PROPNAME);
}
if (props != null) {
filter = ObjectInputFilter.Config.createFilter(props);
if (dgcLog.isLoggable(Log.BRIEF)) {
dgcLog.log(Log.BRIEF, "dgcFilter = " + filter);
}
}
return filter;
}
/**
* Construct a new server-side remote object collector at
* a particular port. Disallow construction from outside.
*/
private DGCImpl() {}
/**
* The dirty call adds the VMID "vmid" to the set of clients
* that hold references to the object associated with the ObjID
* id. The long "sequenceNum" is used to detect late dirty calls. If
* the VMID "vmid" is null, a VMID will be generated on the
* server (for use by the client in subsequent calls) and
* returned.
*
* The client must call the "dirty" method to renew the lease
* before the "lease" time expires or all references to remote
* objects in this VM that the client holds are considered
* "unreferenced".
*/
public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease) {
VMID vmid = lease.getVMID();
/*
* The server specifies the lease value; the client has
* no say in the matter.
*/
long duration = leaseValue;
if (dgcLog.isLoggable(Log.VERBOSE)) {
dgcLog.log(Log.VERBOSE, "vmid = " + vmid);
}
// create a VMID if one wasn't supplied
if (vmid == null) {
vmid = new VMID();
if (dgcLog.isLoggable(Log.BRIEF)) {
String clientHost;
try {
clientHost = RemoteServer.getClientHost();
} catch (ServerNotActiveException e) {
clientHost = "<unknown host>";
}
dgcLog.log(Log.BRIEF, " assigning vmid " + vmid +
" to client " + clientHost);
}
}
lease = new Lease(vmid, duration);
// record lease information
synchronized (leaseTable) {
LeaseInfo info = leaseTable.get(vmid);
if (info == null) {
leaseTable.put(vmid, new LeaseInfo(vmid, duration));
if (checker == null) {
checker = scheduler.scheduleWithFixedDelay(
new Runnable() {
public void run() {
checkLeases();
}
},
leaseCheckInterval,
leaseCheckInterval, TimeUnit.MILLISECONDS);
}
} else {
info.renew(duration);
}
}
for (ObjID id : ids) {
if (dgcLog.isLoggable(Log.VERBOSE)) {
dgcLog.log(Log.VERBOSE, "id = " + id +
", vmid = " + vmid + ", duration = " + duration);
}
ObjectTable.referenced(id, sequenceNum, vmid);
}
// return the VMID used
return lease;
}
/**
* The clean call removes the VMID from the set of clients
* that hold references to the object associated with the LiveRef
* ref. The sequence number is used to detect late clean calls. If the
* argument "strong" is true, then the clean call is a result of a
* failed "dirty" call, thus the sequence number for the VMID needs
* to be remembered until the client goes away.
*/
public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong)
{
for (ObjID id : ids) {
if (dgcLog.isLoggable(Log.VERBOSE)) {
dgcLog.log(Log.VERBOSE, "id = " + id +
", vmid = " + vmid + ", strong = " + strong);
}
ObjectTable.unreferenced(id, sequenceNum, vmid, strong);
}
}
/**
* Register interest in receiving a callback when this VMID
* becomes inaccessible.
*/
void registerTarget(VMID vmid, Target target) {
synchronized (leaseTable) {
LeaseInfo info = leaseTable.get(vmid);
if (info == null) {
target.vmidDead(vmid);
} else {
info.notifySet.add(target);
}
}
}
/**
* Remove notification request.
*/
void unregisterTarget(VMID vmid, Target target) {
synchronized (leaseTable) {
LeaseInfo info = leaseTable.get(vmid);
if (info != null) {
info.notifySet.remove(target);
}
}
}
/**
* Check if leases have expired. If a lease has expired, remove
* it from the table and notify all interested parties that the
* VMID is essentially "dead".
*
* @return if true, there are leases outstanding; otherwise leases
* no longer need to be checked
*/
private void checkLeases() {
long time = System.currentTimeMillis();
/* List of vmids that need to be removed from the leaseTable */
List<LeaseInfo> toUnregister = new ArrayList<>();
/* Build a list of leaseInfo objects that need to have
* targets removed from their notifySet. Remove expired
* leases from leaseTable.
*/
synchronized (leaseTable) {
Iterator<LeaseInfo> iter = leaseTable.values().iterator();
while (iter.hasNext()) {
LeaseInfo info = iter.next();
if (info.expired(time)) {
toUnregister.add(info);
iter.remove();
}
}
if (leaseTable.isEmpty()) {
checker.cancel(false);
checker = null;
}
}
/* Notify and unegister targets without holding the lock on
* the leaseTable so we avoid deadlock.
*/
for (LeaseInfo info : toUnregister) {
for (Target target : info.notifySet) {
target.vmidDead(info.vmid);
}
}
}
static {
/*
* "Export" the singleton DGCImpl in a context isolated from
* the arbitrary current thread context.
*/
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
ClassLoader savedCcl =
Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(
ClassLoader.getSystemClassLoader());
/*
* Put remote collector object in table by hand to prevent
* listen on port. (UnicastServerRef.exportObject would
* cause transport to listen.)
*/
try {
dgc = new DGCImpl();
ObjID dgcID = new ObjID(ObjID.DGC_ID);
LiveRef ref = new LiveRef(dgcID, 0);
UnicastServerRef disp = new UnicastServerRef(ref,
DGCImpl::checkInput);
Remote stub =
Util.createProxy(DGCImpl.class,
new UnicastRef(ref), true);
disp.setSkeleton(dgc);
Permissions perms = new Permissions();
perms.add(new SocketPermission("*", "accept,resolve"));
ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
AccessControlContext acceptAcc = new AccessControlContext(pd);
Target target = AccessController.doPrivileged(
new PrivilegedAction<Target>() {
public Target run() {
return new Target(dgc, disp, stub, dgcID, true);
}
}, acceptAcc);
ObjectTable.putTarget(target);
} catch (RemoteException e) {
throw new Error(
"exception initializing server-side DGC", e);
}
} finally {
Thread.currentThread().setContextClassLoader(savedCcl);
}
return null;
}
});
}
/**
* ObjectInputFilter to filter DGC input objects.
* The list of acceptable classes is very short and explicit.
* The depth and array sizes are limited.
*
* @param filterInfo access to class, arrayLength, etc.
* @return {@link ObjectInputFilter.Status#ALLOWED} if allowed,
* {@link ObjectInputFilter.Status#REJECTED} if rejected,
* otherwise {@link ObjectInputFilter.Status#UNDECIDED}
*/
private static ObjectInputFilter.Status checkInput(ObjectInputFilter.FilterInfo filterInfo) {
if (dgcFilter != null) {
ObjectInputFilter.Status status = dgcFilter.checkInput(filterInfo);
if (status != ObjectInputFilter.Status.UNDECIDED) {
// The DGC filter can override the built-in white-list
return status;
}
}
if (filterInfo.depth() > DGC_MAX_DEPTH) {
return ObjectInputFilter.Status.REJECTED;
}
Class<?> clazz = filterInfo.serialClass();
if (clazz != null) {
while (clazz.isArray()) {
if (filterInfo.arrayLength() >= 0 && filterInfo.arrayLength() > DGC_MAX_ARRAY_SIZE) {
return ObjectInputFilter.Status.REJECTED;
}
// Arrays are allowed depending on the component type
clazz = clazz.getComponentType();
}
if (clazz.isPrimitive()) {
// Arrays of primitives are allowed
return ObjectInputFilter.Status.ALLOWED;
}
return (clazz == ObjID.class ||
clazz == UID.class ||
clazz == VMID.class ||
clazz == Lease.class)
? ObjectInputFilter.Status.ALLOWED
: ObjectInputFilter.Status.REJECTED;
}
// Not a class, not size limited
return ObjectInputFilter.Status.UNDECIDED;
}
private static class LeaseInfo {
VMID vmid;
long expiration;
Set<Target> notifySet = new HashSet<>();
LeaseInfo(VMID vmid, long lease) {
this.vmid = vmid;
expiration = System.currentTimeMillis() + lease;
}
synchronized void renew(long lease) {
long newExpiration = System.currentTimeMillis() + lease;
if (newExpiration > expiration)
expiration = newExpiration;
}
boolean expired(long time) {
if (expiration < time) {
if (dgcLog.isLoggable(Log.BRIEF)) {
dgcLog.log(Log.BRIEF, vmid.toString());
}
return true;
} else {
return false;
}
}
}
}

View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.IOException;
/**
* Skeleton to dispatch DGC methods.
* Originally generated by RMIC but frozen to match the stubs.
*/
@SuppressWarnings({"deprecation", "serial"})
public final class DGCImpl_Skel
implements java.rmi.server.Skeleton {
private static final java.rmi.server.Operation[] operations = {
new java.rmi.server.Operation("void clean(java.rmi.server.ObjID[], long, java.rmi.dgc.VMID, boolean)"),
new java.rmi.server.Operation("java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)")
};
private static final long interfaceHash = -669196253586618813L;
public java.rmi.server.Operation[] getOperations() {
return operations.clone();
}
public void dispatch(java.rmi.Remote obj, java.rmi.server.RemoteCall remoteCall, int opnum, long hash)
throws java.lang.Exception {
if (hash != interfaceHash)
throw new java.rmi.server.SkeletonMismatchException("interface hash mismatch");
sun.rmi.transport.DGCImpl server = (sun.rmi.transport.DGCImpl) obj;
StreamRemoteCall call = (StreamRemoteCall) remoteCall;
switch (opnum) {
case 0: // clean(ObjID[], long, VMID, boolean)
{
java.rmi.server.ObjID[] $param_arrayOf_ObjID_1;
long $param_long_2;
java.rmi.dgc.VMID $param_VMID_3;
boolean $param_boolean_4;
try {
java.io.ObjectInput in = call.getInputStream();
$param_arrayOf_ObjID_1 = (java.rmi.server.ObjID[]) in.readObject();
$param_long_2 = in.readLong();
$param_VMID_3 = (java.rmi.dgc.VMID) in.readObject();
$param_boolean_4 = in.readBoolean();
} catch (ClassCastException | IOException | ClassNotFoundException e) {
call.discardPendingRefs();
throw new java.rmi.UnmarshalException("error unmarshalling arguments", e);
} finally {
call.releaseInputStream();
}
server.clean($param_arrayOf_ObjID_1, $param_long_2, $param_VMID_3, $param_boolean_4);
try {
call.getResultStream(true);
} catch (java.io.IOException e) {
throw new java.rmi.MarshalException("error marshalling return", e);
}
break;
}
case 1: // dirty(ObjID[], long, Lease)
{
java.rmi.server.ObjID[] $param_arrayOf_ObjID_1;
long $param_long_2;
java.rmi.dgc.Lease $param_Lease_3;
try {
java.io.ObjectInput in = call.getInputStream();
$param_arrayOf_ObjID_1 = (java.rmi.server.ObjID[]) in.readObject();
$param_long_2 = in.readLong();
$param_Lease_3 = (java.rmi.dgc.Lease) in.readObject();
} catch (ClassCastException | IOException | ClassNotFoundException e) {
call.discardPendingRefs();
throw new java.rmi.UnmarshalException("error unmarshalling arguments", e);
} finally {
call.releaseInputStream();
}
java.rmi.dgc.Lease $result = server.dirty($param_arrayOf_ObjID_1, $param_long_2, $param_Lease_3);
try {
java.io.ObjectOutput out = call.getResultStream(true);
out.writeObject($result);
} catch (java.io.IOException e) {
throw new java.rmi.MarshalException("error marshalling return", e);
}
break;
}
default:
throw new java.rmi.UnmarshalException("invalid method number");
}
}
}

View File

@@ -0,0 +1,191 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import sun.rmi.transport.tcp.TCPConnection;
import java.io.IOException;
import java.rmi.dgc.Lease;
import java.rmi.dgc.VMID;
import java.rmi.server.UID;
import java.util.ArrayList;
import sun.misc.ObjectInputFilter;
/**
* Stubs to invoke DGC remote methods.
* Originally generated from RMIC but frozen to insert serialFilter.
*/
@SuppressWarnings({"deprecation", "serial"})
public final class DGCImpl_Stub
extends java.rmi.server.RemoteStub
implements java.rmi.dgc.DGC {
private static final java.rmi.server.Operation[] operations = {
new java.rmi.server.Operation("void clean(java.rmi.server.ObjID[], long, java.rmi.dgc.VMID, boolean)"),
new java.rmi.server.Operation("java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)")
};
private static final long interfaceHash = -669196253586618813L;
/** Registry max depth of remote invocations. **/
private static int DGCCLIENT_MAX_DEPTH = 6;
/** Registry maximum array size in remote invocations. **/
private static int DGCCLIENT_MAX_ARRAY_SIZE = 10000;
// constructors
public DGCImpl_Stub() {
super();
}
public DGCImpl_Stub(java.rmi.server.RemoteRef ref) {
super(ref);
}
// methods from remote interfaces
// implementation of clean(ObjID[], long, VMID, boolean)
public void clean(java.rmi.server.ObjID[] $param_arrayOf_ObjID_1, long $param_long_2, java.rmi.dgc.VMID $param_VMID_3, boolean $param_boolean_4)
throws java.rmi.RemoteException {
try {
StreamRemoteCall call = (StreamRemoteCall)ref.newCall((java.rmi.server.RemoteObject) this,
operations, 0, interfaceHash);
call.setObjectInputFilter(DGCImpl_Stub::leaseFilter);
try {
java.io.ObjectOutput out = call.getOutputStream();
out.writeObject($param_arrayOf_ObjID_1);
out.writeLong($param_long_2);
out.writeObject($param_VMID_3);
out.writeBoolean($param_boolean_4);
} catch (java.io.IOException e) {
throw new java.rmi.MarshalException("error marshalling arguments", e);
}
ref.invoke(call);
ref.done(call);
} catch (java.lang.RuntimeException e) {
throw e;
} catch (java.rmi.RemoteException e) {
throw e;
} catch (java.lang.Exception e) {
throw new java.rmi.UnexpectedException("undeclared checked exception", e);
}
}
// implementation of dirty(ObjID[], long, Lease)
public java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[] $param_arrayOf_ObjID_1, long $param_long_2, java.rmi.dgc.Lease $param_Lease_3)
throws java.rmi.RemoteException {
try {
StreamRemoteCall call =
(StreamRemoteCall)ref.newCall((java.rmi.server.RemoteObject) this,
operations, 1, interfaceHash);
call.setObjectInputFilter(DGCImpl_Stub::leaseFilter);
try {
java.io.ObjectOutput out = call.getOutputStream();
out.writeObject($param_arrayOf_ObjID_1);
out.writeLong($param_long_2);
out.writeObject($param_Lease_3);
} catch (java.io.IOException e) {
throw new java.rmi.MarshalException("error marshalling arguments", e);
}
ref.invoke(call);
java.rmi.dgc.Lease $result;
Connection connection = call.getConnection();
try {
java.io.ObjectInput in = call.getInputStream();
$result = (java.rmi.dgc.Lease) in.readObject();
} catch (ClassCastException | IOException | ClassNotFoundException e) {
if (connection instanceof TCPConnection) {
// Modified to prevent re-use of the connection after an exception
((TCPConnection) connection).getChannel().free(connection, false);
}
call.discardPendingRefs();
throw new java.rmi.UnmarshalException("error unmarshalling return", e);
} finally {
ref.done(call);
}
return $result;
} catch (java.lang.RuntimeException e) {
throw e;
} catch (java.rmi.RemoteException e) {
throw e;
} catch (java.lang.Exception e) {
throw new java.rmi.UnexpectedException("undeclared checked exception", e);
}
}
/**
* ObjectInputFilter to filter DGCClient return value (a Lease).
* The list of acceptable classes is very short and explicit.
* The depth and array sizes are limited.
* <p>
* The filter must accept normal and exception returns.
* A DGC server may throw exceptions that may have a cause
* and suppressed exceptions.
*
* @param filterInfo access to class, arrayLength, etc.
* @return {@link ObjectInputFilter.Status#ALLOWED} if allowed,
* {@link ObjectInputFilter.Status#REJECTED} if rejected,
* otherwise {@link ObjectInputFilter.Status#UNDECIDED}
*/
private static ObjectInputFilter.Status leaseFilter(ObjectInputFilter.FilterInfo filterInfo) {
if (filterInfo.depth() > DGCCLIENT_MAX_DEPTH) {
return ObjectInputFilter.Status.REJECTED;
}
Class<?> clazz = filterInfo.serialClass();
if (clazz != null) {
while (clazz.isArray()) {
if (filterInfo.arrayLength() >= 0 && filterInfo.arrayLength() > DGCCLIENT_MAX_ARRAY_SIZE) {
return ObjectInputFilter.Status.REJECTED;
}
// Arrays are allowed depending on the component type
clazz = clazz.getComponentType();
}
if (clazz.isPrimitive()) {
// Arrays of primitives are allowed
return ObjectInputFilter.Status.ALLOWED;
}
return (clazz == UID.class ||
clazz == VMID.class ||
clazz == Lease.class ||
(Throwable.class.isAssignableFrom(clazz) &&
clazz.getClassLoader() ==
Object.class.getClassLoader()) ||
clazz == StackTraceElement.class ||
clazz == ArrayList.class || // for suppressed exceptions, if any
clazz == Object.class ||
clazz.getName().equals("java.util.Collections$UnmodifiableList") ||
clazz.getName().equals("java.util.Collections$UnmodifiableCollection") ||
clazz.getName().equals("java.util.Collections$UnmodifiableRandomAccessList"))
? ObjectInputFilter.Status.ALLOWED
: ObjectInputFilter.Status.REJECTED;
}
// Not a class, not size limited
return ObjectInputFilter.Status.UNDECIDED;
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.rmi.RemoteException;
import java.rmi.Remote;
import java.rmi.server.ObjID;
import java.rmi.server.RemoteServer;
public interface Endpoint {
/**
* Return a channel that generates connections to the remote
* endpoint.
*/
Channel getChannel();
/**
* Export the object so that it can accept incoming calls at
* the endpoint.
*/
void exportObject(Target target)
throws RemoteException;
/**
* Returns the transport for incoming connections to this endpoint.
**/
Transport getInboundTransport();
/**
* Returns transport for making connections to remote endpoints.
**/
Transport getOutboundTransport();
}

View File

@@ -0,0 +1,317 @@
/*
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.ObjID;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.util.Arrays;
import sun.rmi.transport.tcp.TCPEndpoint;
/**
* NOTE: There is a JDK-internal dependency on the existence of this
* class and its getClientSocketFactory method in the implementation
* of javax.management.remote.rmi.RMIConnector.
**/
public class LiveRef implements Cloneable {
/** wire representation for the object*/
private final Endpoint ep;
private final ObjID id;
/** cached connection service for the object */
private transient Channel ch;
/** flag to indicate whether this ref specifies a local server or
* is a ref for a remote object (surrogate)
*/
private final boolean isLocal;
/**
* Construct a "well-known" live reference to a remote object
* @param isLocalServer If true, indicates this ref specifies a local
* server in this address space; if false, the ref is for a remote
* object (hence a surrogate or proxy) in another address space.
*/
public LiveRef(ObjID objID, Endpoint endpoint, boolean isLocal) {
ep = endpoint;
id = objID;
this.isLocal = isLocal;
}
/**
* Construct a new live reference for a server object in the local
* address space.
*/
public LiveRef(int port) {
this((new ObjID()), port);
}
/**
* Construct a new live reference for a server object in the local
* address space, to use sockets of the specified type.
*/
public LiveRef(int port,
RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
this((new ObjID()), port, csf, ssf);
}
/**
* Construct a new live reference for a "well-known" server object
* in the local address space.
*/
public LiveRef(ObjID objID, int port) {
this(objID, TCPEndpoint.getLocalEndpoint(port), true);
}
/**
* Construct a new live reference for a "well-known" server object
* in the local address space, to use sockets of the specified type.
*/
public LiveRef(ObjID objID, int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
this(objID, TCPEndpoint.getLocalEndpoint(port, csf, ssf), true);
}
/**
* Return a shallow copy of this ref.
*/
public Object clone() {
try {
LiveRef newRef = (LiveRef) super.clone();
return newRef;
} catch (CloneNotSupportedException e) {
throw new InternalError(e.toString(), e);
}
}
/**
* Return the port number associated with this ref.
*/
public int getPort() {
return ((TCPEndpoint) ep).getPort();
}
/**
* Return the client socket factory associated with this ref.
*
* NOTE: There is a JDK-internal dependency on the existence of
* this method in the implementation of
* javax.management.remote.rmi.RMIConnector.
**/
public RMIClientSocketFactory getClientSocketFactory() {
return ((TCPEndpoint) ep).getClientSocketFactory();
}
/**
* Return the server socket factory associated with this ref.
*/
public RMIServerSocketFactory getServerSocketFactory() {
return ((TCPEndpoint) ep).getServerSocketFactory();
}
/**
* Export the object to accept incoming calls.
*/
public void exportObject(Target target) throws RemoteException {
ep.exportObject(target);
}
public Channel getChannel() throws RemoteException {
if (ch == null) {
ch = ep.getChannel();
}
return ch;
}
public ObjID getObjID() {
return id;
}
Endpoint getEndpoint() {
return ep;
}
public String toString() {
String type;
if (isLocal)
type = "local";
else
type = "remote";
return "[endpoint:" + ep + "(" + type + ")," +
"objID:" + id + "]";
}
public int hashCode() {
return id.hashCode();
}
public boolean equals(Object obj) {
if (obj != null && obj instanceof LiveRef) {
LiveRef ref = (LiveRef) obj;
return (ep.equals(ref.ep) && id.equals(ref.id) &&
isLocal == ref.isLocal);
} else {
return false;
}
}
public boolean remoteEquals(Object obj) {
if (obj != null && obj instanceof LiveRef) {
LiveRef ref = (LiveRef) obj;
TCPEndpoint thisEp = ((TCPEndpoint) ep);
TCPEndpoint refEp = ((TCPEndpoint) ref.ep);
RMIClientSocketFactory thisClientFactory =
thisEp.getClientSocketFactory();
RMIClientSocketFactory refClientFactory =
refEp.getClientSocketFactory();
/**
* Fix for 4254103: LiveRef.remoteEquals should not fail
* if one of the objects in the comparison has a null
* server socket. Comparison should only consider the
* following criteria:
*
* hosts, ports, client socket factories and object IDs.
*/
if (thisEp.getPort() != refEp.getPort() ||
!thisEp.getHost().equals(refEp.getHost()))
{
return false;
}
if ((thisClientFactory == null) ^ (refClientFactory == null)) {
return false;
}
if ((thisClientFactory != null) &&
!((thisClientFactory.getClass() ==
refClientFactory.getClass()) &&
(thisClientFactory.equals(refClientFactory))))
{
return false;
}
return (id.equals(ref.id));
} else {
return false;
}
}
public void write(ObjectOutput out, boolean useNewFormat)
throws IOException
{
boolean isResultStream = false;
if (out instanceof ConnectionOutputStream) {
ConnectionOutputStream stream = (ConnectionOutputStream) out;
isResultStream = stream.isResultStream();
/*
* Ensure that referential integrity is not broken while
* this LiveRef is in transit. If it is being marshalled
* as part of a result, it may not otherwise be strongly
* reachable after the remote call has completed; even if
* it is being marshalled as part of an argument, the VM
* may determine that the reference on the stack is no
* longer reachable after marshalling (see 6181943)--
* therefore, tell the stream to save a reference until a
* timeout expires or, for results, a DGCAck message has
* been received from the caller, or for arguments, the
* remote call has completed. For a "local" LiveRef, save
* a reference to the impl directly, because the impl is
* not reachable from the LiveRef (see 4114579);
* otherwise, save a reference to the LiveRef, for the
* client-side DGC to watch over. (Also see 4017232.)
*/
if (isLocal) {
ObjectEndpoint oe =
new ObjectEndpoint(id, ep.getInboundTransport());
Target target = ObjectTable.getTarget(oe);
if (target != null) {
Remote impl = target.getImpl();
if (impl != null) {
stream.saveObject(impl);
}
}
} else {
stream.saveObject(this);
}
}
// All together now write out the endpoint, id, and flag
// (need to choose whether or not to use old JDK1.1 endpoint format)
if (useNewFormat) {
((TCPEndpoint) ep).write(out);
} else {
((TCPEndpoint) ep).writeHostPortFormat(out);
}
id.write(out);
out.writeBoolean(isResultStream);
}
public static LiveRef read(ObjectInput in, boolean useNewFormat)
throws IOException, ClassNotFoundException
{
Endpoint ep;
ObjID id;
// Now read in the endpoint, id, and result flag
// (need to choose whether or not to read old JDK1.1 endpoint format)
if (useNewFormat) {
ep = TCPEndpoint.read(in);
} else {
ep = TCPEndpoint.readHostPortFormat(in);
}
id = ObjID.read(in);
boolean isResultStream = in.readBoolean();
LiveRef ref = new LiveRef(id, ep, false);
if (in instanceof ConnectionInputStream) {
ConnectionInputStream stream = (ConnectionInputStream)in;
// save ref to send "dirty" call after all args/returns
// have been unmarshaled.
stream.saveRef(ref);
if (isResultStream) {
// set flag in stream indicating that remote objects were
// unmarshaled. A DGC ack should be sent by the transport.
stream.setAckNeeded();
}
} else {
DGCClient.registerRefs(ep, Arrays.asList(new LiveRef[] { ref }));
}
return ref;
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.rmi.server.ObjID;
/**
* An object used as a key to the object table that maps an
* instance of this class to a Target.
*
* @author Ann Wollrath
**/
class ObjectEndpoint {
private final ObjID id;
private final Transport transport;
/**
* Constructs a new ObjectEndpoint instance with the specified id and
* transport. The specified id must be non-null, and the specified
* transport must either be non-null or the specified id must be
* equivalent to an ObjID constructed with ObjID.DGC_ID.
*
* @param id the object identifier
* @param transport the transport
* @throws NullPointerException if id is null
**/
ObjectEndpoint(ObjID id, Transport transport) {
if (id == null) {
throw new NullPointerException();
}
assert transport != null || id.equals(new ObjID(ObjID.DGC_ID));
this.id = id;
this.transport = transport;
}
/**
* Compares the specified object with this object endpoint for
* equality.
*
* This method returns true if and only if the specified object is an
* ObjectEndpoint instance with the same object identifier and
* transport as this object.
**/
public boolean equals(Object obj) {
if (obj instanceof ObjectEndpoint) {
ObjectEndpoint oe = (ObjectEndpoint) obj;
return id.equals(oe.id) && transport == oe.transport;
} else {
return false;
}
}
/**
* Returns the hash code value for this object endpoint.
*/
public int hashCode() {
return id.hashCode() ^ (transport != null ? transport.hashCode() : 0);
}
/**
* Returns a string representation for this object endpoint.
*/
public String toString() {
return id.toString();
}
}

View File

@@ -0,0 +1,371 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.lang.ref.ReferenceQueue;
import java.rmi.NoSuchObjectException;
import java.rmi.Remote;
import java.rmi.dgc.VMID;
import java.rmi.server.ExportException;
import java.rmi.server.ObjID;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
import sun.misc.GC;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.security.action.GetLongAction;
/**
* Object table shared by all implementors of the Transport interface.
* This table maps object ids to remote object targets in this address
* space.
*
* @author Ann Wollrath
* @author Peter Jones
*/
public final class ObjectTable {
/** maximum interval between complete garbage collections of local heap */
private final static long gcInterval = // default 1 hour
AccessController.doPrivileged(
new GetLongAction("sun.rmi.dgc.server.gcInterval", 3600000));
/**
* lock guarding objTable and implTable.
* Holders MAY acquire a Target instance's lock or keepAliveLock.
*/
private static final Object tableLock = new Object();
/** tables mapping to Target, keyed from ObjectEndpoint and impl object */
private static final Map<ObjectEndpoint,Target> objTable =
new HashMap<>();
private static final Map<WeakRef,Target> implTable =
new HashMap<>();
/**
* lock guarding keepAliveCount, reaper, and gcLatencyRequest.
* Holders may NOT acquire a Target instance's lock or tableLock.
*/
private static final Object keepAliveLock = new Object();
/** count of non-permanent objects in table or still processing calls */
private static int keepAliveCount = 0;
/** thread to collect unreferenced objects from table */
private static Thread reaper = null;
/** queue notified when weak refs in the table are cleared */
static final ReferenceQueue<Object> reapQueue = new ReferenceQueue<>();
/** handle for GC latency request (for future cancellation) */
private static GC.LatencyRequest gcLatencyRequest = null;
/*
* Disallow anyone from creating one of these.
*/
private ObjectTable() {}
/**
* Returns the target associated with the object id.
*/
static Target getTarget(ObjectEndpoint oe) {
synchronized (tableLock) {
return objTable.get(oe);
}
}
/**
* Returns the target associated with the remote object
*/
public static Target getTarget(Remote impl) {
synchronized (tableLock) {
return implTable.get(new WeakRef(impl));
}
}
/**
* Returns the stub for the remote object <b>obj</b> passed
* as a parameter. This operation is only valid <i>after</i>
* the object has been exported.
*
* @return the stub for the remote object, <b>obj</b>.
* @exception NoSuchObjectException if the stub for the
* remote object could not be found.
*/
public static Remote getStub(Remote impl)
throws NoSuchObjectException
{
Target target = getTarget(impl);
if (target == null) {
throw new NoSuchObjectException("object not exported");
} else {
return target.getStub();
}
}
/**
* Remove the remote object, obj, from the RMI runtime. If
* successful, the object can no longer accept incoming RMI calls.
* If the force parameter is true, the object is forcibly unexported
* even if there are pending calls to the remote object or the
* remote object still has calls in progress. If the force
* parameter is false, the object is only unexported if there are
* no pending or in progress calls to the object.
*
* @param obj the remote object to be unexported
* @param force if true, unexports the object even if there are
* pending or in-progress calls; if false, only unexports the object
* if there are no pending or in-progress calls
* @return true if operation is successful, false otherwise
* @exception NoSuchObjectException if the remote object is not
* currently exported
*/
public static boolean unexportObject(Remote obj, boolean force)
throws java.rmi.NoSuchObjectException
{
synchronized (tableLock) {
Target target = getTarget(obj);
if (target == null) {
throw new NoSuchObjectException("object not exported");
} else {
if (target.unexport(force)) {
removeTarget(target);
return true;
} else {
return false;
}
}
}
}
/**
* Add target to object table. If it is not a permanent entry, then
* make sure that reaper thread is running to remove collected entries
* and keep VM alive.
*/
static void putTarget(Target target) throws ExportException {
ObjectEndpoint oe = target.getObjectEndpoint();
WeakRef weakImpl = target.getWeakImpl();
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
DGCImpl.dgcLog.log(Log.VERBOSE, "add object " + oe);
}
synchronized (tableLock) {
/**
* Do nothing if impl has already been collected (see 6597112). Check while
* holding tableLock to ensure that Reaper cannot process weakImpl in between
* null check and put/increment effects.
*/
if (target.getImpl() != null) {
if (objTable.containsKey(oe)) {
throw new ExportException(
"internal error: ObjID already in use");
} else if (implTable.containsKey(weakImpl)) {
throw new ExportException("object already exported");
}
objTable.put(oe, target);
implTable.put(weakImpl, target);
if (!target.isPermanent()) {
incrementKeepAliveCount();
}
}
}
}
/**
* Remove target from object table.
*
* NOTE: This method must only be invoked while synchronized on
* the "tableLock" object, because it does not do so itself.
*/
private static void removeTarget(Target target) {
// assert Thread.holdsLock(tableLock);
ObjectEndpoint oe = target.getObjectEndpoint();
WeakRef weakImpl = target.getWeakImpl();
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
DGCImpl.dgcLog.log(Log.VERBOSE, "remove object " + oe);
}
objTable.remove(oe);
implTable.remove(weakImpl);
target.markRemoved(); // handles decrementing keep-alive count
}
/**
* Process client VM signalling reference for given ObjID: forward to
* corresponding Target entry. If ObjID is not found in table,
* no action is taken.
*/
static void referenced(ObjID id, long sequenceNum, VMID vmid) {
synchronized (tableLock) {
ObjectEndpoint oe =
new ObjectEndpoint(id, Transport.currentTransport());
Target target = objTable.get(oe);
if (target != null) {
target.referenced(sequenceNum, vmid);
}
}
}
/**
* Process client VM dropping reference for given ObjID: forward to
* corresponding Target entry. If ObjID is not found in table,
* no action is taken.
*/
static void unreferenced(ObjID id, long sequenceNum, VMID vmid,
boolean strong)
{
synchronized (tableLock) {
ObjectEndpoint oe =
new ObjectEndpoint(id, Transport.currentTransport());
Target target = objTable.get(oe);
if (target != null)
target.unreferenced(sequenceNum, vmid, strong);
}
}
/**
* Increments the "keep-alive count".
*
* The "keep-alive count" is the number of non-permanent remote objects
* that are either in the object table or still have calls in progress.
* Therefore, this method should be invoked exactly once for every
* non-permanent remote object exported (a remote object must be
* exported before it can have any calls in progress).
*
* The VM is "kept alive" while the keep-alive count is greater than
* zero; this is accomplished by keeping a non-daemon thread running.
*
* Because non-permanent objects are those that can be garbage
* collected while exported, and thus those for which the "reaper"
* thread operates, the reaper thread also serves as the non-daemon
* VM keep-alive thread; a new reaper thread is created if necessary.
*/
static void incrementKeepAliveCount() {
synchronized (keepAliveLock) {
keepAliveCount++;
if (reaper == null) {
reaper = AccessController.doPrivileged(
new NewThreadAction(new Reaper(), "Reaper", false));
reaper.start();
}
/*
* While there are non-"permanent" objects in the object table,
* request a maximum latency for inspecting the entire heap
* from the local garbage collector, to place an upper bound
* on the time to discover remote objects that have become
* unreachable (and thus can be removed from the table).
*/
if (gcLatencyRequest == null) {
gcLatencyRequest = GC.requestLatency(gcInterval);
}
}
}
/**
* Decrements the "keep-alive count".
*
* The "keep-alive count" is the number of non-permanent remote objects
* that are either in the object table or still have calls in progress.
* Therefore, this method should be invoked exactly once for every
* previously-exported non-permanent remote object that both has been
* removed from the object table and has no calls still in progress.
*
* If the keep-alive count is decremented to zero, then the current
* reaper thread is terminated to cease keeping the VM alive (and
* because there are no more non-permanent remote objects to reap).
*/
static void decrementKeepAliveCount() {
synchronized (keepAliveLock) {
keepAliveCount--;
if (keepAliveCount == 0) {
if (!(reaper != null)) { throw new AssertionError(); }
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
reaper.interrupt();
return null;
}
});
reaper = null;
/*
* If there are no longer any non-permanent objects in the
* object table, we are no longer concerned with the latency
* of local garbage collection here.
*/
gcLatencyRequest.cancel();
gcLatencyRequest = null;
}
}
}
/**
* The Reaper thread waits for notifications that weak references in the
* object table have been cleared. When it receives a notification, it
* removes the corresponding entry from the table.
*
* Since the Reaper is created as a non-daemon thread, it also serves
* to keep the VM from exiting while there are objects in the table
* (other than permanent entries that should neither be reaped nor
* keep the VM alive).
*/
private static class Reaper implements Runnable {
public void run() {
try {
do {
// wait for next cleared weak reference
WeakRef weakImpl = (WeakRef) reapQueue.remove();
synchronized (tableLock) {
Target target = implTable.get(weakImpl);
if (target != null) {
if (!target.isEmpty()) {
throw new Error(
"object with known references collected");
} else if (target.isPermanent()) {
throw new Error("permanent object collected");
}
removeTarget(target);
}
}
} while (!Thread.interrupted());
} catch (InterruptedException e) {
// pass away if interrupted
}
}
}
}

View File

@@ -0,0 +1,342 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.StreamCorruptedException;
import java.rmi.RemoteException;
import java.rmi.MarshalException;
import java.rmi.UnmarshalException;
import java.rmi.server.ObjID;
import java.rmi.server.RemoteCall;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.misc.ObjectInputFilter;
import sun.rmi.runtime.Log;
import sun.rmi.server.UnicastRef;
import sun.rmi.transport.tcp.TCPEndpoint;
/**
* Stream-based implementation of the RemoteCall interface.
*
* @author Ann Wollrath
*/
@SuppressWarnings("deprecation")
public class StreamRemoteCall implements RemoteCall {
private ConnectionInputStream in = null;
private ConnectionOutputStream out = null;
private Connection conn;
private ObjectInputFilter filter = null;
private boolean resultStarted = false;
private Exception serverException = null;
public StreamRemoteCall(Connection c) {
conn = c;
}
public StreamRemoteCall(Connection c, ObjID id, int op, long hash)
throws RemoteException
{
try {
conn = c;
Transport.transportLog.log(Log.VERBOSE,
"write remote call header...");
// write out remote call header info...
// call header, part 1 (read by Transport)
conn.getOutputStream().write(TransportConstants.Call);
getOutputStream(); // creates a MarshalOutputStream
id.write(out); // object id (target of call)
// call header, part 2 (read by Dispatcher)
out.writeInt(op); // method number (operation index)
out.writeLong(hash); // stub/skeleton hash
} catch (IOException e) {
throw new MarshalException("Error marshaling call header", e);
}
}
/**
* Return the connection associated with this call.
*/
public Connection getConnection() {
return conn;
}
/**
* Return the output stream the stub/skeleton should put arguments/results
* into.
*/
public ObjectOutput getOutputStream() throws IOException {
return getOutputStream(false);
}
private ObjectOutput getOutputStream(boolean resultStream)
throws IOException
{
if (out == null) {
Transport.transportLog.log(Log.VERBOSE, "getting output stream");
out = new ConnectionOutputStream(conn, resultStream);
}
return out;
}
/**
* Release the outputStream Currently, will not complain if the
* output stream is released more than once.
*/
public void releaseOutputStream() throws IOException {
try {
if (out != null) {
try {
out.flush();
} finally {
out.done(); // always start DGC ack timer
}
}
conn.releaseOutputStream();
} finally {
out = null;
}
}
public void setObjectInputFilter(ObjectInputFilter filter) {
if (in != null) {
throw new IllegalStateException("set filter must occur before calling getInputStream");
}
this.filter = filter;
}
/**
* Get the InputStream the stub/skeleton should get results/arguments
* from.
*/
public ObjectInput getInputStream() throws IOException {
if (in == null) {
Transport.transportLog.log(Log.VERBOSE, "getting input stream");
in = new ConnectionInputStream(conn.getInputStream());
if (filter != null) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
ObjectInputFilter.Config.setObjectInputFilter(in, filter);
return null;
});
}
}
return in;
}
/**
* Release the input stream, this would allow some transports to release
* the channel early.
*/
public void releaseInputStream() throws IOException {
/* WARNING: Currently, the UnicastRef.java invoke methods rely
* upon this method not throwing an IOException.
*/
try {
if (in != null) {
// execute MarshalInputStream "done" callbacks
try {
in.done();
} catch (RuntimeException e) {
}
// add saved references to DGC table
in.registerRefs();
/* WARNING: The connection being passed to done may have
* already been freed.
*/
in.done(conn);
}
conn.releaseInputStream();
} finally {
in = null;
}
}
/**
* Discard any post-processing of refs the InputStream.
*/
public void discardPendingRefs() {
in.discardRefs();
}
/**
* Returns an output stream (may put out header information
* relating to the success of the call).
* @param success If true, indicates normal return, else indicates
* exceptional return.
* @exception StreamCorruptedException If result stream previously
* acquired
* @exception IOException For any other problem with I/O.
*/
public ObjectOutput getResultStream(boolean success) throws IOException {
/* make sure result code only marshaled once. */
if (resultStarted)
throw new StreamCorruptedException("result already in progress");
else
resultStarted = true;
// write out return header
// return header, part 1 (read by Transport)
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeByte(TransportConstants.Return);// transport op
getOutputStream(true); // creates a MarshalOutputStream
// return header, part 2 (read by client-side RemoteCall)
if (success) //
out.writeByte(TransportConstants.NormalReturn);
else
out.writeByte(TransportConstants.ExceptionalReturn);
out.writeID(); // write id for gcAck
return out;
}
/**
* Do whatever it takes to execute the call.
*/
@SuppressWarnings("fallthrough")
public void executeCall() throws Exception {
byte returnType;
// read result header
DGCAckHandler ackHandler = null;
try {
if (out != null) {
ackHandler = out.getDGCAckHandler();
}
releaseOutputStream();
DataInputStream rd = new DataInputStream(conn.getInputStream());
byte op = rd.readByte();
if (op != TransportConstants.Return) {
if (Transport.transportLog.isLoggable(Log.BRIEF)) {
Transport.transportLog.log(Log.BRIEF,
"transport return code invalid: " + op);
}
throw new UnmarshalException("Transport return code invalid");
}
getInputStream();
returnType = in.readByte();
in.readID(); // id for DGC acknowledgement
} catch (UnmarshalException e) {
throw e;
} catch (IOException e) {
throw new UnmarshalException("Error unmarshaling return header",
e);
} finally {
if (ackHandler != null) {
ackHandler.release();
}
}
// read return value
switch (returnType) {
case TransportConstants.NormalReturn:
break;
case TransportConstants.ExceptionalReturn:
Object ex;
try {
ex = in.readObject();
} catch (Exception e) {
discardPendingRefs();
throw new UnmarshalException("Error unmarshaling return", e);
}
// An exception should have been received,
// if so throw it, else flag error
if (ex instanceof Exception) {
exceptionReceivedFromServer((Exception) ex);
} else {
discardPendingRefs();
throw new UnmarshalException("Return type not Exception");
}
// Exception is thrown before fallthrough can occur
default:
if (Transport.transportLog.isLoggable(Log.BRIEF)) {
Transport.transportLog.log(Log.BRIEF,
"return code invalid: " + returnType);
}
throw new UnmarshalException("Return code invalid");
}
}
/**
* Routine that causes the stack traces of remote exceptions to be
* filled in with the current stack trace on the client. Detail
* exceptions are filled in iteratively.
*/
protected void exceptionReceivedFromServer(Exception ex) throws Exception {
serverException = ex;
StackTraceElement[] serverTrace = ex.getStackTrace();
StackTraceElement[] clientTrace = (new Throwable()).getStackTrace();
StackTraceElement[] combinedTrace =
new StackTraceElement[serverTrace.length + clientTrace.length];
System.arraycopy(serverTrace, 0, combinedTrace, 0,
serverTrace.length);
System.arraycopy(clientTrace, 0, combinedTrace, serverTrace.length,
clientTrace.length);
ex.setStackTrace(combinedTrace);
/*
* Log the details of a server exception thrown as a result of a
* remote method invocation.
*/
if (UnicastRef.clientCallLog.isLoggable(Log.BRIEF)) {
/* log call exception returned from server before it is rethrown */
TCPEndpoint ep = (TCPEndpoint) conn.getChannel().getEndpoint();
UnicastRef.clientCallLog.log(Log.BRIEF, "outbound call " +
"received exception: [" + ep.getHost() + ":" +
ep.getPort() + "] exception: ", ex);
}
throw ex;
}
/*
* method to retrieve possible server side exceptions (which will
* be throw from exceptionReceivedFromServer(...) )
*/
public Exception getServerException() {
return serverException;
}
public void done() throws IOException {
/* WARNING: Currently, the UnicastRef.java invoke methods rely
* upon this method not throwing an IOException.
*/
releaseInputStream();
}
}

View File

@@ -0,0 +1,461 @@
/*
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.rmi.Remote;
import java.rmi.NoSuchObjectException;
import java.rmi.dgc.VMID;
import java.rmi.server.ObjID;
import java.rmi.server.Unreferenced;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.server.Dispatcher;
/**
* A target contains information pertaining to a remote object that
* resides in this address space. Targets are located via the
* ObjectTable.
*/
public final class Target {
/** object id for target */
private final ObjID id;
/** flag indicating whether target is subject to collection */
private final boolean permanent;
/** weak reference to remote object implementation */
private final WeakRef weakImpl;
/** dispatcher for remote object */
private volatile Dispatcher disp;
/** stub for remote object */
private final Remote stub;
/** set of clients that hold references to this target */
private final Vector<VMID> refSet = new Vector<>();
/** table that maps client endpoints to sequence numbers */
private final Hashtable<VMID, SequenceEntry> sequenceTable =
new Hashtable<>(5);
/** access control context in which target was created */
private final AccessControlContext acc;
/** context class loader in which target was created */
private final ClassLoader ccl;
/** number of pending/executing calls */
private int callCount = 0;
/** true if this target has been removed from the object table */
private boolean removed = false;
/**
* the transport through which this target was exported and
* through which remote calls will be allowed
*/
private volatile Transport exportedTransport = null;
/** number to identify next callback thread created here */
private static int nextThreadNum = 0;
/**
* Construct a Target for a remote object "impl" with
* a specific object id.
*
* If "permanent" is true, then the impl is pinned permanently
* (the impl will not be collected via distributed and/or local
* GC). If "on" is false, than the impl is subject to
* collection. Permanent objects do not keep a server from
* exiting.
*/
public Target(Remote impl, Dispatcher disp, Remote stub, ObjID id,
boolean permanent)
{
this.weakImpl = new WeakRef(impl, ObjectTable.reapQueue);
this.disp = disp;
this.stub = stub;
this.id = id;
this.acc = AccessController.getContext();
/*
* Fix for 4149366: so that downloaded parameter types unmarshalled
* for this impl will be compatible with types known only to the
* impl class's class loader (when it's not identical to the
* exporting thread's context class loader), mark the impl's class
* loader as the loader to use as the context class loader in the
* server's dispatch thread while a call to this impl is being
* processed (unless this exporting thread's context class loader is
* a child of the impl's class loader, such as when a registry is
* exported by an application, in which case this thread's context
* class loader is preferred).
*/
ClassLoader threadContextLoader =
Thread.currentThread().getContextClassLoader();
ClassLoader serverLoader = impl.getClass().getClassLoader();
if (checkLoaderAncestry(threadContextLoader, serverLoader)) {
this.ccl = threadContextLoader;
} else {
this.ccl = serverLoader;
}
this.permanent = permanent;
if (permanent) {
pinImpl();
}
}
/**
* Return true if the first class loader is a child of (or identical
* to) the second class loader. Either loader may be "null", which is
* considered to be the parent of any non-null class loader.
*
* (utility method added for the 1.2beta4 fix for 4149366)
*/
private static boolean checkLoaderAncestry(ClassLoader child,
ClassLoader ancestor)
{
if (ancestor == null) {
return true;
} else if (child == null) {
return false;
} else {
for (ClassLoader parent = child;
parent != null;
parent = parent.getParent())
{
if (parent == ancestor) {
return true;
}
}
return false;
}
}
/** Get the stub (proxy) object for this target
*/
public Remote getStub() {
return stub;
}
/**
* Returns the object endpoint for the target.
*/
ObjectEndpoint getObjectEndpoint() {
return new ObjectEndpoint(id, exportedTransport);
}
/**
* Get the weak reference for the Impl of this target.
*/
WeakRef getWeakImpl() {
return weakImpl;
}
/**
* Returns the dispatcher for this remote object target.
*/
Dispatcher getDispatcher() {
return disp;
}
AccessControlContext getAccessControlContext() {
return acc;
}
ClassLoader getContextClassLoader() {
return ccl;
}
/**
* Get the impl for this target.
* Note: this may return null if the impl has been garbage collected.
* (currently, there is no need to make this method public)
*/
Remote getImpl() {
return (Remote)weakImpl.get();
}
/**
* Returns true if the target is permanent.
*/
boolean isPermanent() {
return permanent;
}
/**
* Pin impl in target. Pin the WeakRef object so it holds a strong
* reference to the object to it will not be garbage collected locally.
* This way there is a single object responsible for the weak ref
* mechanism.
*/
synchronized void pinImpl() {
weakImpl.pin();
}
/**
* Unpin impl in target. Weaken the reference to impl so that it
* can be garbage collected locally. But only if there the refSet
* is empty. All of the weak/strong handling is in WeakRef
*/
synchronized void unpinImpl() {
/* only unpin if:
* a) impl is not permanent, and
* b) impl is not already unpinned, and
* c) there are no external references (outside this
* address space) for the impl
*/
if (!permanent && refSet.isEmpty()) {
weakImpl.unpin();
}
}
/**
* Enable the transport through which remote calls to this target
* are allowed to be set if it has not already been set.
*/
void setExportedTransport(Transport exportedTransport) {
if (this.exportedTransport == null) {
this.exportedTransport = exportedTransport;
}
}
/**
* Add an endpoint to the remembered set. Also adds a notifier
* to call back if the address space associated with the endpoint
* dies.
*/
synchronized void referenced(long sequenceNum, VMID vmid) {
// check sequence number for vmid
SequenceEntry entry = sequenceTable.get(vmid);
if (entry == null) {
sequenceTable.put(vmid, new SequenceEntry(sequenceNum));
} else if (entry.sequenceNum < sequenceNum) {
entry.update(sequenceNum);
} else {
// late dirty call; ignore.
return;
}
if (!refSet.contains(vmid)) {
/*
* A Target must be pinned while its refSet is not empty. It may
* have become unpinned if external LiveRefs only existed in
* serialized form for some period of time, or if a client failed
* to renew its lease due to a transient network failure. So,
* make sure that it is pinned here; this fixes bugid 4069644.
*/
pinImpl();
if (getImpl() == null) // too late if impl was collected
return;
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
DGCImpl.dgcLog.log(Log.VERBOSE, "add to dirty set: " + vmid);
}
refSet.addElement(vmid);
DGCImpl.getDGCImpl().registerTarget(vmid, this);
}
}
/**
* Remove endpoint from remembered set. If set becomes empty,
* remove server from Transport's object table.
*/
synchronized void unreferenced(long sequenceNum, VMID vmid, boolean strong)
{
// check sequence number for vmid
SequenceEntry entry = sequenceTable.get(vmid);
if (entry == null || entry.sequenceNum > sequenceNum) {
// late clean call; ignore
return;
} else if (strong) {
// strong clean call; retain sequenceNum
entry.retain(sequenceNum);
} else if (entry.keep == false) {
// get rid of sequence number
sequenceTable.remove(vmid);
}
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
DGCImpl.dgcLog.log(Log.VERBOSE, "remove from dirty set: " + vmid);
}
refSetRemove(vmid);
}
/**
* Remove endpoint from the reference set.
*/
synchronized private void refSetRemove(VMID vmid) {
// remove notification request
DGCImpl.getDGCImpl().unregisterTarget(vmid, this);
if (refSet.removeElement(vmid) && refSet.isEmpty()) {
// reference set is empty, so server can be garbage collected.
// remove object from table.
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
DGCImpl.dgcLog.log(Log.VERBOSE,
"reference set is empty: target = " + this);
}
/*
* If the remote object implements the Unreferenced interface,
* invoke its unreferenced callback in a separate thread.
*/
Remote obj = getImpl();
if (obj instanceof Unreferenced) {
final Unreferenced unrefObj = (Unreferenced) obj;
AccessController.doPrivileged(
new NewThreadAction(() -> {
Thread.currentThread().setContextClassLoader(ccl);
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
unrefObj.unreferenced();
return null;
}, acc);
}, "Unreferenced-" + nextThreadNum++, false, true)).start();
// REMIND: access to nextThreadNum not synchronized; you care?
}
unpinImpl();
}
}
/**
* Mark this target as not accepting new calls if any of the
* following conditions exist: a) the force parameter is true,
* b) the target's call count is zero, or c) the object is already
* not accepting calls. Returns true if target is marked as not
* accepting new calls; returns false otherwise.
*/
synchronized boolean unexport(boolean force) {
if ((force == true) || (callCount == 0) || (disp == null)) {
disp = null;
/*
* Fix for 4331349: unpin object so that it may be gc'd.
* Also, unregister all vmids referencing this target
* so target can be gc'd.
*/
unpinImpl();
DGCImpl dgc = DGCImpl.getDGCImpl();
Enumeration<VMID> enum_ = refSet.elements();
while (enum_.hasMoreElements()) {
VMID vmid = enum_.nextElement();
dgc.unregisterTarget(vmid, this);
}
return true;
} else {
return false;
}
}
/**
* Mark this target as having been removed from the object table.
*/
synchronized void markRemoved() {
if (!(!removed)) { throw new AssertionError(); }
removed = true;
if (!permanent && callCount == 0) {
ObjectTable.decrementKeepAliveCount();
}
if (exportedTransport != null) {
exportedTransport.targetUnexported();
}
}
/**
* Increment call count.
*/
synchronized void incrementCallCount() throws NoSuchObjectException {
if (disp != null) {
callCount ++;
} else {
throw new NoSuchObjectException("object not accepting new calls");
}
}
/**
* Decrement call count.
*/
synchronized void decrementCallCount() {
if (--callCount < 0) {
throw new Error("internal error: call count less than zero");
}
/*
* The "keep-alive count" is the number of non-permanent remote
* objects that are either in the object table or still have calls
* in progress. Therefore, this state change may affect the
* keep-alive count: if this target is for a non-permanent remote
* object that has been removed from the object table and now has a
* call count of zero, it needs to be decremented.
*/
if (!permanent && removed && callCount == 0) {
ObjectTable.decrementKeepAliveCount();
}
}
/**
* Returns true if remembered set is empty; otherwise returns
* false
*/
boolean isEmpty() {
return refSet.isEmpty();
}
/**
* This method is called if the address space associated with the
* vmid dies. In that case, the vmid should be removed
* from the reference set.
*/
synchronized public void vmidDead(VMID vmid) {
if (DGCImpl.dgcLog.isLoggable(Log.BRIEF)) {
DGCImpl.dgcLog.log(Log.BRIEF, "removing endpoint " +
vmid + " from reference set");
}
sequenceTable.remove(vmid);
refSetRemove(vmid);
}
}
class SequenceEntry {
long sequenceNum;
boolean keep;
SequenceEntry(long sequenceNum) {
this.sequenceNum = sequenceNum;
keep = false;
}
void retain(long sequenceNum) {
this.sequenceNum = sequenceNum;
keep = true;
}
void update(long sequenceNum) {
this.sequenceNum = sequenceNum;
}
}

View File

@@ -0,0 +1,258 @@
/*
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.io.IOException;
import java.io.ObjectOutput;
import java.rmi.MarshalException;
import java.rmi.NoSuchObjectException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.LogStream;
import java.rmi.server.ObjID;
import java.rmi.server.RemoteCall;
import java.rmi.server.RemoteServer;
import java.rmi.server.ServerNotActiveException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import sun.rmi.runtime.Log;
import sun.rmi.server.Dispatcher;
import sun.rmi.server.UnicastServerRef;
/**
* Transport abstraction for enabling communication between different
* VMs.
*
* @author Ann Wollrath
*/
@SuppressWarnings("deprecation")
public abstract class Transport {
/** "transport" package log level */
static final int logLevel = LogStream.parseLevel(getLogLevel());
private static String getLogLevel() {
return java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.rmi.transport.logLevel"));
}
/* transport package log */
static final Log transportLog =
Log.getLog("sun.rmi.transport.misc", "transport", Transport.logLevel);
/** References the current transport when a call is being serviced */
private static final ThreadLocal<Transport> currentTransport = new ThreadLocal<>();
/** ObjID for DGCImpl */
private static final ObjID dgcID = new ObjID(ObjID.DGC_ID);
/** AccessControlContext for setting context ClassLoader */
private static final AccessControlContext SETCCL_ACC;
static {
Permissions perms = new Permissions();
perms.add(new RuntimePermission("setContextClassLoader"));
ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
SETCCL_ACC = new AccessControlContext(pd);
}
/**
* Returns a <I>Channel</I> that generates connections to the
* endpoint <I>ep</I>. A Channel is an object that creates and
* manages connections of a particular type to some particular
* address space.
* @param ep the endpoint to which connections will be generated.
* @return the channel or null if the transport cannot
* generate connections to this endpoint
*/
public abstract Channel getChannel(Endpoint ep);
/**
* Removes the <I>Channel</I> that generates connections to the
* endpoint <I>ep</I>.
*/
public abstract void free(Endpoint ep);
/**
* Export the object so that it can accept incoming calls.
*/
public void exportObject(Target target) throws RemoteException {
target.setExportedTransport(this);
ObjectTable.putTarget(target);
}
/**
* Invoked when an object that was exported on this transport has
* become unexported, either by being garbage collected or by
* being explicitly unexported.
**/
protected void targetUnexported() { }
/**
* Returns the current transport if a call is being serviced, otherwise
* returns null.
**/
static Transport currentTransport() {
return currentTransport.get();
}
/**
* Verify that the current access control context has permission to accept
* the connection being dispatched by the current thread. The current
* access control context is passed as a parameter to avoid the overhead of
* an additional call to AccessController.getContext.
*/
protected abstract void checkAcceptPermission(AccessControlContext acc);
/**
* Sets the context class loader for the current thread.
*/
private static void setContextClassLoader(ClassLoader ccl) {
AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
Thread.currentThread().setContextClassLoader(ccl);
return null;
}, SETCCL_ACC);
}
/**
* Service an incoming remote call. When a message arrives on the
* connection indicating the beginning of a remote call, the
* threads are required to call the <I>serviceCall</I> method of
* their transport. The default implementation of this method
* locates and calls the dispatcher object. Ordinarily a
* transport implementation will not need to override this method.
* At the entry to <I>tr.serviceCall(conn)</I>, the connection's
* input stream is positioned at the start of the incoming
* message. The <I>serviceCall</I> method processes the incoming
* remote invocation and sends the result on the connection's
* output stream. If it returns "true", then the remote
* invocation was processed without error and the transport can
* cache the connection. If it returns "false", a protocol error
* occurred during the call, and the transport should destroy the
* connection.
*/
public boolean serviceCall(final RemoteCall call) {
try {
/* read object id */
final Remote impl;
ObjID id;
try {
id = ObjID.read(call.getInputStream());
} catch (java.io.IOException e) {
throw new MarshalException("unable to read objID", e);
}
/* get the remote object */
Transport transport = id.equals(dgcID) ? null : this;
Target target =
ObjectTable.getTarget(new ObjectEndpoint(id, transport));
if (target == null || (impl = target.getImpl()) == null) {
throw new NoSuchObjectException("no such object in table");
}
final Dispatcher disp = target.getDispatcher();
target.incrementCallCount();
try {
/* call the dispatcher */
transportLog.log(Log.VERBOSE, "call dispatcher");
final AccessControlContext acc =
target.getAccessControlContext();
ClassLoader ccl = target.getContextClassLoader();
ClassLoader savedCcl = Thread.currentThread().getContextClassLoader();
try {
setContextClassLoader(ccl);
currentTransport.set(this);
try {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction<Void>() {
public Void run() throws IOException {
checkAcceptPermission(acc);
disp.dispatch(impl, call);
return null;
}
}, acc);
} catch (java.security.PrivilegedActionException pae) {
throw (IOException) pae.getException();
}
} finally {
setContextClassLoader(savedCcl);
currentTransport.set(null);
}
} catch (IOException ex) {
transportLog.log(Log.BRIEF,
"exception thrown by dispatcher: ", ex);
return false;
} finally {
target.decrementCallCount();
}
} catch (RemoteException e) {
// if calls are being logged, write out exception
if (UnicastServerRef.callLog.isLoggable(Log.BRIEF)) {
// include client host name if possible
String clientHost = "";
try {
clientHost = "[" +
RemoteServer.getClientHost() + "] ";
} catch (ServerNotActiveException ex) {
}
String message = clientHost + "exception: ";
UnicastServerRef.callLog.log(Log.BRIEF, message, e);
}
/* We will get a RemoteException if either a) the objID is
* not readable, b) the target is not in the object table, or
* c) the object is in the midst of being unexported (note:
* NoSuchObjectException is thrown by the incrementCallCount
* method if the object is being unexported). Here it is
* relatively safe to marshal an exception to the client
* since the client will not have seen a return value yet.
*/
try {
ObjectOutput out = call.getResultStream(false);
UnicastServerRef.clearStackTraces(e);
out.writeObject(e);
call.releaseOutputStream();
} catch (IOException ie) {
transportLog.log(Log.BRIEF,
"exception thrown marshalling exception: ", ie);
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
public class TransportConstants {
/** Transport magic number: "JRMI"*/
public static final int Magic = 0x4a524d49;
/** Transport version number */
public static final short Version = 2;
/** Connection uses stream protocol */
public static final byte StreamProtocol = 0x4b;
/** Protocol for single operation per connection; no ack required */
public static final byte SingleOpProtocol = 0x4c;
/** Connection uses multiplex protocol */
public static final byte MultiplexProtocol = 0x4d;
/** Ack for transport protocol */
public static final byte ProtocolAck = 0x4e;
/** Negative ack for transport protocol (protocol not supported) */
public static final byte ProtocolNack = 0x4f;
/** RMI call */
public static final byte Call = 0x50;
/** RMI return */
public static final byte Return = 0x51;
/** Ping operation */
public static final byte Ping = 0x52;
/** Acknowledgment for Ping operation */
public static final byte PingAck = 0x53;
/** Acknowledgment for distributed GC */
public static final byte DGCAck = 0x54;
/** Normal return (with or without return value) */
public static final byte NormalReturn = 0x01;
/** Exceptional return */
public static final byte ExceptionalReturn = 0x02;
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport;
import java.lang.ref.*;
import sun.rmi.runtime.Log;
/**
* WeakRef objects are used by the RMI runtime to hold potentially weak
* references to exported remote objects in the local object table.
*
* This class extends the functionality of java.lang.ref.WeakReference in
* several ways. The methods pin() and unpin() can be used to set
* whether the contained reference is strong or weak (it is weak upon
* construction). The hashCode() and equals() methods are overridden so
* that WeakRef objects hash and compare to each other according to the
* object identity of their referents.
*
* @author Ann Wollrath
* @author Peter Jones
*/
class WeakRef extends WeakReference<Object> {
/** value of the referent's "identity" hash code */
private int hashValue;
/** strong reference to the referent, for when this WeakRef is "pinned" */
private Object strongRef = null;
/**
* Create a new WeakRef to the given object.
*/
public WeakRef(Object obj) {
super(obj);
setHashValue(obj); // cache object's "identity" hash code
}
/**
* Create a new WeakRef to the given object, registered with a queue.
*/
public WeakRef(Object obj, ReferenceQueue<Object> q) {
super(obj, q);
setHashValue(obj); // cache object's "identity" hash code
}
/**
* Pin the contained reference (make this a strong reference).
*/
public synchronized void pin() {
if (strongRef == null) {
strongRef = get();
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
DGCImpl.dgcLog.log(Log.VERBOSE,
"strongRef = " + strongRef);
}
}
}
/**
* Unpin the contained reference (make this a weak reference).
*/
public synchronized void unpin() {
if (strongRef != null) {
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
DGCImpl.dgcLog.log(Log.VERBOSE,
"strongRef = " + strongRef);
}
strongRef = null;
}
}
/*
* Cache referent's "identity" hash code (so that we still have the
* value after the referent gets cleared).
*
* We cannot use the value from the object's hashCode() method, since
* if the object is of a remote class not extended from RemoteObject
* and it is trying to implement hashCode() and equals() so that it
* can be compared to stub objects, its own hash code could not have
* been initialized yet (see bugid 4102938). Also, object table keys
* based on server objects are indeed matched on object identity, so
* this is the correct hash technique regardless.
*/
private void setHashValue(Object obj) {
if (obj != null) {
hashValue = System.identityHashCode(obj);
} else {
hashValue = 0;
}
}
/**
* Always return the "identity" hash code of the original referent.
*/
public int hashCode() {
return hashValue;
}
/**
* Return true if "obj" is this identical WeakRef object, or, if the
* contained reference has not been cleared, if "obj" is another WeakRef
* object with the identical non-null referent. Otherwise, return false.
*/
public boolean equals(Object obj) {
if (obj instanceof WeakRef) {
if (obj == this)
return true;
Object referent = get();
return (referent != null) && (referent == ((WeakRef) obj).get());
} else {
return false;
}
}
}

View File

@@ -0,0 +1,423 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
import java.net.*;
import java.util.Hashtable;
/**
* CGIClientException is thrown when an error is detected
* in a client's request.
*/
class CGIClientException extends Exception {
private static final long serialVersionUID = 8147981687059865216L;
public CGIClientException(String s) {
super(s);
}
public CGIClientException(String s, Throwable cause) {
super(s, cause);
}
}
/**
* CGIServerException is thrown when an error occurs here on the server.
*/
class CGIServerException extends Exception {
private static final long serialVersionUID = 6928425456704527017L;
public CGIServerException(String s) {
super(s);
}
public CGIServerException(String s, Throwable cause) {
super(s, cause);
}
}
/**
* CGICommandHandler is the interface to an object that handles a
* particular supported command.
*/
interface CGICommandHandler {
/**
* Return the string form of the command
* to be recognized in the query string.
*/
public String getName();
/**
* Execute the command with the given string as parameter.
*/
public void execute(String param) throws CGIClientException, CGIServerException;
}
/**
* The CGIHandler class contains methods for executing as a CGI program.
* The main function interprets the query string as a command of the form
* "<command>=<parameters>".
*
* This class depends on the CGI 1.0 environment variables being set as
* properties of the same name in this Java VM.
*
* All data and methods of this class are static because they are specific
* to this particular CGI process.
*/
public final class CGIHandler {
/* get CGI parameters that we need */
static int ContentLength;
static String QueryString;
static String RequestMethod;
static String ServerName;
static int ServerPort;
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
ContentLength =
Integer.getInteger("CONTENT_LENGTH", 0).intValue();
QueryString = System.getProperty("QUERY_STRING", "");
RequestMethod = System.getProperty("REQUEST_METHOD", "");
ServerName = System.getProperty("SERVER_NAME", "");
ServerPort = Integer.getInteger("SERVER_PORT", 0).intValue();
return null;
}
});
}
/* list of handlers for supported commands */
private static CGICommandHandler commands[] = {
new CGIForwardCommand(),
new CGIGethostnameCommand(),
new CGIPingCommand(),
new CGITryHostnameCommand()
};
/* construct table mapping command strings to handlers */
private static Hashtable<String, CGICommandHandler> commandLookup;
static {
commandLookup = new Hashtable<>();
for (int i = 0; i < commands.length; ++ i)
commandLookup.put(commands[i].getName(), commands[i]);
}
/* prevent instantiation of this class */
private CGIHandler() {}
/**
* Execute command given in query string on URL. The string before
* the first '=' is interpreted as the command name, and the string
* after the first '=' is the parameters to the command.
*/
public static void main(String args[])
{
try {
String command, param;
int delim = QueryString.indexOf("=");
if (delim == -1) {
command = QueryString;
param = "";
}
else {
command = QueryString.substring(0, delim);
param = QueryString.substring(delim + 1);
}
CGICommandHandler handler =
commandLookup.get(command);
if (handler != null)
try {
handler.execute(param);
} catch (CGIClientException e) {
e.printStackTrace();
returnClientError(e.getMessage());
} catch (CGIServerException e) {
e.printStackTrace();
returnServerError(e.getMessage());
}
else
returnClientError("invalid command.");
} catch (Exception e) {
e.printStackTrace();
returnServerError("internal error: " + e.getMessage());
}
System.exit(0);
}
/**
* Return an HTML error message indicating there was error in
* the client's request.
*/
private static void returnClientError(String message)
{
System.out.println("Status: 400 Bad Request: " + message);
System.out.println("Content-type: text/html");
System.out.println("");
System.out.println("<HTML>" +
"<HEAD><TITLE>Java RMI Client Error" +
"</TITLE></HEAD>" +
"<BODY>");
System.out.println("<H1>Java RMI Client Error</H1>");
System.out.println("");
System.out.println(message);
System.out.println("</BODY></HTML>");
System.exit(1);
}
/**
* Return an HTML error message indicating an error occurred
* here on the server.
*/
private static void returnServerError(String message)
{
System.out.println("Status: 500 Server Error: " + message);
System.out.println("Content-type: text/html");
System.out.println("");
System.out.println("<HTML>" +
"<HEAD><TITLE>Java RMI Server Error" +
"</TITLE></HEAD>" +
"<BODY>");
System.out.println("<H1>Java RMI Server Error</H1>");
System.out.println("");
System.out.println(message);
System.out.println("</BODY></HTML>");
System.exit(1);
}
}
/**
* "forward" command: Forward request body to local port on the server,
* and send response back to client.
*/
final class CGIForwardCommand implements CGICommandHandler {
public String getName() {
return "forward";
}
@SuppressWarnings("deprecation")
private String getLine (DataInputStream socketIn) throws IOException {
return socketIn.readLine();
}
public void execute(String param) throws CGIClientException, CGIServerException
{
if (!CGIHandler.RequestMethod.equals("POST"))
throw new CGIClientException("can only forward POST requests");
int port;
try {
port = Integer.parseInt(param);
} catch (NumberFormatException e) {
throw new CGIClientException("invalid port number.", e);
}
if (port <= 0 || port > 0xFFFF)
throw new CGIClientException("invalid port: " + port);
if (port < 1024)
throw new CGIClientException("permission denied for port: " +
port);
byte buffer[];
Socket socket;
try {
socket = new Socket(InetAddress.getLocalHost(), port);
} catch (IOException e) {
throw new CGIServerException("could not connect to local port", e);
}
/*
* read client's request body
*/
DataInputStream clientIn = new DataInputStream(System.in);
buffer = new byte[CGIHandler.ContentLength];
try {
clientIn.readFully(buffer);
} catch (EOFException e) {
throw new CGIClientException("unexpected EOF reading request body", e);
} catch (IOException e) {
throw new CGIClientException("error reading request body", e);
}
/*
* send to local server in HTTP
*/
try {
DataOutputStream socketOut =
new DataOutputStream(socket.getOutputStream());
socketOut.writeBytes("POST / HTTP/1.0\r\n");
socketOut.writeBytes("Content-length: " +
CGIHandler.ContentLength + "\r\n\r\n");
socketOut.write(buffer);
socketOut.flush();
} catch (IOException e) {
throw new CGIServerException("error writing to server", e);
}
/*
* read response
*/
DataInputStream socketIn;
try {
socketIn = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
throw new CGIServerException("error reading from server", e);
}
String key = "Content-length:".toLowerCase();
boolean contentLengthFound = false;
String line;
int responseContentLength = -1;
do {
try {
line = getLine(socketIn);
} catch (IOException e) {
throw new CGIServerException("error reading from server", e);
}
if (line == null)
throw new CGIServerException(
"unexpected EOF reading server response");
if (line.toLowerCase().startsWith(key)) {
if (contentLengthFound) {
throw new CGIServerException(
"Multiple Content-length entries found.");
} else {
responseContentLength =
Integer.parseInt(line.substring(key.length()).trim());
contentLengthFound = true;
}
}
} while ((line.length() != 0) &&
(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
if (!contentLengthFound || responseContentLength < 0)
throw new CGIServerException(
"missing or invalid content length in server response");
buffer = new byte[responseContentLength];
try {
socketIn.readFully(buffer);
} catch (EOFException e) {
throw new CGIServerException(
"unexpected EOF reading server response", e);
} catch (IOException e) {
throw new CGIServerException("error reading from server", e);
}
/*
* send response back to client
*/
System.out.println("Status: 200 OK");
System.out.println("Content-type: application/octet-stream");
System.out.println("");
try {
System.out.write(buffer);
} catch (IOException e) {
throw new CGIServerException("error writing response", e);
}
System.out.flush();
}
}
/**
* "gethostname" command: Return the host name of the server as the
* response body
*/
final class CGIGethostnameCommand implements CGICommandHandler {
public String getName() {
return "gethostname";
}
public void execute(String param)
{
System.out.println("Status: 200 OK");
System.out.println("Content-type: application/octet-stream");
System.out.println("Content-length: " +
CGIHandler.ServerName.length());
System.out.println("");
System.out.print(CGIHandler.ServerName);
System.out.flush();
}
}
/**
* "ping" command: Return an OK status to indicate that connection
* was successful.
*/
final class CGIPingCommand implements CGICommandHandler {
public String getName() {
return "ping";
}
public void execute(String param)
{
System.out.println("Status: 200 OK");
System.out.println("Content-type: application/octet-stream");
System.out.println("Content-length: 0");
System.out.println("");
}
}
/**
* "tryhostname" command: Return a human readable message describing
* what host name is available to local Java VMs.
*/
final class CGITryHostnameCommand implements CGICommandHandler {
public String getName() {
return "tryhostname";
}
public void execute(String param)
{
System.out.println("Status: 200 OK");
System.out.println("Content-type: text/html");
System.out.println("");
System.out.println("<HTML>" +
"<HEAD><TITLE>Java RMI Server Hostname Info" +
"</TITLE></HEAD>" +
"<BODY>");
System.out.println("<H1>Java RMI Server Hostname Info</H1>");
System.out.println("<H2>Local host name available to Java VM:</H2>");
System.out.print("<P>InetAddress.getLocalHost().getHostName()");
try {
String localHostName = InetAddress.getLocalHost().getHostName();
System.out.println(" = " + localHostName);
} catch (UnknownHostException e) {
System.out.println(" threw java.net.UnknownHostException");
}
System.out.println("<H2>Server host information obtained through CGI interface from HTTP server:</H2>");
System.out.println("<P>SERVER_NAME = " + CGIHandler.ServerName);
System.out.println("<P>SERVER_PORT = " + CGIHandler.ServerPort);
System.out.println("</BODY></HTML>");
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import sun.rmi.runtime.Log;
/**
* The HttpAwareServerSocket class extends the java.net.ServerSocket
* class. It behaves like a ServerSocket, except that if
* the first four bytes of an accepted socket are the letters "POST",
* then it returns an HttpReceiveSocket instead of a java.net.Socket.
* This means that the accept method blocks until four bytes have been
* read from the new socket's input stream.
*/
class HttpAwareServerSocket extends ServerSocket {
/**
* Create a server socket on a specified port.
* @param port the port
* @exception IOException IO error when opening the socket.
*/
public HttpAwareServerSocket(int port) throws IOException
{
super(port);
}
/**
* Create a server socket, bind it to the specified local port
* and listen to it. You can connect to an annonymous port by
* specifying the port number to be 0. <i>backlog</i> specifies
* how many connection requests the system will queue up while waiting
* for the ServerSocket to execute accept().
* @param port the specified port
* @param backlog the number of queued connect requests pending accept
*/
public HttpAwareServerSocket(int port, int backlog) throws IOException
{
super(port, backlog);
}
/**
* Accept a connection. This method will block until the connection
* is made and four bytes can be read from the input stream.
* If the first four bytes are "POST", then an HttpReceiveSocket is
* returned, which will handle the HTTP protocol wrapping.
* Otherwise, a WrappedSocket is returned. The input stream will be
* reset to the beginning of the transmission.
* In either case, a BufferedInputStream will already be on top of
* the underlying socket's input stream.
* @exception IOException IO error when waiting for the connection.
*/
public Socket accept() throws IOException
{
Socket socket = super.accept();
BufferedInputStream in =
new BufferedInputStream(socket.getInputStream());
RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
"socket accepted (checking for POST)");
in.mark(4);
boolean isHttp = (in.read() == 'P') &&
(in.read() == 'O') &&
(in.read() == 'S') &&
(in.read() == 'T');
in.reset();
if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.BRIEF)) {
RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
(isHttp ? "POST found, HTTP socket returned" :
"POST not found, direct socket returned"));
}
if (isHttp)
return new HttpReceiveSocket(socket, in, null);
else
return new WrappedSocket(socket, in, null);
}
/**
* Return the implementation address and implementation port of
* the HttpAwareServerSocket as a String.
*/
public String toString()
{
return "HttpAware" + super.toString();
}
}

View File

@@ -0,0 +1,204 @@
/*
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
import sun.rmi.runtime.Log;
/**
* The HttpInputStream class assists the HttpSendSocket and HttpReceiveSocket
* classes by filtering out the header for the message as well as any
* data after its proper content length.
*/
class HttpInputStream extends FilterInputStream {
/** bytes remaining to be read from proper content of message */
protected int bytesLeft;
/** bytes remaining to be read at time of last mark */
protected int bytesLeftAtMark;
/**
* Create new filter on a given input stream.
* @param in the InputStream to filter from
*/
public HttpInputStream(InputStream in) throws IOException
{
super(in);
if (in.markSupported())
in.mark(0); // prevent resetting back to old marks
// pull out header, looking for content length
DataInputStream dis = new DataInputStream(in);
String key = "Content-length:".toLowerCase();
boolean contentLengthFound = false;
String line;
do {
line = dis.readLine();
if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"received header line: \"" + line + "\"");
}
if (line == null)
throw new EOFException();
if (line.toLowerCase().startsWith(key)) {
if (contentLengthFound) {
throw new IOException(
"Multiple Content-length entries found.");
} else {
bytesLeft =
Integer.parseInt(line.substring(key.length()).trim());
contentLengthFound = true;
}
}
// The idea here is to go past the first blank line.
// Some DataInputStream.readLine() documentation specifies that
// it does include the line-terminating character(s) in the
// returned string, but it actually doesn't, so we'll cover
// all cases here...
} while ((line.length() != 0) &&
(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
if (!contentLengthFound || bytesLeft < 0) {
// This really shouldn't happen, but if it does, shoud we fail??
// For now, just give up and let a whole lot of bytes through...
bytesLeft = Integer.MAX_VALUE;
}
bytesLeftAtMark = bytesLeft;
if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"content length: " + bytesLeft);
}
}
/**
* Returns the number of bytes that can be read with blocking.
* Make sure that this does not exceed the number of bytes remaining
* in the proper content of the message.
*/
public int available() throws IOException
{
int bytesAvailable = in.available();
if (bytesAvailable > bytesLeft)
bytesAvailable = bytesLeft;
return bytesAvailable;
}
/**
* Read a byte of data from the stream. Make sure that one is available
* from the proper content of the message, else -1 is returned to
* indicate to the user that the end of the stream has been reached.
*/
public int read() throws IOException
{
if (bytesLeft > 0) {
int data = in.read();
if (data != -1)
-- bytesLeft;
if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"received byte: '" +
((data & 0x7F) < ' ' ? " " : String.valueOf((char) data)) +
"' " + data);
}
return data;
}
else {
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"read past content length");
return -1;
}
}
public int read(byte b[], int off, int len) throws IOException
{
if (bytesLeft == 0 && len > 0) {
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"read past content length");
return -1;
}
if (len > bytesLeft)
len = bytesLeft;
int bytesRead = in.read(b, off, len);
bytesLeft -= bytesRead;
if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"read " + bytesRead + " bytes, " + bytesLeft + " remaining");
}
return bytesRead;
}
/**
* Mark the current position in the stream (for future calls to reset).
* Remember where we are within the proper content of the message, so
* that a reset method call can recreate our state properly.
* @param readlimit how many bytes can be read before mark becomes invalid
*/
public void mark(int readlimit)
{
in.mark(readlimit);
if (in.markSupported())
bytesLeftAtMark = bytesLeft;
}
/**
* Repositions the stream to the last marked position. Make sure to
* adjust our position within the proper content accordingly.
*/
public void reset() throws IOException
{
in.reset();
bytesLeft = bytesLeftAtMark;
}
/**
* Skips bytes of the stream. Make sure to adjust our
* position within the proper content accordingly.
* @param n number of bytes to be skipped
*/
public long skip(long n) throws IOException
{
if (n > bytesLeft)
n = bytesLeft;
long bytesSkipped = in.skip(n);
bytesLeft -= bytesSkipped;
return bytesSkipped;
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
/**
* The HttpOutputStream class assists the HttpSendSocket and HttpReceiveSocket
* classes by providing an output stream that buffers its entire input until
* closed, and then it sends the complete transmission prefixed by the end of
* an HTTP header that specifies the content length.
*/
class HttpOutputStream extends ByteArrayOutputStream {
/** the output stream to send response to */
protected OutputStream out;
/** true if HTTP response has been sent */
boolean responseSent = false;
/**
* Begin buffering new HTTP response to be sent to a given stream.
* @param out the OutputStream to send response to
*/
public HttpOutputStream(OutputStream out) {
super();
this.out = out;
}
/**
* On close, send HTTP-packaged response.
*/
public synchronized void close() throws IOException {
if (!responseSent) {
/*
* If response would have zero content length, then make it
* have some arbitrary data so that certain clients will not
* fail because the "document contains no data".
*/
if (size() == 0)
write(emptyData);
DataOutputStream dos = new DataOutputStream(out);
dos.writeBytes("Content-type: application/octet-stream\r\n");
dos.writeBytes("Content-length: " + size() + "\r\n");
dos.writeBytes("\r\n");
writeTo(dos);
dos.flush();
// Do not close the underlying stream here, because that would
// close the underlying socket and prevent reading a response.
reset(); // reset byte array
responseSent = true;
}
}
/** data to send if the response would otherwise be empty */
private static byte[] emptyData = { 0 };
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright (c) 1996, 2000, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
import java.net.Socket;
import java.net.InetAddress;
/**
* The HttpReceiveSocket class extends the WrappedSocket class
* by removing the HTTP protocol packaging from the input stream and
* formatting the output stream as an HTTP response.
*
* NOTES:
*
* The output stream must be explicitly closed for the output to be
* sent, since the HttpResponseOutputStream needs to buffer the entire
* transmission to be able to fill in the content-length field of
* the HTTP header. Closing this socket will do this.
*
* The constructor blocks until the HTTP protocol header
* is received. This could be fixed, but I don't think it should be a
* problem because this object would not be created unless the
* HttpAwareServerSocket has detected the beginning of the header
* anyway, so the rest should be there.
*
* This socket can only be used to process one POST and reply to it.
* Another message would be received on a newly accepted socket anyway.
*/
public class HttpReceiveSocket extends WrappedSocket implements RMISocketInfo {
/** true if the HTTP header has pushed through the output stream yet */
private boolean headerSent = false;
/**
* Layer on top of a pre-existing Socket object, and use specified
* input and output streams.
* @param socket the pre-existing socket to use
* @param in the InputStream to use for this socket (can be null)
* @param out the OutputStream to use for this socket (can be null)
*/
public HttpReceiveSocket(Socket socket, InputStream in, OutputStream out)
throws IOException
{
super(socket, in, out);
this.in = new HttpInputStream(in != null ? in :
socket.getInputStream());
this.out = (out != null ? out :
socket.getOutputStream());
}
/**
* Indicate that this socket is not reusable.
*/
public boolean isReusable()
{
return false;
}
/**
* Get the address to which this socket is connected. "null" is always
* returned (to indicate an unknown address) because the originating
* host's IP address cannot be reliably determined: both because the
* request probably went through a proxy server, and because if it was
* delivered by a local forwarder (CGI script or servlet), we do NOT
* want it to appear as if the call is coming from the local host (in
* case the remote object makes access control decisions based on the
* "client host" of a remote call; see bugid 4399040).
*/
public InetAddress getInetAddress() {
return null;
}
/**
* Get an OutputStream for this socket.
*/
public OutputStream getOutputStream() throws IOException
{
if (!headerSent) { // could this be done in constructor??
DataOutputStream dos = new DataOutputStream(out);
dos.writeBytes("HTTP/1.0 200 OK\r\n");
dos.flush();
headerSent = true;
out = new HttpOutputStream(out);
}
return out;
}
/**
* Close the socket.
*/
public synchronized void close() throws IOException
{
getOutputStream().close(); // make sure response is sent
socket.close();
}
/**
* Return string representation of the socket.
*/
public String toString()
{
return "HttpReceive" + socket.toString();
}
}

View File

@@ -0,0 +1,161 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
/**
* The HttpSendInputStream class is used by the HttpSendSocket class as
* a layer on the top of the InputStream it returns so that it can be
* notified of attempts to read from it. This allows the HttpSendSocket
* to know when it should push across its output message.
*/
class HttpSendInputStream extends FilterInputStream {
/** the HttpSendSocket object that is providing this stream */
HttpSendSocket owner;
/**
* Create new filter on a given input stream.
* @param in the InputStream to filter from
* @param owner the HttpSendSocket that is providing this stream
*/
public HttpSendInputStream(InputStream in, HttpSendSocket owner)
throws IOException
{
super(in);
this.owner = owner;
}
/**
* Mark this stream as inactive for its owner socket, so the next time
* a read is attempted, the owner will be notified and a new underlying
* input stream obtained.
*/
public void deactivate()
{
in = null;
}
/**
* Read a byte of data from the stream.
*/
public int read() throws IOException
{
if (in == null)
in = owner.readNotify();
return in.read();
}
/**
* Read into an array of bytes.
* @param b the buffer into which the data is to be read
* @param off the start offset of the data
* @param len the maximum number of bytes to read
*/
public int read(byte b[], int off, int len) throws IOException
{
if (len == 0)
return 0;
if (in == null)
in = owner.readNotify();
return in.read(b, off, len);
}
/**
* Skip bytes of input.
* @param n the number of bytes to be skipped
*/
public long skip(long n) throws IOException
{
if (n == 0)
return 0;
if (in == null)
in = owner.readNotify();
return in.skip(n);
}
/**
* Return the number of bytes that can be read without blocking.
*/
public int available() throws IOException
{
if (in == null)
in = owner.readNotify();
return in.available();
}
/**
* Close the stream.
*/
public void close() throws IOException
{
owner.close();
}
/**
* Mark the current position in the stream.
* @param readlimit how many bytes can be read before mark becomes invalid
*/
public synchronized void mark(int readlimit)
{
if (in == null) {
try {
in = owner.readNotify();
}
catch (IOException e) {
return;
}
}
in.mark(readlimit);
}
/**
* Reposition the stream to the last marked position.
*/
public synchronized void reset() throws IOException
{
if (in == null)
in = owner.readNotify();
in.reset();
}
/**
* Return true if this stream type supports mark/reset.
*/
public boolean markSupported()
{
if (in == null) {
try {
in = owner.readNotify();
}
catch (IOException e) {
return false;
}
}
return in.markSupported();
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
/**
* The HttpSendOutputStream class is used by the HttpSendSocket class as
* a layer on the top of the OutputStream it returns so that it can be
* notified of attempts to write to it. This allows the HttpSendSocket
* to know when it should construct a new message.
*/
class HttpSendOutputStream extends FilterOutputStream {
/** the HttpSendSocket object that is providing this stream */
HttpSendSocket owner;
/**
* Create new filter on a given output stream.
* @param out the OutputStream to filter from
* @param owner the HttpSendSocket that is providing this stream
*/
public HttpSendOutputStream(OutputStream out, HttpSendSocket owner)
throws IOException
{
super(out);
this.owner = owner;
}
/**
* Mark this stream as inactive for its owner socket, so the next time
* a write is attempted, the owner will be notified and a new underlying
* output stream obtained.
*/
public void deactivate()
{
out = null;
}
/**
* Write a byte of data to the stream.
*/
public void write(int b) throws IOException
{
if (out == null)
out = owner.writeNotify();
out.write(b);
}
/**
* Write a subarray of bytes.
* @param b the buffer from which the data is to be written
* @param off the start offset of the data
* @param len the number of bytes to be written
*/
public void write(byte b[], int off, int len) throws IOException
{
if (len == 0)
return;
if (out == null)
out = owner.writeNotify();
out.write(b, off, len);
}
/**
* Flush the stream.
*/
public void flush() throws IOException
{
if (out != null)
out.flush();
}
/**
* Close the stream.
*/
public void close() throws IOException
{
flush();
owner.close();
}
}

View File

@@ -0,0 +1,343 @@
/*
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
import java.net.*;
import sun.rmi.runtime.Log;
/**
* The HttpSendSocket class extends the java.net.Socket class
* by enclosing the data output stream in, then extracting the input
* stream from, an HTTP protocol transmission.
*
* NOTES:
*
* Since the length of the output request must be known before the
* HTTP header can be completed, all of the output is buffered by
* an HttpOutputStream object until either an attempt is made to
* read from this socket, or the socket is explicitly closed.
*
* On the first read attempt to read from this socket, the buffered
* output is sent to the destination as the body of an HTTP POST
* request. All reads will then acquire data from the body of
* the response. A subsequent attempt to write to this socket will
* throw an IOException.
*/
class HttpSendSocket extends Socket implements RMISocketInfo {
/** the host to connect to */
protected String host;
/** the port to connect to */
protected int port;
/** the URL to forward through */
protected URL url;
/** the object managing this connection through the URL */
protected URLConnection conn = null;
/** internal input stream for this socket */
protected InputStream in = null;
/** internal output stream for this socket */
protected OutputStream out = null;
/** the notifying input stream returned to users */
protected HttpSendInputStream inNotifier;
/** the notifying output stream returned to users */
protected HttpSendOutputStream outNotifier;
/**
* Line separator string. This is the value of the line.separator
* property at the moment that the socket was created.
*/
private String lineSeparator =
java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
/**
* Create a stream socket and connect it to the specified port on
* the specified host.
* @param host the host
* @param port the port
*/
public HttpSendSocket(String host, int port, URL url) throws IOException
{
super((SocketImpl)null); // no underlying SocketImpl for this object
if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"host = " + host + ", port = " + port + ", url = " + url);
}
this.host = host;
this.port = port;
this.url = url;
inNotifier = new HttpSendInputStream(null, this);
outNotifier = new HttpSendOutputStream(writeNotify(), this);
}
/**
* Create a stream socket and connect it to the specified port on
* the specified host.
* @param host the host
* @param port the port
*/
public HttpSendSocket(String host, int port) throws IOException
{
this(host, port, new URL("http", host, port, "/"));
}
/**
* Create a stream socket and connect it to the specified address on
* the specified port.
* @param address the address
* @param port the port
*/
public HttpSendSocket(InetAddress address, int port) throws IOException
{
this(address.getHostName(), port);
}
/**
* Indicate that this socket is not reusable.
*/
public boolean isReusable()
{
return false;
}
/**
* Create a new socket connection to host (or proxy), and prepare to
* send HTTP transmission.
*/
public synchronized OutputStream writeNotify() throws IOException
{
if (conn != null) {
throw new IOException("attempt to write on HttpSendSocket after " +
"request has been sent");
}
conn = url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-type", "application/octet-stream");
inNotifier.deactivate();
in = null;
return out = conn.getOutputStream();
}
/**
* Send HTTP output transmission and prepare to receive response.
*/
public synchronized InputStream readNotify() throws IOException
{
RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
"sending request and activating input stream");
outNotifier.deactivate();
out.close();
out = null;
try {
in = conn.getInputStream();
} catch (IOException e) {
RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
"failed to get input stream, exception: ", e);
throw new IOException("HTTP request failed");
}
/*
* If an HTTP error response is returned, sometimes an IOException
* is thrown, which is handled above, and other times it isn't, and
* the error response body will be available for reading.
* As a safety net to catch any such unexpected HTTP behavior, we
* verify that the content type of the response is what the
* HttpOutputStream generates: "application/octet-stream".
* (Servers' error responses will generally be "text/html".)
* Any error response body is printed to the log.
*/
String contentType = conn.getContentType();
if (contentType == null ||
!conn.getContentType().equals("application/octet-stream"))
{
if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.BRIEF)) {
String message;
if (contentType == null) {
message = "missing content type in response" +
lineSeparator;
} else {
message = "invalid content type in response: " +
contentType + lineSeparator;
}
message += "HttpSendSocket.readNotify: response body: ";
try {
BufferedReader din = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = din.readLine()) != null)
message += line + lineSeparator;
} catch (IOException e) {
}
RMIMasterSocketFactory.proxyLog.log(Log.BRIEF, message);
}
throw new IOException("HTTP request failed");
}
return in;
}
/**
* Get the address to which the socket is connected.
*/
public InetAddress getInetAddress()
{
try {
return InetAddress.getByName(host);
} catch (UnknownHostException e) {
return null; // null if couldn't resolve destination host
}
}
/**
* Get the local address to which the socket is bound.
*/
public InetAddress getLocalAddress()
{
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
return null; // null if couldn't determine local host
}
}
/**
* Get the remote port to which the socket is connected.
*/
public int getPort()
{
return port;
}
/**
* Get the local port to which the socket is connected.
*/
public int getLocalPort()
{
return -1; // request not applicable to this socket type
}
/**
* Get an InputStream for this socket.
*/
public InputStream getInputStream() throws IOException
{
return inNotifier;
}
/**
* Get an OutputStream for this socket.
*/
public OutputStream getOutputStream() throws IOException
{
return outNotifier;
}
/**
* Enable/disable TCP_NODELAY.
* This operation has no effect for an HttpSendSocket.
*/
public void setTcpNoDelay(boolean on) throws SocketException
{
}
/**
* Retrieve whether TCP_NODELAY is enabled.
*/
public boolean getTcpNoDelay() throws SocketException
{
return false; // imply option is disabled
}
/**
* Enable/disable SO_LINGER with the specified linger time.
* This operation has no effect for an HttpSendSocket.
*/
public void setSoLinger(boolean on, int val) throws SocketException
{
}
/**
* Retrive setting for SO_LINGER.
*/
public int getSoLinger() throws SocketException
{
return -1; // imply option is disabled
}
/**
* Enable/disable SO_TIMEOUT with the specified timeout
* This operation has no effect for an HttpSendSocket.
*/
public synchronized void setSoTimeout(int timeout) throws SocketException
{
}
/**
* Retrive setting for SO_TIMEOUT.
*/
public synchronized int getSoTimeout() throws SocketException
{
return 0; // imply option is disabled
}
/**
* Close the socket.
*/
public synchronized void close() throws IOException
{
if (out != null) // push out transmission if not done
out.close();
}
/**
* Return string representation of this pseudo-socket.
*/
public String toString()
{
return "HttpSendSocket[host=" + host +
",port=" + port +
",url=" + url + "]";
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.rmi.server.RMISocketFactory;
/**
* RMIDirectSocketFactory creates a direct socket connection to the
* specified port on the specified host.
*/
public class RMIDirectSocketFactory extends RMISocketFactory {
public Socket createSocket(String host, int port) throws IOException
{
return new Socket(host, port);
}
public ServerSocket createServerSocket(int port) throws IOException
{
return new ServerSocket(port);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.URL;
import java.rmi.server.RMISocketFactory;
/**
* RMIHttpToCGISocketFactory creates a socket connection to the
* specified host that is comminicated within an HTTP request,
* forwarded through the default firewall proxy, to the target host's
* normal HTTP server, to a CGI program which forwards the request to
* the actual specified port on the socket.
*/
public class RMIHttpToCGISocketFactory extends RMISocketFactory {
public Socket createSocket(String host, int port)
throws IOException
{
return new HttpSendSocket(host, port,
new URL("http", host,
"/cgi-bin/java-rmi.cgi" +
"?forward=" + port));
}
public ServerSocket createServerSocket(int port) throws IOException
{
return new HttpAwareServerSocket(port);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.URL;
import java.rmi.server.RMISocketFactory;
/**
* RMIHttpToPortSocketFactory creates a socket connection to the
* specified host that is communicated within an HTTP request,
* forwarded through the default firewall proxy, directly to the
* specified port.
*/
public class RMIHttpToPortSocketFactory extends RMISocketFactory {
public Socket createSocket(String host, int port)
throws IOException
{
return new HttpSendSocket(host, port,
new URL("http", host, port, "/"));
}
public ServerSocket createServerSocket(int port)
throws IOException
{
return new HttpAwareServerSocket(port);
}
}

View File

@@ -0,0 +1,472 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;
import java.rmi.server.LogStream;
import java.rmi.server.RMISocketFactory;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.security.action.GetBooleanAction;
import sun.security.action.GetLongAction;
import sun.security.action.GetPropertyAction;
/**
* RMIMasterSocketFactory attempts to create a socket connection to the
* specified host using successively less efficient mechanisms
* until one succeeds. If the host is successfully connected to,
* the factory for the successful mechanism is stored in an internal
* hash table keyed by the host name, so that future attempts to
* connect to the same host will automatically use the same
* mechanism.
*/
@SuppressWarnings("deprecation")
public class RMIMasterSocketFactory extends RMISocketFactory {
/** "proxy" package log level */
static int logLevel = LogStream.parseLevel(getLogLevel());
private static String getLogLevel() {
return java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.rmi.transport.proxy.logLevel"));
}
/* proxy package log */
static final Log proxyLog =
Log.getLog("sun.rmi.transport.tcp.proxy",
"transport", RMIMasterSocketFactory.logLevel);
/** timeout for attemping direct socket connections */
private static long connectTimeout = getConnectTimeout();
private static long getConnectTimeout() {
return java.security.AccessController.doPrivileged(
new GetLongAction("sun.rmi.transport.proxy.connectTimeout",
15000)).longValue(); // default: 15 seconds
}
/** whether to fallback to HTTP on general connect failures */
private static final boolean eagerHttpFallback =
java.security.AccessController.doPrivileged(new GetBooleanAction(
"sun.rmi.transport.proxy.eagerHttpFallback")).booleanValue();
/** table of hosts successfully connected to and the factory used */
private Hashtable<String, RMISocketFactory> successTable =
new Hashtable<>();
/** maximum number of hosts to remember successful connection to */
private static final int MaxRememberedHosts = 64;
/** list of the hosts in successTable in initial connection order */
private Vector<String> hostList = new Vector<>(MaxRememberedHosts);
/** default factory for initial use for direct socket connection */
protected RMISocketFactory initialFactory = new RMIDirectSocketFactory();
/** ordered list of factories to try as alternate connection
* mechanisms if a direct socket connections fails */
protected Vector<RMISocketFactory> altFactoryList;
/**
* Create a RMIMasterSocketFactory object. Establish order of
* connection mechanisms to attempt on createSocket, if a direct
* socket connection fails.
*/
public RMIMasterSocketFactory() {
altFactoryList = new Vector<>(2);
boolean setFactories = false;
try {
String proxyHost;
proxyHost = java.security.AccessController.doPrivileged(
new GetPropertyAction("http.proxyHost"));
if (proxyHost == null)
proxyHost = java.security.AccessController.doPrivileged(
new GetPropertyAction("proxyHost"));
boolean disable = java.security.AccessController.doPrivileged(
new GetPropertyAction("java.rmi.server.disableHttp", "true"))
.equalsIgnoreCase("true");
if (!disable && proxyHost != null && proxyHost.length() > 0) {
setFactories = true;
}
} catch (Exception e) {
// unable to obtain the properties, so use the default behavior.
}
if (setFactories) {
altFactoryList.addElement(new RMIHttpToPortSocketFactory());
altFactoryList.addElement(new RMIHttpToCGISocketFactory());
}
}
/**
* Create a new client socket. If we remember connecting to this host
* successfully before, then use the same factory again. Otherwise,
* try using a direct socket connection and then the alternate factories
* in the order specified in altFactoryList.
*/
public Socket createSocket(String host, int port)
throws IOException
{
if (proxyLog.isLoggable(Log.BRIEF)) {
proxyLog.log(Log.BRIEF, "host: " + host + ", port: " + port);
}
/*
* If we don't have any alternate factories to consult, short circuit
* the fallback procedure and delegate to the initial factory.
*/
if (altFactoryList.size() == 0) {
return initialFactory.createSocket(host, port);
}
RMISocketFactory factory;
/*
* If we remember successfully connecting to this host before,
* use the same factory.
*/
factory = successTable.get(host);
if (factory != null) {
if (proxyLog.isLoggable(Log.BRIEF)) {
proxyLog.log(Log.BRIEF,
"previously successful factory found: " + factory);
}
return factory.createSocket(host, port);
}
/*
* Next, try a direct socket connection. Open socket in another
* thread and only wait for specified timeout, in case the socket
* would otherwise spend minutes trying an unreachable host.
*/
Socket initialSocket = null;
Socket fallbackSocket = null;
final AsyncConnector connector =
new AsyncConnector(initialFactory, host, port,
AccessController.getContext());
// connection must be attempted with
// this thread's access control context
IOException initialFailure = null;
try {
synchronized (connector) {
Thread t = java.security.AccessController.doPrivileged(
new NewThreadAction(connector, "AsyncConnector", true));
t.start();
try {
long now = System.currentTimeMillis();
long deadline = now + connectTimeout;
do {
connector.wait(deadline - now);
initialSocket = checkConnector(connector);
if (initialSocket != null)
break;
now = System.currentTimeMillis();
} while (now < deadline);
} catch (InterruptedException e) {
throw new InterruptedIOException(
"interrupted while waiting for connector");
}
}
// assume no route to host (for now) if no connection yet
if (initialSocket == null)
throw new NoRouteToHostException(
"connect timed out: " + host);
proxyLog.log(Log.BRIEF, "direct socket connection successful");
return initialSocket;
} catch (UnknownHostException | NoRouteToHostException e) {
initialFailure = e;
} catch (SocketException e) {
if (eagerHttpFallback) {
initialFailure = e;
} else {
throw e;
}
} finally {
if (initialFailure != null) {
if (proxyLog.isLoggable(Log.BRIEF)) {
proxyLog.log(Log.BRIEF,
"direct socket connection failed: ", initialFailure);
}
// Finally, try any alternate connection mechanisms.
for (int i = 0; i < altFactoryList.size(); ++ i) {
factory = altFactoryList.elementAt(i);
if (proxyLog.isLoggable(Log.BRIEF)) {
proxyLog.log(Log.BRIEF,
"trying with factory: " + factory);
}
try (Socket testSocket =
factory.createSocket(host, port)) {
// For HTTP connections, the output (POST request) must
// be sent before we verify a successful connection.
// So, sacrifice a socket for the sake of testing...
// The following sequence should verify a successful
// HTTP connection if no IOException is thrown.
InputStream in = testSocket.getInputStream();
int b = in.read(); // probably -1 for EOF...
} catch (IOException ex) {
if (proxyLog.isLoggable(Log.BRIEF)) {
proxyLog.log(Log.BRIEF, "factory failed: ", ex);
}
continue;
}
proxyLog.log(Log.BRIEF, "factory succeeded");
// factory succeeded, open new socket for caller's use
try {
fallbackSocket = factory.createSocket(host, port);
} catch (IOException ex) { // if it fails 2nd time,
} // just give up
break;
}
}
}
synchronized (successTable) {
try {
// check once again to see if direct connection succeeded
synchronized (connector) {
initialSocket = checkConnector(connector);
}
if (initialSocket != null) {
// if we had made another one as well, clean it up...
if (fallbackSocket != null)
fallbackSocket.close();
return initialSocket;
}
// if connector ever does get socket, it won't be used
connector.notUsed();
} catch (UnknownHostException | NoRouteToHostException e) {
initialFailure = e;
} catch (SocketException e) {
if (eagerHttpFallback) {
initialFailure = e;
} else {
throw e;
}
}
// if we had found an alternate mechanism, go and use it
if (fallbackSocket != null) {
// remember this successful host/factory pair
rememberFactory(host, factory);
return fallbackSocket;
}
throw initialFailure;
}
}
/**
* Remember a successful factory for connecting to host.
* Currently, excess hosts are removed from the remembered list
* using a Least Recently Created strategy.
*/
void rememberFactory(String host, RMISocketFactory factory) {
synchronized (successTable) {
while (hostList.size() >= MaxRememberedHosts) {
successTable.remove(hostList.elementAt(0));
hostList.removeElementAt(0);
}
hostList.addElement(host);
successTable.put(host, factory);
}
}
/**
* Check if an AsyncConnector succeeded. If not, return socket
* given to fall back to.
*/
Socket checkConnector(AsyncConnector connector)
throws IOException
{
Exception e = connector.getException();
if (e != null) {
e.fillInStackTrace();
/*
* The AsyncConnector implementation guaranteed that the exception
* will be either an IOException or a RuntimeException, and we can
* only throw one of those, so convince that compiler that it must
* be one of those.
*/
if (e instanceof IOException) {
throw (IOException) e;
} else if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new Error("internal error: " +
"unexpected checked exception: " + e.toString());
}
}
return connector.getSocket();
}
/**
* Create a new server socket.
*/
public ServerSocket createServerSocket(int port) throws IOException {
//return new HttpAwareServerSocket(port);
return initialFactory.createServerSocket(port);
}
/**
* AsyncConnector is used by RMIMasterSocketFactory to attempt socket
* connections on a separate thread. This allows RMIMasterSocketFactory
* to control how long it will wait for the connection to succeed.
*/
private class AsyncConnector implements Runnable {
/** what factory to use to attempt connection */
private RMISocketFactory factory;
/** the host to connect to */
private String host;
/** the port to connect to */
private int port;
/** access control context to attempt connection within */
private AccessControlContext acc;
/** exception that occurred during connection, if any */
private Exception exception = null;
/** the connected socket, if successful */
private Socket socket = null;
/** socket should be closed after created, if ever */
private boolean cleanUp = false;
/**
* Create a new asynchronous connector object.
*/
AsyncConnector(RMISocketFactory factory, String host, int port,
AccessControlContext acc)
{
this.factory = factory;
this.host = host;
this.port = port;
this.acc = acc;
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkConnect(host, port);
}
}
/**
* Attempt socket connection in separate thread. If successful,
* notify master waiting,
*/
public void run() {
try {
/*
* Using the privileges of the thread that wants to make the
* connection is tempting, but it will fail with applets with
* the current applet security manager because the applet
* network connection policy is not captured in the permission
* framework of the access control context we have.
*
* java.security.AccessController.beginPrivileged(acc);
*/
try {
Socket temp = factory.createSocket(host, port);
synchronized (this) {
socket = temp;
notify();
}
rememberFactory(host, factory);
synchronized (this) {
if (cleanUp)
try {
socket.close();
} catch (IOException e) {
}
}
} catch (Exception e) {
/*
* Note that the only exceptions which could actually have
* occurred here are IOException or RuntimeException.
*/
synchronized (this) {
exception = e;
notify();
}
}
} finally {
/*
* See above comments for matching beginPrivileged() call that
* is also commented out.
*
* java.security.AccessController.endPrivileged();
*/
}
}
/**
* Get exception that occurred during connection attempt, if any.
* In the current implementation, this is guaranteed to be either
* an IOException or a RuntimeException.
*/
private synchronized Exception getException() {
return exception;
}
/**
* Get successful socket, if any.
*/
private synchronized Socket getSocket() {
return socket;
}
/**
* Note that this connector's socket, if ever successfully created,
* will not be used, so it should be cleaned up quickly
*/
synchronized void notUsed() {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
cleanUp = true;
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
/**
* RMISocketInfo is an interface that extensions of the java.net.Socket
* class may use to provide more information on its capabilities.
*/
public interface RMISocketInfo {
/**
* Return true if this socket can be used for more than one
* RMI call. If a socket does not implement this interface, then
* it is assumed to be reusable.
*/
public boolean isReusable();
}

View File

@@ -0,0 +1,192 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.proxy;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* The WrappedSocket class provides a general wrapper for providing an
* extended implementation of java.net.Socket that can be attached to
* a pre-existing Socket object. WrappedSocket itself provides a
* constructor for specifying alternate input or output streams to be
* returned than those of the underlying Socket.
*/
class WrappedSocket extends Socket {
/** the underlying concrete socket */
protected Socket socket;
/** the input stream to return for socket */
protected InputStream in = null;
/** the output stream to return for socket */
protected OutputStream out = null;
/**
* Layer on top of a pre-existing Socket object, and use specified
* input and output streams. This allows the creator of the
* underlying socket to peek at the beginning of the input with a
* BufferedInputStream and determine which kind of socket
* to create, without consuming the input.
* @param socket the pre-existing socket to use
* @param in the InputStream to return to users (can be null)
* @param out the OutputStream to return to users (can be null)
*/
public WrappedSocket(Socket socket, InputStream in, OutputStream out)
throws IOException
{
super((java.net.SocketImpl)null); // no underlying SocketImpl for this object
this.socket = socket;
this.in = in;
this.out = out;
}
/**
* Get the address to which the socket is connected.
*/
public InetAddress getInetAddress()
{
return socket.getInetAddress();
}
/**
* Get the local address to which the socket is bound.
*/
public InetAddress getLocalAddress() {
return AccessController.doPrivileged(
new PrivilegedAction<InetAddress>() {
@Override
public InetAddress run() {
return socket.getLocalAddress();
}
});
}
/**
* Get the remote port to which the socket is connected.
*/
public int getPort()
{
return socket.getPort();
}
/**
* Get the local port to which the socket is connected.
*/
public int getLocalPort()
{
return socket.getLocalPort();
}
/**
* Get an InputStream for this socket.
*/
public InputStream getInputStream() throws IOException
{
if (in == null)
in = socket.getInputStream();
return in;
}
/**
* Get an OutputStream for this socket.
*/
public OutputStream getOutputStream() throws IOException
{
if (out == null)
out = socket.getOutputStream();
return out;
}
/**
* Enable/disable TCP_NODELAY.
*/
public void setTcpNoDelay(boolean on) throws SocketException
{
socket.setTcpNoDelay(on);
}
/**
* Retrieve whether TCP_NODELAY is enabled.
*/
public boolean getTcpNoDelay() throws SocketException
{
return socket.getTcpNoDelay();
}
/**
* Enable/disable SO_LINGER with the specified linger time.
*/
public void setSoLinger(boolean on, int val) throws SocketException
{
socket.setSoLinger(on, val);
}
/**
* Retrive setting for SO_LINGER.
*/
public int getSoLinger() throws SocketException
{
return socket.getSoLinger();
}
/**
* Enable/disable SO_TIMEOUT with the specified timeout
*/
public synchronized void setSoTimeout(int timeout) throws SocketException
{
socket.setSoTimeout(timeout);
}
/**
* Retrive setting for SO_TIMEOUT.
*/
public synchronized int getSoTimeout() throws SocketException
{
return socket.getSoTimeout();
}
/**
* Close the socket.
*/
public synchronized void close() throws IOException
{
socket.close();
}
/**
* Return string representation of the socket.
*/
public String toString()
{
return "Wrapped" + socket.toString();
}
}

View File

@@ -0,0 +1,447 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
import java.util.*;
import java.rmi.server.LogStream;
import sun.rmi.runtime.Log;
/**
* ConnectionMultiplexer manages the transparent multiplexing of
* multiple virtual connections from one endpoint to another through
* one given real connection to that endpoint. The input and output
* streams for the the underlying real connection must be supplied.
* A callback object is also supplied to be informed of new virtual
* connections opened by the remote endpoint. After creation, the
* run() method must be called in a thread created for demultiplexing
* the connections. The openConnection() method is called to
* initiate a virtual connection from this endpoint.
*
* @author Peter Jones
*/
@SuppressWarnings("deprecation")
final class ConnectionMultiplexer {
/** "multiplex" log level */
static int logLevel = LogStream.parseLevel(getLogLevel());
private static String getLogLevel() {
return java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.rmi.transport.tcp.multiplex.logLevel"));
}
/* multiplex system log */
static final Log multiplexLog =
Log.getLog("sun.rmi.transport.tcp.multiplex",
"multiplex", ConnectionMultiplexer.logLevel);
/** multiplexing protocol operation codes */
private final static int OPEN = 0xE1;
private final static int CLOSE = 0xE2;
private final static int CLOSEACK = 0xE3;
private final static int REQUEST = 0xE4;
private final static int TRANSMIT = 0xE5;
/** object to notify for new connections from remote endpoint */
private TCPChannel channel;
/** input stream for underlying single connection */
private InputStream in;
/** output stream for underlying single connection */
private OutputStream out;
/** true if underlying connection originated from this endpoint
(used for generating unique connection IDs) */
private boolean orig;
/** layered stream for reading formatted data from underlying connection */
private DataInputStream dataIn;
/** layered stream for writing formatted data to underlying connection */
private DataOutputStream dataOut;
/** table holding currently open connection IDs and related info */
private Hashtable<Integer, MultiplexConnectionInfo> connectionTable = new Hashtable<>(7);
/** number of currently open connections */
private int numConnections = 0;
/** maximum allowed open connections */
private final static int maxConnections = 256;
/** ID of last connection opened */
private int lastID = 0x1001;
/** true if this mechanism is still alive */
private boolean alive = true;
/**
* Create a new ConnectionMultiplexer using the given underlying
* input/output stream pair. The run method must be called
* (possibly on a new thread) to handle the demultiplexing.
* @param channel object to notify when new connection is received
* @param in input stream of underlying connection
* @param out output stream of underlying connection
* @param orig true if this endpoint intiated the underlying
* connection (needs to be set differently at both ends)
*/
public ConnectionMultiplexer(
TCPChannel channel,
InputStream in,
OutputStream out,
boolean orig)
{
this.channel = channel;
this.in = in;
this.out = out;
this.orig = orig;
dataIn = new DataInputStream(in);
dataOut = new DataOutputStream(out);
}
/**
* Process multiplexing protocol received from underlying connection.
*/
public void run() throws IOException
{
try {
int op, id, length;
MultiplexConnectionInfo info;
while (true) {
// read next op code from remote endpoint
op = dataIn.readUnsignedByte();
switch (op) {
// remote endpoint initiating new connection
case OPEN:
id = dataIn.readUnsignedShort();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE, "operation OPEN " + id);
}
info = connectionTable.get(id);
if (info != null)
throw new IOException(
"OPEN: Connection ID already exists");
info = new MultiplexConnectionInfo(id);
info.in = new MultiplexInputStream(this, info, 2048);
info.out = new MultiplexOutputStream(this, info, 2048);
synchronized (connectionTable) {
connectionTable.put(id, info);
++ numConnections;
}
sun.rmi.transport.Connection conn;
conn = new TCPConnection(channel, info.in, info.out);
channel.acceptMultiplexConnection(conn);
break;
// remote endpoint closing connection
case CLOSE:
id = dataIn.readUnsignedShort();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE, "operation CLOSE " + id);
}
info = connectionTable.get(id);
if (info == null)
throw new IOException(
"CLOSE: Invalid connection ID");
info.in.disconnect();
info.out.disconnect();
if (!info.closed)
sendCloseAck(info);
synchronized (connectionTable) {
connectionTable.remove(id);
-- numConnections;
}
break;
// remote endpoint acknowledging close of connection
case CLOSEACK:
id = dataIn.readUnsignedShort();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE,
"operation CLOSEACK " + id);
}
info = connectionTable.get(id);
if (info == null)
throw new IOException(
"CLOSEACK: Invalid connection ID");
if (!info.closed)
throw new IOException(
"CLOSEACK: Connection not closed");
info.in.disconnect();
info.out.disconnect();
synchronized (connectionTable) {
connectionTable.remove(id);
-- numConnections;
}
break;
// remote endpoint declaring additional bytes receivable
case REQUEST:
id = dataIn.readUnsignedShort();
info = connectionTable.get(id);
if (info == null)
throw new IOException(
"REQUEST: Invalid connection ID");
length = dataIn.readInt();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE,
"operation REQUEST " + id + ": " + length);
}
info.out.request(length);
break;
// remote endpoint transmitting data packet
case TRANSMIT:
id = dataIn.readUnsignedShort();
info = connectionTable.get(id);
if (info == null)
throw new IOException("SEND: Invalid connection ID");
length = dataIn.readInt();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE,
"operation TRANSMIT " + id + ": " + length);
}
info.in.receive(length, dataIn);
break;
default:
throw new IOException("Invalid operation: " +
Integer.toHexString(op));
}
}
} finally {
shutDown();
}
}
/**
* Initiate a new multiplexed connection through the underlying
* connection.
*/
public synchronized TCPConnection openConnection() throws IOException
{
// generate ID that should not be already used
// If all possible 32768 IDs are used,
// this method will block searching for a new ID forever.
int id;
do {
lastID = (++ lastID) & 0x7FFF;
id = lastID;
// The orig flag (copied to the high bit of the ID) is used
// to have two distinct ranges to choose IDs from for the
// two endpoints.
if (orig)
id |= 0x8000;
} while (connectionTable.get(id) != null);
// create multiplexing streams and bookkeeping information
MultiplexConnectionInfo info = new MultiplexConnectionInfo(id);
info.in = new MultiplexInputStream(this, info, 2048);
info.out = new MultiplexOutputStream(this, info, 2048);
// add to connection table if multiplexer has not died
synchronized (connectionTable) {
if (!alive)
throw new IOException("Multiplexer connection dead");
if (numConnections >= maxConnections)
throw new IOException("Cannot exceed " + maxConnections +
" simultaneous multiplexed connections");
connectionTable.put(id, info);
++ numConnections;
}
// inform remote endpoint of new connection
synchronized (dataOut) {
try {
dataOut.writeByte(OPEN);
dataOut.writeShort(id);
dataOut.flush();
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
return new TCPConnection(channel, info.in, info.out);
}
/**
* Shut down all connections and clean up.
*/
public void shutDown()
{
// inform all associated streams
synchronized (connectionTable) {
// return if multiplexer already officially dead
if (!alive)
return;
alive = false;
Enumeration<MultiplexConnectionInfo> enum_ =
connectionTable.elements();
while (enum_.hasMoreElements()) {
MultiplexConnectionInfo info = enum_.nextElement();
info.in.disconnect();
info.out.disconnect();
}
connectionTable.clear();
numConnections = 0;
}
// close underlying connection, if possible (and not already done)
try {
in.close();
} catch (IOException e) {
}
try {
out.close();
} catch (IOException e) {
}
}
/**
* Send request for more data on connection to remote endpoint.
* @param info connection information structure
* @param len number of more bytes that can be received
*/
void sendRequest(MultiplexConnectionInfo info, int len) throws IOException
{
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(REQUEST);
dataOut.writeShort(info.id);
dataOut.writeInt(len);
dataOut.flush();
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Send packet of requested data on connection to remote endpoint.
* @param info connection information structure
* @param buf array containing bytes to send
* @param off offset of first array index of packet
* @param len number of bytes in packet to send
*/
void sendTransmit(MultiplexConnectionInfo info,
byte buf[], int off, int len) throws IOException
{
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(TRANSMIT);
dataOut.writeShort(info.id);
dataOut.writeInt(len);
dataOut.write(buf, off, len);
dataOut.flush();
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Inform remote endpoint that connection has been closed.
* @param info connection information structure
*/
void sendClose(MultiplexConnectionInfo info) throws IOException
{
info.out.disconnect();
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(CLOSE);
dataOut.writeShort(info.id);
dataOut.flush();
info.closed = true;
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Acknowledge remote endpoint's closing of connection.
* @param info connection information structure
*/
void sendCloseAck(MultiplexConnectionInfo info) throws IOException
{
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(CLOSEACK);
dataOut.writeShort(info.id);
dataOut.flush();
info.closed = true;
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Shut down connection upon finalization.
*/
protected void finalize() throws Throwable
{
super.finalize();
shutDown();
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
/**
* MultiplexConnectionInfo groups related information about a
* virtual connection managed by a ConnectionMultiplexer object.
*
* @author Peter Jones
*/
class MultiplexConnectionInfo {
/** integer that uniquely identifies this connection */
int id;
/** input stream for reading from connection */
MultiplexInputStream in = null;
/** output stream for writing to connection */
MultiplexOutputStream out = null;
/** true if this connection has been closed */
boolean closed = false;
/**
* Create information structure for given connection identifier.
* @param id connection identifier
*/
MultiplexConnectionInfo(int id)
{
this.id = id;
}
}

View File

@@ -0,0 +1,213 @@
/*
* Copyright (c) 1996, 1997, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
/**
* MultiplexInputStream manages receiving data over a connection managed
* by a ConnectionMultiplexer object. This object is responsible for
* requesting more bytes of data as space in its internal buffer becomes
* available.
*
* @author Peter Jones
*/
final class MultiplexInputStream extends InputStream {
/** object managing multiplexed connection */
private ConnectionMultiplexer manager;
/** information about the connection this is the input stream for */
private MultiplexConnectionInfo info;
/** input buffer */
private byte buffer[];
/** number of real data bytes present in buffer */
private int present = 0;
/** current position to read from in input buffer */
private int pos = 0;
/** pending number of bytes this stream has requested */
private int requested = 0;
/** true if this connection has been disconnected */
private boolean disconnected = false;
/**
* lock acquired to access shared variables:
* buffer, present, pos, requested, & disconnected
* WARNING: Any of the methods manager.send*() should not be
* invoked while this lock is held, since they could potentially
* block if the underlying connection's transport buffers are
* full, and the manager may need to acquire this lock to process
* and consume data coming over the underlying connection.
*/
private Object lock = new Object();
/** level at which more data is requested when read past */
private int waterMark;
/** data structure for holding reads of one byte */
private byte temp[] = new byte[1];
/**
* Create a new MultiplexInputStream for the given manager.
* @param manager object that manages this connection
* @param info structure for connection this stream reads from
* @param bufferLength length of input buffer
*/
MultiplexInputStream(
ConnectionMultiplexer manager,
MultiplexConnectionInfo info,
int bufferLength)
{
this.manager = manager;
this.info = info;
buffer = new byte[bufferLength];
waterMark = bufferLength / 2;
}
/**
* Read a byte from the connection.
*/
public synchronized int read() throws IOException
{
int n = read(temp, 0, 1);
if (n != 1)
return -1;
return temp[0] & 0xFF;
}
/**
* Read a subarray of bytes from connection. This method blocks for
* at least one byte, and it returns the number of bytes actually read,
* or -1 if the end of the stream was detected.
* @param b array to read bytes into
* @param off offset of beginning of bytes to read into
* @param len number of bytes to read
*/
public synchronized int read(byte b[], int off, int len) throws IOException
{
if (len <= 0)
return 0;
int moreSpace;
synchronized (lock) {
if (pos >= present)
pos = present = 0;
else if (pos >= waterMark) {
System.arraycopy(buffer, pos, buffer, 0, present - pos);
present -= pos;
pos = 0;
}
int freeSpace = buffer.length - present;
moreSpace = Math.max(freeSpace - requested, 0);
}
if (moreSpace > 0)
manager.sendRequest(info, moreSpace);
synchronized (lock) {
requested += moreSpace;
while ((pos >= present) && !disconnected) {
try {
lock.wait();
} catch (InterruptedException e) {
}
}
if (disconnected && pos >= present)
return -1;
int available = present - pos;
if (len < available) {
System.arraycopy(buffer, pos, b, off, len);
pos += len;
return len;
}
else {
System.arraycopy(buffer, pos, b, off, available);
pos = present = 0;
// could send another request here, if len > available??
return available;
}
}
}
/**
* Return the number of bytes immediately available for reading.
*/
public int available() throws IOException
{
synchronized (lock) {
return present - pos;
}
}
/**
* Close this connection.
*/
public void close() throws IOException
{
manager.sendClose(info);
}
/**
* Receive bytes transmitted from connection at remote endpoint.
* @param length number of bytes transmitted
* @param in input stream with those bytes ready to be read
*/
void receive(int length, DataInputStream in)
throws IOException
{
/* TO DO: Optimize so that data received from stream can be loaded
* directly into user's buffer if there is a pending read().
*/
synchronized (lock) {
if ((pos > 0) && ((buffer.length - present) < length)) {
System.arraycopy(buffer, pos, buffer, 0, present - pos);
present -= pos;
pos = 0;
}
if ((buffer.length - present) < length)
throw new IOException("Receive buffer overflow");
in.readFully(buffer, present, length);
present += length;
requested -= length;
lock.notifyAll();
}
}
/**
* Disconnect this stream from all connection activity.
*/
void disconnect()
{
synchronized (lock) {
disconnected = true;
lock.notifyAll();
}
}
}

View File

@@ -0,0 +1,231 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
/**
* MultiplexOutputStream manages sending data over a connection managed
* by a ConnectionMultiplexer object. Data written is buffered until the
* internal buffer is full or the flush() method is called, at which
* point it attempts to push a packet of bytes through to the remote
* endpoint. This will never push more bytes than the amount already
* requested by the remote endpoint (to prevent receive buffer from
* overflowing), so if the write() and flush() methods will block
* until their operation can complete if enough bytes cannot be
* pushed immediately.
*
* @author Peter Jones
*/
final class MultiplexOutputStream extends OutputStream {
/** object managing multiplexed connection */
private ConnectionMultiplexer manager;
/** information about the connection this is the output stream for */
private MultiplexConnectionInfo info;
/** output buffer */
private byte buffer[];
/** current position to write to in output buffer */
private int pos = 0;
/** pending number of bytes requested by remote endpoint */
private int requested = 0;
/** true if this connection has been disconnected */
private boolean disconnected = false;
/**
* lock acquired to access shared variables:
* requested & disconnected
* WARNING: Any of the methods manager.send*() should not be
* invoked while this lock is held, since they could potentially
* block if the underlying connection's transport buffers are
* full, and the manager may need to acquire this lock to process
* and consume data coming over the underlying connection.
*/
private Object lock = new Object();
/**
* Create a new MultiplexOutputStream for the given manager.
* @param manager object that manages this connection
* @param info structure for connection this stream writes to
* @param bufferLength length of output buffer
*/
MultiplexOutputStream(
ConnectionMultiplexer manager,
MultiplexConnectionInfo info,
int bufferLength)
{
this.manager = manager;
this.info = info;
buffer = new byte[bufferLength];
pos = 0;
}
/**
* Write a byte over connection.
* @param b byte of data to write
*/
public synchronized void write(int b) throws IOException
{
while (pos >= buffer.length)
push();
buffer[pos ++] = (byte) b;
}
/**
* Write a subarray of bytes over connection.
* @param b array containing bytes to write
* @param off offset of beginning of bytes to write
* @param len number of bytes to write
*/
public synchronized void write(byte b[], int off, int len)
throws IOException
{
if (len <= 0)
return;
// if enough free space in output buffer, just copy into there
int freeSpace = buffer.length - pos;
if (len <= freeSpace) {
System.arraycopy(b, off, buffer, pos, len);
pos += len;
return;
}
// else, flush buffer and send rest directly to avoid array copy
flush();
int local_requested;
while (true) {
synchronized (lock) {
while ((local_requested = requested) < 1 && !disconnected) {
try {
lock.wait();
} catch (InterruptedException e) {
}
}
if (disconnected)
throw new IOException("Connection closed");
}
if (local_requested < len) {
manager.sendTransmit(info, b, off, local_requested);
off += local_requested;
len -= local_requested;
synchronized (lock) {
requested -= local_requested;
}
}
else {
manager.sendTransmit(info, b, off, len);
synchronized (lock) {
requested -= len;
}
// len = 0;
break;
}
}
}
/**
* Guarantee that all data written to this stream has been pushed
* over and made available to the remote endpoint.
*/
public synchronized void flush() throws IOException {
while (pos > 0)
push();
}
/**
* Close this connection.
*/
public void close() throws IOException
{
manager.sendClose(info);
}
/**
* Take note of more bytes requested by connection at remote endpoint.
* @param num number of additional bytes requested
*/
void request(int num)
{
synchronized (lock) {
requested += num;
lock.notifyAll();
}
}
/**
* Disconnect this stream from all connection activity.
*/
void disconnect()
{
synchronized (lock) {
disconnected = true;
lock.notifyAll();
}
}
/**
* Push bytes in output buffer to connection at remote endpoint.
* This method blocks until at least one byte has been pushed across.
*/
private void push() throws IOException
{
int local_requested;
synchronized (lock) {
while ((local_requested = requested) < 1 && !disconnected) {
try {
lock.wait();
} catch (InterruptedException e) {
}
}
if (disconnected)
throw new IOException("Connection closed");
}
if (local_requested < pos) {
manager.sendTransmit(info, buffer, 0, local_requested);
System.arraycopy(buffer, local_requested,
buffer, 0, pos - local_requested);
pos -= local_requested;
synchronized (lock) {
requested -= local_requested;
}
}
else {
manager.sendTransmit(info, buffer, 0, pos);
synchronized (lock) {
requested -= pos;
}
pos = 0;
}
}
}

View File

@@ -0,0 +1,540 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.net.Socket;
import java.rmi.ConnectIOException;
import java.rmi.RemoteException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.WeakHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.runtime.RuntimeUtil;
import sun.rmi.transport.Channel;
import sun.rmi.transport.Connection;
import sun.rmi.transport.Endpoint;
import sun.rmi.transport.TransportConstants;
import sun.security.action.GetIntegerAction;
import sun.security.action.GetLongAction;
/**
* TCPChannel is the socket-based implementation of the RMI Channel
* abstraction.
*
* @author Ann Wollrath
*/
public class TCPChannel implements Channel {
/** endpoint for this channel */
private final TCPEndpoint ep;
/** transport for this channel */
private final TCPTransport tr;
/** list of cached connections */
private final List<TCPConnection> freeList =
new ArrayList<>();
/** frees cached connections that have expired (guarded by freeList) */
private Future<?> reaper = null;
/** using multiplexer (for bi-directional applet communication */
private boolean usingMultiplexer = false;
/** connection multiplexer, if used */
private ConnectionMultiplexer multiplexer = null;
/** connection acceptor (should be in TCPTransport) */
private ConnectionAcceptor acceptor;
/** most recently authorized AccessControlContext */
private AccessControlContext okContext;
/** cache of authorized AccessControlContexts */
private WeakHashMap<AccessControlContext,
Reference<AccessControlContext>> authcache;
/** the SecurityManager which authorized okContext and authcache */
private SecurityManager cacheSecurityManager = null;
/** client-side connection idle usage timeout */
private static final long idleTimeout = // default 15 seconds
AccessController.doPrivileged(
new GetLongAction("sun.rmi.transport.connectionTimeout", 15000));
/** client-side connection handshake read timeout */
private static final int handshakeTimeout = // default 1 minute
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.handshakeTimeout",
60000));
/** client-side connection response read timeout (after handshake) */
private static final int responseTimeout = // default infinity
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.responseTimeout", 0));
/** thread pool for scheduling delayed tasks */
private static final ScheduledExecutorService scheduler =
AccessController.doPrivileged(
new RuntimeUtil.GetInstanceAction()).getScheduler();
/**
* Create channel for endpoint.
*/
TCPChannel(TCPTransport tr, TCPEndpoint ep) {
this.tr = tr;
this.ep = ep;
}
/**
* Return the endpoint for this channel.
*/
public Endpoint getEndpoint() {
return ep;
}
/**
* Checks if the current caller has sufficient privilege to make
* a connection to the remote endpoint.
* @exception SecurityException if caller is not allowed to use this
* Channel.
*/
private void checkConnectPermission() throws SecurityException {
SecurityManager security = System.getSecurityManager();
if (security == null)
return;
if (security != cacheSecurityManager) {
// The security manager changed: flush the cache
okContext = null;
authcache = new WeakHashMap<AccessControlContext,
Reference<AccessControlContext>>();
cacheSecurityManager = security;
}
AccessControlContext ctx = AccessController.getContext();
// If ctx is the same context as last time, or if it
// appears in the cache, bypass the checkConnect.
if (okContext == null ||
!(okContext.equals(ctx) || authcache.containsKey(ctx)))
{
security.checkConnect(ep.getHost(), ep.getPort());
authcache.put(ctx, new SoftReference<AccessControlContext>(ctx));
// A WeakHashMap is transformed into a SoftHashSet by making
// each value softly refer to its own key (Peter's idea).
}
okContext = ctx;
}
/**
* Supplies a connection to the endpoint of the address space
* for which this is a channel. The returned connection may
* be one retrieved from a cache of idle connections.
*/
public Connection newConnection() throws RemoteException {
TCPConnection conn;
// loop until we find a free live connection (in which case
// we return) or until we run out of freelist (in which case
// the loop exits)
do {
conn = null;
// try to get a free connection
synchronized (freeList) {
int elementPos = freeList.size()-1;
if (elementPos >= 0) {
// If there is a security manager, make sure
// the caller is allowed to connect to the
// requested endpoint.
checkConnectPermission();
conn = freeList.get(elementPos);
freeList.remove(elementPos);
}
}
// at this point, conn is null iff the freelist is empty,
// and nonnull if a free connection of uncertain vitality
// has been found.
if (conn != null) {
// check to see if the connection has closed since last use
if (!conn.isDead()) {
TCPTransport.tcpLog.log(Log.BRIEF, "reuse connection");
return conn;
}
// conn is dead, and cannot be reused (reuse => false)
this.free(conn, false);
}
} while (conn != null);
// none free, so create a new connection
return (createConnection());
}
/**
* Create a new connection to the remote endpoint of this channel.
* The returned connection is new. The caller must already have
* passed a security checkConnect or equivalent.
*/
private Connection createConnection() throws RemoteException {
Connection conn;
TCPTransport.tcpLog.log(Log.BRIEF, "create connection");
if (!usingMultiplexer) {
Socket sock = ep.newSocket();
conn = new TCPConnection(this, sock);
try {
DataOutputStream out =
new DataOutputStream(conn.getOutputStream());
writeTransportHeader(out);
// choose protocol (single op if not reusable socket)
if (!conn.isReusable()) {
out.writeByte(TransportConstants.SingleOpProtocol);
} else {
out.writeByte(TransportConstants.StreamProtocol);
out.flush();
/*
* Set socket read timeout to configured value for JRMP
* connection handshake; this also serves to guard against
* non-JRMP servers that do not respond (see 4322806).
*/
int originalSoTimeout = 0;
try {
originalSoTimeout = sock.getSoTimeout();
sock.setSoTimeout(handshakeTimeout);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
DataInputStream in =
new DataInputStream(conn.getInputStream());
byte ack = in.readByte();
if (ack != TransportConstants.ProtocolAck) {
throw new ConnectIOException(
ack == TransportConstants.ProtocolNack ?
"JRMP StreamProtocol not supported by server" :
"non-JRMP server at remote endpoint");
}
String suggestedHost = in.readUTF();
int suggestedPort = in.readInt();
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"server suggested " + suggestedHost + ":" +
suggestedPort);
}
// set local host name, if unknown
TCPEndpoint.setLocalHost(suggestedHost);
// do NOT set the default port, because we don't
// know if we can't listen YET...
// write out default endpoint to match protocol
// (but it serves no purpose)
TCPEndpoint localEp =
TCPEndpoint.getLocalEndpoint(0, null, null);
out.writeUTF(localEp.getHost());
out.writeInt(localEp.getPort());
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE, "using " +
localEp.getHost() + ":" + localEp.getPort());
}
/*
* After JRMP handshake, set socket read timeout to value
* configured for the rest of the lifetime of the
* connection. NOTE: this timeout, if configured to a
* finite duration, places an upper bound on the time
* that a remote method call is permitted to execute.
*/
try {
/*
* If socket factory had set a non-zero timeout on its
* own, then restore it instead of using the property-
* configured value.
*/
sock.setSoTimeout((originalSoTimeout != 0 ?
originalSoTimeout :
responseTimeout));
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
out.flush();
}
} catch (IOException e) {
try {
conn.close();
} catch (Exception ex) {}
if (e instanceof RemoteException) {
throw (RemoteException) e;
} else {
throw new ConnectIOException(
"error during JRMP connection establishment", e);
}
}
} else {
try {
conn = multiplexer.openConnection();
} catch (IOException e) {
synchronized (this) {
usingMultiplexer = false;
multiplexer = null;
}
throw new ConnectIOException(
"error opening virtual connection " +
"over multiplexed connection", e);
}
}
return conn;
}
/**
* Free the connection generated by this channel.
* @param conn The connection
* @param reuse If true, the connection is in a state in which it
* can be reused for another method call.
*/
public void free(Connection conn, boolean reuse) {
if (conn == null) return;
if (reuse && conn.isReusable()) {
long lastuse = System.currentTimeMillis();
TCPConnection tcpConnection = (TCPConnection) conn;
TCPTransport.tcpLog.log(Log.BRIEF, "reuse connection");
/*
* Cache connection; if reaper task for expired
* connections isn't scheduled, then schedule it.
*/
synchronized (freeList) {
freeList.add(tcpConnection);
if (reaper == null) {
TCPTransport.tcpLog.log(Log.BRIEF, "create reaper");
reaper = scheduler.scheduleWithFixedDelay(
new Runnable() {
public void run() {
TCPTransport.tcpLog.log(Log.VERBOSE,
"wake up");
freeCachedConnections();
}
}, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS);
}
}
tcpConnection.setLastUseTime(lastuse);
tcpConnection.setExpiration(lastuse + idleTimeout);
} else {
TCPTransport.tcpLog.log(Log.BRIEF, "close connection");
try {
conn.close();
} catch (IOException ignored) {
}
}
}
/**
* Send transport header over stream.
*/
private void writeTransportHeader(DataOutputStream out)
throws RemoteException
{
try {
// write out transport header
DataOutputStream dataOut =
new DataOutputStream(out);
dataOut.writeInt(TransportConstants.Magic);
dataOut.writeShort(TransportConstants.Version);
} catch (IOException e) {
throw new ConnectIOException(
"error writing JRMP transport header", e);
}
}
/**
* Use given connection multiplexer object to obtain new connections
* through this channel.
*/
synchronized void useMultiplexer(ConnectionMultiplexer newMultiplexer) {
// for now, always just use the last one given
multiplexer = newMultiplexer;
usingMultiplexer = true;
}
/**
* Accept a connection provided over a multiplexed channel.
*/
void acceptMultiplexConnection(Connection conn) {
if (acceptor == null) {
acceptor = new ConnectionAcceptor(tr);
acceptor.startNewAcceptor();
}
acceptor.accept(conn);
}
/**
* Closes all the connections in the cache, whether timed out or not.
*/
public void shedCache() {
// Build a list of connections, to avoid holding the freeList
// lock during (potentially long-running) close() calls.
Connection[] conn;
synchronized (freeList) {
conn = freeList.toArray(new Connection[freeList.size()]);
freeList.clear();
}
// Close all the connections that were free
for (int i = conn.length; --i >= 0; ) {
Connection c = conn[i];
conn[i] = null; // help gc
try {
c.close();
} catch (java.io.IOException e) {
// eat exception
}
}
}
private void freeCachedConnections() {
/*
* Remove each connection whose time out has expired.
*/
synchronized (freeList) {
int size = freeList.size();
if (size > 0) {
long time = System.currentTimeMillis();
ListIterator<TCPConnection> iter = freeList.listIterator(size);
while (iter.hasPrevious()) {
TCPConnection conn = iter.previous();
if (conn.expired(time)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"connection timeout expired");
try {
conn.close();
} catch (java.io.IOException e) {
// eat exception
}
iter.remove();
}
}
}
if (freeList.isEmpty()) {
reaper.cancel(false);
reaper = null;
}
}
}
}
/**
* ConnectionAcceptor manages accepting new connections and giving them
* to TCPTransport's message handler on new threads.
*
* Since this object only needs to know which transport to give new
* connections to, it doesn't need to be per-channel as currently
* implemented.
*/
class ConnectionAcceptor implements Runnable {
/** transport that will handle message on accepted connections */
private TCPTransport transport;
/** queue of connections to be accepted */
private List<Connection> queue = new ArrayList<>();
/** thread ID counter */
private static int threadNum = 0;
/**
* Create a new ConnectionAcceptor that will give connections
* to the specified transport on a new thread.
*/
public ConnectionAcceptor(TCPTransport transport) {
this.transport = transport;
}
/**
* Start a new thread to accept connections.
*/
public void startNewAcceptor() {
Thread t = AccessController.doPrivileged(
new NewThreadAction(ConnectionAcceptor.this,
"Multiplex Accept-" + ++ threadNum,
true));
t.start();
}
/**
* Add connection to queue of connections to be accepted.
*/
public void accept(Connection conn) {
synchronized (queue) {
queue.add(conn);
queue.notify();
}
}
/**
* Give transport next accepted connection, when available.
*/
public void run() {
Connection conn;
synchronized (queue) {
while (queue.size() == 0) {
try {
queue.wait();
} catch (InterruptedException e) {
}
}
startNewAcceptor();
conn = queue.remove(0);
}
transport.handleMessages(conn, true);
}
}

View File

@@ -0,0 +1,236 @@
/*
* Copyright (c) 1996, 2001, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.rmi.*;
import java.rmi.server.RMISocketFactory;
import sun.rmi.runtime.Log;
import sun.rmi.transport.*;
import sun.rmi.transport.proxy.*;
public class TCPConnection implements Connection {
private Socket socket;
private Channel channel;
private InputStream in = null;
private OutputStream out = null;
private long expiration = Long.MAX_VALUE;
private long lastuse = Long.MIN_VALUE;
private long roundtrip = 5; // round-trip time for ping
/**
* Constructor used for creating a connection to accept call
* (an input connection)
*/
TCPConnection(TCPChannel ch, Socket s, InputStream in, OutputStream out)
{
socket = s;
channel = ch;
this.in = in;
this.out = out;
}
/**
* Constructor used by subclass when underlying input and output streams
* are already available.
*/
TCPConnection(TCPChannel ch, InputStream in, OutputStream out)
{
this(ch, null, in, out);
}
/**
* Constructor used when socket is available, but not underlying
* streams.
*/
TCPConnection(TCPChannel ch, Socket s)
{
this(ch, s, null, null);
}
/**
* Gets the output stream for this connection
*/
public OutputStream getOutputStream() throws IOException
{
if (out == null)
out = new BufferedOutputStream(socket.getOutputStream());
return out;
}
/**
* Release the output stream for this connection.
*/
public void releaseOutputStream() throws IOException
{
if (out != null)
out.flush();
}
/**
* Gets the input stream for this connection.
*/
public InputStream getInputStream() throws IOException
{
if (in == null)
in = new BufferedInputStream(socket.getInputStream());
return in;
}
/**
* Release the input stream for this connection.
*/
public void releaseInputStream()
{
}
/**
* Determine if this connection can be used for multiple operations.
* If the socket implements RMISocketInfo, then we can query it about
* this; otherwise, assume that it does provide a full-duplex
* persistent connection like java.net.Socket.
*/
public boolean isReusable()
{
if ((socket != null) && (socket instanceof RMISocketInfo))
return ((RMISocketInfo) socket).isReusable();
else
return true;
}
/**
* Set the expiration time of this connection.
* @param time The time at which the time out expires.
*/
void setExpiration(long time)
{
expiration = time;
}
/**
* Set the timestamp at which this connection was last used successfully.
* The connection will be pinged for liveness if reused long after
* this time.
* @param time The time at which the connection was last active.
*/
void setLastUseTime(long time)
{
lastuse = time;
}
/**
* Returns true if the timeout has expired on this connection;
* otherwise returns false.
* @param time The current time.
*/
boolean expired(long time)
{
return expiration <= time;
}
/**
* Probes the connection to see if it still alive and connected to
* a responsive server. If the connection has been idle for too
* long, the server is pinged. ``Too long'' means ``longer than the
* last ping round-trip time''.
* <P>
* This method may misdiagnose a dead connection as live, but it
* will never misdiagnose a live connection as dead.
* @return true if the connection and server are recently alive
*/
public boolean isDead()
{
InputStream i;
OutputStream o;
// skip ping if recently used within 1 RTT
long start = System.currentTimeMillis();
if ((roundtrip > 0) && (start < lastuse + roundtrip))
return (false); // still alive and warm
// Get the streams
try {
i = getInputStream();
o = getOutputStream();
} catch (IOException e) {
return (true); // can't even get a stream, must be very dead
}
// Write the ping byte and read the reply byte
int response = 0;
try {
o.write(TransportConstants.Ping);
o.flush();
response = i.read();
} catch (IOException ex) {
TCPTransport.tcpLog.log(Log.VERBOSE, "exception: ", ex);
TCPTransport.tcpLog.log(Log.BRIEF, "server ping failed");
return (true); // server failed the ping test
}
if (response == TransportConstants.PingAck) {
// save most recent RTT for future use
roundtrip = (System.currentTimeMillis() - start) * 2;
// clock-correction may make roundtrip < 0; doesn't matter
return (false); // it's alive and 5-by-5
}
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
(response == -1 ? "server has been deactivated" :
"server protocol error: ping response = " + response));
}
return (true);
}
/**
* Close the connection. */
public void close() throws IOException
{
TCPTransport.tcpLog.log(Log.BRIEF, "close connection");
if (socket != null)
socket.close();
else {
in.close();
out.close();
}
}
/**
* Returns the channel for this connection.
*/
public Channel getChannel()
{
return channel;
}
}

View File

@@ -0,0 +1,790 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Proxy;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.ConnectIOException;
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.RMISocketFactory;
import java.security.AccessController;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.transport.Channel;
import sun.rmi.transport.Endpoint;
import sun.rmi.transport.Target;
import sun.rmi.transport.Transport;
import sun.security.action.GetBooleanAction;
import sun.security.action.GetIntegerAction;
import sun.security.action.GetPropertyAction;
/**
* TCPEndpoint represents some communication endpoint for an address
* space (VM).
*
* @author Ann Wollrath
*/
public class TCPEndpoint implements Endpoint {
/** IP address or host name */
private String host;
/** port number */
private int port;
/** custom client socket factory (null if not custom factory) */
private final RMIClientSocketFactory csf;
/** custom server socket factory (null if not custom factory) */
private final RMIServerSocketFactory ssf;
/** if local, the port number to listen on */
private int listenPort = -1;
/** if local, the transport object associated with this endpoint */
private TCPTransport transport = null;
/** the local host name */
private static String localHost;
/** true if real local host name is known yet */
private static boolean localHostKnown;
// this should be a *private* method since it is privileged
private static int getInt(String name, int def) {
return AccessController.doPrivileged(new GetIntegerAction(name, def));
}
// this should be a *private* method since it is privileged
private static boolean getBoolean(String name) {
return AccessController.doPrivileged(new GetBooleanAction(name));
}
/**
* Returns the value of the java.rmi.server.hostname property.
*/
private static String getHostnameProperty() {
return AccessController.doPrivileged(
new GetPropertyAction("java.rmi.server.hostname"));
}
/**
* Find host name of local machine. Property "java.rmi.server.hostname"
* is used if set, so server administrator can compensate for the possible
* inablility to get fully qualified host name from VM.
*/
static {
localHostKnown = true;
localHost = getHostnameProperty();
// could try querying CGI program here?
if (localHost == null) {
try {
InetAddress localAddr = InetAddress.getLocalHost();
byte[] raw = localAddr.getAddress();
if ((raw[0] == 127) &&
(raw[1] == 0) &&
(raw[2] == 0) &&
(raw[3] == 1)) {
localHostKnown = false;
}
/* if the user wishes to use a fully qualified domain
* name then attempt to find one.
*/
if (getBoolean("java.rmi.server.useLocalHostName")) {
localHost = FQDN.attemptFQDN(localAddr);
} else {
/* default to using ip addresses, names will
* work across seperate domains.
*/
localHost = localAddr.getHostAddress();
}
} catch (Exception e) {
localHostKnown = false;
localHost = null;
}
}
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"localHostKnown = " + localHostKnown +
", localHost = " + localHost);
}
}
/** maps an endpoint key containing custom socket factories to
* their own unique endpoint */
// TBD: should this be a weak hash table?
private static final
Map<TCPEndpoint,LinkedList<TCPEndpoint>> localEndpoints =
new HashMap<>();
/**
* Create an endpoint for a specified host and port.
* This should not be used by external classes to create endpoints
* for servers in this VM; use getLocalEndpoint instead.
*/
public TCPEndpoint(String host, int port) {
this(host, port, null, null);
}
/**
* Create a custom socket factory endpoint for a specified host and port.
* This should not be used by external classes to create endpoints
* for servers in this VM; use getLocalEndpoint instead.
*/
public TCPEndpoint(String host, int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
if (host == null)
host = "";
this.host = host;
this.port = port;
this.csf = csf;
this.ssf = ssf;
}
/**
* Get an endpoint for the local address space on specified port.
* If port number is 0, it returns shared default endpoint object
* whose host name and port may or may not have been determined.
*/
public static TCPEndpoint getLocalEndpoint(int port) {
return getLocalEndpoint(port, null, null);
}
public static TCPEndpoint getLocalEndpoint(int port,
RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
/*
* Find mapping for an endpoint key to the list of local unique
* endpoints for this client/server socket factory pair (perhaps
* null) for the specific port.
*/
TCPEndpoint ep = null;
synchronized (localEndpoints) {
TCPEndpoint endpointKey = new TCPEndpoint(null, port, csf, ssf);
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
String localHost = resampleLocalHost();
if (epList == null) {
/*
* Create new endpoint list.
*/
ep = new TCPEndpoint(localHost, port, csf, ssf);
epList = new LinkedList<TCPEndpoint>();
epList.add(ep);
ep.listenPort = port;
ep.transport = new TCPTransport(epList);
localEndpoints.put(endpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"created local endpoint for socket factory " + ssf +
" on port " + port);
}
} else {
synchronized (epList) {
ep = epList.getLast();
String lastHost = ep.host;
int lastPort = ep.port;
TCPTransport lastTransport = ep.transport;
// assert (localHost == null ^ lastHost != null)
if (localHost != null && !localHost.equals(lastHost)) {
/*
* Hostname has been updated; add updated endpoint
* to list.
*/
if (lastPort != 0) {
/*
* Remove outdated endpoints only if the
* port has already been set on those endpoints.
*/
epList.clear();
}
ep = new TCPEndpoint(localHost, lastPort, csf, ssf);
ep.listenPort = port;
ep.transport = lastTransport;
epList.add(ep);
}
}
}
}
return ep;
}
/**
* Resamples the local hostname and returns the possibly-updated
* local hostname.
*/
private static String resampleLocalHost() {
String hostnameProperty = getHostnameProperty();
synchronized (localEndpoints) {
// assert(localHostKnown ^ (localHost == null))
if (hostnameProperty != null) {
if (!localHostKnown) {
/*
* If the local hostname is unknown, update ALL
* existing endpoints with the new hostname.
*/
setLocalHost(hostnameProperty);
} else if (!hostnameProperty.equals(localHost)) {
/*
* Only update the localHost field for reference
* in future endpoint creation.
*/
localHost = hostnameProperty;
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"updated local hostname to: " + localHost);
}
}
}
return localHost;
}
}
/**
* Set the local host name, if currently unknown.
*/
static void setLocalHost(String host) {
// assert (host != null)
synchronized (localEndpoints) {
/*
* If host is not known, change the host field of ALL
* the local endpoints.
*/
if (!localHostKnown) {
localHost = host;
localHostKnown = true;
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"local host set to " + host);
}
for (LinkedList<TCPEndpoint> epList : localEndpoints.values())
{
synchronized (epList) {
for (TCPEndpoint ep : epList) {
ep.host = host;
}
}
}
}
}
}
/**
* Set the port of the (shared) default endpoint object.
* When first created, it contains port 0 because the transport
* hasn't tried to listen to get assigned a port, or if listening
* failed, a port hasn't been assigned from the server.
*/
static void setDefaultPort(int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
TCPEndpoint endpointKey = new TCPEndpoint(null, 0, csf, ssf);
synchronized (localEndpoints) {
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
synchronized (epList) {
int size = epList.size();
TCPEndpoint lastEp = epList.getLast();
for (TCPEndpoint ep : epList) {
ep.port = port;
}
if (size > 1) {
/*
* Remove all but the last element of the list
* (which contains the most recent hostname).
*/
epList.clear();
epList.add(lastEp);
}
}
/*
* Allow future exports to use the actual bound port
* explicitly (see 6269166).
*/
TCPEndpoint newEndpointKey = new TCPEndpoint(null, port, csf, ssf);
localEndpoints.put(newEndpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"default port for server socket factory " + ssf +
" and client socket factory " + csf +
" set to " + port);
}
}
}
/**
* Returns transport for making connections to remote endpoints;
* (here, the default transport at port 0 is used).
*/
public Transport getOutboundTransport() {
TCPEndpoint localEndpoint = getLocalEndpoint(0, null, null);
return localEndpoint.transport;
}
/**
* Returns the current list of known transports.
* The returned list is an unshared collection of Transports,
* including all transports which may have channels to remote
* endpoints.
*/
private static Collection<TCPTransport> allKnownTransports() {
// Loop through local endpoints, getting the transport of each one.
Set<TCPTransport> s;
synchronized (localEndpoints) {
// presize s to number of localEndpoints
s = new HashSet<TCPTransport>(localEndpoints.size());
for (LinkedList<TCPEndpoint> epList : localEndpoints.values()) {
/*
* Each local endpoint has its transport added to s.
* Note: the transport is the same for all endpoints
* in the list, so it is okay to pick any one of them.
*/
TCPEndpoint ep = epList.getFirst();
s.add(ep.transport);
}
}
return s;
}
/**
* Release idle outbound connections to reduce demand on I/O resources.
* All transports are asked to release excess connections.
*/
public static void shedConnectionCaches() {
for (TCPTransport transport : allKnownTransports()) {
transport.shedConnectionCaches();
}
}
/**
* Export the object to accept incoming calls.
*/
public void exportObject(Target target) throws RemoteException {
transport.exportObject(target);
}
/**
* Returns a channel for this (remote) endpoint.
*/
public Channel getChannel() {
return getOutboundTransport().getChannel(this);
}
/**
* Returns address for endpoint
*/
public String getHost() {
return host;
}
/**
* Returns the port for this endpoint. If this endpoint was
* created as a server endpoint (using getLocalEndpoint) for a
* default/anonymous port and its inbound transport has started
* listening, this method returns (instead of zero) the actual
* bound port suitable for passing to clients.
**/
public int getPort() {
return port;
}
/**
* Returns the port that this endpoint's inbound transport listens
* on, if this endpoint was created as a server endpoint (using
* getLocalEndpoint). If this endpoint was created for the
* default/anonymous port, then this method returns zero even if
* the transport has started listening.
**/
public int getListenPort() {
return listenPort;
}
/**
* Returns the transport for incoming connections to this
* endpoint, if this endpoint was created as a server endpoint
* (using getLocalEndpoint).
**/
public Transport getInboundTransport() {
return transport;
}
/**
* Get the client socket factory associated with this endpoint.
*/
public RMIClientSocketFactory getClientSocketFactory() {
return csf;
}
/**
* Get the server socket factory associated with this endpoint.
*/
public RMIServerSocketFactory getServerSocketFactory() {
return ssf;
}
/**
* Return string representation for endpoint.
*/
public String toString() {
return "[" + host + ":" + port +
(ssf != null ? "," + ssf : "") +
(csf != null ? "," + csf : "") +
"]";
}
public int hashCode() {
return port;
}
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof TCPEndpoint)) {
TCPEndpoint ep = (TCPEndpoint) obj;
if (port != ep.port || !host.equals(ep.host))
return false;
if (((csf == null) ^ (ep.csf == null)) ||
((ssf == null) ^ (ep.ssf == null)))
return false;
/*
* Fix for 4254510: perform socket factory *class* equality check
* before socket factory equality check to avoid passing
* a potentially naughty socket factory to this endpoint's
* {client,server} socket factory equals method.
*/
if ((csf != null) &&
!(csf.getClass() == ep.csf.getClass() && csf.equals(ep.csf)))
return false;
if ((ssf != null) &&
!(ssf.getClass() == ep.ssf.getClass() && ssf.equals(ep.ssf)))
return false;
return true;
} else {
return false;
}
}
/* codes for the self-describing formats of wire representation */
private static final int FORMAT_HOST_PORT = 0;
private static final int FORMAT_HOST_PORT_FACTORY = 1;
/**
* Write endpoint to output stream.
*/
public void write(ObjectOutput out) throws IOException {
if (csf == null) {
out.writeByte(FORMAT_HOST_PORT);
out.writeUTF(host);
out.writeInt(port);
} else {
out.writeByte(FORMAT_HOST_PORT_FACTORY);
out.writeUTF(host);
out.writeInt(port);
out.writeObject(csf);
}
}
/**
* Get the endpoint from the input stream.
* @param in the input stream
* @exception IOException If id could not be read (due to stream failure)
*/
public static TCPEndpoint read(ObjectInput in)
throws IOException, ClassNotFoundException
{
String host;
int port;
RMIClientSocketFactory csf = null;
byte format = in.readByte();
switch (format) {
case FORMAT_HOST_PORT:
host = in.readUTF();
port = in.readInt();
break;
case FORMAT_HOST_PORT_FACTORY:
host = in.readUTF();
port = in.readInt();
csf = (RMIClientSocketFactory) in.readObject();
if (csf != null && Proxy.isProxyClass(csf.getClass())) {
throw new IOException("Invalid SocketFactory");
}
break;
default:
throw new IOException("invalid endpoint format");
}
return new TCPEndpoint(host, port, csf, null);
}
/**
* Write endpoint to output stream in older format used by
* UnicastRef for JDK1.1 compatibility.
*/
public void writeHostPortFormat(DataOutput out) throws IOException {
if (csf != null) {
throw new InternalError("TCPEndpoint.writeHostPortFormat: " +
"called for endpoint with non-null socket factory");
}
out.writeUTF(host);
out.writeInt(port);
}
/**
* Create a new endpoint from input stream data.
* @param in the input stream
*/
public static TCPEndpoint readHostPortFormat(DataInput in)
throws IOException
{
String host = in.readUTF();
int port = in.readInt();
return new TCPEndpoint(host, port);
}
private static RMISocketFactory chooseFactory() {
RMISocketFactory sf = RMISocketFactory.getSocketFactory();
if (sf == null) {
sf = TCPTransport.defaultSocketFactory;
}
return sf;
}
/**
* Open and return new client socket connection to endpoint.
*/
Socket newSocket() throws RemoteException {
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"opening socket to " + this);
}
Socket socket;
try {
RMIClientSocketFactory clientFactory = csf;
if (clientFactory == null) {
clientFactory = chooseFactory();
}
socket = clientFactory.createSocket(host, port);
} catch (java.net.UnknownHostException e) {
throw new java.rmi.UnknownHostException(
"Unknown host: " + host, e);
} catch (java.net.ConnectException e) {
throw new java.rmi.ConnectException(
"Connection refused to host: " + host, e);
} catch (IOException e) {
// We might have simply run out of file descriptors
try {
TCPEndpoint.shedConnectionCaches();
// REMIND: should we retry createSocket?
} catch (OutOfMemoryError | Exception mem) {
// don't quit if out of memory
// or shed fails non-catastrophically
}
throw new ConnectIOException("Exception creating connection to: " +
host, e);
}
// set socket to disable Nagle's algorithm (always send immediately)
// TBD: should this be left up to socket factory instead?
try {
socket.setTcpNoDelay(true);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
// fix 4187495: explicitly set SO_KEEPALIVE to prevent client hangs
try {
socket.setKeepAlive(true);
} catch (Exception e) {
// ignore and proceed
}
return socket;
}
/**
* Return new server socket to listen for connections on this endpoint.
*/
ServerSocket newServerSocket() throws IOException {
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"creating server socket on " + this);
}
RMIServerSocketFactory serverFactory = ssf;
if (serverFactory == null) {
serverFactory = chooseFactory();
}
ServerSocket server = serverFactory.createServerSocket(listenPort);
// if we listened on an anonymous port, set the default port
// (for this socket factory)
if (listenPort == 0)
setDefaultPort(server.getLocalPort(), csf, ssf);
return server;
}
/**
* The class FQDN encapsulates a routine that makes a best effort
* attempt to retrieve the fully qualified domain name of the local
* host.
*
* @author Laird Dornin
*/
private static class FQDN implements Runnable {
/**
* strings in which we can store discovered fqdn
*/
private String reverseLookup;
private String hostAddress;
private FQDN(String hostAddress) {
this.hostAddress = hostAddress;
}
/**
* Do our best to obtain a fully qualified hostname for the local
* host. Perform the following steps to get a localhostname:
*
* 1. InetAddress.getLocalHost().getHostName() - if contains
* '.' use as FQDN
* 2. if no '.' query name service for FQDN in a thread
* Note: We query the name service for an FQDN by creating
* an InetAddress via a stringified copy of the local ip
* address; this creates an InetAddress with a null hostname.
* Asking for the hostname of this InetAddress causes a name
* service lookup.
*
* 3. if name service takes too long to return, use ip address
* 4. if name service returns but response contains no '.'
* default to ipaddress.
*/
static String attemptFQDN(InetAddress localAddr)
throws java.net.UnknownHostException
{
String hostName = localAddr.getHostName();
if (hostName.indexOf('.') < 0 ) {
String hostAddress = localAddr.getHostAddress();
FQDN f = new FQDN(hostAddress);
int nameServiceTimeOut =
TCPEndpoint.getInt("sun.rmi.transport.tcp.localHostNameTimeOut",
10000);
try {
synchronized(f) {
f.getFQDN();
/* wait to obtain an FQDN */
f.wait(nameServiceTimeOut);
}
} catch (InterruptedException e) {
/* propagate the exception to the caller */
Thread.currentThread().interrupt();
}
hostName = f.getHost();
if ((hostName == null) || (hostName.equals(""))
|| (hostName.indexOf('.') < 0 )) {
hostName = hostAddress;
}
}
return hostName;
}
/**
* Method that that will start a thread to wait to retrieve a
* fully qualified domain name from a name service. The spawned
* thread may never return but we have marked it as a daemon so the vm
* will terminate appropriately.
*/
private void getFQDN() {
/* FQDN finder will run in RMI threadgroup. */
Thread t = AccessController.doPrivileged(
new NewThreadAction(FQDN.this, "FQDN Finder", true));
t.start();
}
private synchronized String getHost() {
return reverseLookup;
}
/**
* thread to query a name service for the fqdn of this host.
*/
public void run() {
String name = null;
try {
name = InetAddress.getByName(hostAddress).getHostName();
} catch (java.net.UnknownHostException e) {
} finally {
synchronized(this) {
reverseLookup = name;
this.notify();
}
}
}
}
}

View File

@@ -0,0 +1,893 @@
/*
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.rmi.server.LogStream;
import java.rmi.server.RMIFailureHandler;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.RemoteCall;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.UID;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.transport.Channel;
import sun.rmi.transport.Connection;
import sun.rmi.transport.DGCAckHandler;
import sun.rmi.transport.Endpoint;
import sun.rmi.transport.StreamRemoteCall;
import sun.rmi.transport.Target;
import sun.rmi.transport.Transport;
import sun.rmi.transport.TransportConstants;
import sun.rmi.transport.proxy.HttpReceiveSocket;
import sun.security.action.GetIntegerAction;
import sun.security.action.GetLongAction;
import sun.security.action.GetPropertyAction;
/**
* TCPTransport is the socket-based implementation of the RMI Transport
* abstraction.
*
* @author Ann Wollrath
* @author Peter Jones
*/
@SuppressWarnings("deprecation")
public class TCPTransport extends Transport {
/* tcp package log */
static final Log tcpLog = Log.getLog("sun.rmi.transport.tcp", "tcp",
LogStream.parseLevel(AccessController.doPrivileged(
new GetPropertyAction("sun.rmi.transport.tcp.logLevel"))));
/** maximum number of connection handler threads */
private static final int maxConnectionThreads = // default no limit
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.maxConnectionThreads",
Integer.MAX_VALUE));
/** keep alive time for idle connection handler threads */
private static final long threadKeepAliveTime = // default 1 minute
AccessController.doPrivileged(
new GetLongAction("sun.rmi.transport.tcp.threadKeepAliveTime",
60000));
/** thread pool for connection handlers */
private static final ExecutorService connectionThreadPool =
new ThreadPoolExecutor(0, maxConnectionThreads,
threadKeepAliveTime, TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>(),
new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return AccessController.doPrivileged(new NewThreadAction(
runnable, "TCP Connection(idle)", true, true));
}
});
private static final boolean disableIncomingHttp =
java.security.AccessController.doPrivileged(
new GetPropertyAction("sun.rmi.server.disableIncomingHttp", "true"))
.equalsIgnoreCase("true");
/** total connections handled */
private static final AtomicInteger connectionCount = new AtomicInteger(0);
/** client host for the current thread's connection */
private static final ThreadLocal<ConnectionHandler>
threadConnectionHandler = new ThreadLocal<>();
/** an AccessControlContext with no permissions */
private static final AccessControlContext NOPERMS_ACC;
static {
Permissions perms = new Permissions();
ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
NOPERMS_ACC = new AccessControlContext(pd);
}
/** endpoints for this transport */
private final LinkedList<TCPEndpoint> epList;
/** number of objects exported on this transport */
private int exportCount = 0;
/** server socket for this transport */
private ServerSocket server = null;
/** table mapping endpoints to channels */
private final Map<TCPEndpoint,Reference<TCPChannel>> channelTable =
new WeakHashMap<>();
static final RMISocketFactory defaultSocketFactory =
RMISocketFactory.getDefaultSocketFactory();
/** number of milliseconds in accepted-connection timeout.
* Warning: this should be greater than 15 seconds (the client-side
* timeout), and defaults to 2 hours.
* The maximum representable value is slightly more than 24 days
* and 20 hours.
*/
private static final int connectionReadTimeout = // default 2 hours
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.readTimeout",
2 * 3600 * 1000));
/**
* Constructs a TCPTransport.
*/
TCPTransport(LinkedList<TCPEndpoint> epList) {
// assert ((epList.size() != null) && (epList.size() >= 1))
this.epList = epList;
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "Version = " +
TransportConstants.Version + ", ep = " + getEndpoint());
}
}
/**
* Closes all cached connections in every channel subordinated to this
* transport. Currently, this only closes outgoing connections.
*/
public void shedConnectionCaches() {
List<TCPChannel> channels;
synchronized (channelTable) {
channels = new ArrayList<TCPChannel>(channelTable.values().size());
for (Reference<TCPChannel> ref : channelTable.values()) {
TCPChannel ch = ref.get();
if (ch != null) {
channels.add(ch);
}
}
}
for (TCPChannel channel : channels) {
channel.shedCache();
}
}
/**
* Returns a <I>Channel</I> that generates connections to the
* endpoint <I>ep</I>. A Channel is an object that creates and
* manages connections of a particular type to some particular
* address space.
* @param ep the endpoint to which connections will be generated.
* @return the channel or null if the transport cannot
* generate connections to this endpoint
*/
public TCPChannel getChannel(Endpoint ep) {
TCPChannel ch = null;
if (ep instanceof TCPEndpoint) {
synchronized (channelTable) {
Reference<TCPChannel> ref = channelTable.get(ep);
if (ref != null) {
ch = ref.get();
}
if (ch == null) {
TCPEndpoint tcpEndpoint = (TCPEndpoint) ep;
ch = new TCPChannel(this, tcpEndpoint);
channelTable.put(tcpEndpoint,
new WeakReference<TCPChannel>(ch));
}
}
}
return ch;
}
/**
* Removes the <I>Channel</I> that generates connections to the
* endpoint <I>ep</I>.
*/
public void free(Endpoint ep) {
if (ep instanceof TCPEndpoint) {
synchronized (channelTable) {
Reference<TCPChannel> ref = channelTable.remove(ep);
if (ref != null) {
TCPChannel channel = ref.get();
if (channel != null) {
channel.shedCache();
}
}
}
}
}
/**
* Export the object so that it can accept incoming calls.
*/
public void exportObject(Target target) throws RemoteException {
/*
* Ensure that a server socket is listening, and count this
* export while synchronized to prevent the server socket from
* being closed due to concurrent unexports.
*/
synchronized (this) {
listen();
exportCount++;
}
/*
* Try to add the Target to the exported object table; keep
* counting this export (to keep server socket open) only if
* that succeeds.
*/
boolean ok = false;
try {
super.exportObject(target);
ok = true;
} finally {
if (!ok) {
synchronized (this) {
decrementExportCount();
}
}
}
}
protected synchronized void targetUnexported() {
decrementExportCount();
}
/**
* Decrements the count of exported objects, closing the current
* server socket if the count reaches zero.
**/
private void decrementExportCount() {
assert Thread.holdsLock(this);
exportCount--;
if (exportCount == 0 && getEndpoint().getListenPort() != 0) {
ServerSocket ss = server;
server = null;
try {
ss.close();
} catch (IOException e) {
}
}
}
/**
* Verify that the current access control context has permission to
* accept the connection being dispatched by the current thread.
*/
protected void checkAcceptPermission(AccessControlContext acc) {
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
return;
}
ConnectionHandler h = threadConnectionHandler.get();
if (h == null) {
throw new Error(
"checkAcceptPermission not in ConnectionHandler thread");
}
h.checkAcceptPermission(sm, acc);
}
private TCPEndpoint getEndpoint() {
synchronized (epList) {
return epList.getLast();
}
}
/**
* Listen on transport's endpoint.
*/
private void listen() throws RemoteException {
assert Thread.holdsLock(this);
TCPEndpoint ep = getEndpoint();
int port = ep.getPort();
if (server == null) {
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF,
"(port " + port + ") create server socket");
}
try {
server = ep.newServerSocket();
/*
* Don't retry ServerSocket if creation fails since
* "port in use" will cause export to hang if an
* RMIFailureHandler is not installed.
*/
Thread t = AccessController.doPrivileged(
new NewThreadAction(new AcceptLoop(server),
"TCP Accept-" + port, true));
t.start();
} catch (java.net.BindException e) {
throw new ExportException("Port already in use: " + port, e);
} catch (IOException e) {
throw new ExportException("Listen failed on port: " + port, e);
}
} else {
// otherwise verify security access to existing server socket
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkListen(port);
}
}
}
/**
* Worker for accepting connections from a server socket.
**/
private class AcceptLoop implements Runnable {
private final ServerSocket serverSocket;
// state for throttling loop on exceptions (local to accept thread)
private long lastExceptionTime = 0L;
private int recentExceptionCount;
AcceptLoop(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public void run() {
try {
executeAcceptLoop();
} finally {
try {
/*
* Only one accept loop is started per server
* socket, so after no more connections will be
* accepted, ensure that the server socket is no
* longer listening.
*/
serverSocket.close();
} catch (IOException e) {
}
}
}
/**
* Accepts connections from the server socket and executes
* handlers for them in the thread pool.
**/
private void executeAcceptLoop() {
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "listening on port " +
getEndpoint().getPort());
}
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
/*
* Find client host name (or "0.0.0.0" if unknown)
*/
InetAddress clientAddr = socket.getInetAddress();
String clientHost = (clientAddr != null
? clientAddr.getHostAddress()
: "0.0.0.0");
/*
* Execute connection handler in the thread pool,
* which uses non-system threads.
*/
try {
connectionThreadPool.execute(
new ConnectionHandler(socket, clientHost));
} catch (RejectedExecutionException e) {
closeSocket(socket);
tcpLog.log(Log.BRIEF,
"rejected connection from " + clientHost);
}
} catch (Throwable t) {
try {
/*
* If the server socket has been closed, such
* as because there are no more exported
* objects, then we expect accept to throw an
* exception, so just terminate normally.
*/
if (serverSocket.isClosed()) {
break;
}
try {
if (tcpLog.isLoggable(Level.WARNING)) {
tcpLog.log(Level.WARNING,
"accept loop for " + serverSocket +
" throws", t);
}
} catch (Throwable tt) {
}
} finally {
/*
* Always close the accepted socket (if any)
* if an exception occurs, but only after
* logging an unexpected exception.
*/
if (socket != null) {
closeSocket(socket);
}
}
/*
* In case we're running out of file descriptors,
* release resources held in caches.
*/
if (!(t instanceof SecurityException)) {
try {
TCPEndpoint.shedConnectionCaches();
} catch (Throwable tt) {
}
}
/*
* A NoClassDefFoundError can occur if no file
* descriptors are available, in which case this
* loop should not terminate.
*/
if (t instanceof Exception ||
t instanceof OutOfMemoryError ||
t instanceof NoClassDefFoundError)
{
if (!continueAfterAcceptFailure(t)) {
return;
}
// continue loop
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new UndeclaredThrowableException(t);
}
}
}
}
/**
* Returns true if the accept loop should continue after the
* specified exception has been caught, or false if the accept
* loop should terminate (closing the server socket). If
* there is an RMIFailureHandler, this method returns the
* result of passing the specified exception to it; otherwise,
* this method always returns true, after sleeping to throttle
* the accept loop if necessary.
**/
private boolean continueAfterAcceptFailure(Throwable t) {
RMIFailureHandler fh = RMISocketFactory.getFailureHandler();
if (fh != null) {
return fh.failure(t instanceof Exception ? (Exception) t :
new InvocationTargetException(t));
} else {
throttleLoopOnException();
return true;
}
}
/**
* Throttles the accept loop after an exception has been
* caught: if a burst of 10 exceptions in 5 seconds occurs,
* then wait for 10 seconds to curb busy CPU usage.
**/
private void throttleLoopOnException() {
long now = System.currentTimeMillis();
if (lastExceptionTime == 0L || (now - lastExceptionTime) > 5000) {
// last exception was long ago (or this is the first)
lastExceptionTime = now;
recentExceptionCount = 0;
} else {
// exception burst window was started recently
if (++recentExceptionCount >= 10) {
try {
Thread.sleep(10000);
} catch (InterruptedException ignore) {
}
}
}
}
}
/** close socket and eat exception */
private static void closeSocket(Socket sock) {
try {
sock.close();
} catch (IOException ex) {
// eat exception
}
}
/**
* handleMessages decodes transport operations and handles messages
* appropriately. If an exception occurs during message handling,
* the socket is closed.
*/
void handleMessages(Connection conn, boolean persistent) {
int port = getEndpoint().getPort();
try {
DataInputStream in = new DataInputStream(conn.getInputStream());
do {
int op = in.read(); // transport op
if (op == -1) {
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "(port " +
port + ") connection closed");
}
break;
}
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "(port " + port +
") op = " + op);
}
switch (op) {
case TransportConstants.Call:
// service incoming RMI call
RemoteCall call = new StreamRemoteCall(conn);
if (serviceCall(call) == false)
return;
break;
case TransportConstants.Ping:
// send ack for ping
DataOutputStream out =
new DataOutputStream(conn.getOutputStream());
out.writeByte(TransportConstants.PingAck);
conn.releaseOutputStream();
break;
case TransportConstants.DGCAck:
DGCAckHandler.received(UID.read(in));
break;
default:
throw new IOException("unknown transport op " + op);
}
} while (persistent);
} catch (IOException e) {
// exception during processing causes connection to close (below)
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "(port " + port +
") exception: ", e);
}
} finally {
try {
conn.close();
} catch (IOException ex) {
// eat exception
}
}
}
/**
* Returns the client host for the current thread's connection. Throws
* ServerNotActiveException if no connection is active for this thread.
*/
public static String getClientHost() throws ServerNotActiveException {
ConnectionHandler h = threadConnectionHandler.get();
if (h != null) {
return h.getClientHost();
} else {
throw new ServerNotActiveException("not in a remote call");
}
}
/**
* Services messages on accepted connection
*/
private class ConnectionHandler implements Runnable {
/** int value of "POST" in ASCII (Java's specified data formats
* make this once-reviled tactic again socially acceptable) */
private static final int POST = 0x504f5354;
/** most recently accept-authorized AccessControlContext */
private AccessControlContext okContext;
/** cache of accept-authorized AccessControlContexts */
private Map<AccessControlContext,
Reference<AccessControlContext>> authCache;
/** security manager which authorized contexts in authCache */
private SecurityManager cacheSecurityManager = null;
private Socket socket;
private String remoteHost;
ConnectionHandler(Socket socket, String remoteHost) {
this.socket = socket;
this.remoteHost = remoteHost;
}
String getClientHost() {
return remoteHost;
}
/**
* Verify that the given AccessControlContext has permission to
* accept this connection.
*/
void checkAcceptPermission(SecurityManager sm,
AccessControlContext acc)
{
/*
* Note: no need to synchronize on cache-related fields, since this
* method only gets called from the ConnectionHandler's thread.
*/
if (sm != cacheSecurityManager) {
okContext = null;
authCache = new WeakHashMap<AccessControlContext,
Reference<AccessControlContext>>();
cacheSecurityManager = sm;
}
if (acc.equals(okContext) || authCache.containsKey(acc)) {
return;
}
InetAddress addr = socket.getInetAddress();
String host = (addr != null) ? addr.getHostAddress() : "*";
sm.checkAccept(host, socket.getPort());
authCache.put(acc, new SoftReference<AccessControlContext>(acc));
okContext = acc;
}
public void run() {
Thread t = Thread.currentThread();
String name = t.getName();
try {
t.setName("RMI TCP Connection(" +
connectionCount.incrementAndGet() +
")-" + remoteHost);
AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
run0();
return null;
}, NOPERMS_ACC);
} finally {
t.setName(name);
}
}
private void run0() {
TCPEndpoint endpoint = getEndpoint();
int port = endpoint.getPort();
threadConnectionHandler.set(this);
// set socket to disable Nagle's algorithm (always send
// immediately)
// TBD: should this be left up to socket factory instead?
try {
socket.setTcpNoDelay(true);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
// set socket to timeout after excessive idle time
try {
if (connectionReadTimeout > 0)
socket.setSoTimeout(connectionReadTimeout);
} catch (Exception e) {
// too bad, continue anyway
}
try {
InputStream sockIn = socket.getInputStream();
InputStream bufIn = sockIn.markSupported()
? sockIn
: new BufferedInputStream(sockIn);
// Read magic (or HTTP wrapper)
bufIn.mark(4);
DataInputStream in = new DataInputStream(bufIn);
int magic = in.readInt();
if (magic == POST) {
if (disableIncomingHttp) {
throw new RemoteException("RMI over HTTP is disabled");
}
tcpLog.log(Log.BRIEF, "decoding HTTP-wrapped call");
// It's really a HTTP-wrapped request. Repackage
// the socket in a HttpReceiveSocket, reinitialize
// sockIn and in, and reread magic.
bufIn.reset(); // unread "POST"
try {
socket = new HttpReceiveSocket(socket, bufIn, null);
remoteHost = "0.0.0.0";
sockIn = socket.getInputStream();
bufIn = new BufferedInputStream(sockIn);
in = new DataInputStream(bufIn);
magic = in.readInt();
} catch (IOException e) {
throw new RemoteException("Error HTTP-unwrapping call",
e);
}
}
// bufIn's mark will invalidate itself when it overflows
// so it doesn't have to be turned off
// read and verify transport header
short version = in.readShort();
if (magic != TransportConstants.Magic ||
version != TransportConstants.Version) {
// protocol mismatch detected...
// just close socket: this would recurse if we marshal an
// exception to the client and the protocol at other end
// doesn't match.
closeSocket(socket);
return;
}
OutputStream sockOut = socket.getOutputStream();
BufferedOutputStream bufOut =
new BufferedOutputStream(sockOut);
DataOutputStream out = new DataOutputStream(bufOut);
int remotePort = socket.getPort();
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "accepted socket from [" +
remoteHost + ":" + remotePort + "]");
}
TCPEndpoint ep;
TCPChannel ch;
TCPConnection conn;
// send ack (or nack) for protocol
byte protocol = in.readByte();
switch (protocol) {
case TransportConstants.SingleOpProtocol:
// no ack for protocol
// create dummy channel for receiving messages
ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
endpoint.getClientSocketFactory(),
endpoint.getServerSocketFactory());
ch = new TCPChannel(TCPTransport.this, ep);
conn = new TCPConnection(ch, socket, bufIn, bufOut);
// read input messages
handleMessages(conn, false);
break;
case TransportConstants.StreamProtocol:
// send ack
out.writeByte(TransportConstants.ProtocolAck);
// suggest endpoint (in case client doesn't know host name)
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") " + "suggesting " + remoteHost + ":" +
remotePort);
}
out.writeUTF(remoteHost);
out.writeInt(remotePort);
out.flush();
// read and discard (possibly bogus) endpoint
// REMIND: would be faster to read 2 bytes then skip N+4
String clientHost = in.readUTF();
int clientPort = in.readInt();
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") client using " + clientHost + ":" + clientPort);
}
// create dummy channel for receiving messages
// (why not use clientHost and clientPort?)
ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
endpoint.getClientSocketFactory(),
endpoint.getServerSocketFactory());
ch = new TCPChannel(TCPTransport.this, ep);
conn = new TCPConnection(ch, socket, bufIn, bufOut);
// read input messages
handleMessages(conn, true);
break;
case TransportConstants.MultiplexProtocol:
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") accepting multiplex protocol");
}
// send ack
out.writeByte(TransportConstants.ProtocolAck);
// suggest endpoint (in case client doesn't already have one)
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") suggesting " + remoteHost + ":" + remotePort);
}
out.writeUTF(remoteHost);
out.writeInt(remotePort);
out.flush();
// read endpoint client has decided to use
ep = new TCPEndpoint(in.readUTF(), in.readInt(),
endpoint.getClientSocketFactory(),
endpoint.getServerSocketFactory());
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " +
port + ") client using " +
ep.getHost() + ":" + ep.getPort());
}
ConnectionMultiplexer multiplexer;
synchronized (channelTable) {
// create or find channel for this endpoint
ch = getChannel(ep);
multiplexer =
new ConnectionMultiplexer(ch, bufIn, sockOut,
false);
ch.useMultiplexer(multiplexer);
}
multiplexer.run();
break;
default:
// protocol not understood, send nack and close socket
out.writeByte(TransportConstants.ProtocolNack);
out.flush();
break;
}
} catch (IOException e) {
// socket in unknown state: destroy socket
tcpLog.log(Log.BRIEF, "terminated with exception:", e);
} finally {
closeSocket(socket);
}
}
}
}