unicastdelegate.java

来自「一个java方面的消息订阅发送的源码」· Java 代码 · 共 185 行

JAVA
185
字号
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: UnicastDelegate.java,v 1.1 2004/11/26 01:51:05 tanderson Exp $
 */
package org.exolab.jms.net.orb;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.rmi.server.ObjID;

import org.exolab.jms.net.connector.Connection;
import org.exolab.jms.net.connector.ConnectionContext;
import org.exolab.jms.net.connector.ConnectionFactory;
import org.exolab.jms.net.connector.Request;
import org.exolab.jms.net.connector.ResourceException;
import org.exolab.jms.net.connector.Response;
import org.exolab.jms.net.proxy.Delegate;
import org.exolab.jms.net.uri.InvalidURIException;
import org.exolab.jms.net.uri.URIHelper;


/**
 * <code>UnicastDelegate</code> supports the invocation of methods on a single
 * remote object, over arbitrary transport protocols
 *
 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:05 $
 */
public class UnicastDelegate implements Delegate, Serializable {

    /**
     * The target object identifier
     */
    private ObjID _objID;

    /**
     * The connection URI
     */
    private String _uri;

    /**
     * The connection used to make invocations
     */
    private transient Connection _connection;

    /**
     * The connection factory
     */
    private transient ConnectionFactory _factory;


    /**
     * Default constructor for serialization support
     */
    protected UnicastDelegate() {
    }

    /**
     * Construct a new <code>UnicastDelegate</code>
     *
     * @param objID   the identifier of the target object
     * @param uri     the connection URI
     * @param factory the connector factory
     */
    public UnicastDelegate(ObjID objID, String uri,
                           ConnectionFactory factory) {
        _objID = objID;
        _uri = uri;
        _factory = factory;
    }

    /**
     * Construct a new <code>UnicastDelegate</code>
     *
     * @param objID      the identifier of the target object
     * @param connection the connection used to make invocations
     */
    public UnicastDelegate(ObjID objID, Connection connection) {
        _objID = objID;
        _connection = connection;
    }

    /**
     * Invoke a method
     *
     * @param method   the method to invoke
     * @param args     the arguments to pass
     * @param methodID the unique identifier for the method
     * @return the result of the invocation
     * @throws Throwable for any error
     */
    public Object invoke(Method method, Object[] args, long methodID)
            throws Throwable {
        Request request = new Request(_objID, method, args, methodID);
        Response response = getConnection().invoke(request);
        if (response.isException()) {
            throw response.getException();
        }
        return response.getObject();
    }

    /**
     * Returns the connection to perform invocations
     *
     * @return the connection to perform invocations
     * @throws InvalidURIException if the URI is invalid
     * @throws ResourceException   if a connection cannot be created
     */
    protected synchronized Connection getConnection()
            throws InvalidURIException, ResourceException {
        if (_connection == null) {
            _connection = _factory.getConnection(null, URIHelper.parse(_uri));
        }
        return _connection;
    }

    /**
     * Write this to a stream
     *
     * @param out the stream to write to
     * @throws IOException for any I/O error
     */
    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
    }

    /**
     * Read the state of this from a stream
     *
     * @param in the stream to read from
     * @throws ClassNotFoundException if a class cannot be deserialized
     * @throws IOException            for any I/O error
     */
    private void readObject(ObjectInputStream in)
            throws ClassNotFoundException, IOException {
        in.defaultReadObject();
        _factory = ConnectionContext.getConnectionFactory();
        _connection = ConnectionContext.getConnection(URIHelper.parse(_uri));
    }

}

⌨️ 快捷键说明

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