diff --git a/config-example.json b/config-example.json index 4a5a721..11fcdf1 100644 --- a/config-example.json +++ b/config-example.json @@ -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", diff --git a/src/org/linkeddatafragments/config/ConfigReader.java b/src/org/linkeddatafragments/config/ConfigReader.java index a3240cc..7b2d99a 100644 --- a/src/org/linkeddatafragments/config/ConfigReader.java +++ b/src/org/linkeddatafragments/config/ConfigReader.java @@ -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; @@ -13,8 +15,10 @@ * Reads the configuration of a Linked Data Fragments server. * * @author Ruben Verborgh + * @author Olaf Hartig */ public class ConfigReader { + private final Map dataSourceTypes = new HashMap<>(); private final Map dataSources = new HashMap<>(); private final Map prefixes = new HashMap<>(); private final String baseURL; @@ -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 entry : root.getAsJsonObject("datasourcetypes").entrySet()) { + final String className = entry.getValue().getAsString(); + dataSourceTypes.put(entry.getKey(), initDataSouceType(className) ); + } for (Entry entry : root.getAsJsonObject("datasources").entrySet()) { JsonObject dataSource = entry.getValue().getAsJsonObject(); this.dataSources.put(entry.getKey(), dataSource); @@ -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 getDataSourceTypes() { + return dataSourceTypes; + } + /** * Gets the data sources. * @@ -58,4 +75,35 @@ public Map 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; + } + }