Search in sources :

Example 1 with ObjID

use of java.rmi.server.ObjID in project jdk8u_jdk by JetBrains.

the class LiveRef method read.

public static LiveRef read(ObjectInput in, boolean useNewFormat) throws IOException, ClassNotFoundException {
    Endpoint ep;
    ObjID id;
    // (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;
}
Also used : TCPEndpoint(sun.rmi.transport.tcp.TCPEndpoint) ObjID(java.rmi.server.ObjID)

Example 2 with ObjID

use of java.rmi.server.ObjID in project jdk8u_jdk by JetBrains.

the class LocateRegistry method getRegistry.

/**
     * Returns a locally created remote reference to the remote object
     * <code>Registry</code> on the specified <code>host</code> and
     * <code>port</code>.  Communication with this remote registry will
     * use the supplied <code>RMIClientSocketFactory</code> <code>csf</code>
     * to create <code>Socket</code> connections to the registry on the
     * remote <code>host</code> and <code>port</code>.
     *
     * @param host host for the remote registry
     * @param port port on which the registry accepts requests
     * @param csf  client-side <code>Socket</code> factory used to
     *      make connections to the registry.  If <code>csf</code>
     *      is null, then the default client-side <code>Socket</code>
     *      factory will be used in the registry stub.
     * @return reference (a stub) to the remote registry
     * @exception RemoteException if the reference could not be created
     * @since 1.2
     */
public static Registry getRegistry(String host, int port, RMIClientSocketFactory csf) throws RemoteException {
    Registry registry = null;
    if (port <= 0)
        port = Registry.REGISTRY_PORT;
    if (host == null || host.length() == 0) {
        // that the RegistryImpl's checkAccess will not fail.
        try {
            host = java.net.InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            // If that failed, at least try "" (localhost) anyway...
            host = "";
        }
    }
    /*
         * Create a proxy for the registry with the given host, port, and
         * client socket factory.  If the supplied client socket factory is
         * null, then the ref type is a UnicastRef, otherwise the ref type
         * is a UnicastRef2.  If the property
         * java.rmi.server.ignoreStubClasses is true, then the proxy
         * returned is an instance of a dynamic proxy class that implements
         * the Registry interface; otherwise the proxy returned is an
         * instance of the pregenerated stub class for RegistryImpl.
         **/
    LiveRef liveRef = new LiveRef(new ObjID(ObjID.REGISTRY_ID), new TCPEndpoint(host, port, csf, null), false);
    RemoteRef ref = (csf == null) ? new UnicastRef(liveRef) : new UnicastRef2(liveRef);
    return (Registry) Util.createProxy(RegistryImpl.class, ref, false);
}
Also used : RegistryImpl(sun.rmi.registry.RegistryImpl) TCPEndpoint(sun.rmi.transport.tcp.TCPEndpoint) LiveRef(sun.rmi.transport.LiveRef) ObjID(java.rmi.server.ObjID) RemoteRef(java.rmi.server.RemoteRef) UnicastRef2(sun.rmi.server.UnicastRef2) UnicastRef(sun.rmi.server.UnicastRef) RemoteException(java.rmi.RemoteException)

Example 3 with ObjID

use of java.rmi.server.ObjID in project jdk8u_jdk by JetBrains.

the class InterfaceHash method main.

