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

📄 ipcjmsserverconnection.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.
 *
 * $Id: IpcJmsServerConnection.java,v 1.8 2003/08/17 01:32:26 tanderson Exp $
 *
 * Date         Author  Changes
 * $Date	    jimm    Created
 */
package org.exolab.jms.server.mipc;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.Vector;

import javax.jms.JMSException;

import org.exolab.core.ipc.NotifierIfc;
import org.exolab.jms.server.JmsServerConnection;
import org.exolab.jms.server.JmsServerConnectionManager;
import org.exolab.jms.server.JmsServerSession;


/**
 * This class is responsible for interpreting server connection requests and
 * delegating them to the server. And passing back any necessary replies.
 *
 * @version     $Revision: 1.8 $ $Date: 2003/08/17 01:32:26 $
 * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
 * @see	        org.exolab.jms.server.mipc.IpcJmsReceiver
 * @see	        org.exolab.jms.server.JmsServerConnection
 * @see	        org.exolab.jms.server.JmsServerConnectionManager
 * @see	        org.exolab.core.ipc.NotifierIfc
 */
public class IpcJmsServerConnection implements NotifierIfc {

    /**
     * A hashtable of Vectors. Each node containing the list of Connection Id's
     * for a particular IPC connection.
     */
    private static Hashtable _connections = new Hashtable();

    /**
     * Default constructor
     */
    public IpcJmsServerConnection() {
    }

    /**
     * A new request has been received.
     * Carry out the request, and pass back any relevent data.
     *
     * @param ob The data received,
     * @return Object Return any requested result. This must never be null.
     *
     */
    public Serializable notify(Object ob, String id) {
        Vector v = (Vector) ob;
        String func = (String) v.get(1);
        Serializable result = null;

        if (func.equals("createConnection")) {
            // Use the unique Ipc id for every new connection.
            result = createConnection(id, (String) v.get(2), (String) v.get(3),
                (String) v.get(4));
        } else if (func.equals("createSession")) {
            result = createSession(id, (String) v.get(2), (String) v.get(3),
                (Integer) v.get(4), (Boolean) v.get(5));
        } else if (func.equals("close")) {
            result = close(id, (String) v.get(2), (String) v.get(3));
        } else {
            String st = ("Unknown request received: " + func);
            System.err.println(st);
            result = pack(new Boolean(false), st);
        }

        return result;
    }


    /**
     * The connection has been broken.
     *
     * @param The unique IPC identifier of this connection.
     *
     */
    public void disconnection(String id) {
        Vector v;
        synchronized (_connections) {
            v = (Vector) _connections.remove(id);
        }

        if (v != null) {
            Object[] ob = v.toArray();
            JmsServerConnection con = null;

            for (int i = 0, j = v.size(); i < j; i++) {
                con = JmsServerConnectionManager.instance().getConnection
                    ((String) ob[i]);
                if (con != null) {
                    con.close();
                }
            }
            // help clean up.
            v.clear();
            v = null;
        }
    }

    /**
     * Add all the client id's for this IPC connection into the connections
     * list. Use the IPC id as the key, so all client connections from
     * a single physical client are on the same key.
     *
     * @param ipc The IPC client id if this connection
     * @param id The unique clientId of this client
     */
    private void addConnection(String ipc, String id) {
        synchronized (_connections) {
            Vector v = (Vector) _connections.get(ipc);

            if (v == null) {
                v = new Vector();
                _connections.put(ipc, v);
            }
            v.add(id);
        }
    }

    /**
     * A new connection request has been made.
     * Pass on the request to the JmsServerConnectionMgr. If the request
     * succeeds return the connectionId to the client.
     *
     * @param ipcId The unique IPC id of this connection.
     * @param clientId The unique id of the client.
     * @param username the client's username
     * @param password the client's password
     * @return Vector The result of the request.
     */
    private Vector createConnection(String ipcId,
                                    String clientId, String username,
                                    String password) {
        Vector result;
        try {
            JmsServerConnection connection =
                JmsServerConnectionManager.instance().createConnection(
                    clientId, username, password);

            addConnection(ipcId, clientId);
            result = pack(new Boolean(true), connection.getConnectionId());
        } catch (JMSException exception) {
            result = pack(new Boolean(false), exception);
        }
        return result;
    }

    /**
     * A create session request has been received. Get the JmsServerConnection
     * object from the manager and create a new session. If the request
     * succeeds return the unique session id of the session, and the flow
     * control notification limit.
     *
     * @param ipcId The unique IPC id of this connection.
     * @param clientId The unique id of the client.
     * @param connectionId The unique connection id of the client.
     * @return Vector The result of the request.
     *
     */
    private Vector createSession(String ipcId, String clientId,
                                 String connectionId, Integer ackMode,
                                 Boolean transacted) {
        Vector result = null;

        JmsServerConnection connection =
            JmsServerConnectionManager.instance().getConnection(clientId);
        JmsServerSession session = null;

        if (connection != null) {
            session = connection.createSession(ackMode.intValue(),
                transacted.booleanValue());
        }
        if (session != null) {
            Vector state = new Vector(2);
            state.add(session.getSessionId());
            result = pack(new Boolean(true), state);
        } else {
            result = pack(new Boolean(false),
                "Failed to create session");
        }
        return result;
    }

    /**
     * A close connection request has been received. Get the
     * JmsServerConnection object from the manager and close the connection.
     *
     * @param ipcId The unique IPC id of this connection.
     * @param clientId The unique id of the client.
     * @param connectionId The unique connection id of the client.
     * @return Vector The result of the request.
     */
    private Vector close(String ipcId, String clientId, String connectionId) {
        synchronized (_connections) {
            Vector connections = (Vector) _connections.get(ipcId);
            if (connections != null) {
                connections.remove(clientId);
                if (connections.isEmpty()) {
                    _connections.remove(ipcId);
                }
            }
        }
        JmsServerConnectionManager.instance().closeConnection(clientId);

        return pack(new Boolean(true), null);
    }

    /**
     * Pack all the data that is required by the server in a vector.
     * Set the size of the vector to be exactly the right size for efficiency.
     *
     * @param success Boolean indicating success or failure of request.
     * @param ob The Object being returned.
     * @return Vector The vector containing all the data.
     *
     */
    private Vector pack(Boolean success, Object ob) {
        Vector v = new Vector(2);
        v.add(success);
        v.add(ob);
        return v;
    }

} // End IpcJmsServerConnection

⌨️ 快捷键说明

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