Skip to content

Commit

Permalink
added the possibility to specify relevant IDataSourceType implementat…
Browse files Browse the repository at this point in the history
…ions in the config file
  • Loading branch information
Olaf Hartig committed Dec 25, 2015
1 parent 29a2efe commit fc14a72
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"title": "My Linked Data Fragments server",

"datasourcetypes": {
"HdtDatasource" : "org.linkeddatafragments.datasource.hdt.HdtDataSourceType",
"JenaTDBDatasource" : "org.linkeddatafragments.datasource.tdb.JenaTDBDataSourceType"
},

"datasources": {
"dbpedia": {
"title": "DBPedia",
Expand Down
48 changes: 48 additions & 0 deletions src/org/linkeddatafragments/config/ConfigReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Map;
import java.util.Map.Entry;

import org.linkeddatafragments.datasource.IDataSourceType;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand All @@ -13,8 +15,10 @@
* Reads the configuration of a Linked Data Fragments server.
*
* @author Ruben Verborgh
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public class ConfigReader {
private final Map<String, IDataSourceType> dataSourceTypes = new HashMap<>();
private final Map<String, JsonObject> dataSources = new HashMap<>();
private final Map<String, String> prefixes = new HashMap<>();
private final String baseURL;
Expand All @@ -28,6 +32,10 @@ public ConfigReader(Reader configReader) {
JsonObject root = new JsonParser().parse(configReader).getAsJsonObject();
this.baseURL = root.has("baseURL") ? root.getAsJsonPrimitive("baseURL").getAsString() : null;

for (Entry<String, JsonElement> entry : root.getAsJsonObject("datasourcetypes").entrySet()) {
final String className = entry.getValue().getAsString();
dataSourceTypes.put(entry.getKey(), initDataSouceType(className) );
}
for (Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
JsonObject dataSource = entry.getValue().getAsJsonObject();
this.dataSources.put(entry.getKey(), dataSource);
Expand All @@ -37,6 +45,15 @@ public ConfigReader(Reader configReader) {
}
}

/**
* Gets the data source types.
*
* @return a mapping of names of data source types to these types
*/
public Map<String, IDataSourceType> getDataSourceTypes() {
return dataSourceTypes;
}

/**
* Gets the data sources.
*
Expand All @@ -58,4 +75,35 @@ public Map<String, String> getPrefixes() {
public String getBaseURL() {
return baseURL;
}

protected IDataSourceType initDataSouceType( final String className )
{
final Class<?> c;
try {
c = Class.forName( className );
}
catch ( ClassNotFoundException e ) {
throw new IllegalArgumentException( "Class not found: " + className,
e );
}

final Object o;
try {
o = c.newInstance();
}
catch ( Exception e ) {
throw new IllegalArgumentException(
"Creating an instance of class '" + className + "' " +
"caused a " + e.getClass().getSimpleName() + ": " +
e.getMessage(), e );
}

if ( ! (o instanceof IDataSourceType) )
throw new IllegalArgumentException(
"Class '" + className + "' is not an implementation " +
"of IDataSourceType." );

return (IDataSourceType) o;
}

}

0 comments on commit fc14a72

Please sign in to comment.