public static void main(String[] args) throws Exception {
    System.err.println("\nRegression test for bug 4472769");
    System.err.println("\n=== verifying that J2SE registry's skeleton uses" + "\ncorrect interface hash and operation numbers:");
    Registry testImpl = LocateRegistry.createRegistry(PORT);
    System.err.println("created test registry on port " + PORT);
    RemoteRef ref = new UnicastRef(new LiveRef(new ObjID(ObjID.REGISTRY_ID), new TCPEndpoint("", PORT), false));
    Registry referenceStub = new ReferenceRegistryStub(ref);
    System.err.println("created reference registry stub: " + referenceStub);
    referenceStub.bind(NAME, referenceStub);
    System.err.println("bound name \"" + NAME + "\" in registry");
    String[] list = referenceStub.list();
    System.err.println("list of registry contents: " + Arrays.asList(list));
    if (list.length != 1 || !list[0].equals(NAME)) {
        throw new RuntimeException("TEST FAILED: unexpected list contents");
    }
    Registry result = (Registry) referenceStub.lookup(NAME);
    System.err.println("lookup of name \"" + NAME + "\" returned: " + result);
    if (!result.equals(referenceStub)) {
        throw new RuntimeException("TEST FAILED: unexpected lookup result");
    }
    referenceStub.rebind(NAME, referenceStub);
    referenceStub.unbind(NAME);
    System.err.println("unbound name \"" + NAME + "\"");
    list = referenceStub.list();
    System.err.println("list of registry contents: " + Arrays.asList(list));
    if (list.length != 0) {
        throw new RuntimeException("TEST FAILED: list not empty");
    }
    System.err.println("\n=== verifying that J2SE registry's stub uses" + "correct interface hash:");
    class FakeRemoteRef implements RemoteRef {

        long hash;

        int opnum;

        public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) {
            this.hash = hash;
            this.opnum = opnum;
            throw new UnsupportedOperationException();
        }

        public void invoke(RemoteCall call) {
        }

        public void done(RemoteCall call) {
        }

        public Object invoke(Remote obj, Method method, Object[] args, long hash) {
            throw new UnsupportedOperationException();
        }

        public String getRefClass(java.io.ObjectOutput out) {
            return "FakeRemoteRef";
        }

        public int remoteHashCode() {
            return 1013;
        }

        public boolean remoteEquals(RemoteRef obj) {
            return false;
        }

        public String remoteToString() {
            return "FakeRemoteRef";
        }

        public void writeExternal(java.io.ObjectOutput out) {
        }

        public void readExternal(java.io.ObjectInput in) {
        }
    }
    FakeRemoteRef f = new FakeRemoteRef();
    Registry testRegistry = LocateRegistry.getRegistry(PORT);
    System.err.println("created original test registry stub: " + testRegistry);
    Class stubClass = testRegistry.getClass();
    System.err.println("test registry stub class: " + stubClass);
    Constructor cons = stubClass.getConstructor(new Class[] { RemoteRef.class });
    Registry testStub = (Registry) cons.newInstance(new Object[] { f });
    System.err.println("created new instrumented test registry stub: " + testStub);
    System.err.println("invoking bind:");
    try {
        testStub.bind(NAME, referenceStub);
    } catch (UnsupportedOperationException e) {
    }
    System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
    if (f.hash != 4905912898345647071L) {
        throw new RuntimeException("TEST FAILED: wrong interface hash");
    } else if (f.opnum != 0) {
        throw new RuntimeException("TEST FAILED: wrong operation number");
    }
    System.err.println("invoking list:");
    try {
        testStub.list();
    } catch (UnsupportedOperationException e) {
    }
    System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
    if (f.hash != 4905912898345647071L) {
        throw new RuntimeException("TEST FAILED: wrong interface hash");
    } else if (f.opnum != 1) {
        throw new RuntimeException("TEST FAILED: wrong operation number");
    }
    System.err.println("invoking lookup:");
    try {
        testStub.lookup(NAME);
    } catch (UnsupportedOperationException e) {
    }
    System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
    if (f.hash != 4905912898345647071L) {
        throw new RuntimeException("TEST FAILED: wrong interface hash");
    } else if (f.opnum != 2) {
        throw new RuntimeException("TEST FAILED: wrong operation number");
    }
    System.err.println("invoking rebind:");
    try {
        testStub.rebind(NAME, referenceStub);
    } catch (UnsupportedOperationException e) {
    }
    System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
    if (f.hash != 4905912898345647071L) {
        throw new RuntimeException("TEST FAILED: wrong interface hash");
    } else if (f.opnum != 3) {
        throw new RuntimeException("TEST FAILED: wrong operation number");
    }
    System.err.println("invoking unbind:");
    try {
        testStub.unbind(NAME);
    } catch (UnsupportedOperationException e) {
    }
    System.err.println("hash == " + f.hash + ", opnum == " + f.opnum);
    if (f.hash != 4905912898345647071L) {
        throw new RuntimeException("TEST FAILED: wrong interface hash");
    } else if (f.opnum != 4) {
        throw new RuntimeException("TEST FAILED: wrong operation number");
    }
    System.err.println("TEST PASSED");
}
Also used : Constructor(java.lang.reflect.Constructor) RemoteRef(java.rmi.server.RemoteRef) Remote(java.rmi.Remote) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) Method(java.lang.reflect.Method) TCPEndpoint(sun.rmi.transport.tcp.TCPEndpoint) LiveRef(sun.rmi.transport.LiveRef) RemoteObject(java.rmi.server.RemoteObject) ObjID(java.rmi.server.ObjID) RemoteObject(java.rmi.server.RemoteObject) UnicastRef(sun.rmi.server.UnicastRef) RemoteCall(java.rmi.server.RemoteCall)

Example 4 with ObjID

use of java.rmi.server.ObjID in project jdk8u_jdk by JetBrains.

the class RandomIDs method main.

