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