1
2
3
4
5
6
7
8
9
10 package org.apache.commons.messagelet;
11
12 import java.util.Iterator;
13
14 import javax.jms.JMSException;
15 import javax.servlet.ServletContext;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.commons.messagelet.model.SubscriptionDigester;
20 import org.apache.commons.messagelet.model.SubscriptionList;
21 import org.apache.commons.messenger.Messenger;
22 import org.apache.commons.messenger.MessengerManager;
23
24
25
26
27
28
29
30
31
32 public class Main {
33
34
35 private static final Log log = LogFactory.getLog(Main.class);
36
37
38 private MessengerManager manager;
39
40
41 private SubscriptionList subscriptionList;
42
43
44 private String connectionsConfig = "Messenger.xml";
45
46
47 private String subscriptionsConfig = "subscriptions.xml";
48
49
50 private boolean useStopWatch = false;
51
52
53 public static void main(String[] args) {
54 Main main = new Main();
55 if ( args.length <= 0 ) {
56 System.out.println( "Usage <subscriptionConfigFile> [<connectionsConfigFile>]" );
57 return;
58 }
59 if ( args.length > 0 ) {
60 main.setSubscriptionsConfig( args[0] );
61 }
62 if ( args.length > 1 ) {
63 main.setConnectionsConfig( args[1] );
64 }
65
66 try {
67 main.run();
68 }
69 catch (Exception e) {
70 log.error( "Caught: " + e, e );
71 }
72 }
73
74 public Main() {
75 }
76
77
78
79
80
81
82 public void run() throws Exception {
83
84
85 SubscriptionManager subscriber = new SubscriptionManager();
86 subscriber.setMessengerManager( getMessengerManager() );
87 subscriber.setSubscriptionList( createSubscriptionList() );
88 subscriber.setServletContext( getServletContext() );
89
90 subscriber.subscribe();
91
92
93 for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) {
94 String name = (String) iter.next();
95 Messenger messenger = manager.getMessenger( name );
96 try {
97 messenger.getConnection().start();
98 }
99 catch (JMSException e) {
100 log.error( "Caught exception trying to start messenger: " + name + ". Exception: " + e, e );
101 }
102 }
103
104
105 }
106
107
108 public Messenger getMessenger(String name) throws JMSException {
109 return getMessengerManager().getMessenger( name );
110 }
111
112
113
114
115
116 public String getConnectionsConfig() {
117 return connectionsConfig;
118 }
119
120 public void setConnectionsConfig(String connectionsConfig) {
121 this.connectionsConfig = connectionsConfig;
122 }
123
124 public String getSubscriptionsConfig() {
125 return subscriptionsConfig;
126 }
127
128 public void setSubscriptionsConfig(String subscriptionsConfig) {
129 this.subscriptionsConfig = subscriptionsConfig;
130 }
131
132 public MessengerManager getMessengerManager() throws JMSException {
133 if ( manager == null ) {
134 manager = createMessengerManager();
135 MessengerManager.setInstance( manager );
136 }
137 return manager;
138 }
139
140 public void setMessengerManager(MessengerManager manager) {
141 this.manager = manager;
142 }
143
144
145
146 protected MessengerManager createMessengerManager() throws JMSException {
147 String config = connectionsConfig;
148
149 log.info( "Creating the JMS connections from the file: " + config );
150
151 try {
152 return MessengerManager.load( config );
153 }
154 catch (JMSException e) {
155 log.error( "Could not parse Messenger connection XML deployment document for URL: " + config, e );
156
157 throw new JMSException(
158 "Could not parse Messenger connection XML deployment document for URL: " + config
159 + " reason: " + e
160 );
161 }
162 }
163
164 protected SubscriptionList createSubscriptionList() throws JMSException {
165 String config = subscriptionsConfig;
166
167 log.info( "Loading the JMS subscriptions from: " + config );
168
169 try {
170 SubscriptionDigester digester = new SubscriptionDigester();
171 return (SubscriptionList) digester.parse( config );
172 }
173 catch (Exception e) {
174 log.error( "Could not parse Messenger subscription XML deployment document for URL: " + config, e );
175
176 throw new JMSException(
177 "Could not parse Messenger subscription XML deployment document for URL: " + config
178 + " reason: " + e
179 );
180 }
181 }
182
183 protected ServletContext getServletContext() {
184 return null;
185 }
186
187
188
189
190 protected void waitForever() {
191 Object lock = new Object();
192 synchronized (lock) {
193 while (true) {
194 try {
195 lock.wait();
196 }
197 catch (Exception e) {
198 log.warn( "Main thread interupted: " + e, e );
199 }
200 }
201 }
202 }
203 }