55import java .util .Map ;
66import java .util .Map .Entry ;
77
8+ import org .linkeddatafragments .datasource .IDataSourceType ;
9+
810import com .google .gson .JsonElement ;
911import com .google .gson .JsonObject ;
1012import com .google .gson .JsonParser ;
1315 * Reads the configuration of a Linked Data Fragments server.
1416 *
1517 * @author Ruben Verborgh
18+ * @author <a href="http://olafhartig.de">Olaf Hartig</a>
1619 */
1720public class ConfigReader {
21+ private final Map <String , IDataSourceType > dataSourceTypes = new HashMap <>();
1822 private final Map <String , JsonObject > dataSources = new HashMap <>();
1923 private final Map <String , String > prefixes = new HashMap <>();
2024 private final String baseURL ;
@@ -28,6 +32,10 @@ public ConfigReader(Reader configReader) {
2832 JsonObject root = new JsonParser ().parse (configReader ).getAsJsonObject ();
2933 this .baseURL = root .has ("baseURL" ) ? root .getAsJsonPrimitive ("baseURL" ).getAsString () : null ;
3034
35+ for (Entry <String , JsonElement > entry : root .getAsJsonObject ("datasourcetypes" ).entrySet ()) {
36+ final String className = entry .getValue ().getAsString ();
37+ dataSourceTypes .put (entry .getKey (), initDataSouceType (className ) );
38+ }
3139 for (Entry <String , JsonElement > entry : root .getAsJsonObject ("datasources" ).entrySet ()) {
3240 JsonObject dataSource = entry .getValue ().getAsJsonObject ();
3341 this .dataSources .put (entry .getKey (), dataSource );
@@ -37,6 +45,15 @@ public ConfigReader(Reader configReader) {
3745 }
3846 }
3947
48+ /**
49+ * Gets the data source types.
50+ *
51+ * @return a mapping of names of data source types to these types
52+ */
53+ public Map <String , IDataSourceType > getDataSourceTypes () {
54+ return dataSourceTypes ;
55+ }
56+
4057 /**
4158 * Gets the data sources.
4259 *
@@ -58,4 +75,35 @@ public Map<String, String> getPrefixes() {
5875 public String getBaseURL () {
5976 return baseURL ;
6077 }
78+
79+ protected IDataSourceType initDataSouceType ( final String className )
80+ {
81+ final Class <?> c ;
82+ try {
83+ c = Class .forName ( className );
84+ }
85+ catch ( ClassNotFoundException e ) {
86+ throw new IllegalArgumentException ( "Class not found: " + className ,
87+ e );
88+ }
89+
90+ final Object o ;
91+ try {
92+ o = c .newInstance ();
93+ }
94+ catch ( Exception e ) {
95+ throw new IllegalArgumentException (
96+ "Creating an instance of class '" + className + "' " +
97+ "caused a " + e .getClass ().getSimpleName () + ": " +
98+ e .getMessage (), e );
99+ }
100+
101+ if ( ! (o instanceof IDataSourceType ) )
102+ throw new IllegalArgumentException (
103+ "Class '" + className + "' is not an implementation " +
104+ "of IDataSourceType." );
105+
106+ return (IDataSourceType ) o ;
107+ }
108+
61109}
0 commit comments