Search in sources :

Example 1 with RemoteObjectInvocationHandler

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

the class ActivationID method readObject.

/**
     * <code>readObject</code> for custom serialization.
     *
     * <p>This method reads this object's serialized form for this
     * class as follows:
     *
     * <p>The <code>readObject</code> method is invoked on
     * <code>in</code> to read this object's unique identifier
     * (a {@link java.rmi.server.UID UID} instance).
     *
     * <p>Next, the <code>readUTF</code> method is invoked on
     * <code>in</code> to read the external ref type name of the
     * <code>RemoteRef</code> instance for this object's
     * activator.  Next, the <code>RemoteRef</code>
     * instance is created of an implementation-specific class
     * corresponding to the external ref type name (returned by
     * <code>readUTF</code>), and the <code>readExternal</code>
     * method is invoked on that <code>RemoteRef</code> instance
     * to read the external form corresponding to the external
     * ref type name.
     *
     * <p>Note: If the external ref type name is
     * <code>"UnicastRef"</code>, <code>"UnicastServerRef"</code>,
     * <code>"UnicastRef2"</code>, <code>"UnicastServerRef2"</code>,
     * or <code>"ActivatableRef"</code>, a corresponding
     * implementation-specific class must be found, and its
     * <code>readExternal</code> method must read the serial data
     * for that external ref type name as specified to be written
     * in the <b>serialData</b> documentation for this class.
     * If the external ref type name is any other string (of non-zero
     * length), a <code>ClassNotFoundException</code> will be thrown,
     * unless the implementation provides an implementation-specific
     * class corresponding to that external ref type name, in which
     * case the <code>RemoteRef</code> will be an instance of
     * that implementation-specific class.
     */
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    uid = (UID) in.readObject();
    try {
        Class<? extends RemoteRef> refClass = Class.forName(RemoteRef.packagePrefix + "." + in.readUTF()).asSubclass(RemoteRef.class);
        RemoteRef ref = refClass.newInstance();
        ref.readExternal(in);
        activator = (Activator) Proxy.newProxyInstance(null, new Class<?>[] { Activator.class }, new RemoteObjectInvocationHandler(ref));
    } catch (InstantiationException e) {
        throw (IOException) new InvalidObjectException("Unable to create remote reference").initCause(e);
    } catch (IllegalAccessException e) {
        throw (IOException) new InvalidObjectException("Unable to create remote reference").initCause(e);
    }
}
Also used : RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) RemoteRef(java.rmi.server.RemoteRef) InvalidObjectException(java.io.InvalidObjectException)

Example 2 with RemoteObjectInvocationHandler

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

the class ActivationID method writeObject.

/**
     * <code>writeObject</code> for custom serialization.
     *
     * <p>This method writes this object's serialized form for
     * this class as follows:
     *
     * <p>The <code>writeObject</code> method is invoked on
     * <code>out</code> passing this object's unique identifier
     * (a {@link java.rmi.server.UID UID} instance) as the argument.
     *
     * <p>Next, the {@link
     * java.rmi.server.RemoteRef#getRefClass(java.io.ObjectOutput)
     * getRefClass} method is invoked on the activator's
     * <code>RemoteRef</code> instance to obtain its external ref
     * type name.  Next, the <code>writeUTF</code> method is
     * invoked on <code>out</code> with the value returned by
     * <code>getRefClass</code>, and then the
     * <code>writeExternal</code> method is invoked on the
     * <code>RemoteRef</code> instance passing <code>out</code>
     * as the argument.
     *
     * @serialData The serialized data for this class comprises a
     * <code>java.rmi.server.UID</code> (written with
     * <code>ObjectOutput.writeObject</code>) followed by the
     * external ref type name of the activator's
     * <code>RemoteRef</code> instance (a string written with
     * <code>ObjectOutput.writeUTF</code>), followed by the
     * external form of the <code>RemoteRef</code> instance as
     * written by its <code>writeExternal</code> method.
     *
     * <p>The external ref type name of the
     * <code>RemoteRef</Code> instance is
     * determined using the definitions of external ref type
     * names specified in the {@link java.rmi.server.RemoteObject
     * RemoteObject} <code>writeObject</code> method
     * <b>serialData</b> specification.  Similarly, the data
     * written by the <code>writeExternal</code> method and read
     * by the <code>readExternal</code> method of
     * <code>RemoteRef</code> implementation classes
     * corresponding to each of the defined external ref type
     * names is specified in the {@link
     * java.rmi.server.RemoteObject RemoteObject}
     * <code>writeObject</code> method <b>serialData</b>
     * specification.
     **/
