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

📄 offlineconnection.java

📁 一个java方面的消息订阅发送的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 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.
 *
 * $Id: OfflineConnection.java,v 1.1 2004/11/26 01:51:15 tanderson Exp $
 *
 * Date         Author  Changes
 * $Date	    jimm    Created
 */


package org.exolab.jms.tools.admin;

import java.awt.Component;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.transaction.TransactionManager;

import org.exolab.jms.authentication.User;
import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.config.Configuration;
import org.exolab.jms.config.ConfigurationManager;
import org.exolab.jms.config.DatabaseConfiguration;
import org.exolab.jms.persistence.DatabaseService;
import org.exolab.jms.persistence.PersistenceException;


/**
 * Connect directly to the Persistent store to retrieve information and perfrom
 * updates.
 *
 * <P> Note: If the OpenJMSServer is active, this connection will fail, since
 * it requires and exclusive lock on the database to avoid database corruption.
 * Similarly, if this connection is active the OpenJMSServer cannot be started
 * for the same reasons.
 *
 * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:51:15 $
 * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
 * @see		AbstractAdminConnection
 * @see		AdminMgr
 */
public class OfflineConnection extends AbstractAdminConnection {

    // The parent Gui
    private Component _parent;

    // The deafult JNDI context.
    private Context _context = null;

    /**
     * Connect to the RMIAdmin Server if in online mode, or open
     * the database and update the data directly in offline mode.
     *
     * @param online Indicates if we are connecting in online or offline mode.
     * @param parent The component parent.
     * @throws IllegalStateException if {@link ConfigurationManager} has not
     * been initialised
     * @throws OfflineConnectionException When the database cannot be opened
     */
    public OfflineConnection(Component parent)
        throws OfflineConnectionException {
        try {
            if (_instance == null) {
                _parent = parent;

                Configuration config = ConfigurationManager.getConfig();

                DatabaseConfiguration dbconfig =
                    config.getDatabaseConfiguration();

                // determine the database type and instantiate the appropriate
                // database adapter
                if (dbconfig.getRdbmsDatabaseConfiguration() != null) {
                    DatabaseService.instance().getAdapter();
                    _instance = this;
                } else {
                    JFileChooser chooser = new JFileChooser(".");
                    chooser.setDialogTitle
                        ("Select OpenJMS Database to connect to");
                    chooser.setFileFilter(new DatabaseFilter());
                    int returnVal = chooser.showOpenDialog(parent);

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        DatabaseService.instance().getAdapter();
                        _instance = this;
                    }
                }
            } else {
                throw new org.exolab.jms.tools.admin.OfflineConnectionException("Already connected");
            }
        } catch (Exception err) {
            throw new org.exolab.jms.tools.admin.OfflineConnectionException
                ("Database Error: " + err.getMessage());
        }
    }

    // implementation of AbstractAdminConnection.close
    public void close() {
        DatabaseService.getAdapter().close();
        _instance = null;
    }

    // implementation of AbstractAdminConnection.addDurableConsumer
    public boolean addDurableConsumer(String topic, String name) {
        boolean result = false;
        Connection connection = null;

        try {
            connection = DatabaseService.getConnection();
            DatabaseService.getAdapter().addDurableConsumer(connection, topic,
                name);
            connection.commit();
            result = true;

        } catch (PersistenceException exception) {
            try {
                connection.rollback();
            } catch (Exception nested) {
                // ignore
            }
        } catch (Exception exception) {
            // ignore for the moment
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception nested) {
                    // ignore
                }
            }
        }

        return result;
    }

    // implementation of AbstractAdminConnection.removeDurableConsumer
    public boolean removeDurableConsumer(String name) {
        boolean result = false;
        Connection connection = null;

        try {
            connection = DatabaseService.getConnection();

            DatabaseService.getAdapter().removeDurableConsumer(connection, name);
            connection.commit();
            result = true;

        } catch (PersistenceException exception) {
            try {
                connection.rollback();
            } catch (Exception nested) {
                // ignore
            }
        } catch (Exception exception) {
            // ignore for the moment
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception nested) {
                    // ignore
                }
            }
        }

        return result;
    }

    // implementation of AbstractAdminConnection.unregisterConsumer
    public boolean unregisterConsumer(String name) {
        return false;
    }

    // implementation of AbstractAdminConnection.isConnected
    public boolean isConnected(String name) {
        return false;
    }

    // implementation of AbstractAdminConnection.getAllDestinations
    public Enumeration getAllDestinations() {
        Enumeration result = null;
        Connection connection = null;

        try {
            connection = DatabaseService.getConnection();

            result = DatabaseService.getAdapter().getAllDestinations(
                connection);
            connection.commit();
        } catch (PersistenceException exception) {
            try {
                connection.rollback();
            } catch (Exception nested) {
                // ignore
            }
        } catch (Exception exception) {
            // ignore for the moment
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception nested) {
                    // ignore
                }
            }
        }

        return result;
    }

    // implementation of AbstractAdminConnection.addDestination
    public boolean addDestination(String destination, boolean isQueue) {
        Connection connection = null;
        boolean success = false;

        try {
            connection = DatabaseService.getConnection();

            DatabaseService.getAdapter().addDestination(connection,
                destination, isQueue);
            if (_context == null) {
                // connect to the JNDI server and get a reference to
                // root context
                Hashtable props = new Hashtable();
                props.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.exolab.jms.jndi.intravm.IntravmJndiServer");
                _context = new InitialContext(props);
            }

            Object ob = (isQueue ? (Object) (new JmsQueue(destination)) :
                (Object) (new JmsTopic(destination)));

            if (ob instanceof JmsQueue) {
                ((JmsDestination) ob).setPersistent(true);
            }

            _context.rebind(destination, ob);
            connection.commit();
            success = true;
        } catch (PersistenceException exception) {
            System.err.println("Failed to add destination " + destination +
                " b/c " + exception.toString());
            try {
                connection.rollback();
            } catch (Exception nested) {
                // ignore
            }
        } catch (javax.naming.NamingException err) {
            System.err.println("Failed to add " + destination +
                " in JNDI context");
            try {
                connection.rollback();
            } catch (Exception nested) {
                // ignore
            }
        } catch (Exception exception) {
            // ignore for the moment
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception nested) {
                    // ignore
                }
            }
        }

        return success;
    }

    // implementation of AbstractAdminConnection.getDurableConsumerMessageCount
    public int getDurableConsumerMessageCount(String topic, String name) {
        int count = -1;
        Connection connection = null;

        try {
            connection = DatabaseService.getConnection();

            count = DatabaseService.getAdapter().getDurableConsumerMessageCount(
                connection, topic, name);
            connection.commit();
        } catch (PersistenceException exception) {

⌨️ 快捷键说明

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