abstractorb.java

来自「一个java方面的消息订阅发送的源码」· Java 代码 · 共 569 行 · 第 1/2 页

JAVA
569
字号
            parsed = URIHelper.parse(uri);
        } catch (InvalidURIException exception) {
            throw new ExportException(exception.getMessage(), exception);

        }
        Proxy proxy = null;
        ObjectRef ref = (ObjectRef) _objectMap.get(object);
        if (ref != null) {
            proxy = addProxy(ref, parsed, object, ref.getProxyClass());
        } else {
            proxy = doExport(object, objID, parsed, getProxyClass(object));
        }
        return proxy;
    }

    /**
     * Export an object to a specific URI.
     *
     * @param object the object to export
     * @param uri    the target URI from which connections to the object are
     *               made.
     * @return a proxy which may be used to invoke methods on the object
     * @throws ExportException       if the object cannot be exported
     * @throws StubNotFoundException if the proxy class cannot be found
     */
    public Proxy exportObjectTo(Object object, String uri)
            throws ExportException, StubNotFoundException {
        return exportObjectTo(object, uri, null, null);
    }

    /**
     * Export an object to a specific URI. Only callers from the target URI may
     * perform invocations.
     *
     * @param object      the object to export
     * @param uri         the target URI from which connections to the object
     *                    are made.
     * @param principal   the security principal. May be <code>null</code>
     * @param credentials the security credentials. May be <code>null</code>
     * @return a proxy which may be used to invoke methods on the object
     * @throws ExportException       if the object cannot be exported
     * @throws StubNotFoundException if the proxy class cannot be found
     */
    public Proxy exportObjectTo(Object object, String uri, String principal,
                                String credentials)
            throws ExportException, StubNotFoundException {
        if (object == null) {
            throw new IllegalArgumentException("Argument 'object' is null");
        }
        if (uri == null) {
            throw new IllegalArgumentException("Argument 'uri' is null");
        }
        URI remoteURI = null;
        URI localURI = null;
        try {
            remoteURI = URIHelper.parse(uri);
        } catch (InvalidURIException exception) {
            throw new ExportException(exception.getMessage(), exception);
        }

        localURI = connect(remoteURI, principal, credentials);

        return doExportTo(object, localURI);
    }

    /**
     * Unexport an object.
     *
     * @param object the object to export
     * @throws NoSuchObjectException if the object isn't exported
     */
    public synchronized void unexportObject(Object object)
            throws NoSuchObjectException {

        ObjectRef ref = (ObjectRef) _objectMap.remove(object);
        if (ref != null) {
            _objIDMap.remove(ref.getObjID());
        } else {
            throw new NoSuchObjectException("Object not exported");
        }
    }

    /**
     * Connect to the specified URI.
     *
     * @param uri         the URI to establish a connection with
     * @param principal   specifies the identity of the principal. If
     *                    <code>null</code>, indicates to connect anonymously.
     * @param credentials the credentials of the principal
     * @return the local address that the connection is bound to
     * @throws ExportException for any error
     */
    protected abstract URI connect(URI uri, String principal,
                                   String credentials) throws ExportException;

    /**
     * Accept connections on the specified URI.
     *
     * @param uri the URI to accept connections on
     * @throws ExportException for any error
     */
    protected abstract void accept(URI uri) throws ExportException;

    /**
     * Returns the proxy class loader.
     *
     * @return the proxy class loader
     */
    protected ClassLoader getProxyClassLoader() {
        return _loader;
    }

    /**
     * Returns the configuration properties.
     *
     * @return the configuration properties
     */
    protected Map getProperties() {
        return _properties;
    }

    /**
     * Export an object to a specific URI.
     * 
     * @param object the object to export
     * @param uri the URI via which connections to the object are made
     * @throws ExportException if the object cannot be exported
     * @throws StubNotFoundException if the proxy class cannot be found
     */ 
    protected Proxy doExportTo(Object object, URI uri)
            throws ExportException, StubNotFoundException {
        Proxy proxy = null;
        ObjectRef ref = (ObjectRef) _objectMap.get(object);
        if (ref != null) {
            proxy = addProxyTo(ref, uri, object, ref.getProxyClass());
        } else {
            ObjID objID = new ObjID();
            proxy = doExportTo(object, objID, uri, getProxyClass(object));
        }

        return proxy;
    }
    
    /**
     * Export an object on a specific URI.
     *
     * @param object     the object to export
     * @param objID      the identifier of the object
     * @param uri        the URI via which connections to the object are made
     * @param proxyClass the proxy class
     * @return a proxy which may be used to invoke methods on the object
     * @throws ExportException if the object cannot be exported
     */
    private Proxy doExport(Object object, ObjID objID, URI uri,
                           Class proxyClass) throws ExportException {
        accept(uri);
        ObjectRef ref = new ObjectRef(objID, object, proxyClass);
        Proxy proxy = ref.addProxy(getRoute(uri));
        _objIDMap.put(objID, ref);
        _objectMap.put(object, ref);
        return proxy;
    }
    
    /**
     * Export an object to a specific URI.
     *
     * @param object     the object to export
     * @param objID      the identifier of the object
     * @param uri        the URI via which connections to the object are made
     * @param proxyClass the proxy class
     * @return a proxy which may be used to invoke methods on the object
     * @throws ExportException if the object cannot be exported
     */
    private Proxy doExportTo(Object object, ObjID objID, URI uri,
                             Class proxyClass) throws ExportException {
        ObjectRef ref = new ObjectRef(objID, object, proxyClass);
        Proxy proxy = ref.addProxy(getRoute(uri));
        _objIDMap.put(objID, ref);
        _objectMap.put(object, ref);
        return proxy;
    }

    /**
     * Add a proxy for an exported object.
     *
     * @param ref        a reference to the exported object
     * @param uri        the URI via which connections to the object are made
     * @param object     the exported object
     * @param proxyClass the proxy class
     * @return a proxy which may be used to invoke methods on the object
     * @throws ExportException if the object cannot be exported
     */
    private Proxy addProxy(ObjectRef ref, URI uri, Object object,
                           Class proxyClass) throws ExportException {

        if (object != ref.getObject()) {
            throw new ExportException("Cannot export object on URI=" + uri
                                      + ": object mismatch");
        }
        if (proxyClass != ref.getProxyClass()) {
            throw new ExportException("Cannot export object on URI=" + uri
                                      + ": proxy class mismatch");
        }

        accept(uri);
        return ref.addProxy(getRoute(uri));
    }

    /**
     * Add a proxy for an exported object.
     *
     * @param ref        a reference to the exported object
     * @param uri        the URI via which connections to the object are made
     * @param object     the exported object
     * @param proxyClass the proxy class
     * @return a proxy which may be used to invoke methods on the object
     * @throws ExportException if the object cannot be exported
     */
    private Proxy addProxyTo(ObjectRef ref, URI uri, Object object,
                             Class proxyClass) throws ExportException {

        if (object != ref.getObject()) {
            throw new ExportException("Cannot export object on URI=" + uri
                                      + ": object mismatch");
        }
        if (proxyClass != ref.getProxyClass()) {
            throw new ExportException("Cannot export object on URI=" + uri
                                      + ": proxy class mismatch");
        }

        return ref.addProxy(uri);
    }

    /**
     * Loads the proxy class for the supplied object.
     *
     * @param object the object to load the proxy for
     * @return the proxy class corresponding to <code>object</code>
     * @throws StubNotFoundException if the proxy class cannot be loaded
     */
    private Class getProxyClass(Object object) throws StubNotFoundException {
        return getProxyClass(object.getClass());
    }

    /**
     * Loads the proxy class for the supplied class.
     *
     * @param clazz the class to load the proxy for
     * @return the proxy class corresponding to <code>class</code>
     * @throws StubNotFoundException if the proxy class cannot be loaded
     */
    private Class getProxyClass(Class clazz) throws StubNotFoundException {
        String proxyName = clazz.getName() + "__Proxy";
        Class proxyClass = null;
        try {
            proxyClass = _loader.loadClass(proxyName);
            if (!Proxy.class.isAssignableFrom(proxyClass)) {
                throw new StubNotFoundException(proxyName);
            }
        } catch (ClassNotFoundException exception) {
            Class superClass = clazz.getSuperclass();
            if (superClass != null && !superClass.isInterface()) {
                proxyClass = getProxyClass(superClass);
            } else {
                throw new StubNotFoundException(proxyName);
            }
        }
        return proxyClass;
    }

    /**
     * Returns the route address of a URI.
     *
     * @param uri the URI
     * @return the route address of <code>uri</code>, or <code>uri</code> if it
     *         isn't routed
     */
    private URI getRoute(URI uri) {
        URI result = (URI) _routes.get(uri);
        return (result == null) ? uri : result;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?