1 /*
2 * The contents of this file are subject to the terms of the Common Development and
3 * Distribution License (the License). You may not use this file except in compliance with the
4 * License.
5 *
6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7 * specific language governing permission and limitations under the License.
8 *
9 * When distributing Covered Software, include this CDDL Header Notice in each file and include
10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11 * Header, with the fields enclosed by brackets [] replaced by your own identifying
12 * information: "Portions copyright [year] [name of copyright owner]".
13 *
14 * Copyright 2016 ForgeRock AS.
15 */
16
17 package org.forgerock.audit.handlers.jms;
18
19 import com.fasterxml.jackson.annotation.JsonProperty;
20 import com.fasterxml.jackson.annotation.JsonPropertyDescription;
21 import jakarta.jms.ConnectionFactory;
22 import jakarta.jms.Session;
23 import jakarta.jms.Topic;
24 import java.util.Collections;
25 import java.util.Map;
26 import javax.naming.InitialContext;
27 import org.forgerock.audit.events.handlers.EventHandlerConfiguration;
28 import org.forgerock.util.Reject;
29
30 /**
31 * Configuration object for the {@link JmsAuditEventHandler}.
32 * <p/>
33 * This configuration object can be created from JSON. Example of valid JSON configuration:
34 * <p/>
35 * <pre>
36 * {
37 * "name" : "jms",
38 * "topics": [ "access", "activity", "config", "authentication" ],
39 * "deliveryMode" : "NON_PERSISTENT",
40 * "sessionMode" : "AUTO",
41 * "jndi" : {
42 * "contextProperties" : {
43 * "initialContextFactory" : "org.apache.activemq.jndi.ActiveMQInitialContextFactory",
44 * "providerUrl" : "tcp://localhost:61616"
45 * },
46 * "topicName" : "audit",
47 * "connectionFactoryName" : "connectionFactory"
48 * }
49 * }
50 * </pre>
51 */
52 public class JmsAuditEventHandlerConfiguration extends EventHandlerConfiguration {
53
54 @JsonProperty(required = true)
55 @JsonPropertyDescription("audit.handlers.jms.deliveryMode")
56 private DeliveryModeConfig deliveryMode;
57
58 @JsonProperty(required = true)
59 @JsonPropertyDescription("audit.handlers.jms.sessionMode")
60 private SessionModeConfig sessionMode;
61
62 @JsonPropertyDescription("audit.handlers.jms.batch")
63 private BatchPublisherConfiguration batch = new BatchPublisherConfiguration();
64
65 @JsonPropertyDescription("audit.handlers.jms.jndi")
66 private JndiConfiguration jndi = new JndiConfiguration();
67
68 /**
69 * Returns the delivery mode configuration that should be used when publishing the JMS messages.
70 *
71 * @return the delivery mode.
72 */
73 public DeliveryModeConfig getDeliveryMode() {
74 return deliveryMode;
75 }
76
77 /**
78 * Sets the delivery mode configuration that should be used when publishing the JMS messages.
79 *
80 * @param deliveryMode the delivery mode
81 */
82 public void setDeliveryMode(DeliveryModeConfig deliveryMode) {
83 this.deliveryMode = deliveryMode;
84 }
85
86 /**
87 * Returns the mode that the JMS session should use when publishing the JMS messages.
88 *
89 * @return the session's mode.
90 * @see Session#getAcknowledgeMode()
91 */
92 public SessionModeConfig getSessionMode() {
93 return sessionMode;
94 }
95
96 /**
97 * Sets the session mode that the JMS session should use when publishing the JMS messages.
98 *
99 * @param sessionMode the session's acknowledgement mode.
100 * @see Session#getAcknowledgeMode()
101 */
102 public void setSessionMode(SessionModeConfig sessionMode) {
103 this.sessionMode = sessionMode;
104 }
105
106 /**
107 * Returns the configuration used to initialize the batch publisher.
108 *
109 * @return the configuration used to initialize the batch publisher.
110 */
111 public BatchPublisherConfiguration getBatch() {
112 return batch;
113 }
114
115 /**
116 * Sets the configuration used to initialize the batch publisher.
117 *
118 * @param batch the configuration used to initialize the batch publisher.
119 */
120 public void setBatch(BatchPublisherConfiguration batch) {
121 this.batch = batch;
122 }
123
124 /**
125 * Gets the {@link JndiConfiguration}.
126 * @return The {@link JndiConfiguration}.
127 */
128 public JndiConfiguration getJndi() {
129 return jndi;
130 }
131
132 /**
133 * Sets the {@link JndiConfiguration}.
134 * @param jndi The {@link JndiConfiguration}
135 */
136 public void setJndi(JndiConfiguration jndi) {
137 this.jndi = jndi;
138 }
139
140 @Override
141 public boolean isUsableForQueries() {
142 return false;
143 }
144
145
146 /**
147 * Stores the JNDI context properties and lookup names.
148 */
149 public static class JndiConfiguration {
150
151 @JsonPropertyDescription("audit.handlers.jms.contextProperties")
152 private Map<String, String> contextProperties = Collections.emptyMap();
153
154 @JsonPropertyDescription("audit.handlers.jms.topicName")
155 private String topicName = "audit";
156
157 @JsonPropertyDescription("audit.handlers.jms.connectionFactoryName")
158 private String connectionFactoryName = "connectionFactory";
159
160 /**
161 * Gets the Jndi {@link InitialContext} properties.
162 * @return The {@link InitialContext} properties.
163 */
164 public Map<String, String> getContextProperties() {
165 return contextProperties;
166 }
167
168 /**
169 * Sets the Jndi {@link InitialContext} properties.
170 * @param contextProperties The {@link InitialContext} properties.
171 */
172 public void setContextProperties(Map<String, String> contextProperties) {
173 Reject.ifNull(contextProperties, "The jndi context properties can't be null");
174 this.contextProperties = Collections.unmodifiableMap(contextProperties);
175 }
176
177 /**
178 * Returns the jndi lookup name for the JMS {@link Topic} to which messages will be published.
179 * Do not confuse this with Audit Topics.
180 * @see InitialContext#lookup(String)
181 *
182 * @return The jndi lookup name for the JMS {@link Topic} to which messages will be published.
183 */
184 public String getTopicName() {
185 return topicName;
186 }
187
188 /**
189 * Sets the jndi lookup name for the JMS {@link Topic} for which the messages will be published on.
190 * @see InitialContext#lookup(String)
191 *
192 * @param jmsTopicName The jndi lookup name for the JMS {@link Topic}.
193 */
194 public void setJmsTopicName(String jmsTopicName) {
195 this.topicName = jmsTopicName;
196 }
197
198 /**
199 * Returns the jndi lookup name for the JMS {@link ConnectionFactory} to which messages will be published.
200 * Do not confuse this with Audit Topics.
201 * @see InitialContext#lookup(String)
202 *
203 * @return The jndi lookup name for the JMS {@link ConnectionFactory} to which messages will be published.
204 */
205 public String getConnectionFactoryName() {
206 return connectionFactoryName;
207 }
208
209 /**
210 * Sets the jndi lookup name for the JMS {@link ConnectionFactory} for which the messages will be published on.
211 * @see InitialContext#lookup(String)
212 *
213 * @param jmsConnectionFactoryName The jndi lookup name for the JMS {@link ConnectionFactory}.
214 */
215 public void setJmsConnectionFactoryName(String jmsConnectionFactoryName) {
216 this.connectionFactoryName = jmsConnectionFactoryName;
217 }
218 }
219 }