Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}
10 changes: 0 additions & 10 deletions src/org/linkeddatafragments/datasource/hdt/HdtDataSourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.io.IOException;

import org.linkeddatafragments.datasource.DataSourceTypesRegistry;
import org.linkeddatafragments.datasource.IDataSource;
import org.linkeddatafragments.datasource.IDataSourceType;
import org.linkeddatafragments.exceptions.DataSourceException;
Expand All @@ -17,15 +16,6 @@
*/
public class HdtDataSourceType implements IDataSourceType
{
public static final String TYPE_NAME = "HdtDatasource";

public static void register() {
if ( ! DataSourceTypesRegistry.isRegistered(TYPE_NAME) ) {
DataSourceTypesRegistry.register( TYPE_NAME,
new HdtDataSourceType() );
}
}

@Override
public IDataSource createDataSource( final String title,
final String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;

import org.linkeddatafragments.datasource.DataSourceTypesRegistry;
import org.linkeddatafragments.datasource.IDataSource;
import org.linkeddatafragments.datasource.IDataSourceType;
import org.linkeddatafragments.exceptions.DataSourceException;
Expand All @@ -17,15 +16,6 @@
*/
public class JenaTDBDataSourceType implements IDataSourceType
{
public static final String TYPE_NAME = "JenaTDBDatasource";

public static void register() {
if ( ! DataSourceTypesRegistry.isRegistered(TYPE_NAME) ) {
DataSourceTypesRegistry.register( TYPE_NAME,
new JenaTDBDataSourceType() );
}
}

@Override
public IDataSource createDataSource( final String title,
final String description,
Expand Down
19 changes: 10 additions & 9 deletions src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import org.apache.jena.riot.RDFLanguages;
import org.linkeddatafragments.config.ConfigReader;
import org.linkeddatafragments.datasource.DataSourceFactory;
import org.linkeddatafragments.datasource.DataSourceTypesRegistry;
import org.linkeddatafragments.datasource.IDataSource;
import org.linkeddatafragments.datasource.hdt.HdtDataSourceType;
import org.linkeddatafragments.datasource.IDataSourceType;
import org.linkeddatafragments.datasource.index.IndexDataSource;
import org.linkeddatafragments.datasource.tdb.JenaTDBDataSourceType;
import org.linkeddatafragments.exceptions.DataSourceException;
import org.linkeddatafragments.exceptions.DataSourceNotFoundException;
import org.linkeddatafragments.fragments.FragmentRequestParserBase;
import org.linkeddatafragments.fragments.LinkedDataFragment;
Expand All @@ -49,11 +48,6 @@ public class LinkedDataFragmentServlet extends HttpServlet {
private final HashMap<String, IDataSource> dataSources = new HashMap<>();
private final Collection<String> mimeTypes = new ArrayList<>();

public LinkedDataFragmentServlet() {
HdtDataSourceType.register();
JenaTDBDataSourceType.register();
}

private File getConfigFile(ServletConfig config) throws IOException {
String path = config.getServletContext().getRealPath("/");
if (path == null) {
Expand All @@ -80,6 +74,13 @@ public void init(ServletConfig servletConfig) throws ServletException {
File configFile = getConfigFile(servletConfig);
config = new ConfigReader(new FileReader(configFile));

// register data source types
for ( Entry<String,IDataSourceType> typeEntry : config.getDataSourceTypes().entrySet() ) {
DataSourceTypesRegistry.register( typeEntry.getKey(),
typeEntry.getValue() );
}

// register data sources
for (Entry<String, JsonObject> dataSource : config.getDataSources().entrySet()) {
dataSources.put(dataSource.getKey(), DataSourceFactory.create(dataSource.getValue()));
}
Expand All @@ -89,7 +90,7 @@ public void init(ServletConfig servletConfig) throws ServletException {
mimeTypes.add(Lang.JSONLD.getHeaderString());
mimeTypes.add(Lang.NTRIPLES.getHeaderString());
mimeTypes.add(Lang.RDFXML.getHeaderString());
} catch (IOException | DataSourceException e) {
} catch (Exception e) {
throw new ServletException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.linkeddatafragments.datasource.DataSourceFactory;
import org.linkeddatafragments.datasource.DataSourceTypesRegistry;
import org.linkeddatafragments.datasource.hdt.HdtDataSourceType;
import org.rdfhdt.hdt.enums.RDFNotation;
import org.rdfhdt.hdt.hdt.HDT;
Expand All @@ -24,7 +25,11 @@ public class HdtDataSourceTest extends DataSourceTest {

@BeforeClass
public static void setUpClass() throws Exception {
HdtDataSourceType.register();
final String typeName = "HdtTestSourceType";
if ( ! DataSourceTypesRegistry.isRegistered(typeName) ) {
DataSourceTypesRegistry.register( typeName, new HdtDataSourceType() );
}

// HDT does not seem to support an InputReader, so write to temp file
File temp = getResourceAsFile();

Expand All @@ -37,8 +42,7 @@ public static void setUpClass() throws Exception {
temp.getAbsoluteFile().delete();

// Everything is in place, now create the LDF datasource
JsonObject config = createConfig("hdt test", "hdt test",
HdtDataSourceType.TYPE_NAME);
JsonObject config = createConfig("hdt test", "hdt test", typeName);

JsonObject settings = new JsonObject();
settings.addProperty("file", hdtfile.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.BeforeClass;

import org.linkeddatafragments.datasource.DataSourceFactory;
import org.linkeddatafragments.datasource.DataSourceTypesRegistry;
import org.linkeddatafragments.datasource.tdb.JenaTDBDataSourceType;

/**
Expand All @@ -31,7 +32,11 @@ public class JenaTDBDataSourceTest extends DataSourceTest {

@BeforeClass
public static void setUpClass() throws Exception {
JenaTDBDataSourceType.register();
final String typeName = "JenaSourceType";
if ( ! DataSourceTypesRegistry.isRegistered(typeName) ) {
DataSourceTypesRegistry.register( typeName,
new JenaTDBDataSourceType() );
}

String tmpdir = System.getProperty("java.io.tmpdir");
jena = new File(tmpdir, "ldf-jena-test");
Expand All @@ -46,7 +51,7 @@ public static void setUpClass() throws Exception {

// Everything is in place, now create the LDF datasource
JsonObject config = createConfig("jena tdb test", "jena tdb test",
JenaTDBDataSourceType.TYPE_NAME);
typeName);

JsonObject settings = new JsonObject();
settings.addProperty("directory", jena.getAbsolutePath());
Expand Down