📄 httpjmsserverservlet.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-2002 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: HttpJmsServerServlet.java,v 1.7 2003/08/07 13:33:09 tanderson Exp $
*
* Date Author Changes
* Fri 21 Sep 2001 mourikis Created
*
*/
package org.exolab.jms.server.http.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.exolab.core.ipc.IpcIfc;
import org.exolab.core.mipc.DisconnectionEventListener;
import org.exolab.core.mipc.MultiplexConnection;
import org.exolab.core.mipc.MultiplexConnectionIfc;
import org.exolab.core.mipc.ObjectChannel;
import org.exolab.core.mipc.TcpConstants;
import org.exolab.jms.jndi.JndiConstants;
/**
* Servlet for handling OpenJMS server requests and message publishing.
*
* @version $Revision: 1.7 $ $Date: 2003/08/07 13:33:09 $
* @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
*/
public class HttpJmsServerServlet extends HttpServlet
implements DisconnectionEventListener {
// This is a reference to the server connection.
private static MultiplexConnectionIfc _mc = null;
private static IpcIfc _connection = null;
/**
* The server host name
*/
private static String _host = TcpConstants.LOCAL_HOST;
/**
* The port number the server is listening to
*/
private static int _port = TcpConstants.DEFAULT_PORT;
/**
* The content type of the data.
*/
private static final String CONTENT_TYPE = "text/html";
/**
* Called by the servlet container to indicate to a servlet that the
* servlet is being placed into service.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
if (config != null) {
String host = config.getInitParameter(JndiConstants.HOST_PROPERTY);
String port = config.getInitParameter(
JndiConstants.PORT_NUMBER_PROPERTY);
if (host != null) {
_host = host;
}
if (port != null) {
_port = Integer.parseInt(port);
}
}
connect();
log("OpenJMS server servlet ready");
}
/**
* Handle HTTP POST requests
*
* @param request contains the request the client has made of the servlet
* @param response contains the response the servlet sends to the client
* @throws IOException if an I/O error is encountered
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
ObjectInputStream input = new ObjectInputStream(
new BufferedInputStream(request.getInputStream()));
ObjectOutputStream output = new ObjectOutputStream(
new BufferedOutputStream(response.getOutputStream()));
response.setContentType(CONTENT_TYPE);
try {
if (_mc == null) {
connect();
}
Object object = input.readObject();
if (object instanceof Vector) {
synchronized (_connection) {
Vector message = (Vector) object;
_connection.send(message);
String type = request.getHeader("jms-response");
if (type != null && type.equals("yes")) {
message = (Vector) _connection.receive();
output.writeObject(message);
}
}
// send response
response.setStatus(HttpServletResponse.SC_OK);
} else {
response.setStatus
(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
output.writeObject("Object is NULL, or UNEXPECTED type");
}
} catch (Exception exception) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
output.writeObject(exception.getMessage());
} finally {
output.flush();
}
}
/**Clean up resources*/
public void destroy() {
disconnected(null);
}
// implementation of DisconnectionEventListener.disconnected
public void disconnected(MultiplexConnectionIfc connection) {
if (_mc != null) {
try {
_mc.finish();
_connection.close();
} catch (Exception ignore) {
// just swallow
} finally {
_mc = null;
_connection = null;
}
}
}
/**
* Connect to the OpenJMS server.
*/
protected void connect() throws ServletException {
try {
_mc = new MultiplexConnection(_host, _port);
} catch (Exception exception) {
String message = "Failed to connect using host=" + _host +
" and port=" + _port + " " + exception.getMessage();
log(message);
throw new ServletException(message);
}
_mc.setDisconnectionEventListener(this);
((Thread) _mc).start();
_connection = new ObjectChannel("server", _mc);
}
} //-- HttpJmsServerServlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -