1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application

File JdbcActions.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
84
9
1
200
179
18
0.21
9.33
9
2

Classes

Class Line # Actions
JdbcActions 35 84 0% 18 2
0.9805825498.1%
 

Contributing tests

This file is covered by 22 tests. .

Source view

1    package org.europa.together.application;
2   
3    import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4    import java.beans.PropertyVetoException;
5    import java.io.BufferedReader;
6    import java.io.IOException;
7    import java.io.InputStreamReader;
8    import java.sql.Connection;
9    import java.sql.DatabaseMetaData;
10    import java.sql.ResultSet;
11    import java.sql.SQLException;
12    import java.sql.Statement;
13    import java.util.HashMap;
14    import java.util.Map;
15    import java.util.regex.Matcher;
16    import java.util.regex.Pattern;
17    import org.apache.commons.dbcp2.BasicDataSource;
18    import org.europa.together.business.DatabaseActions;
19    import org.europa.together.business.Logger;
20    import org.europa.together.business.PropertyReader;
21    import org.europa.together.domain.JdbcConnection;
22    import org.europa.together.domain.LogLevel;
23    import org.europa.together.exceptions.MisconfigurationException;
24    import org.europa.together.exceptions.TimeOutException;
25    import org.europa.together.utils.StringUtils;
26    import org.europa.together.utils.Validator;
27    import org.springframework.context.ApplicationContext;
28    import org.springframework.context.support.ClassPathXmlApplicationContext;
29    import org.springframework.stereotype.Repository;
30   
31    /**
32    * Implementation of Database JDBC Actions.
33    */
34    @Repository
 
