📄 ipcjmsserver.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: IpcJmsServer.java,v 1.18 2003/08/07 13:33:09 tanderson Exp $
*
* Date Author Changes
* $Date jimm Created
*/
package org.exolab.jms.server.mipc;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import org.exolab.core.ipc.NotifierIfc;
import org.exolab.core.mipc.ConnectionNotifierIfc;
import org.exolab.core.mipc.MultiplexConnectionIfc;
import org.exolab.core.mipc.MultiplexConnectionServer;
import org.exolab.core.mipc.MultiplexConnectionServerIfc;
import org.exolab.jms.client.mipc.IpcJmsConstants;
import org.exolab.jms.config.Configuration;
import org.exolab.jms.config.ConfigurationManager;
import org.exolab.jms.config.Connector;
import org.exolab.jms.config.ConnectorHelper;
import org.exolab.jms.config.ConnectorResource;
import org.exolab.jms.config.ServerConfiguration;
import org.exolab.jms.config.TcpConfigurationType;
import org.exolab.jms.config.types.SchemeType;
import org.exolab.jms.server.ConnectionFactoryHelper;
import org.exolab.jms.server.JmsServerIfc;
import org.exolab.jms.server.JmsServerSession;
import org.exolab.jms.server.ServerException;
import org.exolab.jms.server.http.HttpJmsSessionConnection;
/**
* This class implements the JmsServerIfc and provides an IPC-based JMS Server.
*
* @version $Revision: 1.18 $ $Date: 2003/08/07 13:33:09 $
* @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
* @see org.exolab.jms.server.JmsServer
* @see org.exolab.jms.server.mipc.IpcJmsReceiver
*/
public class IpcJmsServer implements JmsServerIfc, ConnectionNotifierIfc {
/**
* The ipc connection server
*/
private MultiplexConnectionServerIfc _server;
/**
* The list of all client consumer connections, used to send JmsMessages
* on.
*/
private HashMap _consumerList = new HashMap(10);
/**
* Construct a new <code>IpcJmsServer</code>
*/
public IpcJmsServer() {
}
/**
* ConnectionNotifierIfc method implementation.
* The MultipleConnectionServer calls this method whenever a new
* MultiplexConnection is established.
* It gives the user of MCS an opportunity to setup channel handlers.
*/
public void connected(MultiplexConnectionIfc newConnection) {
IpcServerChannel cs;
cs = new IpcServerChannel(newConnection, getReceiver(), "server");
cs.start();
}
/**
* Start the Ipc service in its own thread. This will then listen for
* new connections, establish connections as they arrive, and call the
* callback with new data when received.
*/
public void init() throws ServerException {
TcpConfigurationType tcp = getTcpConfiguration();
try {
_server = createServer(tcp.getPort());
} catch (IOException exception) {
throw new ServerException(
"Failed to initialise server for the " + getScheme() +
" connector", exception);
}
((Thread) _server).start();
}
// implementation of JmsServerIfc.bindConnectionFactories
public void bindConnectionFactories(Context context)
throws NamingException, ServerException {
Configuration config = ConfigurationManager.getConfig();
// retrieve the configuration information from the configuration
// manager
ServerConfiguration server = config.getServerConfiguration();
TcpConfigurationType tcp = getTcpConfiguration();
String host = normalizeHost(server.getHost());
String internalHost = normalizeHost(tcp.getInternalHost());
// we need to put together a list of parameters that the
// connection factories will need to use to connect
// to this server
Hashtable env = new Hashtable();
env.put(IpcJmsConstants.IPC_SERVER_HOST, host);
env.put(IpcJmsConstants.IPC_SERVER_PORT,
Integer.toString(tcp.getPort()));
if (internalHost != null) {
env.put(IpcJmsConstants.IPC_INTERNAL_SERVER_HOST, internalHost);
}
Connector connector = ConfigurationManager.getConnector(getScheme());
ConnectorResource resource =
ConnectorHelper.getConnectorResource(getScheme());
Class proxy;
String className = resource.getServer().getProxyClass();
try {
proxy = Class.forName(className);
} catch (ClassNotFoundException exception) {
throw new ServerException(
"Failed to locate the server proxy class: " + className);
}
ConnectionFactoryHelper.bind(
context, connector.getConnectionFactories(), proxy, env);
}
/**
* Add a new connection for this client.
*
* @param session the session
* @param connection the connection to the client
* @throws IOException if a connection error occurs
*/
public synchronized void addConnection(JmsServerSession session,
MultiplexConnectionIfc connection)
throws IOException {
IpcJmsSessionList list =
(IpcJmsSessionList) _consumerList.get(connection);
if (list == null) {
list = new IpcJmsSessionList(connection);
_consumerList.put(connection, list);
}
list.add(session);
}
/**
* Remove this sessions connection
*
* @param session the session
* @param connection the connection to the client
*/
public synchronized void removeConnection(
JmsServerSession session, MultiplexConnectionIfc connection) {
IpcJmsSessionList list = (IpcJmsSessionList) _consumerList.get(
connection);
if (list != null) {
if (list.remove(session)) {
_consumerList.remove(connection);
}
}
}
/**
* Remove all managed client connections
*/
public synchronized void removeAllConnections() {
Object[] e = _consumerList.values().toArray();
for (int i = 0, j = _consumerList.size(); i < j; i++) {
((IpcJmsSessionList) e[i]).removeAll();
}
_consumerList.clear();
}
/**
* Create an normal connection.
*
* @param port The port number to use.
* @return the created connection
* @throws IOException if the server fails to initialise the ip service
*/
protected MultiplexConnectionServerIfc createServer(int port)
throws IOException {
return new MultiplexConnectionServer(port, this);
}
/**
* Returns the connector scheme for this server. Defaults to tcp
*/
protected SchemeType getScheme() {
return SchemeType.TCP;
}
/**
* Returns the TCP configuration
*
* @return the TCP configuration
*/
protected TcpConfigurationType getTcpConfiguration() {
return ConfigurationManager.getConfig().getTcpConfiguration();
}
/**
* Returns a new receiver for handling requests
*/
protected IpcJmsReceiver getReceiver() {
IpcJmsReceiver receiver = new IpcJmsReceiver();
NotifierIfc sessionHandler = new IpcJmsSessionConnection(this);
receiver.addDispatcher(sessionHandler.getClass().getName(),
sessionHandler);
NotifierIfc serverHandler = new IpcJmsServerConnection();
receiver.addDispatcher(serverHandler.getClass().getName(),
serverHandler);
NotifierIfc adminHandler = new IpcJmsAdminConnection();
receiver.addDispatcher(adminHandler.getClass().getName(),
adminHandler);
// this is a hack... The http connector is inextricably tied to the
// tcp connector in the current architecture, so its unavoidable for
// the moment.
NotifierIfc httpHandler = new HttpJmsSessionConnection(this);
receiver.addDispatcher(httpHandler.getClass().getName(), httpHandler);
return receiver;
}
/**
* This method will normalize the host name. If the host is localhost then
* it will be converted to the corresponding IP address
*
* @param host - the raw host name
* @return String - the normalized IP address
*/
private String normalizeHost(String host) {
String server = host;
// ensure that the host is not null
if (host == null) {
return host;
}
if (host.equals("localhost")) {
try {
server = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ignore) {
// ignore
}
}
return server;
}
} //--IpcJmsServer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -