Skip to content

Commit eadf5ed

Browse files
author
Miel Vander Sande
committed
Merge conflicts
2 parents 1b47743 + 7e0473a commit eadf5ed

File tree

8 files changed

+176
-33
lines changed

8 files changed

+176
-33
lines changed
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package org.linkeddatafragments.datasource;
22

33
import com.google.gson.JsonObject;
4-
import java.io.File;
5-
import java.io.IOException;
64
import org.linkeddatafragments.exceptions.DataSourceException;
75
import org.linkeddatafragments.exceptions.UnknownDataSourceTypeException;
86

97
/**
108
*
119
* @author Miel Vander Sande
1210
* @author Bart Hanssens
11+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
1312
*/
1413
public class DataSourceFactory {
15-
public final static String HDT = "HdtDatasource";
16-
public final static String JENA_TDB = "JenaTDBDatasource";
17-
1814
/**
1915
* Create a datasource using a JSON config
2016
*
@@ -25,28 +21,15 @@ public class DataSourceFactory {
2521
public static IDataSource create(JsonObject config) throws DataSourceException {
2622
String title = config.getAsJsonPrimitive("title").getAsString();
2723
String description = config.getAsJsonPrimitive("description").getAsString();
28-
String type = config.getAsJsonPrimitive("type").getAsString();
24+
String typeName = config.getAsJsonPrimitive("type").getAsString();
2925

3026
JsonObject settings = config.getAsJsonObject("settings");
3127

32-
switch (type) {
33-
case HDT:
34-
try {
35-
File file = new File(settings.getAsJsonPrimitive("file").getAsString());
36-
return new HdtDataSource(title, description, file.getAbsolutePath());
37-
} catch (IOException ex) {
38-
throw new DataSourceException(ex);
39-
}
40-
41-
case JENA_TDB:
42-
File file = new File(settings.getAsJsonPrimitive("directory").getAsString());
43-
return new JenaTDBDataSource(title, description, file);
44-
45-
default:
46-
throw new UnknownDataSourceTypeException(type);
47-
48-
}
28+
final IDataSourceType type = DataSourceTypesRegistry.getType(typeName);
29+
if ( type == null )
30+
throw new UnknownDataSourceTypeException(typeName);
4931

32+
return type.createDataSource( title, description, settings );
5033
}
5134

5235
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.linkeddatafragments.datasource;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* A registry of {@link IDataSourceType}s.
8+
*
9+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
10+
*/
11+
public class DataSourceTypesRegistry
12+
{
13+
private static Map<String, IDataSourceType> registry =
14+
new HashMap<String, IDataSourceType>();
15+
16+
public static synchronized IDataSourceType getType( final String typeName )
17+
{
18+
return registry.get( typeName );
19+
}
20+
21+
public static synchronized boolean isRegistered( final String typeName )
22+
{
23+
return registry.containsKey( typeName );
24+
}
25+
26+
public static synchronized void register( final String typeName,
27+
final IDataSourceType type )
28+
{
29+
if ( registry.containsKey(typeName) ) {
30+
throw new IllegalArgumentException( "The registry already " +
31+
"contains a type with the name '" + typeName + "'." );
32+
}
33+
registry.put( typeName, type );
34+
}
35+
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.linkeddatafragments.datasource;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import org.linkeddatafragments.exceptions.DataSourceException;
7+
8+
import com.google.gson.JsonObject;
9+
10+
/**
11+
* The type of HDT-backed Triple Pattern Fragment data sources.
12+
*
13+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
14+
*/
15+
public class HdtDataSourceType implements IDataSourceType
16+
{
17+
public static final String TYPE_NAME = "HdtDatasource";
18+
19+
public static void register() {
20+
if ( ! DataSourceTypesRegistry.isRegistered(TYPE_NAME) ) {
21+
DataSourceTypesRegistry.register( TYPE_NAME,
22+
new HdtDataSourceType() );
23+
}
24+
}
25+
26+
@Override
27+
public IDataSource createDataSource( final String title,
28+
final String description,
29+
final JsonObject settings )
30+
throws DataSourceException
31+
{
32+
final String fname = settings.getAsJsonPrimitive("file").getAsString();
33+
final File file = new File( fname );
34+
35+
try {
36+
return new HdtDataSource(title, description, file.getAbsolutePath());
37+
} catch (IOException ex) {
38+
throw new DataSourceException(ex);
39+
}
40+
}
41+
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.linkeddatafragments.datasource;
2+
3+
import org.linkeddatafragments.exceptions.DataSourceException;
4+
5+
import com.google.gson.JsonObject;
6+
7+
/**
8+
* Represents types of {@link IDataSource}s that can be used to provide some
9+
* Linked Data Fragments interface.
10+
*
11+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
12+
*/
13+
public interface IDataSourceType
14+
{
15+
/**
16+
* Creates a data source of this type.
17+
*
18+
* @param title
19+
* The title of the data source (as given in the config file).
20+
*
21+
* @param description
22+
* The description of the data source (as given in the config file).
23+
*
24+
* @param settings
25+
* The properties of the data source to be created; usually, these
26+
* properties are given in the config file of the LDF server.
27+
*/
28+
IDataSource createDataSource( final String title,
29+
final String description,
30+
final JsonObject settings )
31+
throws DataSourceException;
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.linkeddatafragments.datasource;
2+
3+
import java.io.File;
4+
5+
import org.linkeddatafragments.exceptions.DataSourceException;
6+
7+
import com.google.gson.JsonObject;
8+
9+
/**
10+
* The type of Triple Pattern Fragment data sources that are backed by
11+
* a Jena TDB instance.
12+
*
13+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
14+
*/
15+
public class JenaTDBDataSourceType implements IDataSourceType
16+
{
17+
public static final String TYPE_NAME = "JenaTDBDatasource";
18+
19+
public static void register() {
20+
if ( ! DataSourceTypesRegistry.isRegistered(TYPE_NAME) ) {
21+
DataSourceTypesRegistry.register( TYPE_NAME,
22+
new JenaTDBDataSourceType() );
23+
}
24+
}
25+
26+
@Override
27+
public IDataSource createDataSource( final String title,
28+
final String description,
29+
final JsonObject settings )
30+
throws DataSourceException
31+
{
32+
final String dname = settings.getAsJsonPrimitive("directory").getAsString();
33+
final File dir = new File( dname );
34+
35+
try {
36+
return new JenaTDBDataSource(title, description, dir);
37+
} catch (Exception ex) {
38+
throw new DataSourceException(ex);
39+
}
40+
}
41+
42+
}

src/org/linkeddatafragments/servlet/TriplePatternFragmentServlet.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import org.apache.jena.riot.RDFLanguages;
3232
import org.linkeddatafragments.config.ConfigReader;
3333
import org.linkeddatafragments.datasource.DataSourceFactory;
34+
import org.linkeddatafragments.datasource.HdtDataSourceType;
3435
import org.linkeddatafragments.datasource.IDataSource;
3536
import org.linkeddatafragments.datasource.IndexDataSource;
37+
import org.linkeddatafragments.datasource.JenaTDBDataSourceType;
3638
import org.linkeddatafragments.datasource.TriplePatternFragment;
3739
import org.linkeddatafragments.exceptions.DataSourceException;
3840
import org.linkeddatafragments.util.CommonResources;
@@ -64,6 +66,11 @@ public class TriplePatternFragmentServlet extends HttpServlet {
6466
private final HashMap<String, IDataSource> dataSources = new HashMap<>();
6567
private final Collection<String> mimeTypes = new ArrayList<>();
6668

69+
public TriplePatternFragmentServlet() {
70+
HdtDataSourceType.register();
71+
JenaTDBDataSourceType.register();
72+
}
73+
6774
private File getConfigFile(ServletConfig config) throws IOException {
6875
String path = config.getServletContext().getRealPath("/");
6976
if (path == null) {

src/test/java/org/linkeddatafragments/datasource/HdtDataSourceTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package test.java.org.linkeddatafragments.datasource;
22

33
import com.google.gson.JsonObject;
4-
5-
64
import java.io.File;
7-
85
import org.junit.After;
96
import org.junit.AfterClass;
107
import org.junit.Before;
118
import org.junit.BeforeClass;
12-
139
import org.linkeddatafragments.datasource.DataSourceFactory;
14-
10+
import org.linkeddatafragments.datasource.HdtDataSourceType;
1511
import org.rdfhdt.hdt.enums.RDFNotation;
1612
import org.rdfhdt.hdt.hdt.HDT;
1713
import org.rdfhdt.hdt.hdt.HDTManager;
@@ -28,6 +24,7 @@ public class HdtDataSourceTest extends DataSourceTest {
2824

2925
@BeforeClass
3026
public static void setUpClass() throws Exception {
27+
HdtDataSourceType.register();
3128
// HDT does not seem to support an InputReader, so write to temp file
3229
File temp = getResourceAsFile();
3330

@@ -40,8 +37,9 @@ public static void setUpClass() throws Exception {
4037
temp.getAbsoluteFile().delete();
4138

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

44-
JsonObject config = createConfig("hdt test", "hdt test", DataSourceFactory.HDT);
4543
JsonObject settings = new JsonObject();
4644
settings.addProperty("file", hdtfile.getAbsolutePath());
4745
config.add("settings", settings);

src/test/java/org/linkeddatafragments/datasource/JenaTDBDataSourceTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.linkeddatafragments.datasource.DataSourceFactory;
2222

23+
import org.linkeddatafragments.datasource.JenaTDBDataSourceType;
24+
2325
/**
2426
*
2527
* @author Bart Hanssens <[email protected]>
@@ -30,6 +32,8 @@ public class JenaTDBDataSourceTest extends DataSourceTest {
3032

3133
@BeforeClass
3234
public static void setUpClass() throws Exception {
35+
JenaTDBDataSourceType.register();
36+
3337
String tmpdir = System.getProperty("java.io.tmpdir");
3438
jena = new File(tmpdir, "ldf-jena-test");
3539
jena.mkdir();
@@ -40,11 +44,10 @@ public static void setUpClass() throws Exception {
4044
InputStream in = ClassLoader.getSystemResourceAsStream("demo.nt");
4145
RDFDataMgr.read(model, in, Lang.NTRIPLES);
4246
model.commit();
43-
44-
// Everything is in place, now create the LDF datasource
45-
47+
48+
// Everything is in place, now create the LDF datasource
4649
JsonObject config = createConfig("jena tdb test", "jena tdb test",
47-
DataSourceFactory.JENA_TDB);
50+
JenaTDBDataSourceType.TYPE_NAME);
4851

4952
JsonObject settings = new JsonObject();
5053
settings.addProperty("directory", jena.getAbsolutePath());

0 commit comments

Comments
 (0)