private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException {
    out.writeObject(uid);
    RemoteRef ref;
    if (activator instanceof RemoteObject) {
        ref = ((RemoteObject) activator).getRef();
    } else if (Proxy.isProxyClass(activator.getClass())) {
        InvocationHandler handler = Proxy.getInvocationHandler(activator);
        if (!(handler instanceof RemoteObjectInvocationHandler)) {
            throw new InvalidObjectException("unexpected invocation handler");
        }
        ref = ((RemoteObjectInvocationHandler) handler).getRef();
    } else {
        throw new InvalidObjectException("unexpected activator type");
    }
    out.writeUTF(ref.getRefClass(out));
    ref.writeExternal(out);
}
Also used : RemoteObject(java.rmi.server.RemoteObject) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) RemoteRef(java.rmi.server.RemoteRef) InvalidObjectException(java.io.InvalidObjectException) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 3 with RemoteObjectInvocationHandler

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

the class Util method createProxy.

/**
     * Returns a proxy for the specified implClass.
     *
     * If both of the following criteria is satisfied, a dynamic proxy for
     * the specified implClass is returned (otherwise a RemoteStub instance
     * for the specified implClass is returned):
     *
     *    a) either the property java.rmi.server.ignoreStubClasses is true or
     *       a pregenerated stub class does not exist for the impl class, and
     *    b) forceStubUse is false.
     *
     * If the above criteria are satisfied, this method constructs a
     * dynamic proxy instance (that implements the remote interfaces of
     * implClass) constructed with a RemoteObjectInvocationHandler instance
     * constructed with the clientRef.
     *
     * Otherwise, this method loads the pregenerated stub class (which
     * extends RemoteStub and implements the remote interfaces of
     * implClass) and constructs an instance of the pregenerated stub
     * class with the clientRef.
     *
     * @param implClass the class to obtain remote interfaces from
     * @param clientRef the remote ref to use in the invocation handler
     * @param forceStubUse if true, forces creation of a RemoteStub
     * @throws IllegalArgumentException if implClass implements illegal
     * remote interfaces
     * @throws StubNotFoundException if problem locating/creating stub or
     * creating the dynamic proxy instance
     **/
public static Remote createProxy(Class<?> implClass, RemoteRef clientRef, boolean forceStubUse) throws StubNotFoundException {
    Class<?> remoteClass;
    try {
        remoteClass = getRemoteClass(implClass);
    } catch (ClassNotFoundException ex) {
        throw new StubNotFoundException("object does not implement a remote interface: " + implClass.getName());
    }
    if (forceStubUse || !(ignoreStubClasses || !stubClassExists(remoteClass))) {
        return createStub(remoteClass, clientRef);
    }
    final ClassLoader loader = implClass.getClassLoader();
    final Class<?>[] interfaces = getRemoteInterfaces(implClass);
    final InvocationHandler handler = new RemoteObjectInvocationHandler(clientRef);
    try {
        return AccessController.doPrivileged(new PrivilegedAction<Remote>() {

            public Remote run() {
                return (Remote) Proxy.newProxyInstance(loader, interfaces, handler);
            }
        });
    } catch (IllegalArgumentException e) {
        throw new StubNotFoundException("unable to create proxy", e);
    }
}
Also used : RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) Remote(java.rmi.Remote) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) StubNotFoundException(java.rmi.StubNotFoundException)

Example 4 with RemoteObjectInvocationHandler

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

the class ActivatableRef method activate.

/**
     * private method to activate the remote object.
     *
     * NOTE: the caller must be synchronized on "this" before
     * calling this method.
     */
