Skip to content

Commit 38b4d9c

Browse files
author
Olaf Hartig
committed
Merge pull request #1 from LinkedDataFragments/master
merging in latest changes from origin
2 parents 7988775 + ad28f83 commit 38b4d9c

15 files changed

+113747
-93
lines changed

src/org/linkeddatafragments/datasource/DataSource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ public String getDescription() {
3737
public String getTitle() {
3838
return this.title;
3939
};
40+
41+
@Override
42+
public void close() {}
4043
}
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+
}

src/org/linkeddatafragments/datasource/IDataSource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.linkeddatafragments.datasource;
22

3+
import java.io.Closeable;
4+
35
import com.hp.hpl.jena.rdf.model.Property;
46
import com.hp.hpl.jena.rdf.model.RDFNode;
57
import com.hp.hpl.jena.rdf.model.Resource;
@@ -8,7 +10,7 @@
810
* A data source of Basic Linked Data Fragments.
911
* @author Ruben Verborgh
1012
*/
11-
public interface IDataSource {
13+
public interface IDataSource extends Closeable {
1214
/**
1315
* Gets a page of the Basic Linked Data Fragment matching the specified triple pattern.
1416
* @param subject the subject (null to match any subject)
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+
}

src/org/linkeddatafragments/datasource/JenaTDBDataSource.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.linkeddatafragments.datasource;
22

3-
import com.hp.hpl.jena.graph.Graph;
4-
import com.hp.hpl.jena.graph.GraphStatisticsHandler;
5-
import com.hp.hpl.jena.graph.Node;
63
import com.hp.hpl.jena.query.Dataset;
74
import com.hp.hpl.jena.query.Query;
85
import com.hp.hpl.jena.query.QueryExecution;
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.linkeddatafragments.exceptions;
2+
3+
import org.linkeddatafragments.datasource.IDataSource;
4+
5+
/**
6+
*
7+
* @author mielvandersande
8+
*/
9+
public class DataSourceNotFoundException extends Exception {
10+
private static final long serialVersionUID = 1L;
11+
12+
public DataSourceNotFoundException(Throwable cause) {
13+
super(cause.getMessage());
14+
}
15+
16+
public DataSourceNotFoundException(String dataSourceName) {
17+
super("Data source " + dataSourceName + " not found.");
18+
}
19+
}

src/org/linkeddatafragments/servlet/TriplePatternFragmentServlet.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Collection;
1919
import java.util.HashMap;
2020
import java.util.Map.Entry;
21+
import java.util.logging.Level;
22+
import java.util.logging.Logger;
2123
import java.util.regex.Matcher;
2224
import java.util.regex.Pattern;
2325
import javax.servlet.ServletConfig;
@@ -31,10 +33,13 @@
3133
import org.apache.jena.riot.RDFLanguages;
3234
import org.linkeddatafragments.config.ConfigReader;
3335
import org.linkeddatafragments.datasource.DataSourceFactory;
36+
import org.linkeddatafragments.datasource.HdtDataSourceType;
3437
import org.linkeddatafragments.datasource.IDataSource;
3538
import org.linkeddatafragments.datasource.IndexDataSource;
39+
import org.linkeddatafragments.datasource.JenaTDBDataSourceType;
3640
import org.linkeddatafragments.datasource.TriplePatternFragment;
3741
import org.linkeddatafragments.exceptions.DataSourceException;
42+
import org.linkeddatafragments.exceptions.DataSourceNotFoundException;
3843
import org.linkeddatafragments.util.CommonResources;
3944
import org.linkeddatafragments.util.MIMEParse;
4045

@@ -64,6 +69,11 @@ public class TriplePatternFragmentServlet extends HttpServlet {
6469
private final HashMap<String, IDataSource> dataSources = new HashMap<>();
6570
private final Collection<String> mimeTypes = new ArrayList<>();
6671

72+
public TriplePatternFragmentServlet() {
73+
HdtDataSourceType.register();
74+
JenaTDBDataSourceType.register();
75+
}
76+
6777
private File getConfigFile(ServletConfig config) throws IOException {
6878
String path = config.getServletContext().getRealPath("/");
6979
if (path == null) {
@@ -104,14 +114,27 @@ public void init(ServletConfig servletConfig) throws ServletException {
104114
}
105115
}
106116

117+
@Override
118+
public void destroy()
119+
{
120+
for ( IDataSource dataSource : dataSources.values() ) {
121+
try {
122+
dataSource.close();
123+
}
124+
catch( Exception e ) {
125+
// ignore
126+
}
127+
}
128+
}
129+
107130
/**
108131
* Get the datasource
109132
*
110133
* @param request
111134
* @return
112135
* @throws IOException
113136
*/
114-
private IDataSource getDataSource(HttpServletRequest request) throws IOException {
137+
private IDataSource getDataSource(HttpServletRequest request) throws DataSourceNotFoundException {
115138
String contextPath = request.getContextPath();
116139
String requestURI = request.getRequestURI();
117140

@@ -126,7 +149,7 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
126149
String dataSourceName = path.substring(1);
127150
IDataSource dataSource = dataSources.get(dataSourceName);
128151
if (dataSource == null) {
129-
throw new IOException("Data source not found.");
152+
throw new DataSourceNotFoundException(dataSourceName);
130153
}
131154
return dataSource;
132155
}
@@ -292,8 +315,15 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
292315

293316
RDFDataMgr.write(response.getOutputStream(), output, contentType);
294317
} catch (IOException | URISyntaxException e) {
295-
e.printStackTrace();
296318
throw new ServletException(e);
319+
} catch (DataSourceNotFoundException ex) {
320+
try {
321+
response.setStatus(404);
322+
response.getOutputStream().println(ex.getMessage());
323+
response.getOutputStream().close();
324+
} catch (IOException ex1) {
325+
throw new ServletException(ex1);
326+
}
297327
}
298328
}
299329

0 commit comments

Comments
 (0)