📄 httpjmsjndiservlet.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: HttpJmsJndiServlet.java,v 1.8 2003/08/17 01:32:23 tanderson Exp $
*
* Date Author Changes
* Fri 21 Sep 2001 mourikis Created
*
*/
package org.exolab.jms.jndi.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.Client;
import org.exolab.core.mipc.TcpConstants;
import org.exolab.jms.jndi.JndiConstants;
/**
* Servlet for handling OpenJMS JNDI requests.
*
* @version $Revision: 1.8 $ $Date: 2003/08/17 01:32:23 $
* @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
*/
public class HttpJmsJndiServlet extends HttpServlet {
// This is a reference to the server connection.
private static Client connection_ = null;
// The server host address
private static String serverAddress_ = TcpConstants.LOCAL_HOST;
// The port number the server is listening to
private static int port_ = TcpConstants.DEFAULT_PORT + 5;
// The content of this data.
private static final String CONTENT_TYPE = "text/html";
// Is there a valid connection to OpenJMS server.
private static boolean connected_ = false;
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) {
serverAddress_ = host;
}
if (port != null) {
port_ = Integer.parseInt(port);
}
}
connect();
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// handle request
ObjectInputStream inputStream = new ObjectInputStream(
new BufferedInputStream(request.getInputStream()));
// prepare response
ObjectOutputStream oos = new ObjectOutputStream
(new BufferedOutputStream(response.getOutputStream()));
response.setContentType(CONTENT_TYPE);
try {
if (!connected_) {
connect();
}
Object o = null;
o = inputStream.readObject();
if (o != null && (o instanceof Vector)) {
synchronized (connection_) {
try {
connection_.send((Vector) o);
} catch (Exception err) {
// might have disconnected. Try and re-connect.
// if connection fails again, then exception is just
// re-thrown.
connected_ = false;
connect();
connection_.send((Vector) o);
}
String type = request.getHeader("jms-response");
if (type != null && type.equals("yes")) {
Vector v = (Vector) connection_.receive();
oos.writeObject(v);
}
}
// send response
response.setStatus(HttpServletResponse.SC_OK);
} else {
response.setStatus
(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
oos.writeObject("Object is NULL, or UNEXPECTED type");
}
} catch (Exception e) {
e.printStackTrace();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
oos.writeObject(e.getMessage());
} finally {
oos.flush();
}
}
/**
* Connect to the openjms server.
*
*/
protected void connect() throws ServletException {
try {
connection_ = new Client(serverAddress_, port_);
} catch (Exception err) {
String message =
"HttpJmsJndiServlet: Failed to connect\nReason: " +
err.getMessage();
System.err.println(message);
throw new ServletException(message);
}
System.out.println("JMS JNDI Servlet connected.");
connected_ = true;
}
/**Clean up resources*/
public void destroy() {
if (connection_ != null) {
try {
connection_.close();
} catch (Exception ignored) {
} finally {
connection_ = null;
connected_ = false;
}
}
}
public String getServletInfo() {
return "Not implemented yet";
}
} // End HttpJmsJndiServlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -