1
2
3
4
5
6
7
8
9
10 package org.apache.commons.messagelet.impl;
11
12 import javax.jms.JMSException;
13 import javax.jms.Message;
14 import javax.servlet.ServletException;
15
16 import org.apache.commons.messagelet.MessageDrivenObjectSupport;
17 import org.apache.commons.messenger.Messenger;
18 import org.apache.commons.messenger.MessengerListener;
19
20
21
22
23
24
25
26 public class MessageServletDispatcher extends MessageDrivenObjectSupport implements MessengerListener {
27
28
29
30
31 private MessageletRequestImpl request;
32
33
34
35
36 private MessageletResponseImpl response;
37
38
39
40
41 private Messenger messenger;
42
43
44 private String path;
45
46
47 public MessageServletDispatcher() {
48
49
50 }
51
52 public MessageServletDispatcher(String path) {
53 this();
54 this.path = path;
55 }
56
57
58 public void init() throws ServletException {
59 request = new MessageletRequestImpl( new ServletRequestImpl( getServletContext() ) );
60 response = new MessageletResponseImpl( new ServletResponseImpl() );
61 }
62
63
64
65 public void setMessenger(Messenger messenger) {
66 response.setReplyMessenger( messenger );
67 }
68
69
70
71
72
73
74 public void onMessage(Message message) {
75 try {
76 response.setReplyToDestination( message.getJMSReplyTo() );
77 }
78 catch (JMSException e) {
79 log( "Could not find JMS replyTo destination", e );
80 response.setReplyToDestination( null );
81 }
82
83
84 try {
85
86 request.setMessage( message );
87 response.reset();
88
89
90 getServletContext().getRequestDispatcher( getPath() ).include(request, response);
91
92
93 response.finish();
94 }
95 catch (Throwable e) {
96 handleException( message, e );
97 }
98 }
99
100
101
102
103
104
105
106 public String getPath() {
107 return path;
108 }
109
110
111
112
113 public void setPath(String path) {
114 this.path = path;
115
116
117 }
118
119
120
121
122
123 protected void handleException(Message message, Throwable t) {
124 log( "Caught exception processing message: " + message, t);
125 }
126 }