public static void main(String[] args) throws Exception {
    boolean shouldBeRandom = false;
    boolean shouldBeSequential = false;
    String usage = "Usage: java RandomIDs [random|sequential]";
    if (args.length != 1) {
        System.err.println(usage);
        throw new Error("wrong number of arguments");
    } else if (args[0].equals("random")) {
        shouldBeRandom = true;
    } else if (args[0].equals("sequential")) {
        shouldBeSequential = true;
    } else {
        System.err.println(usage);
        throw new Error("invalid argument");
    }
    System.err.println("\nRegression test for bug 6364692\n");
    String propertyValue = System.getProperty("java.rmi.server.randomIDs");
    System.err.println("Value of java.rmi.server.randomIDs system property: " + (propertyValue != null ? "\"" + propertyValue + "\"" : null));
    System.err.println("Expecting object numbers of unique ObjIDs to be: " + args[0]);
    /*
         * Get the 64-bit "object number" component of COUNT number of
         * unique (not "well-known") ObjID instances created in
         * sequence, by writing each to a dummy ObjectOutputStream and
         * trapping the first writeLong invocation on the stream.
         */
    final long[] objnums = new long[COUNT];
    for (int i = 0; i < COUNT; i++) {
        final int j = i;
        class Escape extends RuntimeException {
        }
        try {
            new ObjID().write(new ObjectOutputStream(new OutputStream() {

                public void write(int b) {
                }
            }) {

                public void writeLong(long val) throws IOException {
                    objnums[j] = val;
                    throw new Escape();
                }
            });
            throw new Error("writeLong not invoked");
        } catch (Escape e) {
        }
    }
    /*
         * If the object numbers should be random, then verify that
         * they are.  (This verification is certainly not a thorough
         * evaluation of randomness, but it performs a couple of
         * simple checks to catch mistakes in ObjID's application of a
         * CSPRNG: are roughly half the bits set, and can the sequence
         * be used to get a rough Monte Carlo estimate of pi.  Errors
         * up to 5% are tolerated for both checks.)
         */
    if (shouldBeRandom) {
        int bitCount = 0;
        int piHitCount = 0;
        for (int i = 0; i < COUNT; i++) {
            bitCount += Long.bitCount(objnums[i]);
            double x = ((double) (objnums[i] >>> 32)) / (1L << 32);
            double y = ((double) (objnums[i] & 0xFFFFFFFFL)) / (1L << 32);
            if (((x * x) + (y * y)) <= 1.0) {
                piHitCount++;
            }
        }
        int bitCountTarget = COUNT * 32;
        double bitCountError = ((double) (bitCount - bitCountTarget)) / bitCountTarget;
        if (Math.abs(bitCountError) > 0.05) {
            // tolerate 5% error
            throw new Error("TEST FAILED: " + "bitCount == " + bitCount);
        }
        double piEstimate = ((double) piHitCount / COUNT) * 4.0;
        double piEstimateError = (piEstimate - Math.PI) / Math.PI;
        if (Math.abs(piEstimateError) > 0.05) {
            // tolerate 5% error
            throw new Error("TEST FAILED: " + "piEstimate == " + piEstimate);
        }
    }
    /*
         * If the object numbers should be sequential, then verify
         * that they are.
         */
    if (shouldBeSequential) {
        long first = objnums[0];
        /*
             * This test currently verifies that the first object
             * number is zero, but that could be false if one or more
             * remote objects get exported as part of VM startup-- if
             * that starts happening, this check could be relaxed.
             */
        if (first != 0) {
            throw new Error("TEST FAILED: " + "first object number == " + first + " (not zero)");
        }
        for (int i = 1; i < COUNT; i++) {
            if (objnums[i] != first + i) {
                throw new Error("TEST FAILED: first == " + first + ", " + "objnums[" + i + "] == " + objnums[i]);
            }
        }
    }
    System.err.println("TEST PASSED");
}
Also used : OutputStream(java.io.OutputStream) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjID(java.rmi.server.ObjID)

Example 5 with ObjID

use of java.rmi.server.ObjID in project jdk8u_jdk by JetBrains.

the class Transport method serviceCall.

/**
     * 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;
}
Also used : MarshalException(java.rmi.MarshalException) ObjectOutput(java.io.ObjectOutput) Remote(java.rmi.Remote) IOException(java.io.IOException) IOException(java.io.IOException) Dispatcher(sun.rmi.server.Dispatcher) ServerNotActiveException(java.rmi.server.ServerNotActiveException) AccessControlContext(java.security.AccessControlContext) ObjID(java.rmi.server.ObjID) NoSuchObjectException(java.rmi.NoSuchObjectException) RemoteException(java.rmi.RemoteException)

Aggregations

ObjID (java.rmi.server.ObjID)6 TCPEndpoint (sun.rmi.transport.tcp.TCPEndpoint)3 IOException (java.io.IOException)2 Remote (java.rmi.Remote)2 RemoteException (java.rmi.RemoteException)2 RemoteRef (java.rmi.server.RemoteRef)2 ServerNotActiveException (java.rmi.server.ServerNotActiveException)2 UnicastRef (sun.rmi.server.UnicastRef)2 LiveRef (sun.rmi.transport.LiveRef)2 ObjectOutput (java.io.ObjectOutput)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 MarshalException (java.rmi.MarshalException)1 NoSuchObjectException (java.rmi.NoSuchObjectException)1 Lease (java.rmi.dgc.Lease)1 VMID (java.rmi.dgc.VMID)1 LocateRegistry (java.rmi.registry.LocateRegistry)1 Registry (java.rmi.registry.Registry)1