-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: new configuration functionality added
- Loading branch information
Showing
7 changed files
with
335 additions
and
1 deletion.
There are no files selected for viewing
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
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
179 changes: 179 additions & 0 deletions
179
bionetdb-core/src/main/java/org/opencb/bionetdb/core/config/BioNetDBConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package org.opencb.bionetdb.core.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by imedina on 05/10/15. | ||
*/ | ||
public class BioNetDBConfiguration { | ||
|
||
private String defaultDatabase; | ||
private String logLevel; | ||
private String logFile; | ||
|
||
private List<DatabaseConfiguration> databases; | ||
|
||
protected static Logger logger = LoggerFactory.getLogger(BioNetDBConfiguration.class); | ||
|
||
public BioNetDBConfiguration() { | ||
this("", new ArrayList<>()); | ||
} | ||
|
||
public BioNetDBConfiguration(String defaultDatabase, List<DatabaseConfiguration> databaseConfigurations) { | ||
this.defaultDatabase = defaultDatabase; | ||
this.databases = databaseConfigurations; | ||
|
||
} | ||
|
||
/* | ||
* This method attempts to find and load the configuration from installation directory, | ||
* if not exists then loads JAR storage-configuration.yml. | ||
*/ | ||
@Deprecated | ||
public static BioNetDBConfiguration load() throws IOException { | ||
String appHome = System.getProperty("app.home", System.getenv("BIONETDB_HOME")); | ||
Path path = Paths.get(appHome + "/configuration.yml"); | ||
if (appHome != null && Files.exists(path)) { | ||
logger.debug("Loading configuration from '{}'", appHome + "/configuration.yml"); | ||
return BioNetDBConfiguration | ||
.load(new FileInputStream(new File(appHome + "/configuration.yml"))); | ||
} else { | ||
logger.debug("Loading configuration from '{}'", | ||
BioNetDBConfiguration.class.getClassLoader() | ||
.getResourceAsStream("configuration.yml") | ||
.toString()); | ||
return BioNetDBConfiguration | ||
.load(BioNetDBConfiguration.class.getClassLoader().getResourceAsStream("configuration.yml")); | ||
} | ||
} | ||
|
||
public static BioNetDBConfiguration load(InputStream configurationInputStream) throws IOException { | ||
return load(configurationInputStream, "yaml"); | ||
} | ||
|
||
public static BioNetDBConfiguration load(InputStream configurationInputStream, String format) throws IOException { | ||
BioNetDBConfiguration bioNetDBConfiguration; | ||
ObjectMapper objectMapper; | ||
switch (format) { | ||
case "json": | ||
objectMapper = new ObjectMapper(); | ||
bioNetDBConfiguration = objectMapper.readValue(configurationInputStream, BioNetDBConfiguration.class); | ||
break; | ||
case "yml": | ||
case "yaml": | ||
default: | ||
objectMapper = new ObjectMapper(new YAMLFactory()); | ||
bioNetDBConfiguration = objectMapper.readValue(configurationInputStream, BioNetDBConfiguration.class); | ||
break; | ||
} | ||
|
||
return bioNetDBConfiguration; | ||
} | ||
|
||
public void serialize(OutputStream configurationOututStream) throws IOException { | ||
ObjectMapper jsonMapper = new ObjectMapper(new YAMLFactory()); | ||
jsonMapper.writerWithDefaultPrettyPrinter().writeValue(configurationOututStream, this); | ||
} | ||
|
||
public DatabaseConfiguration findDatabase() { | ||
if (defaultDatabase != null && !defaultDatabase.isEmpty()) { | ||
return findDatabase(defaultDatabase); | ||
} else { | ||
if (databases.size() > 0) { | ||
return databases.get(0); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
public DatabaseConfiguration findDatabase(String database) { | ||
DatabaseConfiguration databaseConfiguration = null; | ||
for (DatabaseConfiguration databaseConf : databases) { | ||
if (databaseConf.getId().equals(database)) { | ||
databaseConfiguration = databaseConf; | ||
break; | ||
} | ||
} | ||
|
||
return databaseConfiguration; | ||
} | ||
|
||
public void addDatabase(InputStream configurationInputStream) throws IOException { | ||
addDatabase(configurationInputStream, "yaml"); | ||
} | ||
|
||
public void addDatabase(InputStream configurationInputStream, String format) throws IOException { | ||
DatabaseConfiguration databaseConfiguration; | ||
ObjectMapper objectMapper; | ||
switch (format) { | ||
case "json": | ||
objectMapper = new ObjectMapper(); | ||
databaseConfiguration = objectMapper.readValue(configurationInputStream, DatabaseConfiguration.class); | ||
break; | ||
case "yml": | ||
case "yaml": | ||
default: | ||
objectMapper = new ObjectMapper(new YAMLFactory()); | ||
databaseConfiguration = objectMapper.readValue(configurationInputStream, DatabaseConfiguration.class); | ||
break; | ||
} | ||
|
||
if (databaseConfiguration != null) { | ||
this.databases.add(databaseConfiguration); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder sb = new StringBuilder("BioNetDBConfiguration{"); | ||
sb.append("defaultDatabase='").append(defaultDatabase).append('\''); | ||
sb.append(", logLevel='").append(logLevel).append('\''); | ||
sb.append(", logFile='").append(logFile).append('\''); | ||
sb.append(", databases=").append(databases); | ||
sb.append('}'); | ||
return sb.toString(); | ||
} | ||
|
||
public String getDefaultDatabase() { | ||
return defaultDatabase; | ||
} | ||
|
||
public void setDefaultDatabase(String defaultDatabase) { | ||
this.defaultDatabase = defaultDatabase; | ||
} | ||
|
||
public String getLogLevel() { | ||
return logLevel; | ||
} | ||
|
||
public void setLogLevel(String logLevel) { | ||
this.logLevel = logLevel; | ||
} | ||
|
||
public String getLogFile() { | ||
return logFile; | ||
} | ||
|
||
public void setLogFile(String logFile) { | ||
this.logFile = logFile; | ||
} | ||
|
||
public List<DatabaseConfiguration> getDatabases() { | ||
return databases; | ||
} | ||
|
||
public void setDatabases(List<DatabaseConfiguration> databases) { | ||
this.databases = databases; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
bionetdb-core/src/main/java/org/opencb/bionetdb/core/config/DatabaseConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.opencb.bionetdb.core.config; | ||
|
||
import org.opencb.datastore.core.ObjectMap; | ||
|
||
/** | ||
* Created by imedina on 05/10/15. | ||
*/ | ||
public class DatabaseConfiguration { | ||
|
||
private String id; | ||
|
||
private String host; | ||
private int port; | ||
private String user; | ||
private String password; | ||
|
||
private String path; | ||
|
||
/** | ||
* options parameter defines database-specific parameters. | ||
*/ | ||
private ObjectMap options; | ||
|
||
|
||
public DatabaseConfiguration() { | ||
|
||
} | ||
|
||
public DatabaseConfiguration(String id, ObjectMap options) { | ||
this.id = id; | ||
this.options = options; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder sb = new StringBuilder("DatabaseConfiguration{"); | ||
sb.append("id='").append(id).append('\''); | ||
sb.append(", host='").append(host).append('\''); | ||
sb.append(", port=").append(port); | ||
sb.append(", user='").append(user).append('\''); | ||
sb.append(", password='").append(password).append('\''); | ||
sb.append(", path='").append(path).append('\''); | ||
sb.append(", options=").append(options); | ||
sb.append('}'); | ||
return sb.toString(); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
|
||
public String getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(String user) { | ||
this.user = user; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
public ObjectMap getOptions() { | ||
return options; | ||
} | ||
|
||
public void setOptions(ObjectMap options) { | ||
this.options = options; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
defaultDatabase: "testDB" | ||
logLevel: null | ||
logFile: null | ||
databases: | ||
- id: "testDB" | ||
host: null | ||
port: 0 | ||
user: null | ||
password: null | ||
path: null | ||
options: null |
30 changes: 30 additions & 0 deletions
30
bionetdb-core/src/test/java/org/opencb/bionetdb/core/config/BioNetDBConfigurationTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.opencb.bionetdb.core.config; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.FileOutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by imedina on 05/10/15. | ||
*/ | ||
public class BioNetDBConfigurationTest { | ||
|
||
@Test | ||
public void testSerialize() throws Exception { | ||
|
||
BioNetDBConfiguration bioNetDBConfiguration = new BioNetDBConfiguration(); | ||
|
||
bioNetDBConfiguration.setDefaultDatabase("testDB"); | ||
|
||
List<DatabaseConfiguration> databases = new ArrayList<>(); | ||
databases.add(new DatabaseConfiguration("testDB", null)); | ||
|
||
bioNetDBConfiguration.setDatabases(databases); | ||
|
||
bioNetDBConfiguration.serialize(new FileOutputStream("/tmp/bionetdb-configuration-test.yml")); | ||
} | ||
} |
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