1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.apache.commons.messagelet; |
11 | |
|
12 | |
import java.net.URL; |
13 | |
import java.util.Iterator; |
14 | |
|
15 | |
import javax.jms.JMSException; |
16 | |
import javax.servlet.GenericServlet; |
17 | |
import javax.servlet.ServletException; |
18 | |
import javax.servlet.ServletRequest; |
19 | |
import javax.servlet.ServletResponse; |
20 | |
|
21 | |
import org.apache.commons.messagelet.model.SubscriptionDigester; |
22 | |
import org.apache.commons.messagelet.model.SubscriptionList; |
23 | |
import org.apache.commons.messenger.Messenger; |
24 | |
import org.apache.commons.messenger.MessengerManager; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class ManagerServlet extends GenericServlet { |
34 | |
|
35 | |
|
36 | |
private static final boolean USE_HTTP_SERVLETS = true; |
37 | |
|
38 | |
private static final String KEY_CONNECTIONS = "connections"; |
39 | |
private static final String KEY_SUBSCRIPTIONS = "subscriptions"; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
private boolean continueOnSubscribeException; |
46 | |
|
47 | 0 | public ManagerServlet() { |
48 | 0 | } |
49 | |
|
50 | |
public SubscriptionManager getSubscriptionManager() { |
51 | 0 | SubscriptionManager answer = (SubscriptionManager) getServletContext().getAttribute( "subscriptionManager" ); |
52 | 0 | if (answer == null) { |
53 | 0 | answer = new SubscriptionManager(); |
54 | 0 | getServletContext().setAttribute( "subscriptionManager", answer ); |
55 | |
} |
56 | 0 | return answer; |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public synchronized void init() throws ServletException { |
63 | 0 | String text = getServletContext().getInitParameter( "continueOnSubscribeException" ); |
64 | 0 | if ( text != null && text.equals( "true" ) ) { |
65 | 0 | continueOnSubscribeException = true; |
66 | |
} |
67 | |
|
68 | |
|
69 | |
try { |
70 | 0 | SubscriptionManager subscriber = getSubscriptionManager(); |
71 | 0 | MessengerManager manager = subscriber.getMessengerManager(); |
72 | 0 | if ( manager == null ) { |
73 | 0 | manager = createMessengerManager(); |
74 | |
|
75 | 0 | subscriber.setMessengerManager( manager ); |
76 | 0 | subscriber.setSubscriptionList( createSubscriptionList() ); |
77 | 0 | subscriber.setServletContext( getServletContext() ); |
78 | |
|
79 | |
|
80 | 0 | subscriber.subscribe(); |
81 | |
|
82 | |
|
83 | 0 | for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) { |
84 | 0 | String name = (String) iter.next(); |
85 | 0 | Messenger messenger = manager.getMessenger( name ); |
86 | |
try { |
87 | 0 | messenger.getConnection().start(); |
88 | |
} |
89 | 0 | catch (JMSException e) { |
90 | 0 | log( "Caught exception trying to start messenger: " + name + ". Exception: " + e, e ); |
91 | 0 | } |
92 | 0 | } |
93 | |
} |
94 | |
} |
95 | 0 | catch (JMSException e) { |
96 | 0 | throw new ServletException("Failed to initialize: " + e, e ); |
97 | 0 | } |
98 | 0 | } |
99 | |
|
100 | |
public void destroy() { |
101 | |
try { |
102 | 0 | getSubscriptionManager().unsubscribe(); |
103 | |
} |
104 | 0 | catch (Exception e) { |
105 | 0 | log( "Failed to destrory the MBOs: " + e, e ); |
106 | 0 | } |
107 | |
|
108 | |
try { |
109 | 0 | MessengerManager manager = getSubscriptionManager().getMessengerManager(); |
110 | 0 | if ( manager != null ) { |
111 | 0 | log( "Closing the Messenger connections" ); |
112 | 0 | manager.close(); |
113 | |
} |
114 | |
} |
115 | 0 | catch (Exception e) { |
116 | 0 | log( "Failed to close the Messenger Manager: " + e, e ); |
117 | 0 | } |
118 | 0 | getSubscriptionManager().setMessengerManager( null ); |
119 | 0 | } |
120 | |
|
121 | |
public void service(ServletRequest request, ServletResponse response) throws ServletException { |
122 | 0 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public boolean isContinueOnSubscriptionException() { |
128 | 0 | return continueOnSubscribeException; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public void setContinueOnSubscribeException(boolean continueOnSubscribeException) { |
136 | 0 | this.continueOnSubscribeException = continueOnSubscribeException; |
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
protected MessengerManager createMessengerManager() throws ServletException { |
143 | 0 | String config = getURLResource( KEY_CONNECTIONS, "The Messenger connections XML deployment document" ); |
144 | |
|
145 | 0 | log( "Creating the Messenger connections from the file: " + config ); |
146 | |
|
147 | |
try { |
148 | 0 | return MessengerManager.load( config ); |
149 | |
} |
150 | 0 | catch (JMSException e) { |
151 | 0 | log( "Could not parse Messenger connection XML deployment document for URL: " + config, e ); |
152 | |
|
153 | 0 | throw new ServletException( |
154 | |
"Could not parse Messenger connection XML deployment document for URL: " + config |
155 | |
+ " reason: " + e, e |
156 | |
); |
157 | |
} |
158 | |
} |
159 | |
|
160 | |
protected SubscriptionList createSubscriptionList() throws ServletException { |
161 | 0 | String config = getURLResource( KEY_SUBSCRIPTIONS, "The Messenger subscriptions XML deployment document" ); |
162 | |
|
163 | 0 | log( "Loading the Messenger subscriptions from: " + config ); |
164 | |
|
165 | |
try { |
166 | 0 | SubscriptionDigester digester = new SubscriptionDigester(); |
167 | 0 | return (SubscriptionList) digester.parse( config ); |
168 | |
} |
169 | 0 | catch (Exception e) { |
170 | 0 | log( "Could not parse Messenger subscription XML deployment document for URL: " + config, e ); |
171 | |
|
172 | 0 | throw new ServletException( |
173 | |
"Could not parse Messenger subscription XML deployment document for URL: " + config |
174 | |
+ " reason: " + e, e |
175 | |
); |
176 | |
} |
177 | |
} |
178 | |
|
179 | |
protected String getURLResource(String key, String description) throws ServletException { |
180 | 0 | String config = getInitParameter( key ); |
181 | 0 | if ( config == null || config.length() == 0 ) { |
182 | 0 | throw new ServletException( |
183 | |
"No initialization parameter for parameter: " + key |
184 | |
+ " description: " + description |
185 | |
); |
186 | |
} |
187 | |
try { |
188 | 0 | URL url = getServletContext().getResource( config ); |
189 | 0 | config = url.toString(); |
190 | |
} |
191 | 0 | catch (Exception e) { |
192 | |
|
193 | 0 | } |
194 | 0 | return config; |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
protected void handleJMSException(String message, JMSException exception) throws ServletException { |
202 | 0 | log( message, exception ); |
203 | |
|
204 | 0 | if ( ! isContinueOnSubscriptionException() ) { |
205 | 0 | throw new ServletException( message, exception ); |
206 | |
} |
207 | 0 | } |
208 | |
} |