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