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

📄 offlineconnection.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 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: OfflineConnection.java,v 1.11 2003/08/17 01:32:23 tanderson Exp $
 *
 * Date         Author  Changes
 * $Date	    jimm    Created
 */


package org.exolab.jms.jndiadministration;

import java.awt.Component;
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 org.exolab.jms.config.Configuration;
import org.exolab.jms.config.ConfigurationManager;
import org.exolab.jms.config.DatabaseConfiguration;
import org.exolab.jms.persistence.DatabaseService;


/**
 * Use the IntraVM Jndi Context.
 *
 * @version     $Revision: 1.11 $ $Date: 2003/08/17 01:32:23 $
 * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
 * @see		AdminConnection
 * @see		AdminMgr
 */
public class OfflineConnection extends AdminConnection {

    // The parent Gui
    private Component parent_;

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

    /**
     * Connect to the intravm Jndi Server.
     *
     * @param parent The component parent.
     * @exception OfflineConnectionException When the database cannot be opened
     *
     */
    public OfflineConnection(Component parent)
        throws OfflineConnectionException {
        try {
            if (instance_ == null) {
                Configuration config = ConfigurationManager.getConfig();
                parent_ = parent;
                DatabaseConfiguration dbconfig =
                    config.getDatabaseConfiguration();

                if (dbconfig.getRdbmsDatabaseConfiguration() != null) {
                    DatabaseService.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.getAdapter();
                        instance_ = this;
                    }
                }

                if (instance_ != null) {
                    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);
                    }
                }
            } else {
                throw new OfflineConnectionException("Already connected");
            }
        } catch (Exception err) {
            throw new OfflineConnectionException
                ("Database Error: " + err.getMessage());
        }
    }


    /**
     * Close the database connection.
     *
     */
    public void close() {
        try {
            context_.close();
            DatabaseService.getAdapter().close();
            instance_ = null;
            context_ = null;
        } catch (javax.naming.NamingException err) {
            displayError(err, "Failed to close context");
        }
    }


    /**
     * Get an enumerated list of all the Contexts
     *
     * @return 	Enumeration The list of Contexts
     *
     */
    public Enumeration getAllContexts(String name) {
        try {
            if (name == null) {
                name = "";
            }

            return context_.list(name);
        } catch (Exception err) {
            System.err.println("Err in getAllContexts\n" + err);
            return null;
        }
    }


    /**
     * Return the object associated with this context.
     * if none exists return null.
     *
     * @param context The context name
     * @return Object The object for this context.
     *
     */
    public Object lookup(String context) {
        try {
            return context_.lookup(context);
        } catch (Exception err) {
            System.err.println("Failed to get Context " + context + "\n" +
                err);
            return null;
        }
    }


    /**
     * Create a new context with the given name.
     *
     * @param name The new context name.
     * @exception NamingException If the context cannot be created.
     *
     */
    public void createContext(String name) throws javax.naming.NamingException {
        context_.createSubcontext(name);
    }


    /**
     * Destroy context with the given name.
     *
     * @param name The new context name.
     * @exception NamingException If the context cannot be created.
     *
     */
    public void destroyContext(String name) throws javax.naming.NamingException {
        context_.unbind(name);
    }


    /**
     * Rename context with the given name.
     *
     * @param oldName The old context name
     * @param newName The new context name
     * @exception NamingException If the context cannot be created.
     *
     */
    public void renameContext(String oldName, String newName)
        throws javax.naming.NamingException {
        context_.rename(oldName, newName);
    }


    /**
     * Rebind the context with the given object.
     *
     * @param name The context name
     * @param ob The object to bind in this context
     * @exception NamingException If the context cannot be created.
     *
     */
    public void rebind(String name, Object ob)
        throws javax.naming.NamingException {
        context_.rebind(name, ob);
    }


    /**
     * Display the error in a JOptionPane.
     *
     * @param err The Error to display.
     * @param st The string to use as a title on the JOptionPane.
     *
     */
    private void displayError(Exception err, String st) {
        JOptionPane.showMessageDialog
            (parent_, st + "\n" + err, st, JOptionPane.ERROR_MESSAGE);
    }


} // End OfflineConnection

⌨️ 快捷键说明

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