1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 *
42 * Copyright 2001-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
43 *
44 * $Id: HttpJmsJndiServlet.java,v 1.8 2003/08/17 01:32:23 tanderson Exp $
45 *
46 * Date Author Changes
47 * Fri 21 Sep 2001 mourikis Created
48 *
49 */
50
51 package org.exolab.jms.jndi.http.servlet;
52
53 import java.io.BufferedInputStream;
54 import java.io.BufferedOutputStream;
55 import java.io.IOException;
56 import java.io.ObjectInputStream;
57 import java.io.ObjectOutputStream;
58 import java.util.Vector;
59
60 import javax.servlet.ServletConfig;
61 import javax.servlet.ServletException;
62 import javax.servlet.http.HttpServlet;
63 import javax.servlet.http.HttpServletRequest;
64 import javax.servlet.http.HttpServletResponse;
65
66 import org.exolab.core.ipc.Client;
67 import org.exolab.core.mipc.TcpConstants;
68 import org.exolab.jms.jndi.JndiConstants;
69
70
71 /***
72 * Servlet for handling OpenJMS JNDI requests.
73 *
74 * @version $Revision: 1.8 $ $Date: 2003/08/17 01:32:23 $
75 * @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
76 */
77 public class HttpJmsJndiServlet extends HttpServlet {
78
79 // This is a reference to the server connection.
80 private static Client connection_ = null;
81
82 // The server host address
83 private static String serverAddress_ = TcpConstants.LOCAL_HOST;
84
85 // The port number the server is listening to
86 private static int port_ = TcpConstants.DEFAULT_PORT + 5;
87
88 // The content of this data.
89 private static final String CONTENT_TYPE = "text/html";
90
91
92 // Is there a valid connection to OpenJMS server.
93 private static boolean connected_ = false;
94
95 public void init(ServletConfig config) throws ServletException {
96 super.init(config);
97 if (config != null) {
98 String host = config.getInitParameter(JndiConstants.HOST_PROPERTY);
99 String port = config.getInitParameter
100 (JndiConstants.PORT_NUMBER_PROPERTY);
101 if (host != null) {
102 serverAddress_ = host;
103 }
104 if (port != null) {
105 port_ = Integer.parseInt(port);
106 }
107 }
108 connect();
109 }
110
111
112 /***Process the HTTP Post request*/
113 public void doPost(HttpServletRequest request,
114 HttpServletResponse response)
115 throws ServletException, IOException {
116
117 // handle request
118 ObjectInputStream inputStream = new ObjectInputStream(
119 new BufferedInputStream(request.getInputStream()));
120
121 // prepare response
122 ObjectOutputStream oos = new ObjectOutputStream
123 (new BufferedOutputStream(response.getOutputStream()));
124 response.setContentType(CONTENT_TYPE);
125 try {
126 if (!connected_) {
127 connect();
128 }
129 Object o = null;
130 o = inputStream.readObject();
131 if (o != null && (o instanceof Vector)) {
132 synchronized (connection_) {
133 try {
134 connection_.send((Vector) o);
135 } catch (Exception err) {
136 // might have disconnected. Try and re-connect.
137 // if connection fails again, then exception is just
138 // re-thrown.
139 connected_ = false;
140 connect();
141 connection_.send((Vector) o);
142 }
143
144 String type = request.getHeader("jms-response");
145 if (type != null && type.equals("yes")) {
146 Vector v = (Vector) connection_.receive();
147 oos.writeObject(v);
148 }
149 }
150 // send response
151 response.setStatus(HttpServletResponse.SC_OK);
152 } else {
153 response.setStatus
154 (HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
155 oos.writeObject("Object is NULL, or UNEXPECTED type");
156 }
157 } catch (Exception e) {
158 e.printStackTrace();
159 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
160 oos.writeObject(e.getMessage());
161 } finally {
162 oos.flush();
163 }
164 }
165
166
167 /***
168 * Connect to the openjms server.
169 *
170 */
171 protected void connect() throws ServletException {
172
173 try {
174 connection_ = new Client(serverAddress_, port_);
175 } catch (Exception err) {
176 String message =
177 "HttpJmsJndiServlet: Failed to connect\nReason: " +
178 err.getMessage();
179 System.err.println(message);
180 throw new ServletException(message);
181 }
182
183 System.out.println("JMS JNDI Servlet connected.");
184 connected_ = true;
185 }
186
187 /***Clean up resources*/
188 public void destroy() {
189
190 if (connection_ != null) {
191 try {
192 connection_.close();
193 } catch (Exception ignored) {
194 } finally {
195 connection_ = null;
196 connected_ = false;
197 }
198 }
199 }
200
201 public String getServletInfo() {
202 return "Not implemented yet";
203 }
204
205 } // End HttpJmsJndiServlet
This page was automatically generated by Maven