Skip to content

Commit

Permalink
Added support for database server aliases as well as host names.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbw committed Sep 25, 2017
1 parent b767367 commit fe54e7f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
* Configuration and credentials for a database server
*/
public class DatabaseServer {


private String alias;
private String host;
private int port;
private String username;
Expand All @@ -31,6 +32,12 @@ public class DatabaseServer {
*/
private String masterDatabaseName;

public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public String getHost() {
return host;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
* implementations could use a database table, registry, or external API
*/
public interface ServerConfigurationService {

/**
* Gets the specified database server
* @param alias the server alias
* @return a DatabaseServer instance
* @throws Exception if the database configuration file is corrupt
*/
public DatabaseServer getDatabaseServerByAlias(String alias) throws Exception;

/**
* Gets the specified database server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ protected void load() throws Exception {
for (int i = 0; i < serverArray; i++){
DatabaseServer databaseServer = new DatabaseServer();

databaseServer.setAlias(xmlServerConfiguration.getString("server("+i+")[@alias]"));
databaseServer.setHost(xmlServerConfiguration.getString("server("+i+")[@host]"));
databaseServer.setPort(xmlServerConfiguration.getInt("server("+i+")[@port]"));
databaseServer.setUsername(xmlServerConfiguration.getString("server("+i+")[@username]"));
Expand All @@ -130,6 +131,7 @@ protected void load() throws Exception {
}

metadataServer = new DatabaseServer();
metadataServer.setAlias(xmlServerConfiguration.getString("metadata(0)[@alias]"));
metadataServer.setHost(xmlServerConfiguration.getString("metadata(0)[@host]"));
metadataServer.setPort(xmlServerConfiguration.getInt("metadata(0)[@port]"));
metadataServer.setUsername(xmlServerConfiguration.getString("metadata(0)[@username]"));
Expand All @@ -150,13 +152,35 @@ public List<DatabaseServer> getAllDatabaseServers() throws Exception {

@Override
public DatabaseServer getDatabaseServer(String host) throws Exception {

//
// Check by alias first
//
DatabaseServer server = getDatabaseServerByAlias(host);
if (server != null){
return server;
}

//
// Then check by host
//
for (DatabaseServer databaseServer : servers){
if (databaseServer.getHost().equalsIgnoreCase(host)){
return databaseServer;
}
}
return null;
}

@Override
public DatabaseServer getDatabaseServerByAlias(String alias) throws Exception {
for (DatabaseServer databaseServer : servers){
if (databaseServer.getAlias().equalsIgnoreCase(alias)){
return databaseServer;
}
}
return null;
}

@Override
public DatabaseServer getOrdsDatabaseServer() throws Exception {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/databaseservers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
The database servers configured for project data.
-->
<servers>
<metadata host="localhost" port="5432" username="ords" password="ords" database="ords2"/>
<server host="localhost" port="5432" username="ords" password="ords" database="ords2"/>
<metadata alias="meta" host="localhost" port="5432" username="ords" password="ords" database="ords2"/>
<server alias="main" host="localhost" port="5432" username="ords" password="ords" database="ords2"/>
</servers>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package uk.ac.ox.it.ords.security.services.impl.hibernate;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;

Expand All @@ -37,4 +38,23 @@ public void loadServers() throws Exception{
assertEquals(server, ServerConfigurationService.Factory.getInstance().getDatabaseServer("localhost"));
}

@Test
public void getServerByAlias() throws Exception{
DatabaseServer server = ServerConfigurationService.Factory.getInstance().getDatabaseServer();
assertEquals("main", server.getAlias());
assertEquals("localhost", server.getHost());
assertEquals(5432, server.getPort());
assertEquals("ords", server.getUsername());
assertEquals("ords", server.getPassword());
assertEquals("ordstest", server.getMasterDatabaseName());

assertEquals(1, ServerConfigurationService.Factory.getInstance().getAllDatabaseServers().size());
assertEquals(server, ServerConfigurationService.Factory.getInstance().getDatabaseServer("localhost"));
assertEquals(server, ServerConfigurationService.Factory.getInstance().getDatabaseServer("main"));
assertEquals(server, ServerConfigurationService.Factory.getInstance().getDatabaseServerByAlias("main"));
assertNull(ServerConfigurationService.Factory.getInstance().getDatabaseServerByAlias("banana"));
assertNull(ServerConfigurationService.Factory.getInstance().getDatabaseServer("banana"));

}

}
4 changes: 2 additions & 2 deletions src/test/resources/databaseservers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
The database servers configured for project data.
-->
<servers>
<metadata host="localhost" port="5432" username="ords" password="ords" database="ordstest"/>
<server host="localhost" port="5432" username="ords" password="ords" database="ordstest"/>
<metadata alias="meta" host="localhost" port="5432" username="ords" password="ords" database="ordstest"/>
<server alias="main" host="localhost" port="5432" username="ords" password="ords" database="ordstest"/>
</servers>

0 comments on commit fe54e7f

Please sign in to comment.