abstractorb.java

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

JAVA
569
字号
/**
 * 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-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: AbstractORB.java,v 1.7 2005/06/04 14:46:00 tanderson Exp $
 */
package org.exolab.jms.net.orb;

import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;
import java.rmi.StubNotFoundException;
import java.rmi.server.ExportException;
import java.rmi.server.ObjID;
import java.util.HashMap;
import java.util.Map;

import org.exolab.jms.net.proxy.Proxy;
import org.exolab.jms.net.uri.InvalidURIException;
import org.exolab.jms.net.uri.URI;
import org.exolab.jms.net.uri.URIHelper;


/**
 * Abstract implementation of the {@link ORB} interface.
 *
 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @version $Revision: 1.7 $ $Date: 2005/06/04 14:46:00 $
 */
public abstract class AbstractORB implements ORB {

    /**
     * A map of ObjID -> ObjectRef instances.
     */
    private HashMap _objIDMap = new HashMap();

    /**
     * A map of Object -> ObjectRef instances.
     */
    private HashMap _objectMap = new HashMap();

    /**
     * Configuration properties.
     */
    private final Map _properties;

    /**
     * The default URI for exported objects.
     */
    private final String _defaultURI;

    /**
     * A map of routes for exported objects. The key is a URI representing the
     * URI the objects are listening on. The value is the URI of the router.
     */
    private HashMap _routes = new HashMap();

    /**
     * The class loader used to load proxies.
     */
    private ClassLoader _loader;


    /**
     * Construct a new <code>AbstractORB</code>.
     *
     * @param loader the class loader to load proxies
     * @param properties properties to configure this with. May be
     * <code>null</code>.
     */
    public AbstractORB(ClassLoader loader, Map properties) {
        if (loader == null) {
            throw new IllegalArgumentException("Argument 'loader' is null");
        }
        _loader = loader;
        if (properties != null) {
            _properties = properties;
            _defaultURI = (String) properties.get(PROVIDER_URI);
        } else {
            _properties = new HashMap();
            _defaultURI = null;
        }
    }

    /**
     * Add a route for exported objects.
     *
     * @param uri   the URI to route
     * @param toURI the URI to route to
     * @throws RemoteException for any error
     */
    public synchronized void addRoute(String uri, String toURI)
            throws RemoteException {
        if (uri == null) {
            throw new IllegalArgumentException("Argument 'uri' is null");
        }
        if (toURI == null) {
            throw new IllegalArgumentException("Argument 'toURI' is null");
        }
        _routes.put(URIHelper.parse(uri), URIHelper.parse(toURI));
    }

    /**
     * Returns the proxy associated with the specified object, and URI.
     *
     * @param object the object to look up the proxy for
     * @param uri    the URI the object was exported on
     * @return the proxy corresponding to <code>object</code> and
     *         <code>uri</code>
     * @throws NoSuchObjectException if the object hasn't been exported on the
     *                               specified URI
     */
    public synchronized Proxy getProxy(Object object, String uri)
            throws NoSuchObjectException {

//         if (uri == null) {
//             throw new IllegalArgumentException("Argument 'uri' is null");
//         }
        ObjectRef ref = (ObjectRef) _objectMap.get(object);
        if (ref == null) {
            throw new NoSuchObjectException("Object not exported");
        }
        URI parsed = null;
        if (uri != null) {
            try {
                parsed = URIHelper.parse(uri);
            } catch (InvalidURIException exception) {
                throw new NoSuchObjectException(exception.getMessage());
            }
        }
        return ref.getProxy(parsed);
    }

    /**
     * Returns the object associated with the specified ID, and URI.
     *
     * @param objID the identifier of the object
     * @param uri   the URI the object was exported on
     * @return the object corresponding to <code>objID</code> and
     *         <code>uri</code>
     * @throws NoSuchObjectException if the object hasn't been exported on the
     *                               specified URI
     */
    public synchronized Object getObject(ObjID objID, String uri)
            throws NoSuchObjectException {

//         if (uri == null) {
//             throw new IllegalArgumentException("Argument 'uri' is null");
//         }
        ObjectRef ref = (ObjectRef) _objIDMap.get(objID);
        if (ref == null) {
            throw new NoSuchObjectException("Object not exported");
        }
        // ref.getProxy(uri);
        // ensures it has been exported on the specified uri
        return ref.getObject();
    }

    /**
     * Export an object on a default URI.
     *
     * @param object the object to export
     * @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 exportObject(Object object)
            throws ExportException, StubNotFoundException {
        return exportObject(object, _defaultURI);
    }

    /**
     * Export an object on a specific URI.
     *
     * @param object the object to export
     * @param uri    the URI via 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 synchronized Proxy exportObject(Object object, String uri)
            throws ExportException, StubNotFoundException {

        if (object == null) {
            throw new IllegalArgumentException("Argument 'object' is null");
        }
        if (uri == null) {
            throw new IllegalArgumentException("Argument 'uri' is null");
        }

        URI parsed = null;
        try {
            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 {
            ObjID objID = new ObjID();
            proxy = doExport(object, objID, parsed, getProxyClass(object));
        }

        return proxy;
    }

    /**
     * Export an object with a well known identifier on a default URI.
     *
     * @param object the object to export
     * @param objID  the well known object identifier
     * @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 exportObject(Object object, ObjID objID)
            throws ExportException, StubNotFoundException {
        return exportObject(object, objID, _defaultURI);
    }

    /**
     * Export an object with a well known identifier on a specific URI.
     *
     * @param object the object to export
     * @param objID  the well known object identifier
     * @param uri    the URI via 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 synchronized Proxy exportObject(Object object, ObjID objID,
                                           String uri)
            throws ExportException, StubNotFoundException {

        if (object == null) {
            throw new IllegalArgumentException("Argument 'object' is null");
        }
        if (objID == null) {
            throw new IllegalArgumentException("Argument 'objID' is null");
        }
        if (uri == null) {
            throw new IllegalArgumentException("Argument 'uri' is null");
        }
        URI parsed = null;
        try {

⌨️ 快捷键说明

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