-
Notifications
You must be signed in to change notification settings - Fork 458
/
QServer.java
424 lines (379 loc) · 13.6 KB
/
QServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2020 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpos.q2.iso;
import org.jdom2.Element;
import org.jpos.core.ConfigurationException;
import org.jpos.iso.ISOChannel;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISORequestListener;
import org.jpos.iso.ISOServer;
import org.jpos.iso.ISOServerEventListener;
import org.jpos.iso.ISOServerSocketFactory;
import org.jpos.iso.ISOSource;
import org.jpos.iso.ServerChannel;
import org.jpos.q2.QBeanSupport;
import org.jpos.q2.QFactory;
import org.jpos.space.LocalSpace;
import org.jpos.space.Space;
import org.jpos.space.SpaceFactory;
import org.jpos.space.SpaceListener;
import org.jpos.util.LogSource;
import org.jpos.util.NameRegistrar;
import org.jpos.util.ThreadPool;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
/**
* ISO Server wrapper.
*
* @author Alwyn Schoeman
* @version $Revision$ $Date$
*/
@SuppressWarnings("unchecked")
public class QServer
extends QBeanSupport
implements QServerMBean, SpaceListener, ISORequestListener
{
private int port = 0;
private int maxSessions = 100;
private int minSessions = 1;
private String channelString, packagerString, socketFactoryString;
private ISOChannel channel = null;
private ISOServer server;
protected LocalSpace sp;
private String inQueue;
private String outQueue;
private String sendMethod;
AtomicInteger msgn = new AtomicInteger();
public QServer () {
super ();
}
@Override
public void initService() throws ConfigurationException {
Element e = getPersist ();
sp = grabSpace (e.getChild ("space"));
}
private void newChannel () throws ConfigurationException {
Element persist = getPersist ();
Element e = persist.getChild ("channel");
if (e == null) {
throw new ConfigurationException ("channel element missing");
}
ChannelAdaptor adaptor = new ChannelAdaptor ();
channel = adaptor.newChannel (e, getFactory ());
}
private void initServer ()
throws ConfigurationException
{
if (port == 0) {
throw new ConfigurationException ("Port value not set");
}
newChannel();
if (channel == null) {
throw new ConfigurationException ("ISO Channel is null");
}
if (!(channel instanceof ServerChannel)) {
throw new ConfigurationException (channelString +
"does not implement ServerChannel");
}
ThreadPool pool = null;
pool = new ThreadPool (minSessions ,maxSessions, getName() + "-ThreadPool");
pool.setLogger (log.getLogger(), getName() + ".pool");
server = new ISOServer (port, (ServerChannel) channel, pool);
server.setLogger (log.getLogger(), getName() + ".server");
server.setName (getName ());
if (socketFactoryString != null) {
ISOServerSocketFactory sFac = (ISOServerSocketFactory) getFactory().newInstance(socketFactoryString);
if (sFac != null && sFac instanceof LogSource) {
((LogSource) sFac).setLogger(log.getLogger(),getName() + ".socket-factory");
}
server.setSocketFactory(sFac);
}
getFactory().setConfiguration (server, getPersist());
addServerSocketFactory();
addListeners ();// ISORequestListener
addISOServerConnectionListeners();
NameRegistrar.register (getName(), this);
new Thread (server).start();
}
private void initIn() {
Element persist = getPersist();
inQueue = persist.getChildText("in");
if (inQueue != null) {
/*
* We have an 'in' queue to monitor for messages we will
* send out through server in our (SpaceListener)notify(Object, Object) method.
*/
sp.addListener(inQueue, this);
}
}
private void initOut() {
Element persist = getPersist();
outQueue = persist.getChildText("out");
if (outQueue != null) {
/*
* We have an 'out' queue to send any messages to that are received
* by the our requestListener(this).
*
* Note, if additional ISORequestListeners are registered with the server after
* this point, then they won't see anything as our process(ISOSource, ISOMsg)
* always return true.
*/
server.addISORequestListener(this);
}
}
@Override
public void startService () {
try {
initServer ();
initIn();
initOut();
initWhoToSendTo();
} catch (Exception e) {
getLog().warn ("error starting service", e);
}
}
private void initWhoToSendTo() {
Element persist = getPersist();
sendMethod = persist.getChildText("send-request");
if (sendMethod==null) {
sendMethod="LAST";
}
}
@Override
public void stopService () {
if (server != null) {
server.shutdown ();
sp.removeListener(inQueue, this);
}
}
@Override
public void destroyService () {
NameRegistrar.unregister (getName());
NameRegistrar.unregister ("server." + getName());
}
@Override
public synchronized void setPort (int port) {
this.port = port;
setAttr (getAttrs (), "port", port);
setModified (true);
}
@Override
public int getPort () {
return port;
}
@Override
public synchronized void setPackager (String packager) {
packagerString = packager;
setAttr (getAttrs (), "packager", packagerString);
setModified (true);
}
@Override
public String getPackager () {
return packagerString;
}
@Override
public synchronized void setChannel (String channel) {
channelString = channel;
setAttr (getAttrs (), "channel", channelString);
setModified (true);
}
@Override
public String getChannel () {
return channelString;
}
@Override
public synchronized void setMaxSessions (int maxSessions) {
this.maxSessions = maxSessions;
setAttr (getAttrs (), "maxSessions", maxSessions);
setModified (true);
}
@Override
public int getMaxSessions () {
return maxSessions;
}
@Override
public synchronized void setMinSessions (int minSessions) {
this.minSessions = minSessions;
setAttr (getAttrs (), "minSessions", minSessions);
setModified (true);
}
@Override
public int getMinSessions () {
return minSessions;
}
@Override
public synchronized void setSocketFactory (String sFactory) {
socketFactoryString = sFactory;
setAttr (getAttrs(),"socketFactory", socketFactoryString);
setModified (true);
}
@Override
public String getSocketFactory() {
return socketFactoryString;
}
@Override
public String getISOChannelNames() {
return server.getISOChannelNames();
}
public ISOServer getISOServer() {
return server;
}
@Override
public String getCountersAsString () {
return server.getCountersAsString ();
}
@Override
public String getCountersAsString (String isoChannelName) {
return server.getCountersAsString (isoChannelName);
}
private void addServerSocketFactory () throws ConfigurationException {
QFactory factory = getFactory ();
Element persist = getPersist ();
Element serverSocketFactoryElement = persist.getChild ("server-socket-factory");
if (serverSocketFactoryElement != null) {
ISOServerSocketFactory serverSocketFactory = (ISOServerSocketFactory) factory.newInstance (
QFactory.getAttributeValue (serverSocketFactoryElement, "class"));
factory.setLogger (serverSocketFactory, serverSocketFactoryElement);
factory.setConfiguration (serverSocketFactory, serverSocketFactoryElement);
server.setSocketFactory(serverSocketFactory);
}
}
private void addListeners ()
throws ConfigurationException
{
QFactory factory = getFactory ();
Iterator iter = getPersist().getChildren (
"request-listener"
).iterator();
while (iter.hasNext()) {
Element l = (Element) iter.next();
ISORequestListener listener = (ISORequestListener)
factory.newInstance (QFactory.getAttributeValue (l, "class"));
factory.setLogger (listener, l);
factory.setConfiguration (listener, l);
server.addISORequestListener (listener);
}
}
private void addISOServerConnectionListeners()
throws ConfigurationException
{
QFactory factory = getFactory ();
Iterator iter = getPersist().getChildren (
"connection-listener"
).iterator();
while (iter.hasNext()) {
Element l = (Element) iter.next();
ISOServerEventListener listener = (ISOServerEventListener)
factory.newInstance (QFactory.getAttributeValue (l, "class"));
factory.setLogger (listener, l);
factory.setConfiguration (listener, l);
server.addServerEventListener(listener);
}
}
private LocalSpace grabSpace (Element e) throws ConfigurationException
{
String uri = e != null ? e.getText() : "";
Space sp = SpaceFactory.getSpace (uri);
if (sp instanceof LocalSpace) {
return (LocalSpace) sp;
}
throw new ConfigurationException ("Invalid space " + uri);
}
/*
* This method will be invoked through the SpaceListener interface we registered once
* we noticed we had an 'in' queue.
*/
@Override
public void notify(Object key, Object value) {
Object obj = sp.inp(key);
if (obj instanceof ISOMsg) {
ISOMsg m = (ISOMsg) obj;
if ("LAST".equals(sendMethod)) {
try {
ISOChannel c = server.getLastConnectedISOChannel();
if (c == null) {
throw new ISOException("Server has no active connections");
}
if (!c.isConnected()) {
throw new ISOException("Client disconnected");
}
c.send(m);
}
catch (Exception e) {
getLog().warn("notify", e);
}
}
else if ("ALL".equals(sendMethod)) {
String channelNames = getISOChannelNames();
if (channelNames != null) {
StringTokenizer tok = new StringTokenizer(channelNames, " ");
while (tok.hasMoreTokens()) {
try {
ISOChannel c = server.getISOChannel(tok.nextToken());
if (c == null) {
throw new ISOException("Server has no active connections");
}
if (!c.isConnected()) {
throw new ISOException("Client disconnected");
}
c.send(m);
} catch (Exception e) {
getLog().warn("notify", e);
}
}
}
}
else if ("RR".equals(sendMethod)) {
String channelNames = getISOChannelNames();
int i =0;
String[] channelName;
if (channelNames != null) {
StringTokenizer tok = new StringTokenizer(channelNames, " ");
channelName = new String[tok.countTokens()];
while (tok.hasMoreTokens()) {
channelName[i] = tok.nextToken();
i++;
}
try {
ISOChannel c = server.getISOChannel(channelName[msgn.incrementAndGet() % channelName.length]);
if (c == null) {
throw new ISOException("Server has no active connections");
}
if (!c.isConnected()) {
throw new ISOException("Client disconnected");
}
c.send(m);
} catch (Exception e) {
getLog().warn("notify", e);
}
}
}
}
}
/*
* This method will be invoke through the ISORequestListener interface, *if*
* this QServer has an 'out' queue to handle.
*/
@Override
public boolean process(ISOSource source, ISOMsg m) {
sp.out(outQueue, m);
return true;
}
}