rmimanagedconnection.java

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

JAVA
441
字号
        _principal = principal;        _caller = new CallerImpl(_remoteURI, _localURI);    }    /**     * Creates a new connection handle for the underlying physical connection.     *     * @return a new connection handle     * @throws IllegalStateException if an invocation handler hasn't been     *                               registered     */    public synchronized Connection getConnection()            throws IllegalStateException {        if (_invoker == null) {            throw new IllegalStateException("No InvocationHandler registered");        }        return new RMIConnection(this);    }    /**     * Registers a handler for handling invocations.     *     * @param handler the invocation handler     * @throws IllegalStateException if a handler is already registered     */    public synchronized void setInvocationHandler(InvocationHandler handler)            throws IllegalStateException {        if (_invoker != null) {            throw new IllegalStateException(                    "An invocation handler is already registered");        }        _invoker = handler;    }    /**     * Ping the connection. The connection event listener will be notified     * if the ping succeeds.     * NOTE: the notification may occur prior to this call returning.     *     * @throws ResourceException for any error     */    public void ping() throws ResourceException {        RMIInvoker invoker;        synchronized (this) {            invoker = _remoteInvoker;        }        if (invoker != null) {            try {                invoker.ping();                ManagedConnectionListener listener                        = getConnectionEventListener();                if (listener != null) {                    listener.pinged(this);                }            } catch (RemoteException exception) {                throw new ResourceException(exception);            }        } else {            throw new IllegalStateException("Connection not established");        }    }    /**     * Destroys the physical connection.     *     * @throws ResourceException for any error     */    public void destroy() throws ResourceException {        RMIInvoker localInvoker;        RMIInvoker remoteInvoker;        synchronized (this) {            localInvoker = _localInvoker;            remoteInvoker = _remoteInvoker;        }        if (remoteInvoker != null) {            // notify peer of disconnection            try {                remoteInvoker.disconnect();            } catch (RemoteException ignore) {                // no-op            }        }        try {            if (localInvoker != null) {                if (!UnicastRemoteObject.unexportObject(localInvoker, true)) {                    _log.warn("Failed to unexport invocation handler");                }            }        } catch (RemoteException exception) {            throw new ResourceException(                    "Failed to unexport invocation handler", exception);        } finally {            synchronized (this) {                _localInvoker = null;                _remoteInvoker = null;            }        }    }    /**     * Returns the remote address to which this is connected.     *     * @return the remote address to which this is connected     */    public URI getRemoteURI() {        return _remoteURI;    }    /**     * Returns the local address that this connection is bound to.     *     * @return the local address that this connection is bound to     */    public URI getLocalURI() {        return _localURI;    }    /**     * Returns the principal associated with this connection.     *     * @return the principal associated with this connection,     *         or <code>null<code> if none is set     */    public Principal getPrincipal() {        return _principal;    }    /**     * Determines if the security principal that owns this connection is the     * same as that supplied.     *     * @param principal the principal to compare. May be <code>null</code>.     * @return <code>true</code> if the principal that owns this connection is     *         the same as <code>principal</code>     */    public boolean hasPrincipal(Principal principal) {        boolean result = false;        if ((_principal != null && _principal.equals(principal))                || (_principal == null && principal == null)) {            result = true;        }        return result;    }    /**     * Invoke a method on a remote object.     *     * @param connection the connection performing the invocation     * @param request    the request     * @return the result of the invocation     * @throws RemoteException if the distributed call cannot be made     */    protected Response invoke(Connection connection, Request request)            throws RemoteException {        Response response;        try {            MarshalledObject wrappedRequest = new MarshalledObject(request);            MarshalledObject wrappedResponse =                    _remoteInvoker.invoke(wrappedRequest);            response = (Response) wrappedResponse.get();        } catch (ClassNotFoundException exception) {            response = new Response(exception);        } catch (IOException exception) {            response = new Response(exception);        }        return response;    }    /**     * Invoke a method on a local object.     *     * @param request the wrapped <code>Request</code>     * @return the wrapped <code>Response</code>     * @throws MarshalException if the response can't be marshalled     */    protected MarshalledObject invokeLocal(MarshalledObject request)            throws MarshalException {        MarshalledInvocation invocation                = new MarshalledInvocation(request, _caller);        _invoker.invoke(invocation);        MarshalledObject response;        try {            response = invocation.getMarshalledResponse();        } catch (Exception exception) {            throw new MarshalException("Failed to marshal response",                                       exception);        }        return response;    }    /**     * Invoked when the remote peer disconnects.     */    protected void disconnect() {        synchronized (this) {            _remoteInvoker = null;        }        notifyClosed();    }    /**     * Helper to generate a URI for a client RMIManagedConnection instance.     *     * @return a URI that uniquely identifies a client RMIManagedConnection     * @throws ResourceException if the URI cannot be generated     */    private URI generateLocalURI() throws ResourceException {        URI result;        String path = UUIDGenerator.create();        try {            result = URIHelper.create("rmi", null, -1, path);        } catch (InvalidURIException exception) {            throw new ResourceException("Failed to generate local URI",                                        exception);        }        return result;    }}

⌨️ 快捷键说明

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