defaultorb.java

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

JAVA
499
字号
        }
        return doExportTo(object, caller.getLocalURI());
    }

    /**
     * Returns the current caller.
     *
     * @return the current caller, or <code>null</code> if no call is in
     *         progress
     * @throws RemoteException for any error
     */
    public Caller getCaller() throws RemoteException {
        return (Caller) _caller.get();
    }

    /**
     * Register a caller event listener.
     *
     * @param uri      the remote URI to listen on
     * @param listener the listener to notify
     * @throws InvalidURIException if <code>uri</code> is invalid
     */
    public void addCallerListener(String uri, CallerListener listener)
            throws InvalidURIException {
        synchronized (this) {
            if (_listeners == null) {
                _listeners = new MulticastCallerListener();
                _manager.setCallerListener(_listeners);
            }
        }
        _listeners.addCallerListener(uri, listener);
    }

    /**
     * Deregister a caller event listener.
     *
     * @param uri      the remote URI the listener is listening for events on
     * @param listener the listener to remove
     * @throws InvalidURIException if <code>uri</code> is invalid
     */
    public void removeCallerListener(String uri, CallerListener listener)
        throws InvalidURIException {
        MulticastCallerListener listeners = null;
        synchronized (this) {
            listeners = _listeners;
        }
        if (listeners != null) {
            listeners.removeCallerListener(uri, listener);
        }
    }

    /**
     * Shuts down the ORB.
     *
     * @throws RemoteException for any error
     */
    public void shutdown() throws RemoteException {
        try {
            _manager.close();
        } catch (ResourceException exception) {
            throw new RemoteException("Failed to close connection manager",
                                      exception);
        }
        // super.close(); @todo
    }

    /**
     * Creates a new connection manager.
     *
     * @param handler       the invocation handler
     * @param authenticator the connection authenticator
     * @return a new connection manager
     * @throws ResourceException for any error
     */
    protected AbstractConnectionManager createConnectionManager(
            InvocationHandler handler, Authenticator authenticator)
        throws ResourceException {
        return new DefaultConnectionManager(handler, authenticator,
                                            getProperties());
    }

    /**
     * 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 URI connect(URI uri, String principal, String credentials)
            throws ExportException {
        URI result;
        try {
            Principal subject = null;
            if (principal != null) {
                subject = new BasicPrincipal(principal, credentials);
            }
            Connection connection = _manager.getConnection(subject, uri);
            result = connection.getLocalURI();
        } catch (ResourceException exception) {
            throw new ExportException("Failed to connect to URI: " + uri,
                                      exception);
        }
        return result;
    }

    /**
     * Accept connections on the specified URI.
     *
     * @param uri the URI to accept connections on
     * @throws ExportException for any error
     */
    protected void accept(URI uri) throws ExportException {
        try {
            _manager.accept(uri, getProperties());
        } catch (ResourceException exception) {
            throw new ExportException("Failed to accept connections on URI: "
                                      + uri, exception);
        }
    }

    /**
     * Returns the method corresponding to the supplied object and method
     * identifier.
     *
     * @param object   the object to locate the method for
     * @param methodID the method identifier
     * @return the method
     * @throws NoSuchMethodException if a corresponding method cannot be found
     */
    private Method getMethod(Object object, long methodID)
            throws NoSuchMethodException {

        Method result = null;
        Method[] methods = MethodHelper.getAllInterfaceMethods(
                object.getClass());
        for (int i = 0; i < methods.length; ++i) {
            Method method = methods[i];
            if (MethodHelper.getMethodID(method) == methodID) {
                result = method;
                break;
            }
        }
        if (result == null) {
            throw new NoSuchMethodException(
                    "Failed to resolve method for methodID=" + methodID);
        }
        return result;
    }

    /**
     * Invocation handler, that delegates invocations to objects managed by the
     * DefaultORB.
     */
    private class Handler implements InvocationHandler {

        /**
         * Prepare for an invocation.
         */
        public void prepare() {
            // no-op
        }

        /**
         * Handle a method invocation and return the result.
         *
         * @param request the request
         * @param caller  the caller performing the invocation
         * @return the result of the invocation
         */
        public Response invoke(Request request, Caller caller) {
            Response response;
            try {
                Object object = getObject(request.getObjID(),
                                          request.getURI());
                Method method = request.getMethod();
                if (method == null) {
                    // resolve the method using its id
                    method = getMethod(object, request.getMethodID());
                }
                Object[] args = request.getArgs();
                if (args == null) {
                    // deserialize the arguments
                    args = request.readArgs(method);
                }
                if (_log.isDebugEnabled()) {
                    _log.debug("Invoking " + method + " on " + object);
                }
                _caller.set(caller);
                Object result = method.invoke(object, args);
                response = new Response(result, method);
            } catch (InvocationTargetException exception) {
                Throwable target = exception.getTargetException();
                if (target == null) {
                    target = exception;
                }
                response = new Response(target);
            } catch (Throwable exception) {
                response = new Response(exception);
            } finally {
                _caller.set(null);
            }
            return response;
        }

        /**
         * Perform an invocation.
         *
         * @param invocation the invocation
         */
        public void invoke(Invocation invocation) {
            Response response;
            try {
                Request request = invocation.getRequest();
                Caller caller = invocation.getCaller();
                response = invoke(request, caller);
            } catch (Throwable exception) {
                response = new Response(exception);
            }

            invocation.setResponse(response);
        }

    }

    /**
     * Dummy connection authenticator, which simply flags all principals as
     * authenticated.
     */
    private static class DummyAuthenticator implements Authenticator {

        /**
         * Determines if a principal has permissions to connect
         *
         * @param principal the principal to check
         * @return <code>true</code> if the principal has permissions to
         *         connect
         * @throws ResourceException if an error occurs
         */
        public boolean authenticate(Principal principal)
                throws ResourceException {
            return true;
        }
    }

}

⌨️ 快捷键说明

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