⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmsconnectionfactory.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
字号:
/**
 * 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 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: JmsConnectionFactory.java,v 1.19 2003/08/07 13:32:49 tanderson Exp $
 *
 * Date         Author  Changes
 * 3/21/2000    jima    Created
 */
package org.exolab.jms.client;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;


/**
 * This is simply a marker for connection factory classes.
 *
 * @version     $Revision: 1.19 $ $Date: 2003/08/07 13:32:49 $
 * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
 * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @see         org.exolab.jms.client.JmsQueueConnectionFactory
 * @see         org.exolab.jms.client.JmsTopicConnectionFactory
 */
public abstract class JmsConnectionFactory
    implements ConnectionFactory, ExceptionListener, Externalizable,
    Referenceable {

    /**
     * Used for serialization
     */
    static final long serialVersionUID = 1;

    /**
     * This variable maintains the name of the class that will facilitate
     * communication with the JMS Server. This class must have a default
     * constructor.
     */
    protected String _className = null;

    /**
     * This is a list of environment variabels that are used to construct
     * the object
     */
    protected Hashtable _env = null;

    /**
     * The connection_ attribute is used to manage the list of connections
     * created by the factory.
     */
    transient private Vector _connections = new Vector();

    /**
     * This is a transient attribute that holds a reference to the JMS Server.
     * Requests to the server are handled through this proxy.
     */
    transient private JmsServerStubIfc _proxy = null;


    /**
     * Need a default constructor for externalization
     */
    public JmsConnectionFactory() {
    }

    /**
     * Instantiate an instance of the connection factory with the name of the
     * class that will facilitate communication with the JmsServer
     *
     * @param       name            name of the class
     * @param       envc            properties for contructing
     */
    protected JmsConnectionFactory(String name, Hashtable env) {
        _className = name;
        _env = env;
    }

    /**
     * Return a reference to the server proxy. If the proxy has not yet
     * been created then create it prior to returning.
     *
     * @return a reference to the server proxy
     * @throws JMSException if the proxy cannot be created
     */
    public JmsServerStubIfc getProxy() throws JMSException {
        if (_proxy == null) {
            try {
                Class[] argTypes = {Hashtable.class};
                Object[] args = {_env};

                Class factoryClass = Class.forName(_className);
                Constructor constructor =
                    factoryClass.getDeclaredConstructor(argTypes);
                _proxy = (JmsServerStubIfc) constructor.newInstance(args);
                _proxy.setExceptionListener(this);
            } catch (InvocationTargetException exception) {
                if (exception.getTargetException() != null) {
                    throw new JMSException("Failed to create proxy: " +
                        exception.getTargetException());
                } else {
                    throw new JMSException("Failed to create proxy: " +
                        exception);
                }
            } catch (Exception exception) {
                throw new JMSException("Failed to create proxy: " +
                    exception);
            }
        }

        return _proxy;
    }

    /**
     * Add the specified connection to the list of managed sessions
     *
     * @param       connection          connection to register
     */
    protected void addConnection(JmsConnection connection) {
        _connections.addElement(connection);
    }

    /**
     * Remove the specified connection from the list of managed connections..
     * If it doesn't exist then fail silently
     *
     * @param       connection          connection to remove
     */
    protected void removeConnection(JmsConnection connection) {
        _connections.removeElement(connection);
    }

    /**
     * Test whether the specified connection is managed by this factory
     *
     * @param       connection      connection to test against
     * @return      boolean         true if managed
     */
    protected boolean isManaged(JmsConnection connection) {
        return _connections.contains(connection);
    }

    /**
     * Return an enumeration of all connections managed by this factory
     *
     * @return      Enumeration
     */
    protected Enumeration getConnections() {
        return _connections.elements();
    }

    // implementation of Externalizable.writeExternal
    public void writeExternal(ObjectOutput stream) throws IOException {
        stream.writeLong(serialVersionUID);
        stream.writeObject(_className);
        stream.writeObject(_env);
    }

    // implementation of Externalizable.readExternal
    public void readExternal(ObjectInput stream)
        throws IOException, ClassNotFoundException {
        long version = stream.readLong();
        if (version == serialVersionUID) {
            _className = (String) stream.readObject();
            _env = (Hashtable) stream.readObject();
        } else {
            throw new IOException("JmsConnectionFactory with version " +
                version + " is not supported.");
        }
    }

    // implementation of ExceptionListener.onException
    public void onException(JMSException exception) {
        // iterate through the list of connection and call
        // notifyExceptionListener
        Enumeration iter = _connections.elements();
        while (iter.hasMoreElements()) {
            JmsConnection connection = (JmsConnection) iter.nextElement();
            connection.notifyExceptionListener(exception);
        }

        // local clean up
        _connections.clear();
        _proxy = null;
    }

    // implementation of Referenceable.getReference
    public Reference getReference() {
        Reference reference = new Reference(
            this.getClass().getName(),
            new StringRefAddr("serverClass", _className),
            JmsConnectionFactoryBuilder.class.getName(), null);

        // all properties are strings so add them to the reference
        Enumeration iter = _env.keys();
        while (iter.hasMoreElements()) {
            String key = (String) iter.nextElement();
            reference.add(new StringRefAddr(key, (String) _env.get(key)));
        }

        return reference;
    }

} //-- JmsConnectionFactory

⌨️ 快捷键说明

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