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

📄 destinations.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-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 */

package org.exolab.jms.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTopic;


/**
 * This class provides persistency for JmsDestination objects
 * in an RDBMS database
 *
 * @version     $Revision: 1.10 $ $Date: 2003/08/07 13:33:06 $
 * @author      <a href="mailto:tima@intalio.com">Tim Anderson</a>
 * @see         org.exolab.jms.client.JmsDestination
 * @see         org.exolab.jms.persistence.RDBMSAdapter
 **/
class Destinations {

    /**
     * Cache of all destinations indexed on names
     */
    private HashMap _destinations;

    /**
     * Cache of all destinations indexed on identity
     */
    private HashMap _ids;

    /**
     * Singleton instance of this class
     */
    private static Destinations _instance;

    /**
     * This is used to synchronize the creation of the singleton
     */
    private static final Object _block = new Object();

    /**
     * This is the name of the destination id generator, which uniquely created
     * identities for destinations
     */
    private static final String DESTINATION_ID_SEED = "destinationId";

    /**
     * Constructor
     *
     * @throws PersistenceException - if constructor fails
     */
    private Destinations()
        throws PersistenceException {

        _destinations = new HashMap();
        _ids = new HashMap();
    }

    /**
     * Returns the singleton instance.
     *
     * Note that initialise() must have been invoked first for this
     * to return a valid instance.
     *
     * @return      Destinations       the singleton instance
     */
    public static Destinations instance() {
        return _instance;
    }

    /**
     * Initialise the singleton instance
     *
     * @param connection - the connection to use
     * @return Destinations - the singleton instance
     * @throws PersistenceException - if initialisation fails
     */
    public static Destinations initialise(Connection connection)
        throws PersistenceException {

        if (_instance == null) {
            synchronized (_block) {
                if (_instance == null) {
                    _instance = new Destinations();
                    _instance.load(connection);
                }
            }
        }
        return _instance;
    }

    /**
     * Add a new destination to the database. This method will also assign
     * it a unique identity.
     *
     * @param connection - the connection to use.
     * @param destination - the destination to add
     * @throws PersistenceException - if the destination cannot be added
     */
    public synchronized void add(Connection connection,
                                 JmsDestination destination)
        throws PersistenceException {

        PreparedStatement insert = null;
        try {
            long Id = SeedGenerator.instance().next(connection,
                DESTINATION_ID_SEED);
            boolean isQueue = (destination instanceof JmsQueue);

            insert = connection.prepareStatement(
                "insert into destinations values (?, ?, ?)");
            insert.setString(1, destination.getName());
            insert.setBoolean(2, isQueue);
            insert.setLong(3, Id);
            insert.executeUpdate();
            cache(destination, Id);
        } catch (Exception error) {
            throw new PersistenceException("Destinations.add failed with " +
                error.toString());
        } finally {
            SQLHelper.close(insert);
        }
    }

    /**
     * Remove a destination from the database.
     * This removes all associated consumers, and messages.
     *
     * @param connection - the connection to use
     * @param destination - the destination
     * @return boolean - <tt>true</tt> if it was removed
     * @throws PersistenceException - if the request fails
     */
    public synchronized boolean remove(Connection connection,
                                       JmsDestination destination)
        throws PersistenceException {

        boolean success = false;
        PreparedStatement deleteDestinations = null;
        PreparedStatement deleteMessages = null;
        PreparedStatement deleteConsumers = null;
        PreparedStatement deleteMessageHandles = null;

        Pair pair = (Pair) _destinations.get(destination.getName());
        if (pair != null) {
            try {
                deleteDestinations = connection.prepareStatement(
                    "delete from destinations where name=?");
                deleteDestinations.setString(1, destination.getName());

                deleteMessages = connection.prepareStatement(
                    "delete from messages where destinationId=?");
                deleteMessages.setLong(1, pair.Id);

                deleteMessageHandles = connection.prepareStatement(
                    "delete from message_handles where destinationId=?");
                deleteMessageHandles.setLong(1, pair.Id);

                deleteConsumers = connection.prepareStatement(
                    "delete from consumers where destinationId=?");
                deleteConsumers.setLong(1, pair.Id);


                deleteDestinations.executeUpdate();
                deleteMessages.executeUpdate();
                deleteMessageHandles.executeUpdate();
                deleteConsumers.executeUpdate();

                Consumers.instance().removeCached(pair.Id);
                _destinations.remove(destination.getName());
                _ids.remove(new Long(pair.Id));
                success = true;
            } catch (Exception error) {
                throw new PersistenceException("Destinations.remove failed " +
                    error.toString());
            } finally {
                SQLHelper.close(deleteDestinations);
                SQLHelper.close(deleteMessages);
                SQLHelper.close(deleteConsumers);
                SQLHelper.close(deleteMessageHandles);
            }
        }

        return success;
    }

