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 public ManagerServlet() {
48 }
49
50 public SubscriptionManager getSubscriptionManager() {
51 SubscriptionManager answer = (SubscriptionManager) getServletContext().getAttribute( "subscriptionManager" );
52 if (answer == null) {
53 answer = new SubscriptionManager();
54 getServletContext().setAttribute( "subscriptionManager", answer );
55 }
56 return answer;
57 }
58
59
60
61
62 public synchronized void init() throws ServletException {
63 String text = getServletContext().getInitParameter( "continueOnSubscribeException" );
64 if ( text != null && text.equals( "true" ) ) {
65 continueOnSubscribeException = true;
66 }
67
68
69 try {
70 SubscriptionManager subscriber = getSubscriptionManager();
71 MessengerManager manager = subscriber.getMessengerManager();
72 if ( manager == null ) {
73 manager = createMessengerManager();
74
75 subscriber.setMessengerManager( manager );
76 subscriber.setSubscriptionList( createSubscriptionList() );
77 subscriber.setServletContext( getServletContext() );
78
79
80 subscriber.subscribe();
81
82
83 for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) {
84 String name = (String) iter.next();
85 Messenger messenger = manager.getMessenger( name );
86 try {
87 messenger.getConnection().start();
88 }
89 catch (JMSException e) {
90 log( "Caught exception trying to start messenger: " + name + ". Exception: " + e, e );
91 }
92 }
93 }
94 }
95 catch (JMSException e) {
96 throw new ServletException("Failed to initialize: " + e, e );
97 }
98 }
99
100 public void destroy() {
101 try {
102 getSubscriptionManager().unsubscribe();
103 }
104 catch (Exception e) {
105 log( "Failed to destrory the MBOs: " + e, e );
106 }
107
108 try {
109 MessengerManager manager = getSubscriptionManager().getMessengerManager();
110 if ( manager != null ) {
111 log( "Closing the Messenger connections" );
112 manager.close();
113 }
114 }
115 catch (Exception e) {
116 log( "Failed to close the Messenger Manager: " + e, e );
117 }
118 getSubscriptionManager().setMessengerManager( null );
119 }
120
121 public void service(ServletRequest request, ServletResponse response) throws ServletException {
122 }
123
124
125
126
127 public boolean isContinueOnSubscriptionException() {
128 return continueOnSubscribeException;
129 }
130
131
132
133
134
135 public void setContinueOnSubscribeException(boolean continueOnSubscribeException) {
136 this.continueOnSubscribeException = continueOnSubscribeException;
137 }
138
139
140
141
142 protected MessengerManager createMessengerManager() throws ServletException {
143 String config = getURLResource( KEY_CONNECTIONS, "The Messenger connections XML deployment document" );
144
145 log( "Creating the Messenger connections from the file: " + config );
146
147 try {
148 return MessengerManager.load( config );
149 }
150 catch (JMSException e) {
151 log( "Could not parse Messenger connection XML deployment document for URL: " + config, e );
152
153 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 String config = getURLResource( KEY_SUBSCRIPTIONS, "The Messenger subscriptions XML deployment document" );
162
163 log( "Loading the Messenger subscriptions from: " + config );
164
165 try {
166 SubscriptionDigester digester = new SubscriptionDigester();
167 return (SubscriptionList) digester.parse( config );
168 }
169 catch (Exception e) {
170 log( "Could not parse Messenger subscription XML deployment document for URL: " + config, e );
171
172 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 String config = getInitParameter( key );
181 if ( config == null || config.length() == 0 ) {
182 throw new ServletException(
183 "No initialization parameter for parameter: " + key
184 + " description: " + description
185 );
186 }
187 try {
188 URL url = getServletContext().getResource( config );
189 config = url.toString();
190 }
191 catch (Exception e) {
192
193 }
194 return config;
195 }
196
197
198
199
200
201 protected void handleJMSException(String message, JMSException exception) throws ServletException {
202 log( message, exception );
203
204 if ( ! isContinueOnSubscriptionException() ) {
205 throw new ServletException( message, exception );
206 }
207 }
208 }