001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015 ForgeRock AS. 015 */ 016package org.forgerock.audit.handlers.jdbc; 017 018import java.sql.PreparedStatement; 019import java.util.Collections; 020import java.util.LinkedList; 021import java.util.List; 022 023import org.forgerock.audit.events.handlers.EventHandlerConfiguration; 024import org.forgerock.util.Reject; 025 026import com.fasterxml.jackson.annotation.JsonProperty; 027import com.fasterxml.jackson.annotation.JsonPropertyDescription; 028 029/** 030 * Configures the JDBC mapping and connection pool. 031 */ 032public class JdbcAuditEventHandlerConfiguration extends EventHandlerConfiguration { 033 034 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool") 035 private ConnectionPool connectionPool = new ConnectionPool(); 036 037 @JsonProperty(required = true) 038 @JsonPropertyDescription("audit.handlers.jdbc.tableMappings") 039 private List<TableMapping> tableMappings = new LinkedList<>(); 040 041 @JsonProperty(required = true) 042 @JsonPropertyDescription("audit.handlers.jdbc.databaseType") 043 private String databaseType; 044 045 @JsonPropertyDescription("audit.handlers.jdbc.buffering") 046 private EventBufferingConfiguration buffering = new EventBufferingConfiguration(); 047 048 /** 049 * Gets the table mappings for the audit events. 050 * @return The table mappings for the audit events. 051 */ 052 public List<TableMapping> getTableMappings() { 053 if (tableMappings == null) { 054 return Collections.emptyList(); 055 } 056 return Collections.unmodifiableList(tableMappings); 057 } 058 059 /** 060 * Sets the table mappings for the audit events. 061 * @param tableMappings The table mappings for the audit events. 062 */ 063 public void setTableMappings(List<TableMapping> tableMappings) { 064 this.tableMappings = tableMappings; 065 } 066 067 /** 068 * Gets the connection pool settings. 069 * @return The connection pool settings. 070 */ 071 public ConnectionPool getConnectionPool() { 072 return connectionPool; 073 } 074 075 /** 076 * Sets the connection pool settings. 077 * @param connectionPool The connection pool settings. 078 */ 079 public void setConnectionPool(ConnectionPool connectionPool) { 080 this.connectionPool = connectionPool; 081 } 082 083 /** 084 * Gets the type of the database. 085 * @return The type of the database. 086 */ 087 public String getDatabaseType() { 088 return databaseType; 089 } 090 091 /** 092 * Sets the type of the database. 093 * @return The type of the database. 094 */ 095 public void setDatabaseType(String databaseType) { 096 this.databaseType = databaseType; 097 } 098 099 public static class ConnectionPool { 100 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.dataSourceClassName") 101 private String dataSourceClassName; 102 103 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.jdbcUrl") 104 private String jdbcUrl; 105 106 @JsonProperty(required = true) 107 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.username") 108 private String username; 109 110 @JsonProperty(required = true) 111 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.password") 112 private String password; 113 114 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.autoCommit") 115 private boolean autoCommit = true; 116 117 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.connectionTimeout") 118 private int connectionTimeout = 30000; 119 120 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.idleTimeout") 121 private int idleTimeout = 600000; 122 123 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.maxLifetime") 124 private int maxLifetime = 1800000; 125 126 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.minIdle") 127 private int minIdle = 10; 128 129 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.maxPoolSize") 130 private int maxPoolSize = 10; 131 132 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.poolName") 133 private String poolName; 134 135 @JsonPropertyDescription("audit.handlers.jdbc.connectionPool.driverClassName") 136 private String driverClassName; 137 138 /** 139 * Gets the class name of the driver to use for the jdbc connection. 140 * @return The class name. 141 */ 142 public String getDriverClassName() { 143 return driverClassName; 144 } 145 146 /** 147 * Sets the class name of the driver to use for the jdbc connection. 148 * @param driverClassName The driver class name. 149 */ 150 public void setDriverClassName(String driverClassName) { 151 this.driverClassName = driverClassName; 152 } 153 154 /** 155 * Gets the datasource class name for the JDBC database. 156 * @return The JDBC driver class 157 */ 158 public String getDataSourceClassName() { 159 return dataSourceClassName; 160 } 161 162 /** 163 * Sets the datasource class name for the configured database. 164 * @param driverClass The name of the JDBC driver class. 165 */ 166 public void setDataSourceClassName(final String driverClass) { 167 this.dataSourceClassName = driverClass; 168 } 169 170 /** 171 * Gets the JDBC database url. 172 * @return The JDBC database url. 173 */ 174 public String getJdbcUrl() { 175 return jdbcUrl; 176 } 177 178 /** 179 * Sets the JDBC database url. 180 * @param jdbcUrl The name of the JDBC database url. 181 */ 182 public void setJdbcUrl(final String jdbcUrl) { 183 this.jdbcUrl = jdbcUrl; 184 } 185 186 /** 187 * Gets the username to use to connect to the JDBC database. 188 * @return The username to used to connect to the JDBC database. 189 */ 190 public String getUsername() { 191 return username; 192 } 193 194 /** 195 * Sets the username to use to connect to the JDBC database. 196 * @param username The username to used to connect to the JDBC database. 197 */ 198 public void setUsername(final String username) { 199 this.username = username; 200 } 201 202 /** 203 * Gets the password to use to connect to the JDBC database. 204 * @return The password to used to connect to the JDBC database. 205 */ 206 public String getPassword() { 207 return password; 208 } 209 210 /** 211 * Sets the password to use to connect to the JDBC database. 212 * @param password The password to used to connect to the JDBC database. 213 */ 214 public void setPassword(final String password) { 215 this.password = password; 216 } 217 218 /** 219 * Gets the name of the connection pool. 220 * @return The name of the connection pool. 221 */ 222 public String getPoolName() { 223 return poolName; 224 } 225 226 /** 227 * Sets the name of the connection pool. 228 * @param poolName The name of the connection pool. 229 */ 230 public void setPoolName(String poolName) { 231 this.poolName = poolName; 232 } 233 234 /** 235 * Gets the maximum size of the connection pool. 236 * @return The maximum size of the connection pool. 237 */ 238 public int getMaxPoolSize() { 239 return maxPoolSize; 240 } 241 242 /** 243 * Sets the maximum size of the connection pool. 244 * @param maxPoolSize The maximum pool size of the connection pool. 245 */ 246 public void setMaxPoolSize(int maxPoolSize) { 247 this.maxPoolSize = maxPoolSize; 248 } 249 250 /** 251 * Gets the minimum number of idle connections in the connection pool. 252 * @return The minimum number of idle connections in the connection pool. 253 */ 254 public int getMinIdle() { 255 return minIdle; 256 } 257 258 /** 259 * Sets the minimum number of idle connections in the connection pool. 260 * @param minIdle The minimum number of idle connections in the connection pool. 261 */ 262 public void setMinIdle(int minIdle) { 263 this.minIdle = minIdle; 264 } 265 266 /** 267 * Gets the maximum lifetime of a connection in the connection pool. 268 * @return The maximum lifetime of a connection in the connection pool. 269 */ 270 public int getMaxLifetime() { 271 return maxLifetime; 272 } 273 274 /** 275 * Sets the maximum lifetime of a connection in the connection pool. 276 * @param maxLifetime The maximum lifetime of a connection in the connection pool. 277 */ 278 public void setMaxLifetime(int maxLifetime) { 279 this.maxLifetime = maxLifetime; 280 } 281 282 /** 283 * Gets the maximum time a connection is allowed to be idle. 284 * @return The maximum time a connection is allowed to be idle. 285 */ 286 public int getIdleTimeout() { 287 return idleTimeout; 288 } 289 290 /** 291 * Sets the maximum time a connection is allowed to be idle. 292 * @param idleTimeout The maximum time a connection is allowed to be idle. 293 */ 294 public void setIdleTimeout(int idleTimeout) { 295 this.idleTimeout = idleTimeout; 296 } 297 298 /** 299 * Gets the maximum amount of time to wait for a connection from the connection pool. 300 * @return The maximum amount of time to wait for a connection from the connection pool. 301 */ 302 public int getConnectionTimeout() { 303 return connectionTimeout; 304 } 305 306 /** 307 * Sets the maximum amount of time to wait for a connection from the connection pool. 308 * @param connectionTimeout The maximum amount of time to wait for a connection from the connection pool. 309 */ 310 public void setConnectionTimeout(int connectionTimeout) { 311 this.connectionTimeout = connectionTimeout; 312 } 313 314 /** 315 * Gets the auto commit value. 316 * @return The auto commit value. 317 */ 318 public boolean getAutoCommit() { 319 return autoCommit; 320 } 321 322 /** 323 * Sets the auto commit value. 324 * @return The auto commit value. 325 */ 326 public void setAutoCommit(boolean autoCommit) { 327 this.autoCommit = autoCommit; 328 } 329 } 330 331 /** 332 * Returns the configuration for events buffering. 333 * 334 * @return the configuration 335 */ 336 public EventBufferingConfiguration getBuffering() { 337 return buffering; 338 } 339 340 /** 341 * Sets the configuration for events buffering. 342 * 343 * @param bufferingConfiguration 344 * The configuration 345 */ 346 public void setBufferingConfiguration(EventBufferingConfiguration bufferingConfiguration) { 347 this.buffering = bufferingConfiguration; 348 } 349 350 /** 351 * Configuration of event buffering. 352 */ 353 public static class EventBufferingConfiguration { 354 355 @JsonPropertyDescription("audit.handlers.jdbc.buffering.enabled") 356 private boolean enabled = false; 357 358 @JsonPropertyDescription("audit.handlers.jdbc.buffering.autoFlush") 359 private boolean autoFlush = true; 360 361 @JsonPropertyDescription("audit.handlers.jdbc.buffering.maxSize") 362 private int maxSize = 5000; 363 364 @JsonPropertyDescription("audit.handlers.jdbc.buffering.interval") 365 private String writeInterval = "disabled"; 366 367 @JsonPropertyDescription("audit.handlers.jdbc.buffering.writerThreads") 368 private int writerThreads = 1; 369 370 @JsonPropertyDescription("audit.handlers.jdbc.buffering.maxBatchedEvents") 371 private int maxBatchedEvents = 100; 372 373 374 /** 375 * Indicates if event buffering is enabled. 376 * 377 * @return {@code true} if buffering is enabled. 378 */ 379 public boolean isEnabled() { 380 return enabled; 381 } 382 383 /** 384 * Sets the buffering status. 385 * 386 * @param enabled 387 * Indicates if buffering is enabled. 388 */ 389 public void setEnabled(boolean enabled) { 390 this.enabled = enabled; 391 } 392 393 /** 394 * Indicates if events are automatically flushed after being written. 395 * 396 * @return {@code true} if events must be flushed 397 */ 398 public boolean isAutoFlush() { 399 return autoFlush; 400 } 401 402 /** 403 * Sets the auto flush indicator. 404 * 405 * @param auto 406 * Indicates if events are automatically flushed after being written. 407 */ 408 public void setAutoFlush(boolean auto) { 409 this.autoFlush = auto; 410 } 411 412 /** 413 * Returns the maximum size of the queue. 414 * 415 * @return maxSize Maximum number of events in the queue. 416 */ 417 public int getMaxSize() { 418 return maxSize; 419 } 420 421 /** 422 * Sets the maximum size of the events queue. 423 * 424 * @param maxSize 425 * Maximum number of events in the queue. 426 */ 427 public void setMaxSize(int maxSize) { 428 Reject.ifFalse(maxSize >= 1); 429 this.maxSize = maxSize; 430 } 431 432 /** 433 * Gets the interval to write the queued buffered events. 434 * @return The interval as a string. 435 */ 436 public String getWriteInterval() { 437 return writeInterval; 438 } 439 440 /** 441 * Sets the interval to write the queued buffered events. 442 * @param writeInterval The interval as a string. 443 */ 444 public void setWriteInterval(String writeInterval) { 445 this.writeInterval = writeInterval; 446 } 447 448 /** 449 * Gets the number of writer threads to use to write buffered events. 450 * @return The number of writer threads. 451 */ 452 public int getWriterThreads() { 453 return writerThreads; 454 } 455 456 /** 457 * Sets the number of writer threads to use to write buffered events. 458 * @param writerThreads The number of writer threads. 459 */ 460 public void setWriterThreads(int writerThreads) { 461 Reject.ifFalse(writerThreads >= 1); 462 this.writerThreads = writerThreads; 463 } 464 465 /** 466 * Gets the maximum number of events that can be batched into a {@link PreparedStatement}. 467 * @return The maximum number of batches. 468 */ 469 public int getMaxBatchedEvents() { 470 return maxBatchedEvents; 471 } 472 473 /** 474 * Sets the maximum number of events that can be batched into a {@link PreparedStatement}. 475 * @param maxBatchedEvents The maximum number of batches. 476 */ 477 public void setMaxBatchedEvents(int maxBatchedEvents) { 478 this.maxBatchedEvents = maxBatchedEvents; 479 } 480 } 481}