minor refactor to make the WebClient easier to use as a stand alone POJO

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@397149 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-04-26 08:54:07 +00:00
parent 535e16a3e4
commit c1a1dd2689
5 changed files with 30 additions and 31 deletions

View File

@ -101,7 +101,7 @@ public class MessageListenerServlet extends MessageServletSupport {
// lets turn the HTTP post into a JMS Message
WebClient client = getWebClient(request);
WebClient client = WebClient.getWebClient(request);
String message_ids="";
synchronized (client) {
@ -227,7 +227,7 @@ public class MessageListenerServlet extends MessageServletSupport {
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
WebClient client = getWebClient(request);
WebClient client = WebClient.getWebClient(request);
if (log.isDebugEnabled()) {
log.debug("GET client="+client+" session="+request.getSession().getId()+" uri="+request.getRequestURI()+" query="+request.getQueryString());
}
@ -371,7 +371,7 @@ public class MessageListenerServlet extends MessageServletSupport {
HttpSession session = request.getSession();
Listener listener = (Listener) session.getAttribute("mls.listener");
if (listener == null) {
listener = new Listener(getWebClient(request));
listener = new Listener(WebClient.getWebClient(request));
session.setAttribute("mls.listener", listener);
}
return listener;

View File

@ -80,7 +80,7 @@ public class MessageServlet extends MessageServletSupport {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// lets turn the HTTP post into a JMS Message
try {
WebClient client = getWebClient(request);
WebClient client = WebClient.getWebClient(request);
String text = getPostedMessageBody(request);
@ -137,7 +137,7 @@ public class MessageServlet extends MessageServletSupport {
int messages = 0;
try {
WebClient client = getWebClient(request);
WebClient client = WebClient.getWebClient(request);
Destination destination = getDestination(client, request);
if (destination==null)
throw new NoDestinationSuppliedException();
@ -254,7 +254,7 @@ public class MessageServlet extends MessageServletSupport {
int messages = 0;
try {
WebClient client = getWebClient(request);
WebClient client = WebClient.getWebClient(request);
Destination destination = getDestination(client, request);
long timeout = getReadTimeout(request);
boolean ajax = isRicoAjax(request);

View File

@ -28,7 +28,6 @@ import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
@ -58,7 +57,6 @@ public abstract class MessageServletSupport extends HttpServlet {
private int defaultMessagePriority = 5;
private long defaultMessageTimeToLive = 0;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
@ -83,10 +81,6 @@ public abstract class MessageServletSupport extends HttpServlet {
WebClient.initContext(getServletContext());
}
protected WebClient createWebClient(HttpServletRequest request) {
return new WebClient();
}
public static boolean asBoolean(String param) {
return asBoolean(param, false);
}
@ -100,22 +94,6 @@ public abstract class MessageServletSupport extends HttpServlet {
}
}
/**
* Helper method to get the client for the current session
*
* @param request is the current HTTP request
* @return the current client or a newly creates
*/
protected WebClient getWebClient(HttpServletRequest request) {
HttpSession session = request.getSession(true);
WebClient client = WebClient.getWebClient(session);
if (client == null || client.isClosed()) {
client = createWebClient(request);
session.setAttribute(WebClient.webClientAttribute, client);
}
return client;
}
protected void appendParametersToMessage(HttpServletRequest request, TextMessage message) throws JMSException {
Map parameterMap = request.getParameterMap();

View File

@ -63,7 +63,7 @@ public class PortfolioPublishServlet extends MessageServletSupport {
request.getSession().setAttribute("total",total);
try {
WebClient client = getWebClient(request);
WebClient client = WebClient.getWebClient(request);
for (int i = 0; i < count; i++) {
sendMessage(client, stocks);
}

View File

@ -36,6 +36,7 @@ import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
@ -77,6 +78,23 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
private final Semaphore semaphore = new Semaphore(1);
/**
* Helper method to get the client for the current session, lazily creating
* a client if there is none currently
*
* @param request is the current HTTP request
* @return the current client or a newly creates
*/
public static WebClient getWebClient(HttpServletRequest request) {
HttpSession session = request.getSession(true);
WebClient client = getWebClient(session);
if (client == null || client.isClosed()) {
client = WebClient.createWebClient(request);
session.setAttribute(webClientAttribute, client);
}
return client;
}
/**
* @return the web client for the current HTTP session or null if there is
* not a web client created yet
@ -89,8 +107,6 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
initConnectionFactory(context);
}
/**
*/
public WebClient() {
if (factory == null)
throw new IllegalStateException("initContext(ServletContext) not called");
@ -284,4 +300,9 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
public void valueUnbound(HttpSessionBindingEvent event) {
close();
}
protected static WebClient createWebClient(HttpServletRequest request) {
return new WebClient();
}
}