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

📄 httpjmssessionconnection.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 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: HttpJmsSessionConnection.java,v 1.14 2003/08/25 03:38:14 tanderson Exp $
 */
package org.exolab.jms.server.http;

import java.io.IOException;
import java.rmi.UnknownHostException;
import java.util.HashMap;
import java.util.Vector;

import javax.jms.JMSException;
import javax.jms.Message;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.exolab.core.http.HttpClient;
import org.exolab.core.mipc.MultiplexConnectionIfc;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.config.Configuration;
import org.exolab.jms.config.HttpConfiguration;
import org.exolab.jms.config.HttpsConfiguration;
import org.exolab.jms.server.JmsServerSession;
import org.exolab.jms.server.mipc.IpcJmsServer;
import org.exolab.jms.server.mipc.IpcJmsSessionConnection;


/**
 * This is the server side receiver for JmsSession requests. All requests are
 * unpacked and passed on to the appropriate JmsServerSession object.
 *
 * @version     $Revision: 1.14 $ $Date: 2003/08/25 03:38:14 $
 * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
 */
public class HttpJmsSessionConnection extends IpcJmsSessionConnection {

    /**
     * The list of all http client consumer connections, used to send
     * JmsMessages on.
     */
    private HashMap _consumerList = new HashMap();

    /**
     * The logger
     */
    private static final Log _log =
        LogFactory.getLog(HttpJmsSessionConnection.class);


    /**
     * Construct a new <code>HttpJmsSessionConnection</code>
     *
     * @param server the server instance
     */
    public HttpJmsSessionConnection(IpcJmsServer server) {
        super(server);
    }

    /**
     * Create a new receiver
     *
     * @param session The session the request is for.
     * @param queue The queue to create the reciver for
     * @param consumerName The unique name of this consumer,
     * only valid for persitent messages
     * @param selector The selector to filter messages. This may be null.
     * @param connection The MultiplexConnection to the machine the consumer is on
     * @param host The host the client is running on.
     * @param port The port the client is listening on.
     * @param url The url for the clients web server.
     * @return Vector The result of the request.
     *
     */
    protected Vector createReceiver(JmsServerSession session, JmsQueue queue,
                                    Long consumerId, String selector,
                                    MultiplexConnectionIfc not_used, String host, String port, String url) {
        if (session != null) {
            try {
                session.createReceiver
                    (queue, consumerId.longValue(), selector);
                addSession(session, url, host, port);
            } catch (JMSException error) {
                return pack(new Boolean(false), error);
            } catch (Exception error) {
                return pack(new Boolean(false), error.getMessage());
            }
        }

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


    /**
     * Create a new queue browser for the specified session and queue.
     *
     * @param session session that the request is for
     * @param queue queue to browse
     * @param clientId the client identity
     * @param selector message selector. May be null
     * @param connection the connection to the remote machine
     * @param host The host the client is running on.
     * @param port The port the client is listening on.
     * @param url The url for the clients web server.
     * @return      Vector              result of the request
     *
     */
    protected Vector createBrowser(JmsServerSession session,
                                   JmsQueue queue, Long clientId, String selector,
                                   MultiplexConnectionIfc not_used, String host, String port, String url) {
        Vector result = null;
        if (session == null) {
            result = pack(new Boolean(true), null);
        } else {
            try {
                session.createBrowser(queue, clientId.longValue(), selector);
                addSession(session, url, host, port);
                result = pack(new Boolean(true), null);
            } catch (JMSException error) {
                result = pack(new Boolean(false), error);
            } catch (Exception error) {
                return pack(new Boolean(false), error.getMessage());
            }
        }

        return result;
    }

    /**
     * Create a new subscriber, and connect back to the client through the
     * MultiplexConnection.
     *
     * @param session The session the request is for.
     * @param topic The topic the subscriber is subscribing on
     * @param name The unique name of this subscriber,
     * only valid for persitent messages
     * @param selector The selector to filter messages. This may be null.
     * @param connection The MultiplexConnection to the machine the consumer is on
     * @param host The host the client is running on.
     * @param port The port the client is listening on.
     * @param url The url for the clients web server.
     * @return Vector The result of the request.
     *
     */
    protected Vector createSubscriber(JmsServerSession session, JmsTopic topic,
                                      String name, Long clientId, String selector, Boolean noLocal,
                                      MultiplexConnectionIfc not_used, String host, String port, String url) {
        if (session != null) {
            try {
                session.createSubscriber(topic, name, clientId.longValue(),
                    selector, noLocal.booleanValue());
                addSession(session, url, host, port);
            } catch (JMSException error) {
                return pack(new Boolean(false), error);
                // pass JMSExceptions back to the client
            } catch (Exception error) {
                return pack(new Boolean(false), error.getMessage());
            }
        }

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

    /**
     * A close request has been received.
     *
     * @param session the session to close
     * @param connection the connection associated with the session
     * @return the result of the request.
     *
     */
    protected Vector close(JmsServerSession session,
                           MultiplexConnectionIfc connection) {
        removeSession(session);
        return super.close(session, connection);
    }

    /**
     * A disconnection has been detected for this session, by the
     * session sender, perform any neccessary cleanup, and deregister.
     */
    protected void disconnect(JmsServerSession session) {
        if (session != null) {
            removeSession(session);
            try {
                session.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Add a new session for this client.
     *
     * @param session The client session
     * @param url The client url for connecting to the servlet.
     */
    private synchronized void addSession(
        JmsServerSession session, String url, String host, String port)
        throws UnknownHostException, IOException {

        if (_log.isDebugEnabled()) {
            _log.debug("Adding session for client, webserver URL="
                + url + ", host=" + host + ", port=" + port);
        }

        HttpJmsSessionSender sender = (HttpJmsSessionSender) _consumerList.get
            (session.getSessionId());
        if (sender == null) {
            HttpClient client = new HttpClient(url, "HttpJmsSessionConnection");
            sender = new HttpJmsSessionSender(
                this, client, session, host, port);
            _consumerList.put(session.getSessionId(), sender);
            session.setMessageListener(sender);
        }
    }

    /**
     * Remove this sessions connection
     *
     * @param session The client session.
     */
    private synchronized void removeSession(JmsServerSession session) {
        HttpJmsSessionSender sender =
            (HttpJmsSessionSender) _consumerList.remove(
                session.getSessionId());
        if (sender != null) {
            sender.close();
        }
    }

} //-- HttpJmsSessionConnection

⌨️ 快捷键说明

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