-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathDatasourceConfig.java
More file actions
58 lines (43 loc) · 1.75 KB
/
Copy pathDatasourceConfig.java
File metadata and controls
58 lines (43 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.picpay;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.h2.jdbcx.JdbcDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.zip.GZIPInputStream;
@Configuration
public class DatasourceConfig {
@Value("${com.picpay.csv.database.url}")
private final String csvDatabaseUrl = null;
private static final Logger LOG =
LoggerFactory.getLogger(DatasourceConfig.class);
private static final String CSV_PATH = "src/main/resources/users.csv";
@Bean(name = "mainDataSource")
public DataSource createMainDataSource() throws Exception {
LOG.info("Importing and extracting CSV file...");
if (!java.nio.file.Files.exists(Paths.get(CSV_PATH))) {
InputStream zipFileInputStream = new URL(csvDatabaseUrl).openStream();
GZIPInputStream is = new GZIPInputStream(zipFileInputStream);
File targetFile = new File(CSV_PATH);
java.nio.file.Files.copy(
is,
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
IOUtils.closeQuietly(is);
LOG.info("CSV imported and extracted...");
}
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:file:~/testdb;LOG=0;CACHE_SIZE=65536;LOCK_MODE=0;UNDO_LOG=0");
ds.setUser("sa");
ds.setPassword("");
return ds;
}
}