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 2015-2016 ForgeRock AS.
15   */
16  package org.forgerock.audit.handlers.jdbc;
17  
18  import java.sql.PreparedStatement;
19  import java.util.Collections;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import org.forgerock.audit.events.handlers.EventHandlerConfiguration;
24  import org.forgerock.util.Reject;
25  
26  import com.fasterxml.jackson.annotation.JsonProperty;
27  import com.fasterxml.jackson.annotation.JsonPropertyDescription;
28  
29  /**
30   * Configures the JDBC mapping and connection pool.
31   */
32  public class JdbcAuditEventHandlerConfiguration extends EventHandlerConfiguration {
33  
34      @JsonPropertyDescription("audit.handlers.jdbc.connectionPool")
35      private ConnectionPool connectionPool = new ConnectionPool();
36  
37      @JsonProperty(required = true)
38      @JsonPropertyDescription("audit.handlers.jdbc.tableMappings")
39      private List<TableMapping> tableMappings = new LinkedList<>();
40  
41      @JsonProperty(required = true)
42      @JsonPropertyDescription("audit.handlers.jdbc.databaseType")
43      private String databaseType;
44  
45      @JsonPropertyDescription("audit.handlers.jdbc.buffering")
46      private EventBufferingConfiguration buffering = new EventBufferingConfiguration();
47  
48      /**
49       * Gets the table mappings for the audit events.
50       * @return The table mappings for the audit events.
51       */
52      public List<TableMapping> getTableMappings() {
53          if (tableMappings == null) {
54              return Collections.emptyList();
55          }
56          return Collections.unmodifiableList(tableMappings);
57      }
58  
59      /**
60       * Sets the table mappings for the audit events.
61       * @param tableMappings The table mappings for the audit events.
62       */
63      public void setTableMappings(List<TableMapping> tableMappings) {
64          this.tableMappings = tableMappings;
65      }
66  
67      /**
68       * Gets the connection pool settings.
69       * @return The connection pool settings.
70       */
71      public ConnectionPool getConnectionPool() {
72          return connectionPool;
73      }
74  
75      /**
76       * Sets the connection pool settings.
77       * @param connectionPool The connection pool settings.
78       */
79      public void setConnectionPool(ConnectionPool connectionPool) {
80          this.connectionPool = connectionPool;
81      }
82  
83      /**
84       * Gets the type of the database.
85       * @return The type of the database.
86       */
87      public String getDatabaseType() {
88          return databaseType;
89      }
90  
91      /**
92       * Sets the type of the database.
93       * @param databaseType The type of the database.
94       */
95      public void setDatabaseType(String databaseType) {
96          this.databaseType = databaseType;
97      }
98  
99      @Override
100     public boolean isUsableForQueries() {
101         return true;
102     }
103 
104     /**
105      * Configuration for a connection pool.
106      */
107     public static class ConnectionPool {
108         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.dataSourceClassName")
109         private String dataSourceClassName;
110 
111         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.jdbcUrl")
112         private String jdbcUrl;
113 
114         @JsonProperty(required = true)
115         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.username")
116         private String username;
117 
118         @JsonProperty(required = true)
119         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.password")
120         private String password;
121 
122         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.autoCommit")
123         private boolean autoCommit = true;
124 
125         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.connectionTimeout")
126         private int connectionTimeout = 30000;
127 
128         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.idleTimeout")
129         private int idleTimeout = 600000;
130 
131         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.maxLifetime")
132         private int maxLifetime = 1800000;
133 
134         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.minIdle")
135         private int minIdle = 10;
136 
137         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.maxPoolSize")
138         private int maxPoolSize = 10;
139 
140         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.poolName")
141         private String poolName;
142 
143         @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.driverClassName")
144         private String driverClassName;
145 
146         /**
147          * Gets the class name of the driver to use for the jdbc connection.
148          * @return The class name.
149          */
150         public String getDriverClassName() {
151             return driverClassName;
152         }
153 
154         /**
155          * Sets the class name of the driver to use for the jdbc connection.
156          * @param driverClassName The driver class name.
157          */
158         public void setDriverClassName(String driverClassName) {
159             this.driverClassName = driverClassName;
160         }
161 
162         /**
163          * Gets the datasource class name for the JDBC database.
164          * @return The JDBC driver class
165          */
166         public String getDataSourceClassName() {
167             return dataSourceClassName;
168         }
169 
170         /**
171          * Sets the datasource class name for the configured database.
172          * @param driverClass The name of the JDBC driver class.
173          */
174         public void setDataSourceClassName(final String driverClass) {
175             this.dataSourceClassName = driverClass;
176         }
177 
178         /**
179          * Gets the JDBC database url.
180          * @return The JDBC database url.
181          */
182         public String getJdbcUrl() {
183             return jdbcUrl;
184         }
185 
186         /**
187          * Sets the JDBC database url.
188          * @param jdbcUrl The name of the JDBC database url.
189          */
190         public void setJdbcUrl(final String jdbcUrl) {
191             this.jdbcUrl = jdbcUrl;
192         }
193 
194         /**
195          * Gets the username to use to connect to the JDBC database.
196          * @return The username to used to connect to the JDBC database.
197          */
198         public String getUsername() {
199             return username;
200         }
201 
202         /**
203          * Sets the username to use to connect to the JDBC database.
204          * @param username The username to used to connect to the JDBC database.
205          */
206         public void setUsername(final String username) {
207             this.username = username;
208         }
209 
210         /**
211          * Gets the password to use to connect to the JDBC database.
212          * @return The password to used to connect to the JDBC database.
213          */
214         public String getPassword() {
215             return password;
216         }
217 
218         /**
219          * Sets the password to use to connect to the JDBC database.
220          * @param password The password to used to connect to the JDBC database.
221          */
222         public void setPassword(final String password) {
223             this.password = password;
224         }
225 
226         /**
227          * Gets the name of the connection pool.
228          * @return The name of the connection pool.
229          */
230         public String getPoolName() {
231             return poolName;
232         }
233 
234         /**
235          * Sets the name of the connection pool.
236          * @param poolName The name of the connection pool.
237          */
238         public void setPoolName(String poolName) {
239             this.poolName = poolName;
240         }
241 
242         /**
243          * Gets the maximum size of the connection pool.
244          * @return The maximum size of the connection pool.
245          */
246         public int getMaxPoolSize() {
247             return maxPoolSize;
248         }
249 
250         /**
251          * Sets the maximum size of the connection pool.
252          * @param maxPoolSize The maximum pool size of the connection pool.
253          */
254         public void setMaxPoolSize(int maxPoolSize) {
255             this.maxPoolSize = maxPoolSize;
256         }
257 
258         /**
259          * Gets the minimum number of idle connections in the connection pool.
260          * @return The minimum number of idle connections in the connection pool.
261          */
262         public int getMinIdle() {
263             return minIdle;
264         }
265 
266         /**
267          * Sets the minimum number of idle connections in the connection pool.
268          * @param minIdle The minimum number of idle connections in the connection pool.
269          */
270         public void setMinIdle(int minIdle) {
271             this.minIdle = minIdle;
272         }
273 
274         /**
275          * Gets the maximum lifetime of a connection in the connection pool.
276          * @return The maximum lifetime of a connection in the connection pool.
277          */
278         public int getMaxLifetime() {
279             return maxLifetime;
280         }
281 
282         /**
283          * Sets the maximum lifetime of a connection in the connection pool.
284          * @param maxLifetime The maximum lifetime of a connection in the connection pool.
285          */
286         public void setMaxLifetime(int maxLifetime) {
287             this.maxLifetime = maxLifetime;
288         }
289 
290         /**
291          * Gets the maximum time a connection is allowed to be idle.
292          * @return The maximum time a connection is allowed to be idle.
293          */
294         public int getIdleTimeout() {
295             return idleTimeout;
296         }
297 
298         /**
299          * Sets the maximum time a connection is allowed to be idle.
300          * @param idleTimeout The maximum time a connection is allowed to be idle.
301          */
302         public void setIdleTimeout(int idleTimeout) {
303             this.idleTimeout = idleTimeout;
304         }
305 
306         /**
307          * Gets the maximum amount of time to wait for a connection from the connection pool.
308          * @return The maximum amount of time to wait for a connection from the connection pool.
309          */
310         public int getConnectionTimeout() {
311             return connectionTimeout;
312         }
313 
314         /**
315          * Sets the maximum amount of time to wait for a connection from the connection pool.
316          * @param connectionTimeout The maximum amount of time to wait for a connection from the connection pool.
317          */
318         public void setConnectionTimeout(int connectionTimeout) {
319             this.connectionTimeout = connectionTimeout;
320         }
321 
322         /**
323          * Gets the auto commit value.
324          * @return The auto commit value.
325          */
326         public boolean getAutoCommit() {
327             return autoCommit;
328         }
329 
330         /**
331          * Sets the auto commit value.
332          * @param autoCommit The auto commit value.
333          */
334         public void setAutoCommit(boolean autoCommit) {
335             this.autoCommit = autoCommit;
336         }
337     }
338 
339     /**
340      * Returns the configuration for events buffering.
341      *
342      * @return the configuration
343      */
344     public EventBufferingConfiguration getBuffering() {
345         return buffering;
346     }
347 
348     /**
349      * Sets the configuration for events buffering.
350      *
351      * @param bufferingConfiguration
352      *            The configuration
353      */
354     public void setBufferingConfiguration(EventBufferingConfiguration bufferingConfiguration) {
355         this.buffering = bufferingConfiguration;
356     }
357 
358     /**
359      * Configuration of event buffering.
360      */
361     public static class EventBufferingConfiguration {
362 
363         @JsonPropertyDescription("audit.handlers.jdbc.buffering.enabled")
364         private boolean enabled = false;
365 
366         @JsonPropertyDescription("audit.handlers.jdbc.buffering.autoFlush")
367         private boolean autoFlush = true;
368 
369         @JsonPropertyDescription("audit.handlers.jdbc.buffering.maxSize")
370         private int maxSize = 5000;
371 
372         @JsonPropertyDescription("audit.handlers.jdbc.buffering.interval")
373         private String writeInterval = "disabled";
374 
375         @JsonPropertyDescription("audit.handlers.jdbc.buffering.writerThreads")
376         private int writerThreads = 1;
377 
378         @JsonPropertyDescription("audit.handlers.jdbc.buffering.maxBatchedEvents")
379         private int maxBatchedEvents = 100;
380 
381 
382         /**
383          * Indicates if event buffering is enabled.
384          *
385          * @return {@code true} if buffering is enabled.
386          */
387         public boolean isEnabled() {
388             return enabled;
389         }
390 
391         /**
392          * Sets the buffering status.
393          *
394          * @param enabled
395          *            Indicates if buffering is enabled.
396          */
397         public void setEnabled(boolean enabled) {
398             this.enabled = enabled;
399         }
400 
401         /**
402          * Indicates if events are automatically flushed after being written.
403          *
404          * @return {@code true} if events must be flushed
405          */
406         public boolean isAutoFlush() {
407             return autoFlush;
408         }
409 
410         /**
411          * Sets the auto flush indicator.
412          *
413          * @param auto
414          *            Indicates if events are automatically flushed after being written.
415          */
416         public void setAutoFlush(boolean auto) {
417             this.autoFlush = auto;
418         }
419 
420         /**
421          * Returns the maximum size of the queue.
422          *
423          * @return maxSize Maximum number of events in the queue.
424          */
425         public int getMaxSize() {
426             return maxSize;
427         }
428 
429         /**
430          * Sets the maximum size of the events queue.
431          *
432          * @param maxSize
433          *            Maximum number of events in the queue.
434          */
435         public void setMaxSize(int maxSize) {
436             Reject.ifFalse(maxSize >= 1);
437             this.maxSize = maxSize;
438         }
439 
440         /**
441          * Gets the interval to write the queued buffered events.
442          * @return The interval as a string.
443          */
444         public String getWriteInterval() {
445             return writeInterval;
446         }
447 
448         /**
449          * Sets the interval to write the queued buffered events.
450          * @param writeInterval The interval as a string.
451          */
452         public void setWriteInterval(String writeInterval) {
453             this.writeInterval = writeInterval;
454         }
455 
456         /**
457          * Gets the number of writer threads to use to write buffered events.
458          * @return The number of writer threads.
459          */
460         public int getWriterThreads() {
461             return writerThreads;
462         }
463 
464         /**
465          * Sets the number of writer threads to use to write buffered events.
466          * @param writerThreads The number of writer threads.
467          */
468         public void setWriterThreads(int writerThreads) {
469             Reject.ifFalse(writerThreads >= 1);
470             this.writerThreads = writerThreads;
471         }
472 
473         /**
474          * Gets the maximum number of events that can be batched into a {@link PreparedStatement}.
475          * @return The maximum number of batches.
476          */
477         public int getMaxBatchedEvents() {
478             return maxBatchedEvents;
479         }
480 
481         /**
482          * Sets the maximum number of events that can be batched into a {@link PreparedStatement}.
483          * @param maxBatchedEvents The maximum number of batches.
484          */
485         public void setMaxBatchedEvents(int maxBatchedEvents) {
486             this.maxBatchedEvents = maxBatchedEvents;
487         }
488     }
489 }