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   * Portions Copyright 2016 Nomura Research Institute, Ltd.
16   */
17  package org.forgerock.audit.handlers.jdbc;
18  
19  import java.math.BigDecimal;
20  import java.sql.PreparedStatement;
21  import java.sql.SQLException;
22  import java.sql.Types;
23  import java.util.List;
24  
25  import org.forgerock.audit.AuditException;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import com.fasterxml.jackson.core.JsonProcessingException;
30  import com.fasterxml.jackson.databind.ObjectMapper;
31  
32  /**
33   * Utility functions for use within this package.
34   */
35  final class JdbcUtils {
36  
37      private static final Logger LOGGER = LoggerFactory.getLogger(JdbcUtils.class);
38      private static final ObjectMapper MAPPER = new ObjectMapper();
39  
40      private JdbcUtils() {
41          // Prevent instantiation
42      }
43  
44      static void initializePreparedStatement(final PreparedStatement preparedStatement, final List<Parameter> params)
45              throws AuditException, SQLException, JsonProcessingException {
46          int i = 1;
47          for (final Parameter parameter : params) {
48              final Object parameterValue = parameter.getParameter();
49              switch (parameter.getParameterType()) {
50              case STRING:
51                  preparedStatement.setString(i, (String) parameterValue);
52                  break;
53              case NUMBER:
54                  if (parameterValue == null) {
55                      preparedStatement.setNull(i, Types.FLOAT);
56                      break; // avoid fall through into INTEGER case
57                  }
58                  if (parameterValue instanceof Float) {
59                      preparedStatement.setFloat(i, (Float) parameterValue);
60                  } else if (parameterValue instanceof Double) {
61                      preparedStatement.setDouble(i, (Double) parameterValue);
62                  } else if (parameterValue instanceof BigDecimal) {
63                      preparedStatement.setBigDecimal(i, (BigDecimal) parameterValue);
64                  }
65                  // intentional fall through so that number can support the json integer type subset as well
66              case INTEGER:
67                  if (parameterValue == null) {
68                      preparedStatement.setNull(i, Types.INTEGER);
69                  } else if (parameterValue instanceof Long) {
70                      preparedStatement.setLong(i, (Long) parameterValue);
71                  } else if (parameterValue instanceof Integer) {
72                      preparedStatement.setInt(i, (Integer) parameterValue);
73                  } else {
74                      final String error = String.format("Unable to map class type %s to %s for field %d",
75                              parameterValue.getClass().getCanonicalName(),
76                              parameter.getParameterType(),
77                              i);
78                      LOGGER.error(error);
79                      throw new AuditException(error);
80                  }
81                  break;
82              case BOOLEAN:
83                  if (parameterValue == null) {
84                      preparedStatement.setNull(i, Types.BOOLEAN);
85                  } else {
86                      preparedStatement.setBoolean(i, (Boolean) parameterValue);
87                  }
88                  break;
89              case OBJECT:
90              case ARRAY:
91                  if (parameterValue == null) {
92                      preparedStatement.setString(i, null);
93                  } else {
94                      preparedStatement.setString(i, MAPPER.writeValueAsString(parameterValue));
95                  }
96                  break;
97              default:
98                  final String error = String.format("Schema defines unknown type %s for field %d",
99                          parameter.getParameterType(),
100                         i);
101                 LOGGER.error(error);
102                 throw new AuditException(error);
103             }
104             i++;
105         }
106     }
107 }