35    public class JdbcActions implements DatabaseActions {
36   
37    private static final long serialVersionUID = 8L;
38    private static final Logger LOGGER = new LogbackLogger(JdbcActions.class);
39   
40    private static final String JDBC_PROPERTIES
41    = "org/europa/together/configuration/jdbc.properties";
42    private Connection jdbcConnection = null;
43    private Statement statement = null;
44    private DatabaseMetaData metadata;
45    private String connectionUrl;
46    private String driverClass;
47    private String pwd;
48    private String user;
49   
50    /**
51    * Constructor.
52    */
 
53  48 toggle public JdbcActions() {
54  48 LOGGER.log("instance class", LogLevel.INFO);
55    }
56   
 
57  44 toggle @Override
58    public boolean connect(final String propertyFile) {
59  44 boolean connected = true;
60  44 try {
61  44 fetchProperties(propertyFile);
62  44 establishPooledConnection();
63    } catch (Exception ex) {
64  1 connected = false;
65  1 LOGGER.log("Connection failed!", LogLevel.WARN);
66  1 LOGGER.catchException(ex);
67    }
68  44 return connected;
69    }
70   
 
71  74 toggle @Override
72    public boolean executeSqlFromClasspath(final String sqlFile) {
73  74 boolean success = true;
74  74 LOGGER.log("SQL from Classpath -> File(" + sqlFile + ")", LogLevel.DEBUG);
75  74 BufferedReader reader = null;
76  74 StringBuilder sql = new StringBuilder();
77  74 ApplicationContext context = new ClassPathXmlApplicationContext();
78  74 try {
79  74 reader = new BufferedReader(
80    new InputStreamReader(
81    context.getResource(sqlFile).getInputStream(), "UTF8")
82    );
83  73 String line;
84  ? while ((line = reader.readLine()) != null) {
85  3510 if (!line.startsWith("--")) {
86  2895 sql.append(line);
87    }
88    }
89  73 this.executeQuery(sql.toString());
90  68 reader.close();
91    } catch (Exception ex) {
92  6 success = false;
93  6 LOGGER.catchException(ex);
94    }
95  74 return success;
96    }
97   
 
98  145 toggle @Override
99    public ResultSet executeQuery(final String sql)
100    throws SQLException {
101  145 ResultSet resultSet = null;
102  145 LOGGER.log("Execute SQL: " + sql, LogLevel.DEBUG);
103  145 if (jdbcConnection != null) {
104  144 statement = jdbcConnection.createStatement();
105  144 statement.execute(sql);
106  138 resultSet = statement.getResultSet();
107    } else {
108  1 LOGGER.log("No JDBC Connection established.", LogLevel.ERROR);
109    }
110  139 return resultSet;
111    }
112   
 
113  1 toggle @Override
114    public int countResultSets(final ResultSet results)
115    throws SQLException {
116  1 int count = 0;
117  2 while (results.next()) {
118  1 count++;
119    }
120  1 return count;
121    }
122   
 
123  80 toggle @Override
124    public JdbcConnection getJdbcMetaData()
125    throws SQLException {
126  80 Map<String, String> properties = new HashMap<>();
127  80 metadata = jdbcConnection.getMetaData();
128  79 String url = metadata.getURL();
129  79 properties.put("metaJdbcVersion",
130    metadata.getJDBCMajorVersion() + "." + metadata.getJDBCMinorVersion());
131  79 properties.put("metaJdbcDriverName",
132    metadata.getDriverName());
133  79 properties.put("metaJdbcDriverVersion",
134    metadata.getDriverVersion());
135  79 properties.put("metaDbmsName",
136    metadata.getDatabaseProductName());
137  79 properties.put("metaDbmsVersion",
138    metadata.getDatabaseProductVersion());
139  79 properties.put("metaUser",
140    metadata.getUserName());
141  79 properties.put("metaCatalog",
142    metadata.getConnection().getCatalog());
143  79 properties.put("metaUrl", url);
144  79 String[] result = grabIpAndPort(url).split(":");
145  79 String ip = result[0];
146  79 String port = result[1];
147  79 properties.put("metaIP", ip);
148  79 properties.put("metaPort", port);
149  79 return new JdbcConnection(properties);
150    }
151   
152    // ----------------------------------------------------------------------------
 
153  79 toggle @SuppressFBWarnings
154    //SpotBugs: vulnerable to a denial of service attack (ReDOS)
155    private String grabIpAndPort(final String connectionUrl) {
156  79 String result = "";
157  79 LOGGER.log("Grab IP4 Adress an Port from connection string: "
158    + connectionUrl, LogLevel.DEBUG);
159  79 Pattern pattern = Pattern.compile(Validator.IP4_ADDRESS);
160  79 Matcher matcher = pattern.matcher(connectionUrl);
161  79 LOGGER.log("RegEx match found: " + matcher.find()
162    + " GroupCount: " + matcher.groupCount(), LogLevel.DEBUG);
163  79 result = matcher.group(0);
164  79 LOGGER.log("Result: " + result + " - Search: " + connectionUrl, LogLevel.DEBUG);
165  79 return result;
166    }
167   
 
168  44 toggle private void establishPooledConnection()
169    throws TimeOutException, ClassNotFoundException, PropertyVetoException, SQLException {
170  44 LOGGER.log("Try to establish JDBC connection.", LogLevel.DEBUG);
171  44 Class.forName(driverClass);
172  43 BasicDataSource cpds = new BasicDataSource();
173  43 cpds.setDriverClassName(driverClass);
174  43 cpds.setUrl(connectionUrl);
175  43 cpds.setUsername(user);
176  43 cpds.setPassword(pwd);
177  43 this.jdbcConnection = cpds.getConnection();
178  43 LOGGER.log(getJdbcMetaData().toString(), LogLevel.DEBUG);
179    }
180   
 
181  44 toggle private void fetchProperties(final String propertyFile) throws IOException {
182  44 String properties = propertyFile;
183  44 PropertyReader reader = new PropertyFileReader();
184  44 if (StringUtils.isEmpty(properties) || propertyFile.equals("test")) {
185  43 LOGGER.log("Append (test) properties: " + JDBC_PROPERTIES, LogLevel.DEBUG);
186  43 reader.appendPropertiesFromClasspath(JDBC_PROPERTIES);
187    } else {
188  1 LOGGER.log("Append properties from: " + propertyFile, LogLevel.DEBUG);
189  1 reader.appendPropertiesFromFile(propertyFile);
190    }
191  44 try {
192  44 this.driverClass = reader.getPropertyAsString("jdbc.driverClassName");
193  44 this.user = reader.getPropertyAsString("jdbc.user");
194  43 this.pwd = reader.getPropertyAsString("jdbc.password");
195  43 this.connectionUrl = reader.getPropertyAsString("jdbc.url");
196    } catch (MisconfigurationException ex) {
197  1 LOGGER.catchException(ex);
198    }
199    }
200    }