1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.audit.handlers.jms;
18
19 import javax.jms.Connection;
20 import javax.jms.ConnectionFactory;
21 import javax.jms.JMSException;
22 import javax.jms.MessageProducer;
23 import javax.jms.Session;
24 import javax.jms.Topic;
25
26 import org.forgerock.json.resource.InternalServerErrorException;
27 import org.forgerock.json.resource.ResourceException;
28 import org.forgerock.util.Reject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35 class JmsResourceManager {
36 private static final Logger logger = LoggerFactory.getLogger(JmsResourceManager.class);
37
38 private final ConnectionFactory connectionFactory;
39
40
41
42
43
44
45 private final DeliveryModeConfig deliveryMode;
46
47
48
49
50
51 private SessionModeConfig sessionMode;
52
53
54
55
56 private Connection connection;
57
58
59
60
61 private Topic topic;
62
63
64
65
66
67
68
69
70
71 public JmsResourceManager(JmsAuditEventHandlerConfiguration configuration, JmsContextManager jmsContextManager)
72 throws ResourceException {
73
74 Reject.ifNull(configuration.getDeliveryMode(), "JMS Delivery Mode is required");
75 Reject.ifNull(configuration.getSessionMode(), "JMS Session Mode is required");
76 sessionMode = configuration.getSessionMode();
77 deliveryMode = configuration.getDeliveryMode();
78
79
80 this.connectionFactory = jmsContextManager.getConnectionFactory();
81 this.topic = jmsContextManager.getTopic();
82 Reject.ifNull(this.connectionFactory, "Null ConnectionFactory is not permitted.");
83 Reject.ifNull(this.topic, "Null topic is not permitted.");
84
85 }
86
87
88
89
90
91 public void openConnection() throws JMSException {
92 connection = connectionFactory.createConnection();
93 connection.start();
94 logger.debug("JMS Connection created and started");
95 }
96
97
98
99
100
101
102 public void closeConnection() throws JMSException {
103 if (null != connection) {
104 try {
105 connection.close();
106 logger.debug("JMS Connection closed");
107 } finally {
108
109 connection = null;
110 }
111 }
112 }
113
114
115
116
117
118
119
120
121
122
123 public Session createSession() throws JMSException {
124 if (null == connection) {
125 throw new IllegalStateException(
126 "JMS Connection not available to create session. The JMS Audit Service requires a restart.");
127 }
128 return connection.createSession(false, sessionMode.getMode());
129 }
130
131
132
133
134
135
136
137
138
139
140 public MessageProducer createProducer(Session session) throws JMSException {
141 MessageProducer producer = session.createProducer(topic);
142 producer.setDeliveryMode(deliveryMode.getMode());
143 return producer;
144 }
145 }