Skip to content

Commit

Permalink
Merge pull request #5 from LinkedDataFragments/master
Browse files Browse the repository at this point in the history
merging in latest changes from origin
  • Loading branch information
hartig committed Dec 30, 2015
2 parents 29a2efe + 4aad817 commit f03b8ed
Show file tree
Hide file tree
Showing 28 changed files with 681 additions and 342 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;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
package org.linkeddatafragments.datasource;

import com.hp.hpl.jena.rdf.model.Model;

import org.linkeddatafragments.fragments.LinkedDataFragment;
import org.linkeddatafragments.fragments.LinkedDataFragmentRequest;
import org.linkeddatafragments.fragments.tpf.TriplePatternElement;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragment;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentImpl;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest;

import com.hp.hpl.jena.rdf.model.Model;

/**
* Base class for implementations of {@link IFragmentRequestProcessor} that
* process {@link TriplePatternFragmentRequest}s.
*
* @param <TermType> type for representing RDF terms in triple patterns
* @param <VarType> type for representing specific variables in triple patterns
*
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public abstract class AbstractRequestProcessorForTriplePatterns
extends AbstractRequestProcessor
public abstract class
AbstractRequestProcessorForTriplePatterns<TermType,VarType>
extends AbstractRequestProcessor
{
@Override
protected Worker getWorker( final LinkedDataFragmentRequest request )
protected final Worker<TermType,VarType> getWorker(
final LinkedDataFragmentRequest request )
throws IllegalArgumentException
{
if ( request instanceof TriplePatternFragmentRequest )
return getWorker( (TriplePatternFragmentRequest) request );
if ( request instanceof TriplePatternFragmentRequest<?,?> ) {
@SuppressWarnings("unchecked")
final TriplePatternFragmentRequest<TermType,VarType> tpfRequest =
(TriplePatternFragmentRequest<TermType,VarType>) request;
return getTPFSpecificWorker( tpfRequest );
}
else
throw new IllegalArgumentException( request.getClass().getName() );
}

abstract protected Worker getWorker(
final TriplePatternFragmentRequest request )
abstract protected Worker<TermType,VarType> getTPFSpecificWorker(
final TriplePatternFragmentRequest<TermType,VarType> request )
throws IllegalArgumentException;


abstract static protected class Worker
abstract static protected class Worker<TermType,VarType>
extends AbstractRequestProcessor.Worker
{
public Worker( final TriplePatternFragmentRequest request )
public Worker(
final TriplePatternFragmentRequest<TermType,VarType> request )
{
super( request );
}
Expand All @@ -51,20 +62,22 @@ public LinkedDataFragment createRequestedFragment()
else
offset = 0L;

final TriplePatternFragmentRequest tpfRequest =
(TriplePatternFragmentRequest) request;
@SuppressWarnings("unchecked")
final TriplePatternFragmentRequest<TermType,VarType> tpfRequest =
(TriplePatternFragmentRequest<TermType,VarType>) request;

return createFragment( tpfRequest.getSubject(),
tpfRequest.getPredicate(),
tpfRequest.getObject(),
offset, limit );
}

abstract protected LinkedDataFragment createFragment( final String subj,
final String pred,
final String obj,
final long offset,
final long limit )
abstract protected LinkedDataFragment createFragment(
final TriplePatternElement<TermType,VarType> subj,
final TriplePatternElement<TermType,VarType> pred,
final TriplePatternElement<TermType,VarType> obj,
final long offset,
final long limit )
throws IllegalArgumentException;

protected TriplePatternFragment createEmptyTriplePatternFragment()
Expand All @@ -78,17 +91,11 @@ protected TriplePatternFragment createTriplePatternFragment(
final long totalSize,
final boolean isLastPage )
{
final long pageNumber;
if ( request.isPageRequest() )
pageNumber = request.getPageNumber();
else
pageNumber = 1L;

return new TriplePatternFragmentImpl( triples,
totalSize,
request.getFragmentURL(),
request.getDatasetURL(),
pageNumber,
request.getPageNumber(),
isLastPage );
}

Expand Down
18 changes: 0 additions & 18 deletions src/org/linkeddatafragments/datasource/DataSource.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.linkeddatafragments.datasource;

import org.linkeddatafragments.fragments.IFragmentRequestParser;
import org.linkeddatafragments.fragments.LinkedDataFragmentRequest;
import org.linkeddatafragments.fragments.tpf.TPFRequestParser;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest;

/**
*
* @author mielvandersande
Expand All @@ -29,19 +24,6 @@ public String getTitle() {
return this.title;
};

/**
* This implementation assumes that requests are
* {@link TriplePatternFragmentRequest}s.
*
* Data sources for other types of {@link LinkedDataFragmentRequest}s must
* override this method accordingly.
*/
@Override
public IFragmentRequestParser getRequestParser()
{
return new TPFRequestParser();
}

@Override
public void close() {}
}
Loading

0 comments on commit f03b8ed

Please sign in to comment.