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