    /**
     * Returns the destination associated with name
     *
     * @param name - the name of the destination
     * @return JmsDestination - the destination, or null
     */
    public synchronized JmsDestination get(String name) {
        Pair pair = (Pair) _destinations.get(name);
        return (pair != null) ? pair.destination : null;
    }

    /**
     * Returns the destination associated with Id
     *
     * @param id - the destination Id
     * @return JmsDestination - the destination or null
     */
    public synchronized JmsDestination get(long id) {
        Pair pair = (Pair) _ids.get(new Long(id));
        return (pair != null) ? pair.destination : null;
    }

    /**
     * Returns the Id for a given destination name
     *
     * @param name - the destination name
     * @return long - the destination Id, or 0
     */
    public synchronized long getId(String name) {
        Pair pair = (Pair) _destinations.get(name);
        return (pair != null) ? pair.Id : 0;
    }

    /**
     * Returns the list of destination names
     *
     * @return Vector - list of destination names
     */
    public synchronized Vector getNames() {
        // return a Vector for legacy reasons.
        Vector result = new Vector(_destinations.size());
        Iterator iter = _destinations.keySet().iterator();
        while (iter.hasNext()) {
            result.add((String) iter.next());
        }

        return result;
    }

    /**
     * Returns the list of destination objects
     *
     * @return Vector - list of destination objects
     */
    public synchronized Vector getDestinations() {
        // return a Vector for legacy reasons.
        Vector result = new Vector(_destinations.size());
        Iterator iter = _destinations.values().iterator();
        while (iter.hasNext()) {
            result.add(((Pair) iter.next()).destination);
        }

        return result;
    }

    /**
     * Deallocates resources owned or referenced by the instance
     */
    public synchronized void close() {
        _destinations.clear();
        _destinations = null;

        _ids.clear();
        _ids = null;

        _instance = null;
    }

    /**
     * Load all the destinations in memory. It uses the transaction service
     * and the database service to access the appropriate resources.
     *
     * @param connection - the connection to use
     * @throws PersistenceException - problems loading the destinations
     */
    private void load(Connection connection)
        throws PersistenceException {

        PreparedStatement select = null;
        ResultSet set = null;
        try {
            select = connection.prepareStatement(
                "select * from destinations");

            set = select.executeQuery();
            while (set.next()) {
                String name = set.getString("name");
                boolean isQueue = set.getBoolean("isQueue");
                JmsDestination destination = (isQueue)
                    ? (JmsDestination) new JmsQueue(name)
                    : (JmsDestination) new JmsTopic(name);
                long Id = set.getLong("destinationId");
                destination.setPersistent(true);
                cache(destination, Id);
            }
        } catch (Exception error) {
            throw new PersistenceException("Error in Destinations.load " +
                error.toString());
        } finally {
            if (select != null) {
                SQLHelper.close(select);
            }

            if (set != null) {
                SQLHelper.close(set);
            }
        }
    }

    /**
     * This method is used to cache a destination
     *
     * @param destination - the destination to cache
     * @param Id - the destination identity
     */
    private void cache(JmsDestination destination, long Id) {
        Pair pair = new Pair(destination, Id);

        _destinations.put(destination.getName(), pair);
        _ids.put(new Long(Id), pair);
    }


    /**
     * This private static class holds the name and identity of the
     * destination
     */
    private static class Pair {

        public Pair(JmsDestination destination, long Id) {
            this.destination = destination;
            this.Id = Id;
        }

        public JmsDestination destination;
        public long Id;
    }
}

⌨️ 快捷键说明

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