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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
package com.calculateur.warhammer.dao.h2;
import java.io.File;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.apache.commons.io.FileUtils;
import org.h2.Driver;
import com.calculateur.warhammer.base.server.IDatabaseConfiguration;
/**
* La configuration pour une Base de donnée H2
*
* @author phili
*
*/
public class ConfigurationH2 implements IDatabaseConfiguration {
private static final String USER = "sa";
private static final String USER_PASSWORD = "sa";
private File useFile;
private final File initialFile;
private final File copiedFile;
private final Integer port;
private final String path;
private final String databaseName;
public ConfigurationH2(File initialFile, File copiedFile, Integer port, String path, String databaseName) {
this.initialFile = initialFile;
this.copiedFile = copiedFile;
this.port = port;
this.path = path;
this.databaseName = databaseName;
}
@PostConstruct
private void init() throws IOException {
if (copiedFile != null) {
FileUtils.copyDirectory(initialFile, copiedFile);
useFile = copiedFile;
} else {
useFile = initialFile;
}
}
@PreDestroy
private void destroy() throws IOException {
if (copiedFile != null && copiedFile.listFiles() != null) {
for(File file:copiedFile.listFiles()) {
FileUtils.deleteDirectory(file);
}
}
}
@Override
public String getUrl() {
return useFile.getAbsolutePath();
}
@Override
public String getJDBCUrl() {
StringBuilder sb = new StringBuilder("jdbc:h2:file:");
sb.append(useFile.getAbsolutePath());
sb.append("/");
sb.append(path);
sb.append("/");
sb.append(databaseName);
return sb.toString();
}
@Override
public String getUser() {
return USER;
}
@Override
public String getPassword() {
return USER_PASSWORD;
}
@Override
public String getPort() {
return port.toString();
}
@Override
public String getClassDriver() {
return Driver.class.getName();
}
} |
Partager