Convert TPF from Gson to Jackson

This commit is contained in:
Graham Triggs 2017-09-29 00:54:14 +01:00
parent 93884142b4
commit 69c4f5460c
7 changed files with 52 additions and 35 deletions

View file

@ -1,12 +1,14 @@
package org.linkeddatafragments.config; package org.linkeddatafragments.config;
import com.google.gson.JsonElement; import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParser; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.linkeddatafragments.datasource.IDataSourceType; import org.linkeddatafragments.datasource.IDataSourceType;
import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -18,7 +20,7 @@ import java.util.Map.Entry;
*/ */
public class ConfigReader { public class ConfigReader {
private final Map<String, IDataSourceType> dataSourceTypes = new HashMap<>(); private final Map<String, IDataSourceType> dataSourceTypes = new HashMap<>();
private final Map<String, JsonObject> dataSources = new HashMap<>(); private final Map<String, JsonNode> dataSources = new HashMap<>();
private final Map<String, String> prefixes = new HashMap<>(); private final Map<String, String> prefixes = new HashMap<>();
private final String baseURL; private final String baseURL;
@ -28,19 +30,34 @@ public class ConfigReader {
* @param configReader the configuration * @param configReader the configuration
*/ */
public ConfigReader(Reader configReader) { public ConfigReader(Reader configReader) {
JsonObject root = new JsonParser().parse(configReader).getAsJsonObject(); ObjectMapper mapper = new ObjectMapper();
this.baseURL = root.has("baseURL") ? root.getAsJsonPrimitive("baseURL").getAsString() : null; JsonNode root = null;
try {
for (Entry<String, JsonElement> entry : root.getAsJsonObject("datasourcetypes").entrySet()) { root = mapper.readTree(configReader);
final String className = entry.getValue().getAsString(); this.baseURL = root.has("baseURL") ? root.get("baseURL").asText() : null;
dataSourceTypes.put(entry.getKey(), initDataSouceType(className) );
} Iterator<Entry<String, JsonNode>> iterator;
for (Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
JsonObject dataSource = entry.getValue().getAsJsonObject(); iterator = root.get("datasourcetypes").fields();
this.dataSources.put(entry.getKey(), dataSource); while (iterator.hasNext()) {
} Entry<String, JsonNode> entry = iterator.next();
for (Entry<String, JsonElement> entry : root.getAsJsonObject("prefixes").entrySet()) { final String className = entry.getValue().asText();
this.prefixes.put(entry.getKey(), entry.getValue().getAsString()); dataSourceTypes.put(entry.getKey(), initDataSouceType(className) );
}
iterator = root.get("datasources").fields();
while (iterator.hasNext()) {
Entry<String, JsonNode> entry = iterator.next();
this.dataSources.put(entry.getKey(), entry.getValue());
}
iterator = root.get("prefixes").fields();
while (iterator.hasNext()) {
Entry<String, JsonNode> entry = iterator.next();
this.prefixes.put(entry.getKey(), entry.getValue().asText());
}
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
@ -58,7 +75,7 @@ public class ConfigReader {
* *
* @return the data sources * @return the data sources
*/ */
public Map<String, JsonObject> getDataSources() { public Map<String, JsonNode> getDataSources() {
return dataSources; return dataSources;
} }

View file

@ -1,6 +1,6 @@
package org.linkeddatafragments.datasource; package org.linkeddatafragments.datasource;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.JsonNode;
import org.linkeddatafragments.exceptions.DataSourceCreationException; import org.linkeddatafragments.exceptions.DataSourceCreationException;
import org.linkeddatafragments.exceptions.UnknownDataSourceTypeException; import org.linkeddatafragments.exceptions.UnknownDataSourceTypeException;
@ -18,12 +18,12 @@ public class DataSourceFactory {
* @return datasource interface * @return datasource interface
* @throws DataSourceCreationException * @throws DataSourceCreationException
*/ */
public static IDataSource create(JsonObject config) throws DataSourceCreationException { public static IDataSource create(JsonNode config) throws DataSourceCreationException {
String title = config.getAsJsonPrimitive("title").getAsString(); String title = config.get("title").asText();
String description = config.getAsJsonPrimitive("description").getAsString(); String description = config.get("description").asText();
String typeName = config.getAsJsonPrimitive("type").getAsString(); String typeName = config.get("type").asText();
JsonObject settings = config.getAsJsonObject("settings"); JsonNode settings = config.get("settings");
final IDataSourceType type = DataSourceTypesRegistry.getType(typeName); final IDataSourceType type = DataSourceTypesRegistry.getType(typeName);
if ( type == null ) if ( type == null )

View file

@ -1,6 +1,6 @@
package org.linkeddatafragments.datasource; package org.linkeddatafragments.datasource;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.JsonNode;
import org.linkeddatafragments.exceptions.DataSourceCreationException; import org.linkeddatafragments.exceptions.DataSourceCreationException;
/** /**
@ -28,6 +28,6 @@ public interface IDataSourceType
*/ */
IDataSource createDataSource(final String title, IDataSource createDataSource(final String title,
final String description, final String description,
final JsonObject settings) final JsonNode settings)
throws DataSourceCreationException; throws DataSourceCreationException;
} }

View file

@ -1,6 +1,6 @@
package org.linkeddatafragments.datasource.tdb; package org.linkeddatafragments.datasource.tdb;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.JsonNode;
import org.linkeddatafragments.datasource.IDataSource; import org.linkeddatafragments.datasource.IDataSource;
import org.linkeddatafragments.datasource.IDataSourceType; import org.linkeddatafragments.datasource.IDataSourceType;
import org.linkeddatafragments.exceptions.DataSourceCreationException; import org.linkeddatafragments.exceptions.DataSourceCreationException;
@ -18,10 +18,10 @@ public class JenaTDBDataSourceType implements IDataSourceType
@Override @Override
public IDataSource createDataSource( final String title, public IDataSource createDataSource( final String title,
final String description, final String description,
final JsonObject settings ) final JsonNode settings )
throws DataSourceCreationException throws DataSourceCreationException
{ {
final String dname = settings.getAsJsonPrimitive("directory").getAsString(); final String dname = settings.get("directory").asText();
final File dir = new File( dname ); final File dir = new File( dname );
try { try {

View file

@ -1,6 +1,6 @@
package org.linkeddatafragments.servlet; package org.linkeddatafragments.servlet;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.JsonNode;
import org.apache.jena.riot.Lang; import org.apache.jena.riot.Lang;
import org.linkeddatafragments.config.ConfigReader; import org.linkeddatafragments.config.ConfigReader;
import org.linkeddatafragments.datasource.DataSourceFactory; import org.linkeddatafragments.datasource.DataSourceFactory;
@ -89,7 +89,7 @@ public class LinkedDataFragmentServlet extends HttpServlet {
} }
// register data sources // register data sources
for (Entry<String, JsonObject> dataSource : config.getDataSources().entrySet()) { for (Entry<String, JsonNode> dataSource : config.getDataSources().entrySet()) {
dataSources.put(dataSource.getKey(), DataSourceFactory.create(dataSource.getValue())); dataSources.put(dataSource.getKey(), DataSourceFactory.create(dataSource.getValue()));
} }

View file

@ -1,6 +1,6 @@
package org.vivoweb.linkeddatafragments.datasource.rdfservice; package org.vivoweb.linkeddatafragments.datasource.rdfservice;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.JsonNode;
import org.linkeddatafragments.datasource.IDataSource; import org.linkeddatafragments.datasource.IDataSource;
import org.linkeddatafragments.datasource.IDataSourceType; import org.linkeddatafragments.datasource.IDataSourceType;
import org.linkeddatafragments.exceptions.DataSourceCreationException; import org.linkeddatafragments.exceptions.DataSourceCreationException;
@ -16,7 +16,7 @@ public class RDFServiceDataSourceType implements IDataSourceType
@Override @Override
public IDataSource createDataSource( final String title, public IDataSource createDataSource( final String title,
final String description, final String description,
final JsonObject settings ) final JsonNode settings )
throws DataSourceCreationException throws DataSourceCreationException
{ {
try { try {

View file

@ -1,6 +1,6 @@
package org.vivoweb.linkeddatafragments.servlet; package org.vivoweb.linkeddatafragments.servlet;
import com.google.gson.JsonObject; import com.fasterxml.jackson.databind.JsonNode;
import edu.cornell.mannlib.vitro.webapp.beans.Ontology; import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet; import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao; import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
@ -90,7 +90,7 @@ public class VitroLinkedDataFragmentServlet extends VitroHttpServlet {
} }
// register data sources // register data sources
for (Entry<String, JsonObject> dataSource : config.getDataSources().entrySet()) { for (Entry<String, JsonNode> dataSource : config.getDataSources().entrySet()) {
dataSources.put(dataSource.getKey(), DataSourceFactory.create(dataSource.getValue())); dataSources.put(dataSource.getKey(), DataSourceFactory.create(dataSource.getValue()));
} }