-
Notifications
You must be signed in to change notification settings - Fork 33
/
BridgeServiceManagerImpl.java
811 lines (699 loc) · 29.5 KB
/
BridgeServiceManagerImpl.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
* Copyright (c) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Payara Services Ltd.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.bridge.admin;
import java.util.Properties;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.util.Locale;
import jakarta.jms.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import com.sun.messaging.bridge.api.Bridge;
import com.sun.messaging.bridge.api.BridgeUtil;
import com.sun.messaging.bridge.api.BridgeContext;
import com.sun.messaging.bridge.api.BridgeBaseContext;
import com.sun.messaging.bridge.api.BridgeServiceManager;
import com.sun.messaging.bridge.api.FaultInjection;
import com.sun.messaging.bridge.api.BridgeException;
import com.sun.messaging.bridge.admin.resources.BridgeManagerResources;
import com.sun.messaging.bridge.admin.handlers.AdminMessageHandler;
import com.sun.messaging.jmq.io.Status;
import com.sun.messaging.bridge.api.BridgeCmdSharedResources;
import com.sun.messaging.bridge.api.BridgeCmdSharedReplyData;
import com.sun.messaging.bridge.api.JMSBridgeStore;
import com.sun.messaging.bridge.api.DupKeyException;
import com.sun.messaging.ConnectionFactory;
import org.jvnet.hk2.annotations.Service;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.PreDestroy;
import org.glassfish.hk2.runlevel.RunLevel;
import jakarta.inject.Inject;
import org.glassfish.hk2.api.ServiceLocator;
/**
* The Bridge Services Manager
*
* @author amyk
*/
@RunLevel(12) //Startup run level in Glassfish + 2
@Service
//@Singleton
public class BridgeServiceManagerImpl extends BridgeServiceManager implements ExceptionListener, MessageListener, PostConstruct, PreDestroy {
private static final String BROKER_BRIDGE_BASE_CONTEXT_CLASS_STR = "com.sun.messaging.bridge.internal.BrokerBridgeBaseContext";
private static final String className = "com.sun.messaging.bridge.admin.BridgeServiceManagerImpl";
private static final Logger logger = Logger.getLogger("com.sun.messaging.bridge");
private static final String LOGMSG_PREFIX = "MQBRIDGESTRAP: ";
private enum State {
UNINITIALIZED, STOPPING, STOPPED, STARTING, STARTED
}
private static boolean DEBUG = false;
private static BridgeManagerResources _bmr = getBridgeManagerResources();
private static Map<Locale, BridgeManagerResources> _bmrs = Collections.synchronizedMap(new HashMap<>());
private Map<String, Bridge> _bridges = Collections.synchronizedMap(new LinkedHashMap<>());
private BridgeBaseContext _bc = null;
private Connection _connection = null;
private Session _session = null;
private String _user = null;
private String _passwd = null;
private TemporaryQueue _adminQueue = null;
private State _state = State.UNINITIALIZED;
private AdminMessageHandler _adminHandler = null;
@Inject
private ServiceLocator habitat;
@Override
public void postConstruct() {
logger.entering(className, LOGMSG_PREFIX + "postConstruct()", "");
BridgeBaseContext bbc = habitat.getService(BridgeBaseContext.class, BROKER_BRIDGE_BASE_CONTEXT_CLASS_STR);
if (bbc == null) {
String emsg = "BridgeBaseContext null";
logger.log(Level.SEVERE, emsg);
throw new RuntimeException(emsg);
}
try {
init(bbc);
start();
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
throw new RuntimeException("Failed to start bridge service manager", e);
}
}
@Override
public void preDestroy() {
logger.entering(className, LOGMSG_PREFIX + "preDestroy()", "");
logger.info(LOGMSG_PREFIX + "Stopping bridge service manager ..");
try {
stop();
} catch (Exception e) {
logger.log(Level.WARNING, LOGMSG_PREFIX + "Failed to stop bridge service manager", e);
}
logger.info(LOGMSG_PREFIX + "Stopped bridge service manager");
}
/**
* Initialize the bridge service manager
*/
@Override
public synchronized void init(BridgeBaseContext ctx) throws Exception {
com.sun.messaging.jmq.jmsclient.Debug.setUseLogger(true);
FaultInjection.setBridgeBaseContext(ctx);
_bc = ctx;
Properties props = _bc.getBridgeConfig();
String activekey = props.getProperty(BridgeBaseContext.PROP_PREFIX) + ".activelist";
List<String> alist = BridgeUtil.getListProperty(activekey, props);
int size = alist.size();
String name = null;
Iterator<String> itr = alist.iterator();
while (itr.hasNext()) {
name = itr.next();
try {
loadBridge(name);
} catch (BridgeException e) {
if (e.getStatus() == Status.NOT_MODIFIED) {
continue;
}
_bc.logError(_bmr.getKString(_bmr.E_LOAD_BRIDGE_FAILED, name, e.getMessage()), null);
throw e;
}
}
if (_bc.isHAEnabled()) {
JMSBridgeStore store = (JMSBridgeStore) _bc.getJDBCStore();
if (store == null) {
throw new BridgeException("null JDBC store");
}
List jmsbridges = store.getJMSBridges(null);
name = null;
itr = alist.iterator();
while (itr.hasNext()) {
name = itr.next();
String type = props.getProperty(props.getProperty(BridgeBaseContext.PROP_PREFIX) + "." + name + ".type");
if (type == null) {
throw new BridgeException(_bmr.getString(_bmr.X_BRIDGE_NO_TYPE, name));
}
if (!type.trim().toUpperCase(_bmr.getLocale()).equals(Bridge.JMS_TYPE)) {
continue;
}
if (jmsbridges.contains(name)) {
continue;
}
try {
store.addJMSBridge(name, true, null);
} catch (DupKeyException e) {
_bc.logInfo(_bmr.getKString(_bmr.I_JMSBRIDGE_NOT_OWNER, name), null);
itr.remove();
}
}
jmsbridges = store.getJMSBridges(null);
itr = jmsbridges.iterator();
while (itr.hasNext()) {
name = itr.next();
if (alist.contains(name)) {
continue;
}
alist.add(name);
try {
loadBridge(name);
} catch (BridgeException e) {
_bc.logError(_bmr.getKString(_bmr.E_LOAD_BRIDGE_FAILED, name, e.getMessage()), null);
throw e;
}
}
if (alist.size() != size) {
StringBuilder sb = new StringBuilder();
int i = 0;
itr = alist.iterator();
while (itr.hasNext()) {
if (i > 0) {
sb.append(',');
}
sb.append(itr.next());
i++;
}
Properties p = new Properties();
p.setProperty(activekey, sb.toString());
_bc.updateBridgeConfig(p);
}
}
String keyu = props.getProperty(BridgeBaseContext.PROP_PREFIX) + ctx.PROP_ADMIN_USER_SUFFIX;
String keyp = props.getProperty(BridgeBaseContext.PROP_PREFIX) + ctx.PROP_ADMIN_PASSWORD_SUFFIX;
_user = props.getProperty(keyu);
_passwd = props.getProperty(keyp);
if (_user == null || _user.trim().length() == 0) {
throw new JMSException(_bmr.getString(_bmr.X_BRIDGE_NO_ADMIN_USER, keyu));
}
_user = _user.trim();
if (_passwd == null || _passwd.trim().length() == 0) {
throw new JMSException(_bmr.getString(_bmr.X_BRIDGE_NO_ADMIN_PASSWORD, keyp));
}
_passwd = _passwd.trim();
_adminHandler = new AdminMessageHandler(this);
_state = State.STOPPED;
}
@Override
public synchronized boolean isRunning() {
return (_state == State.STARTED);
}
@Override
public synchronized String getAdminDestinationName() throws Exception {
if (_state != State.STARTED) {
throw new BridgeException(_bmr.getString(_bmr.X_BRIDGE_SERVICE_MANAGER_NOT_RUNNING));
}
return _adminQueue.getQueueName();
}
@Override
public String getAdminDestinationClassName() throws Exception {
if (_state != State.STARTED) {
throw new BridgeException(_bmr.getString(_bmr.X_BRIDGE_SERVICE_MANAGER_NOT_RUNNING));
}
return _adminQueue.getClass().getName();
}
/**
* Start the bridge service manager
*/
@Override
public synchronized void start() throws Exception {
if (_bc == null || _state == State.UNINITIALIZED) {
throw new BridgeException(_bmr.getString(_bmr.X_BRIDGE_SERVICE_MANAGER_NOT_INITED));
}
_state = State.STARTING;
Properties props = _bc.getBridgeConfig();
try {
Bridge b = null;
String name = null;
for (Map.Entry<String, Bridge> pair : _bridges.entrySet()) {
b = pair.getValue();
name = b.getName();
String autostart = props.getProperty(props.getProperty(BridgeBaseContext.PROP_PREFIX) + "." + name + ".autostart", "true");
boolean doautostart = Boolean.valueOf(autostart);
try {
if (doautostart) {
String[] args = null;
if (_bc.isStartWithReset()) {
args = new String[] { "-reset" };
}
startBridge(b, args);
}
} catch (BridgeException e) {
if (e.getStatus() == Status.CREATED) {
continue;
}
throw e;
}
}
} catch (Exception e) {
try {
stopBridge(null, null, null);
} catch (Throwable t) {
}
throw e;
}
try {
var _cf = new ConnectionFactory();
_cf.setProperty(com.sun.messaging.ConnectionConfiguration.imqAddressList,
_bc.getBrokerServiceAddress("tcp", com.sun.messaging.jmq.ClientConstants.CONNECTIONTYPE_ADMIN));
_cf.setProperty(com.sun.messaging.ConnectionConfiguration.imqReconnectEnabled, "false");
_connection = _cf.createConnection(_user, _passwd);
_session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
_adminQueue = _session.createTemporaryQueue();
MessageConsumer mc = _session.createConsumer(_adminQueue);
mc.setMessageListener(this);
_connection.start();
} catch (Exception e) {
try {
stopBridge(null, null, null);
if (_connection != null) {
_connection.close();
}
} catch (Throwable t) {
}
throw e;
}
_state = State.STARTED;
}
/**
*
*/
public synchronized void loadBridge(String name) throws Exception {
_bc.logInfo("Loading bridge " + name, null);
Locale loc = _bmr.getLocale();
Bridge b = _bridges.get(name);
if (b != null) {
_bc.logInfo("Bridge " + name + " is already loaded.", null);
throw new BridgeException(_bmr.getString(_bmr.I_BRIDGE_ALREADY_LOADED, name), Status.NOT_MODIFIED);
}
/*
* Properties props = _bc.getBridgeConfig();
*
* String activekey = props.getProperty( BridgeBaseContext.PROP_PREFIX)+".activelist"; List<String> alist =
* BridgeUtil.getListProperty(activekey, props);
*
* String tmpn = null; boolean found = false; Iterator<String> itr = alist.iterator(); while (itr.hasNext()) { tmpn =
* itr.next(); if (tmpn.equals(name)) { found = true; break; } } if (!found) { String oldactives =
* props.getProperty(activekey); String newactives = oldactives+","+name; Properties p = new Properties();
* p.setProperty(activekey, newactives); _bc.updateBridgeConfig(p); }
*/
Properties props = _bc.getBridgeConfig();
String type = props.getProperty(props.getProperty(BridgeBaseContext.PROP_PREFIX) + "." + name + ".type");
if (type == null) {
String emsg = _bmr.getKString(_bmr.E_LOAD_BRIDGE_NO_TYPE, name);
_bc.logError(emsg, null);
throw new BridgeException(emsg);
}
type = type.toLowerCase(loc);
if (!type.toUpperCase(loc).equals(Bridge.JMS_TYPE) && !type.toUpperCase(loc).equals(Bridge.STOMP_TYPE)) {
String emsg = _bmr.getKString(_bmr.X_BRIDGE_TYPE_NOSUPPORT, name, type);
_bc.logError(emsg, null);
throw new BridgeException(emsg);
}
String classn = props.getProperty(props.getProperty(BridgeBaseContext.PROP_PREFIX) + "." + type + ".class");
if (classn == null) {
String emsg = _bmr.getKString(_bmr.E_LOAD_BRIDGE_NO_CLASS, name);
_bc.logError(emsg, null);
throw new BridgeException(emsg);
}
if (_bc.isRunningOnNucleus()) {
b = habitat.getService(Bridge.class, type.toUpperCase(loc));
} else {
b = (Bridge) Class.forName(classn).getDeclaredConstructor().newInstance();
}
if (!b.isMultipliable() && !b.getType().toLowerCase().equals(name.toLowerCase())) {
String emsg = _bmr.getKString(_bmr.E_BRIDGE_NAME_TYPE_NOT_SAME, name, b.getType());
_bc.logError(emsg, null);
throw new BridgeException(emsg);
}
if (DEBUG) {
_bc.logInfo("Loaded brigde " + name + " by classloader " + b.getClass().getClassLoader() + "(parent:" + this.getClass().getClassLoader() + ")",
null);
}
b.setName(name);
_bridges.put(name, b);
_bc.logInfo("Loaded bridge " + name, null);
}
/**
*
* @return true if started successful, false if asynchronously started
*
* @throws Exception if start failed
*/
public synchronized boolean startBridge(String name, String[] args, String type) throws Exception {
if (type != null && !type.equals(Bridge.JMS_TYPE) && !type.equals(Bridge.STOMP_TYPE)) {
throw new IllegalArgumentException(_bmr.getKString(_bmr.X_BRIDGE_INVALID_TYPE, type));
}
Bridge b = null;
if (name != null) {
b = _bridges.get(name);
if (b == null) {
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_NAME_NOT_FOUND, name));
}
if (type != null && !type.equals(b.getType())) {
String[] eparam = new String[] { name, b.getType(), type };
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_TYPE_MISMATCH, eparam));
}
return startBridge(b, args);
}
boolean async = false;
for (Map.Entry<String, Bridge> pair : _bridges.entrySet()) {
b = pair.getValue();
if (type != null && !b.getType().equals(type)) {
continue;
}
if (!startBridge(b, args)) {
async = true;
}
}
return !async;
}
/**
*
* @return true if started successful, false if asynchronously started
*
* @throws Exception if start failed
*/
private boolean startBridge(Bridge b, String[] args) throws Exception {
if (args == null) {
_bc.logInfo(_bmr.getString(_bmr.I_STARTING_BRIDGE, b.getName()), null);
}
if (b.getState() == Bridge.State.STARTED && args == null) {
_bc.logInfo(_bmr.getString(_bmr.I_BRIDGE_ALREADY_STARTED, b.getName()), null);
return true;
}
BridgeContext ctx = new BridgeContextImpl(_bc, b.getName());
StringBuilder sf = new StringBuilder();
sf.append(_bmr.getString(_bmr.I_STARTING_BRIDGE_WITH_PROPS, b.getName()));
String key = null;
Enumeration e = ctx.getConfig().propertyNames();
while (e.hasMoreElements()) {
key = (String) e.nextElement();
sf.append("\t" + key + "=" + ctx.getConfig().getProperty(key) + "\n");
}
if (args == null) {
_bc.logInfo(sf.toString(), null);
}
try {
boolean ret = b.start(ctx, args);
if (args == null) {
_bc.logInfo(_bmr.getString(_bmr.I_STARTED_BRIDGE, b.getName()), null);
}
return ret;
} catch (Exception e1) {
_bc.logError(_bmr.getKString(_bmr.E_START_BRIDGE_FAILED, b.getName(), e1.getMessage()), null);
throw e1;
}
}
public synchronized void stopBridge(String name, String[] args, String type) throws Exception {
if (type != null && !type.equals(Bridge.JMS_TYPE) && !type.equals(Bridge.STOMP_TYPE)) {
throw new IllegalArgumentException(_bmr.getKString(_bmr.X_BRIDGE_INVALID_TYPE, type));
}
Bridge b = null;
if (name != null) {
b = _bridges.get(name);
if (b == null) {
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_NAME_NOT_FOUND, name));
}
if (type != null && !type.equals(b.getType())) {
String[] eparam = new String[] { name, b.getType(), type };
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_TYPE_MISMATCH, eparam));
}
stopBridge(b, args);
return;
}
for (Map.Entry<String, Bridge> pair : _bridges.entrySet()) {
b = pair.getValue();
if (type != null && !b.getType().equals(type)) {
continue;
}
stopBridge(b, args);
}
}
public synchronized void pauseBridge(String name, String[] args, String type) throws Exception {
if (type != null && !type.equals(Bridge.JMS_TYPE) && !type.equals(Bridge.STOMP_TYPE)) {
throw new IllegalArgumentException(_bmr.getKString(_bmr.X_BRIDGE_INVALID_TYPE, type));
}
if (name == null && type == null) {
throw new UnsupportedOperationException(_bmr.getKString(_bmr.X_BRIDGE_PAUSE_NO_TYPE));
}
Bridge b = null;
if (name != null) {
b = _bridges.get(name);
if (b == null) {
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_NAME_NOT_FOUND, name));
}
if (type != null && !type.equals(b.getType())) {
String[] eparam = new String[] { name, b.getType(), type };
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_TYPE_MISMATCH, eparam));
}
pauseBridge(b, args);
return;
}
for (Map.Entry<String, Bridge> pair : _bridges.entrySet()) {
b = pair.getValue();
if (type != null && !b.getType().equals(type)) {
continue;
}
pauseBridge(b, args);
}
}
public synchronized void resumeBridge(String name, String[] args, String type) throws Exception {
if (type != null && !type.equals(Bridge.JMS_TYPE) && !type.equals(Bridge.STOMP_TYPE)) {
throw new IllegalArgumentException(_bmr.getKString(_bmr.X_BRIDGE_INVALID_TYPE, type));
}
if (name == null && type == null) {
throw new UnsupportedOperationException(_bmr.getKString(_bmr.X_BRIDGE_RESUME_NO_TYPE));
}
Bridge b = null;
if (name != null) {
b = _bridges.get(name);
if (b == null) {
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_NAME_NOT_FOUND, name));
}
if (type != null && !type.equals(b.getType())) {
String[] eparam = new String[] { name, b.getType(), type };
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_TYPE_MISMATCH, eparam));
}
resumeBridge(b, args);
return;
}
for (Map.Entry<String, Bridge> pair : _bridges.entrySet()) {
b = pair.getValue();
if (type != null && !b.getType().equals(type)) {
continue;
}
resumeBridge(b, args);
}
}
public synchronized void pauseBridge(Bridge b, String[] args) throws Exception {
_bc.logInfo(_bmr.getString(_bmr.I_PAUSING_BRIDGE, b.getName()), null);
if (b.getState() == Bridge.State.PAUSED && args == null) {
_bc.logInfo(_bmr.getString(_bmr.I_BRIDGE_ALREADY_PAUSED, b.getName()), null);
return;
}
b.pause(new BridgeContextImpl(_bc, b.getName()), args);
_bc.logInfo(_bmr.getString(_bmr.I_PAUSED_BRIDGE, b.getName()), null);
}
public synchronized void resumeBridge(Bridge b, String[] args) throws Exception {
_bc.logInfo(_bmr.getString(_bmr.I_RESUMING_BRIDGE, b.getName()), null);
if (b.getState() == Bridge.State.STARTED && args == null) {
_bc.logInfo(_bmr.getString(_bmr.I_BRIDGE_IS_RUNNING, b.getName()), null);
return;
}
b.resume(new BridgeContextImpl(_bc, b.getName()), args);
_bc.logInfo(_bmr.getString(_bmr.I_RESUMED_BRIDGE, b.getName()), null);
}
public synchronized void unloadBridge(String name) throws Exception {
_bc.logInfo("Unloading bridge " + name, null);
Bridge b = _bridges.get(name);
if (b == null) {
_bc.logInfo("Bridge " + name + " is not loaded.", null);
throw new BridgeException("Bridge " + name + " is not loaded.", Status.NOT_MODIFIED);
}
stopBridge(name, null, null);
_bridges.remove(name);
b.setName(null);
Properties props = _bc.getBridgeConfig();
List<String> alist = BridgeUtil.getListProperty(props.getProperty(BridgeBaseContext.PROP_PREFIX) + ".activelist", props);
String tmpn = null;
StringBuilder sbuf = new StringBuilder();
Iterator<String> itr = alist.iterator();
while (itr.hasNext()) {
tmpn = itr.next();
if (!tmpn.equals(name)) {
if (sbuf.length() > 0) {
sbuf.append(',');
}
sbuf.append(tmpn);
}
}
Properties p = new Properties();
p.setProperty(props.getProperty(BridgeBaseContext.PROP_PREFIX) + ".activelist", sbuf.toString());
_bc.updateBridgeConfig(p);
_bc.logInfo("Unloaded bridge " + name, null);
}
private void stopBridge(Bridge b, String[] args) throws Exception {
if (b.getState() == Bridge.State.STOPPED && args == null) {
_bc.logDebug(_bmr.getString(_bmr.I_BRIDGE_ALREADY_STOPPED, b.getName()), null);
return;
}
_bc.logInfo(_bmr.getString(_bmr.I_STOPPING_BRIDGE, b.getName()), null);
b.stop(new BridgeContextImpl(_bc, b.getName()), args);
_bc.logInfo(_bmr.getString(_bmr.I_STOPPED_BRIDGE, b.getName()), null);
}
public synchronized ArrayList<BridgeCmdSharedReplyData> listBridge(String name, String[] args, String type, BridgeManagerResources bmr) throws Exception {
if (type != null && !type.equals(Bridge.JMS_TYPE) && !type.equals(Bridge.STOMP_TYPE)) {
throw new IllegalArgumentException(_bmr.getString(_bmr.X_BRIDGE_INVALID_TYPE, type));
}
if (name == null) {
_bc.logDebug("Listing all bridges (type=" + type + ")", null);
BridgeCmdSharedReplyData reply = new BridgeCmdSharedReplyData(3, 4, "-");
String oneRow[] = new String[3];
oneRow[0] = bmr.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_BRIDGE_NAME);
oneRow[1] = bmr.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_BRIDGE_TYPE);
oneRow[2] = bmr.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_BRIDGE_STATE);
reply.addTitle(oneRow);
Bridge b = null;
for (Map.Entry<String, Bridge> pair : _bridges.entrySet()) {
b = pair.getValue();
if (type != null && !b.getType().equals(type)) {
continue;
}
oneRow[0] = b.getName();
oneRow[1] = b.getType();
oneRow[2] = b.getState().toString(bmr);
reply.add(oneRow);
}
_bc.logDebug("Listed all bridges (type=" + type + ")", null);
ArrayList<BridgeCmdSharedReplyData> replys = new ArrayList<>();
replys.add(reply);
return replys;
}
if (args == null) {
_bc.logInfo(_bmr.getString(_bmr.I_LISTING_BRIDGE, name), null);
} else {
_bc.logInfo(_bmr.getString(_bmr.I_LISTING_BRIDGE_WITH, name, BridgeUtil.toString(args)), null);
}
Bridge b = _bridges.get(name);
if (b == null) {
String emsg = _bmr.getKString(_bmr.X_BRIDGE_NAME_NOT_FOUND, name);
_bc.logError(emsg, null);
throw new BridgeException(emsg);
}
BridgeContext bc = new BridgeContextImpl(_bc, b.getName());
return b.list(bc, args, bmr);
}
/**
* Stop the bridge service manager
*/
@Override
public void stop() throws Exception {
if (_state == State.STOPPING || _state == State.STOPPED) {
return;
}
synchronized (this) {
if (_bc == null) {
throw new BridgeException(_bmr.getString(_bmr.X_BRIDGE_SERVICE_MANAGER_NOT_INITED));
}
_state = State.STOPPING;
try {
stopBridge(null, null, null);
} catch (Throwable t) {
if (t instanceof java.util.concurrent.RejectedExecutionException) {
_bc.logDebug(_bmr.getKString(_bmr.W_EXCEPTION_STOP_BRIDGES, t.getMessage()), null);
} else {
_bc.logWarn(_bmr.getKString(_bmr.W_EXCEPTION_STOP_BRIDGES, t.getMessage()), t);
}
}
if (_connection != null) {
try {
_connection.close();
} catch (Throwable t) {
if (DEBUG) {
_bc.logWarn(_bmr.getKString(_bmr.W_EXCEPTION_CLOSE_ADMIN_CONN, t.getMessage()), t);
} else {
_bc.logWarn(_bmr.getKString(_bmr.W_EXCEPTION_CLOSE_ADMIN_CONN, t.getMessage()), null);
}
}
}
_state = State.STOPPED;
}
}
public Bridge getBridge(String name) throws Exception {
Bridge b = _bridges.get(name);
if (b == null) {
throw new BridgeException(_bmr.getKString(_bmr.X_BRIDGE_NAME_NOT_FOUND, name));
}
return b;
}
@Override
public BridgeBaseContext getBridgeBaseContext() {
return _bc;
}
@Override
public void onException(JMSException e) {
if (_bc.isEmbeded()) {
_bc.logError(_bmr.getKString(_bmr.E_EXCEPTION_OCCURRED_ADMIN_CONN, e.getMessage()), e);
} else {
// not supported else do reconnect
_bc.logError("Not supported: bridge servie manager is not embeded!", null);
}
}
@Override
public void onMessage(Message msg) {
if (_state != State.STARTED) {
String emsg = _bmr.getKString(_bmr.X_BRIDGE_SERVICE_MANAGER_NOT_RUNNING);
_bc.logInfo(emsg, null);
_adminHandler.sendReply(_session, msg, null, Status.UNAVAILABLE, emsg, _bmr);
}
if (!(msg instanceof ObjectMessage)) {
String emsg = "Unexpected bridge admin message type: " + msg.getClass().getName();
_bc.logError(emsg, null);
_adminHandler.sendReply(_session, msg, null, Status.BAD_REQUEST, emsg, _bmr);
return;
}
_adminHandler.handle(_session, (ObjectMessage) msg);
}
public static synchronized BridgeManagerResources getBridgeManagerResources() {
if (_bmr == null) {
_bmr = BridgeManagerResources.getResources(Locale.getDefault());
}
return _bmr;
}
public static BridgeManagerResources getBridgeManagerResources(Locale l) {
if (l == null) {
return getBridgeManagerResources();
}
BridgeManagerResources bmr = _bmrs.get(l);
if (bmr != null) {
return bmr;
}
synchronized (_bmrs) {
bmr = _bmrs.get(l);
if (bmr == null) {
bmr = BridgeManagerResources.getResources(l);
_bmrs.put(l, bmr);
}
return bmr;
}
}
public boolean getDEBUG() {
return DEBUG;
}
public static void main(String[] args) {
}
}