private RemoteRef activate(boolean force) throws RemoteException {
    assert Thread.holdsLock(this);
    ref = null;
    try {
        /*
             * Activate the object and retrieve the remote reference
             * from inside the stub returned as the result. Then
             * set this activatable ref's internal ref to be the
             * ref inside the ref of the stub. In more clear terms,
             * the stub returned from the activate call contains an
             * ActivatableRef. We need to set the ref in *this*
             * ActivatableRef to the ref inside the ActivatableRef
             * retrieved from the stub. The ref type embedded in the
             * ActivatableRef is typically a UnicastRef.
             */
        Remote proxy = id.activate(force);
        ActivatableRef newRef = null;
        if (proxy instanceof RemoteStub) {
            newRef = (ActivatableRef) ((RemoteStub) proxy).getRef();
        } else {
            /*
                 * Assume that proxy is an instance of a dynamic proxy
                 * class.  If that assumption is not correct, or either of
                 * the casts below fails, the resulting exception will be
                 * wrapped in an ActivateFailedException below.
                 */
            RemoteObjectInvocationHandler handler = (RemoteObjectInvocationHandler) Proxy.getInvocationHandler(proxy);
            newRef = (ActivatableRef) handler.getRef();
        }
        ref = newRef.ref;
        return ref;
    } catch (ConnectException e) {
        throw new ConnectException("activation failed", e);
    } catch (RemoteException e) {
        throw new ConnectIOException("activation failed", e);
    } catch (UnknownObjectException e) {
        throw new NoSuchObjectException("object not registered");
    } catch (ActivationException e) {
        throw new ActivateFailedException("activation failed", e);
    }
}
Also used : RemoteStub(java.rmi.server.RemoteStub) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler)

Example 5 with RemoteObjectInvocationHandler

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

the class RemoteInterface method main.

public static void main(String[] args) throws Exception {
    RemoteInterface server = null;
    RemoteInterface proxy = null;
    try {
        System.setProperty("java.rmi.server.ignoreStubClasses", args[0]);
        boolean ignoreStubClasses = Boolean.parseBoolean(args[0]);
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        System.err.println("export object");
        server = new UseDynamicProxies();
        proxy = (RemoteInterface) UnicastRemoteObject.exportObject(server, 0);
        System.err.println("proxy = " + proxy);
        if (ignoreStubClasses) {
            if (!Proxy.isProxyClass(proxy.getClass())) {
                throw new RuntimeException("server proxy is not a dynamic proxy");
            }
            if (!(Proxy.getInvocationHandler(proxy) instanceof RemoteObjectInvocationHandler)) {
                throw new RuntimeException("invalid invocation handler");
            }
        } else if (!(proxy instanceof RemoteStub)) {
            throw new RuntimeException("server proxy is not a RemoteStub");
        }
        System.err.println("invoke methods");
        Object obj = proxy.passObject(proxy);
        if (!proxy.equals(obj)) {
            throw new RuntimeException("returned proxy not equal");
        }
        int x = proxy.passInt(53);
        if (x != 53) {
            throw new RuntimeException("returned int not equal");
        }
        String string = proxy.passString("test");
        if (!string.equals("test")) {
            throw new RuntimeException("returned string not equal");
        }
        System.err.println("TEST PASSED");
    } finally {
        if (proxy != null) {
            UnicastRemoteObject.unexportObject(server, true);
        }
    }
}
Also used : RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) RemoteStub(java.rmi.server.RemoteStub) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject)

Aggregations

RemoteObjectInvocationHandler (java.rmi.server.RemoteObjectInvocationHandler)6 InvalidObjectException (java.io.InvalidObjectException)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 RemoteRef (java.rmi.server.RemoteRef)2 RemoteStub (java.rmi.server.RemoteStub)2 Remote (java.rmi.Remote)1 StubNotFoundException (java.rmi.StubNotFoundException)1 Registry (java.rmi.registry.Registry)1 ObjID (java.rmi.server.ObjID)1 RemoteObject (java.rmi.server.RemoteObject)1 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)1 Random (java.util.Random)1 UnicastRef (sun.rmi.server.UnicastRef)1 LiveRef (sun.rmi.transport.LiveRef)1 TCPEndpoint (sun.rmi.transport.tcp.TCPEndpoint)1