This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dbhandler; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.util.Properties; | |
/** | |
* Set jdbc connection currentConnctProp, Read jdbc connection currentConnctProp, creates wanted db tables | |
*/ | |
public class JdbcPropertiesManager { | |
private Properties currentConnctProp; | |
private String canonical; | |
private String dirPath; | |
private String filePath; | |
private InputStream in; | |
private final String FILE_SEPARATOR = System.getProperty("file.separator"); | |
private final String FILE_NAME = "jdbc.properties"; | |
private final String DRIVER_SEPARATOR = "#"; | |
private boolean IS_WINDOWS; | |
private String propFilePath; | |
private String propFileHost; | |
public JdbcPropertiesManager() { | |
String os = System.getProperty("os.name").toLowerCase(); | |
if (os.contains("windows")) { | |
IS_WINDOWS = true; | |
} | |
try { | |
canonical = new File(".").getCanonicalPath(); | |
} catch (IOException e) { | |
error("Getting canonical path error"); | |
e.printStackTrace(); | |
} | |
dirPath = canonical + FILE_SEPARATOR + | |
"mykernel" + FILE_SEPARATOR + | |
"build" + FILE_SEPARATOR + | |
"resources" + FILE_SEPARATOR + | |
"main"; | |
filePath = dirPath + FILE_SEPARATOR + FILE_NAME; | |
if (!uploadProperties()) { | |
fatal(); | |
} | |
} | |
public String getResourcePath() { | |
return propFilePath; | |
} | |
public String getResourceHost(){ | |
return propFileHost; | |
} | |
public String getConnectionString() { | |
String protocol = getProtocol(); | |
String dbType = getDbType(); | |
String host = getHost(); | |
String path = getPath(); | |
String dbName = getName(); | |
if (path == null) { | |
path = ""; | |
} | |
if (host == null) { | |
host = ""; | |
} else { | |
host = host + String.format(":%s", FILE_SEPARATOR); | |
} | |
StringBuilder sb = new StringBuilder(); | |
sb.append(protocol).append(":").append(dbType).append(":").append(host).append(path).append(dbName); | |
return sb.toString(); | |
} | |
public String getDriver() { | |
if (currentConnctProp != null) { | |
return currentConnctProp.getProperty("driver"); | |
} | |
return null; | |
} | |
public String getProtocol() { | |
if (currentConnctProp != null) { | |
return currentConnctProp.getProperty("protocol"); | |
} | |
return null; | |
} | |
public String getDbType() { | |
if (currentConnctProp != null) { | |
return currentConnctProp.getProperty("type"); | |
} | |
return null; | |
} | |
public String getName() { | |
if (currentConnctProp != null) { | |
return currentConnctProp.getProperty("name"); | |
} | |
return null; | |
} | |
public String getPath() { | |
if (currentConnctProp != null) { | |
if (currentConnctProp.getProperty("path").equals("")) { | |
return null; | |
} else { | |
return currentConnctProp.getProperty("path"); | |
} | |
} | |
return null; | |
} | |
public String getHost() { | |
if (currentConnctProp != null) { | |
if (currentConnctProp.getProperty("host").equals("")) { | |
return null; | |
} else { | |
return currentConnctProp.getProperty("host"); | |
} | |
} | |
return null; | |
} | |
public boolean setJdbcProperties(String protocol, String type, String name) { | |
return setJdbcProperties(protocol, type, "", "", name, "org.sqlite.JDBC"); | |
} | |
public boolean setJdbcProperties(String protocol, String type, String host, String name) { | |
return setJdbcProperties(protocol, type, host, "", name, "org.sqlite.JDBC"); | |
} | |
public boolean setJdbcProperties(String protocol, String type, String host, String path, String name, String driver) { | |
if (!path.equals("")) { | |
path = path + FILE_SEPARATOR; | |
} | |
if (!host.equals("")) { | |
host = host + ":"; | |
} | |
StringBuilder jdbcUrl = new StringBuilder(); | |
jdbcUrl.append(protocol) | |
.append(":") | |
.append(type) | |
.append(":") | |
.append(host) | |
.append(path) | |
.append(name) | |
.append(DRIVER_SEPARATOR) | |
.append(driver); | |
return setJdbcProperties(jdbcUrl.toString()); | |
} | |
public boolean setJdbcProperties(String jdbcUrl) { | |
String[] tokens = jdbcUrl.split(":"); | |
String[] splitName = null; | |
if (IS_WINDOWS) { | |
splitName = tokens[tokens.length - 1].split(String.format("[%s%<s%s]", FILE_SEPARATOR, DRIVER_SEPARATOR)); | |
} else { | |
splitName = tokens[tokens.length - 1].split(String.format("[%s%s]", FILE_SEPARATOR, DRIVER_SEPARATOR)); | |
} | |
String protocol = tokens[0]; | |
String type = tokens[1]; | |
String host = ""; | |
if (tokens.length > 3) { | |
host = tokens[2]; | |
} | |
String name = splitName[splitName.length - 2]; | |
String driver = splitName[splitName.length - 1]; | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < splitName.length - 2; i++) { | |
sb.append(splitName[i]).append(FILE_SEPARATOR); | |
} | |
String path = sb.toString(); | |
currentConnctProp.setProperty("protocol", protocol); | |
currentConnctProp.setProperty("type", type); | |
currentConnctProp.setProperty("host", host); | |
currentConnctProp.setProperty("path", path); | |
currentConnctProp.setProperty("name", name); | |
currentConnctProp.setProperty("driver", driver); | |
FileOutputStream out = null; | |
try { | |
out = new FileOutputStream(filePath); | |
} catch (FileNotFoundException e) { | |
error("Fail to get OutPutStream from file " + filePath); | |
e.printStackTrace(); | |
return false; | |
} | |
try { | |
currentConnctProp.store(out, "User JDBC propeties"); | |
System.out.println("User jdbc settings stored OK!"); | |
} catch (IOException e) { | |
error("Fail to store user jdbc properties!"); | |
e.printStackTrace(); | |
return false; | |
} finally { | |
try { | |
out.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return true; | |
} | |
private boolean uploadProperties() { | |
//create new property file if not exists and initialize and load jdbc properties from that file | |
int i = 0; | |
while (((in = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME)) == null) | |
&& i <= 1) { | |
System.out.format("Creating file %s. Try %d from 2\n", FILE_NAME, ++i); | |
File newFile = new File(filePath); | |
try { | |
if (newFile.createNewFile()) { | |
System.out.println("New file was created: " + FILE_NAME); | |
} | |
} catch (IOException e) { | |
error("Fail to create new file " + filePath); | |
e.printStackTrace(); | |
} | |
Properties defaultProp = null; | |
if ((defaultProp = defaultJdbcSettingsToFile()) == null) { | |
return false; | |
} else { | |
currentConnctProp = new Properties(defaultProp); | |
} | |
return true; | |
} | |
//initialize and load jdbc currentConnctProp if file already exists | |
try { | |
currentConnctProp = new Properties(); | |
currentConnctProp.load(in); | |
} catch (IOException e) { | |
error("Can't load properties to InputStream"); | |
e.printStackTrace(); | |
return false; | |
} finally { | |
try { | |
in.close(); | |
} catch (IOException e) { | |
error("Fail to close InputStream"); | |
e.printStackTrace(); | |
} | |
} | |
return true; | |
} | |
private Properties defaultJdbcSettingsToFile() { | |
Properties defaultProp = new Properties(); | |
propFilePath = getPropFilepath(); | |
propFileHost = getPropFileHost(); | |
defaultProp.setProperty("protocol", "jdbc"); | |
defaultProp.setProperty("type", "sqlite"); | |
defaultProp.setProperty("host", propFileHost); | |
defaultProp.setProperty("path", propFilePath); | |
defaultProp.setProperty("name", "profitmanager.db"); | |
defaultProp.setProperty("driver", "org.sqlite.JDBC"); | |
FileOutputStream out = null; | |
try { | |
out = new FileOutputStream(filePath); | |
} catch (IOException e) { | |
error("I/O outputStream fail from file" + FILE_NAME); | |
e.printStackTrace(); | |
return null; | |
} | |
try { | |
if (out == null) { | |
fatal("OutPutStream is null from file " + FILE_NAME); | |
} | |
defaultProp.store(out, "Default JDBC properties"); | |
} catch (IOException e) { | |
error("Fail to store properties to file " + FILE_NAME); | |
e.printStackTrace(); | |
return null; | |
} finally { | |
try { | |
out.close(); | |
} catch (IOException e) { | |
error("Fail to close OutPutStream from file " + FILE_NAME); | |
e.printStackTrace(); | |
} | |
} | |
return defaultProp; | |
} | |
private String getPropFilepath() { | |
URL url = this.getClass().getClassLoader().getResource("jdbc.properties"); | |
String[] splitByHost = url.getPath().split(":"); | |
String[] splitByFileSeparator = splitByHost[splitByHost.length - 1].split("/"); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 1; i < splitByFileSeparator.length - 2; i++) { | |
sb.append(splitByFileSeparator[i]).append(FILE_SEPARATOR); | |
} | |
return sb.toString(); | |
} | |
private String getPropFileHost(){ | |
URL url = this.getClass().getClassLoader().getResource("jdbc.properties"); | |
String regex = String.format("[:/]"); | |
String[] splitByHostAndSeparator = url.getPath().split(regex); | |
if(!splitByHostAndSeparator[0].equals("")){ | |
return splitByHostAndSeparator[0]; | |
}else{ | |
return splitByHostAndSeparator[1]; | |
} | |
} | |
private void fatal() { | |
fatal("Upload jdbc currentConnctProp fail"); | |
} | |
private void fatal(String message) { | |
System.err.println(message); | |
System.exit(1); | |
} | |
private void error(String message) { | |
System.err.println(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.junit.Test; | |
import dbhandler.JdbcProperties; | |
public class TestJdbcProperties { | |
@Test | |
public void testClass(){ | |
JdbcProperties db = new JdbcProperties(); | |
System.out.println("Previous connection: " + db.getConnectionString()); | |
db.setJdbcProperties("jdbc", "mysql", "C","main\\src\\builder", "manger.db", "org.sqlite.JDBC"); | |
System.out.println("db.getConnectionString() = " + db.getConnectionString()); | |
System.out.println("db.getDriver() = " + db.getDriver()); | |
System.out.println("db.getProtocol() = " + db.getProtocol()); | |
System.out.println("db.getDbType() = " + db.getDbType()); | |
System.out.println("db.getPath() = " + db.getPath()); | |
System.out.println("db.getName() = " + db.getName()); | |
System.out.println("db.getHost() = " + db.getHost()); | |
db.setJdbcProperties("jdbc", "mysqllll", "manger.db"); | |
System.out.println("\n\ndb.getConnectionString() = " + db.getConnectionString()); | |
System.out.println("db.getDriver() = " + db.getDriver()); | |
System.out.println("db.getProtocol() = " + db.getProtocol()); | |
System.out.println("db.getDbType() = " + db.getDbType()); | |
System.out.println("db.getPath() = " + db.getPath()); | |
System.out.println("db.getName() = " + db.getName()); | |
System.out.println("db.getHost() = " + db.getHost()); | |
db.setJdbcProperties("jdbc", "mysqllll", "C","profitmanager.db"); | |
System.out.println("\n\ndb.getConnectionString() = " + db.getConnectionString()); | |
System.out.println("db.getDriver() = " + db.getDriver()); | |
System.out.println("db.getProtocol() = " + db.getProtocol()); | |
System.out.println("db.getDbType() = " + db.getDbType()); | |
System.out.println("db.getPath() = " + db.getPath()); | |
System.out.println("db.getName() = " + db.getName()); | |
System.out.println("db.getHost() = " + db.getHost()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment