defaultorb.java

来自「OpenJMS是一个开源的Java Message Service API 1.」· Java 代码 · 共 611 行 · 第 1/2 页

JAVA
611
字号
            throw new ExportException("Cannot export - no current caller");        }        return doExportTo(object, caller.getLocalURI());    }    /**     * Unexport an object.     *     * @param object the object to export     * @throws java.rmi.NoSuchObjectException if the object isn't exported     */    public synchronized void unexportObject(Object object)            throws NoSuchObjectException {        super.unexportObject(object);        if (getExported() == 0) {            // no more exported objects, so shutdown the thread pool.            // The other alternative is to reduce the keep alive time for            // unused threads however this is more likely to require            // more resources over time.            synchronized (_poolLock) {                if (_pool != null) {                    _log.debug("Shutting down thread pool");                    _pool.shutdownNow();                    _pool = null;                }            }        }    }    /**     * 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();            // @todo - closing the connection will work for now in that            // connection reference counts for OpenJMS will be correct. Won't            // support the case where a client exports an object to the server            // and then disposes its server proxies - the connection            // will be prematurely closed. The connection needs to be kept            // until the object is unexported.            connection.close();        } 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 thread pool, creating one if it doesn't exist.     *     * @return the thread pool     */    private PooledExecutor getThreadPool() {        synchronized (_poolLock) {            if (_pool == null) {                _pool = _factory.create("ORB", _maxThreads);                _pool.abortWhenBlocked();            }            return _pool;        }    }    /**     * Closes the thread pool.     */    /**     * 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 {        /**         * Perform an invocation.         *         * @param invocation the invocation         */        public void invoke(final Invocation invocation) {            Runnable invoker = new Runnable() {                public void run() {                    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);                }            };            try {                getThreadPool().execute(invoker);            } catch (Throwable exception) {                _log.debug("Pool failed to execute invocation", exception);                invocation.setResponse(new Response(exception));            }        }        /**         * 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         */        protected 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;        }    }    /**     * 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 + -
显示快捷键?