Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:zepheira/backstage

  • Loading branch information...
commit 1512998b37d53be36cbcc3e879dfc4dea61154d9 2 parents 9590e88 + ddb2472
@zepheiryan zepheiryan authored
View
139 modules/backstage/MOD-INF/server.js
@@ -11,8 +11,8 @@ function process(path, request, response) {
//
// Resource URLs;
//
- // /data/ - POST Exhibit JSON data here. Returns a 201 pointing to ...
- // /data/<data-slug>/ - reference URL used in the Exhibit HTML template. POST here to add more data(TBD).
+ // /data/{disk,mem}/ - POST Exhibit JSON data here. Returns a 201 pointing to ...
+ // /data/{disk,mem}/<data-slug>/ - reference URL used in the Exhibit HTML template. POST here to add more data(TBD).
// /exhibit-session - where configurations of lenses and facets are POSTed, returning 201 to...
// /exhibit-session/<sess-slug>/ - which is where the facet queries are performed
// /exhibit-session/<sess-slug>/component/<compid> - components of the exhibit, where you can POST state
@@ -29,14 +29,14 @@ function process(path, request, response) {
}
if (method == "GET") {
if (pathSegs[0] == DATA_SEG) {
- if (pathSegs.length == 1) {
+ if (pathSegs.length == 2) {
// return HTML form for file upload
CORSify(request,response);
butterfly.sendString(request, response, "<html><body>Upload form goes here</body></html>", "utf-8", "text/html");
return;
- } else if (pathSegs.length == 2) {
+ } else if (pathSegs.length == 3) {
importPackage(edu.mit.simile.backstage.util);
- var db = backstage.getDatabase(DATA_URL_ROOT+pathSegs[1]);
+ var db = backstage.getDatabase(DATA_URL_ROOT+pathSegs.slice(1).join("/"));
if (db == null) {
butterfly.sendError(request, response, 404, "Data not found");
return;
@@ -44,7 +44,7 @@ function process(path, request, response) {
var limit = extractQueryParamValue(request,"limit");
if (!limit) limit = 20;
- var result = db.exportRDFa(limit,pathSegs[1]);
+ var result = db.exportRDFa(limit,pathSegs[2]);
respond(request,response,{"contentType":"text/html",
"Cache-Control":"max-age="+String(86400*365),
@@ -56,30 +56,10 @@ function process(path, request, response) {
return;
}
} else if (pathSegs[0] == "exhibit-session") {
- if (pathSegs.length != 2) {
- butterfly.sendError(request, response, 404, "Page not found");
- return;
- }
- // could check for "components" under here, but probably not useful
+ // tried to implement a scissor-UI like session snapshot export here, but
+ // impractical
+ butterfly.sendError(request, response, 403, "Page not found");
- var exhibit = backstage.getExhibit(request, pathSegs[1])
- if (exhibit == null) {
- butterfly.sendError(request, response, 404, "Exhibit session not found");
- return;
- }
- // extract any facet selection state from query params. Unlike Backstage
- // restrictions which were per-facet, "restrictions" contains the state of all
- // facets. JSON for now for simplicity, but would be more transparent as a
- // regular URL query term.
- var restrictions = butterfly.parseJSON(unescape(extractQueryParamValue(request,"restr")));
-
- // remove some state by resetting and rebuilding restrictions with each query.
- // the performance will likely suck, but it's a start.
- var result = clearAllFacetRestrictions(request, exhibit);
-
- if (restrictions) {
- var result = applyAllFacetRestrictions(request, restrictions, exhibit);
- }
CORSify(request,response,exhibit);
respond(request,response,result);
return;
@@ -88,21 +68,40 @@ function process(path, request, response) {
}
} else if (method == "POST") {
if (pathSegs[0] == DATA_SEG) {
- if (pathSegs.length == 1) {
+ if (pathSegs.length == 2) {
+ // next path segment is repository type
+ var repoType = pathSegs[1];
+ if (repoType != "mem" && repoType != "disk") {
+ butterfly.sendError(request, response, 404, "Data not found");
+ return;
+ }
+
importPackage(Packages.java.io);
importPackage(Packages.java.lang);
+
var dataSlug = getSlug(request);
- var dbDir = System.getProperty("backstage.databaseDir","databases");
- var fullDbDir = File(dbDir,dataSlug);
+ var dbRootDir = System.getProperty("backstage.databaseDir","databases");
+ var fullDbDir = File(File(dbRootDir,repoType),dataSlug);
if (fullDbDir.exists()) {
respond(request,response,{"status":500, "out": "The slug '"+dataSlug+"'is already in use"});
}
- var result = uploadExhibitData(request);
+ // verify repo type directory exists, else make it
+ var repoTypeDir = File(dbRootDir,repoType);
+ if (!repoTypeDir.exists()) {
+ // can't do much with exceptions, so punt to user via 500 response
+ if (repoTypeDir.mkdir()) {
+ // pass
+ } else {
+ respond(request,response,{"status":500, "out": "Unable to create database directory under "+dbRootDir});
+ }
+ }
+
+ var result = uploadExhibitData(request,repoType,dataSlug);
respond(request,response,result);
return;
- } else if (pathSegs.length == 2) {
+ } else if (pathSegs.length == 3) {
// uploadExhibitData(), appending to existing data. TBD.
} else {
butterfly.sendError(request, response, 404, "Data not found");
@@ -258,6 +257,7 @@ function uploadExhibitConfig(request,response) {
var result;
try {
result = configureExhibit(request,params,exhibit);
+ var result;
var location = "/exhibit-session/"+exhibitSlug;
result.location = location; // stick in body since Chrome can't expose the header
return {"status":201,"out":result,"location":location};
@@ -324,10 +324,9 @@ function readBodyAsJSON(request) {
return butterfly.parseJSON(json);
}
-function uploadExhibitData(request) {
+function uploadExhibitData(request,repoType,dataSlug) {
importPackage(Packages.edu.mit.simile.backstage.util);
-
- var dataSlug = getSlug(request);
+ importPackage(Packages.java.io);
// create repo then assign to database object, as we assume
// that the repository from an uploaded dataset will be used
@@ -335,37 +334,26 @@ function uploadExhibitData(request) {
// ServletContext dies
var repo = null;
try {
- repo = backstage.createRepository(request,dataSlug);
+ repo = backstage.createRepository(request,repoType,dataSlug);
} catch (e) {
return {"status":500,"out":"Problem creating repository: "+e};
}
- var db = backstage.getDatabase(DATA_URL_ROOT+dataSlug);
+ var dbUrl = DATA_URL_ROOT+repoType+File.separator+dataSlug;
+ var db = backstage.getDatabase(dbUrl);
db.setRepository(repo);
- return {"status":201,"location":DATA_URL_ROOT+dataSlug, "out":"Data successfully uploaded"};
-}
-
-function propRecordToObject(props) {
- // convert a Database$PropertyRecord into a Javascript object
- var obj = new Object();
- //obj.id = props.getProperty("id"); // not used
- obj.label = props.getProperty("label");
- //obj.valueType = props.get("valueType"); // not used
- var propNames = props.getPropertyNames();
- while (propNames.hasMoreElements()) {
- var name = propNames.nextElement();
- obj[name] = props.getProperty(name);
- }
- return obj;
+ return {"status":201,"location":dbUrl, "out":"Data successfully uploaded"};
}
function addDataLink(exhibit, link) {
- if (link.url == SERVER_ROOT+"hosted-database") {
+ // the old "hosted" mode, now known as standalone mode, has been disabled
+
+ //if (link.url == SERVER_ROOT+"hosted-database") {
//exhibit.addHostedDataLink(); // disable hosted mode for now
- } else {
+ //} else {
exhibit.addDataLink(link.url);
- }
+ //}
}
function processBackChannel(result, backChannel) {
@@ -424,6 +412,7 @@ function configureExhibit(request, params, exhibit) {
if ( match == null || match.length < 2 ) {
throw "Invalid URL";
}
+
var host = match[1];
var sr_host = SERVER_ROOT.match(/http:\/\/(\S+?)[\/:]/)[1];
if ( host.toLowerCase() != sr_host.toLowerCase()) {
@@ -489,42 +478,6 @@ function configureExhibit(request, params, exhibit) {
return processBackChannel(result, backChannel);
}
-function applyAllFacetRestrictions(request, restrictions, exhibit) {
- importPackage(Packages.edu.mit.simile.backstage.model);
-
- var result = {};
-
- for (i in restrictions) {
- // only the last backchannel is used to determine the respons. we do it
- // this way to work with the legacy statefulness of Backstage
- var backChannel = new BackChannel();
- var facet = exhibit.getComponent(restrictions[i].facetID);
- if ( facet ) {
- facet.applyRestrictions(restrictions[i].restrictions, backChannel);
- } else {
- return {"status":400,"out":"Invalid facet id: "+restrictions[i].facetID};
- }
- }
-
- return {"status":200,"out":processBackChannel(result, backChannel)};
-}
-
-function clearAllFacetRestrictions(request, exhibit) {
- importPackage(Packages.edu.mit.simile.backstage.model);
-
- var result = {};
-
- comps = exhibit.getAllComponents().toArray();
- for (var i=0; i<comps.length; i++ ) {
- if ( "clearRestrictions" in comps[i] ) { // facets only
- var backChannel = new BackChannel();
- comps[i].clearRestrictions(backChannel);
- }
- }
-
- return {"status":200,"out":processBackChannel(result, backChannel)};
-}
-
function facetApplyRestrictions(facet,restrictions) {
importPackage(Packages.edu.mit.simile.backstage.model);
View
91 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/BackstageModule.java
@@ -5,6 +5,8 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
@@ -13,11 +15,13 @@
import org.apache.commons.collections.ExtendedProperties;
-import edu.mit.simile.backstage.data.InMemHostedDataLink;
+import edu.mit.simile.backstage.data.DataLink;
import edu.mit.simile.backstage.util.DataLoadingUtilities;
import edu.mit.simile.backstage.model.Exhibit;
import edu.mit.simile.backstage.model.data.Database;
import edu.mit.simile.backstage.model.data.HostedDatabase;
+import edu.mit.simile.backstage.model.data.StandaloneDiskHostedDatabase;
+import edu.mit.simile.backstage.model.data.OnDiskHostedDatabase;
import edu.mit.simile.backstage.model.data.InMemHostedDatabase;
import edu.mit.simile.butterfly.ButterflyModuleImpl;
@@ -30,21 +34,26 @@
public class BackstageModule extends ButterflyModuleImpl {
- static Map<URL, InMemHostedDatabase> s_linkDatabaseMap;
- static HostedDatabase s_hostedDatabase;
-
+ static Map<URL, Database> s_linkDatabaseMap;
+ static StandaloneDiskHostedDatabase s_standaloneDatabase;
+
+ // The supported types of repositories. Could use enum but don't
+ // know how to represent them via Rhino
+ public static String REPOTYPE_DISK = "DISK";
+ public static String REPOTYPE_MEM = "MEM";
+
@Override
public void init(ServletConfig config) throws Exception {
super.init(config);
if (s_linkDatabaseMap == null) {
- s_linkDatabaseMap = new HashMap<URL, InMemHostedDatabase>();
+ s_linkDatabaseMap = new HashMap<URL, Database>();
}
}
public Database getDatabase(String url) {
- InMemHostedDataLink link;
+ DataLink link;
try {
- link = new InMemHostedDataLink(new URL(url));
+ link = new DataLink(new URL(url));
} catch (MalformedURLException e) {
_logger.error("Invalid data link URL", e);
return null;
@@ -52,15 +61,23 @@ public Database getDatabase(String url) {
return this.getDatabase(link);
}
- public Repository createRepository(HttpServletRequest request, String slug) throws Exception {
+ public Repository createRepository(HttpServletRequest request, String repoType, String slug) throws Exception {
ExtendedProperties properties = getProperties();
String dbDir = properties.getString("backstage.databaseDir","databases");
- MemoryStore sail = new MemoryStore(new File(dbDir,slug));
- SailRepository repository = new SailRepository(sail);
- try {
- repository.initialize();
- } catch (RepositoryException e) {
+ SailRepository repository = null;
+ File thisDbDir = new File(new File(dbDir,repoType),slug);
+ _logger.error("thisDbDir = "+thisDbDir);
+
+ if (repoType.equals("mem")) {
+ DataLoadingUtilities.RepoSailTuple rs = DataLoadingUtilities.createMemoryRepository(thisDbDir);
+ repository = (SailRepository)rs.repository;
+ rs = null;
+ } else if (repoType.equals("disk")) {
+ DataLoadingUtilities.RepoSailTuple rs = DataLoadingUtilities.createNativeRepository(thisDbDir);
+ repository = (SailRepository)rs.repository;
+ rs = null;
+ } else {
return null;
}
@@ -71,35 +88,63 @@ public Repository createRepository(HttpServletRequest request, String slug) thro
DataLoadingUtilities.loadDataFromStream( (InputStream)request.getInputStream(),
request.getRequestURL().toString(),
- lang, (Sail)sail );
+ lang, repository.getSail() );
return repository;
}
- public Database getDatabase(InMemHostedDataLink dataLink) {
- InMemHostedDatabase db = s_linkDatabaseMap.get(dataLink.url);
+ public Database getDatabase(DataLink dataLink) {
+ Database db = s_linkDatabaseMap.get(dataLink.url);
+ _logger.error("bsmod getdb lookup = "+db);
+
if (db == null) {
- db = new InMemHostedDatabase(dataLink);
+ // inspect the link to determine our repository type, relativizing
+ // against our Butterfly mount point
+ URI dbUri = null;
+ URI mountUri = null;
+ try {
+ dbUri = new URI(dataLink.url.toString());
+ mountUri = new URI(this.getMountPoint().getMountPoint()); // awkward!
+ } catch (URISyntaxException e) {
+ return null;
+ }
+
+ URI fullMountUri = dbUri.resolve(mountUri);
+ String mountPath = dbUri.toString().substring(fullMountUri.toString().length());
+ String[] mountPathSegs = mountPath.toString().split(File.separator);
+ if (mountPathSegs.length != 3) {
+ return null;
+ }
+
+ String repoType = mountPathSegs[1];
+ if (repoType.equals("mem")) {
+ db = new InMemHostedDatabase(dataLink);
+ } else if (repoType.equals("disk")) {
+ db = new OnDiskHostedDatabase(dataLink);
+ } else {
+ return null;
+ }
+
s_linkDatabaseMap.put(dataLink.url, db);
}
return db;
}
- public void releaseDatabase(InMemHostedDatabase database) {
- InMemHostedDataLink link = database.getDataLink();
+ public void releaseDatabase(HostedDatabase database) {
+ DataLink link = database.getDataLink();
s_linkDatabaseMap.remove(link.url);
}
- public Database getHostedDatabase() {
- if (s_hostedDatabase == null) {
+ public Database getStandaloneDatabase() {
+ if (s_standaloneDatabase == null) {
ExtendedProperties properties = getProperties();
String databaseString = properties.getString("backstage.hostedData.database");
File database = (databaseString == null || databaseString.length() == 0) ?
new File("database") : new File(databaseString);
- s_hostedDatabase = new HostedDatabase(database);
+ s_standaloneDatabase = new StandaloneDiskHostedDatabase(database);
}
- return s_hostedDatabase;
+ return s_standaloneDatabase;
}
/**
View
4 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/ScriptableBackstage.java
@@ -51,9 +51,9 @@ public Object jsFunction_getExhibit(Object requestO, String id) throws Malformed
return wrap(exhibit, this);
}
- public Object jsFunction_createRepository(Object requestO, String slug) throws MalformedURLException, Exception {
+ public Object jsFunction_createRepository(Object requestO, String repoType, String slug) throws MalformedURLException, Exception {
HttpServletRequest request = (HttpServletRequest) unwrap(requestO);
- Repository repo = getModule().createRepository(request,slug);
+ Repository repo = getModule().createRepository(request,repoType,slug);
return wrap(repo, this);
}
View
9 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/data/DataLink.java
@@ -3,5 +3,12 @@
*/
package edu.mit.simile.backstage.data;
+import java.net.URL;
+
public class DataLink {
-}
+ final public URL url;
+
+ public DataLink(URL url2) {
+ url = url2;
+ }
+}
View
6 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/data/HostedDataLink.java
@@ -1,5 +1,11 @@
package edu.mit.simile.backstage.data;
+import java.net.URL;
+
public class HostedDataLink extends DataLink {
+ public HostedDataLink(URL url) {
+ super(url);
+ }
+
}
View
14 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/data/InMemHostedDataLink.java
@@ -1,14 +0,0 @@
-package edu.mit.simile.backstage.data;
-
-// used to contain other advisory metadata when the URL was dereferenced for its data,
-// but now only the URL is needed. FIXME; replace by URL
-
-import java.net.URL;
-
-public class InMemHostedDataLink extends DataLink {
- final public URL url;
-
- public InMemHostedDataLink( URL url2) {
- url = url2;
- }
-}
View
22 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/model/Exhibit.java
@@ -12,10 +12,10 @@
import edu.mit.simile.backstage.BackstageModule;
import edu.mit.simile.backstage.ExhibitIdentity;
-import edu.mit.simile.backstage.data.InMemHostedDataLink;
+import edu.mit.simile.backstage.data.DataLink;
import edu.mit.simile.backstage.model.data.Collection;
import edu.mit.simile.backstage.model.data.Database;
-import edu.mit.simile.backstage.model.data.InMemHostedDatabase;
+import edu.mit.simile.backstage.model.data.HostedDatabase;
import edu.mit.simile.backstage.model.ui.Component;
public class Exhibit {
@@ -27,7 +27,7 @@
final private BackstageModule _module;
final private ExhibitIdentity _exhibitIdentity;
- private InMemHostedDataLink _dataLink = null;
+ private DataLink _dataLink = null;
private Database _database;
final private Map<String, Collection> _collectionMap = new HashMap<String, Collection>();
@@ -46,8 +46,8 @@ public void dispose() {
if (_database != null) {
_logger.info("Disposing interaction session for " + _exhibitIdentity.toString());
- if (_dataLink != null) { // unhosted
- _module.releaseDatabase((InMemHostedDatabase) _database);
+ if (_dataLink != null) { // not standalone
+ _module.releaseDatabase((HostedDatabase)_database);
}
_database = null;
}
@@ -55,11 +55,11 @@ public void dispose() {
public Database getDatabase() {
if (_database == null) {
- if (_dataLink != null) {
+ //if (_dataLink != null) {
_database = _module.getDatabase(_dataLink);
- } else {
- _database = _module.getHostedDatabase();
- }
+ //} else {
+ //_database = _module.getStandaloneDatabase();
+ //}
}
return _database;
}
@@ -105,12 +105,12 @@ public void addDataLink(String url) throws MalformedURLException {
throw new InternalError("Cannot add more data link after exhibit already initialized");
}
- InMemHostedDataLink dataLink = new InMemHostedDataLink(new URL(url));
+ DataLink dataLink = new DataLink(new URL(url));
_dataLink = dataLink;
}
- public void addHostedDataLink() {
+ public void addStandaloneDataLink() {
if (_database != null) {
throw new InternalError("Cannot add more data link after exhibit already initialized");
}
View
18 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/model/data/Database.java
@@ -32,6 +32,7 @@
import org.openrdf.OpenRDFException;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryResult;
+import org.openrdf.repository.sail.SailRepository;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.BindingSet;
@@ -132,6 +133,8 @@ private void computeCachedInformation() {
sc = _sail.getConnection();
} catch (SailException e) {
_logger.error("Failed to open sail connection in order to compute cached information", e);
+ } catch (Exception e) {
+ } finally {
}
if (sc != null) {
@@ -375,7 +378,20 @@ private String getItemId(URI uri, SailConnection sc) {
return id;
}
-
+
+ synchronized public void setRepository(Repository repo) {
+ // break encapsulation to simplify data upload integration
+ _repository = repo;
+
+ // all repos should be sail repos, but our Java master must be served
+ try {
+ SailRepository sr = (SailRepository)repo;
+ _sail = sr.getSail();
+ } catch (ClassCastException e) {
+ // pass
+ }
+ }
+
abstract public Repository getRepository();
public Sail getSail() {
getRepository();
View
70 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/model/data/HostedDatabase.java
@@ -3,22 +3,66 @@
import java.io.File;
import org.openrdf.repository.Repository;
+import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.openrdf.sail.Sail;
+import org.openrdf.sail.memory.MemoryStore;
+import edu.mit.simile.backstage.ExhibitIdentity;
+import edu.mit.simile.backstage.data.DataLink;
import edu.mit.simile.backstage.util.DataLoadingUtilities;
-public class HostedDatabase extends Database {
- protected static Logger _logger = LoggerFactory.getLogger("backstage.hosted-database");
+// In the original Backstage, the term "Unhosted" indicated that the data was loaded
+// just-in-time from an URL. We're removing that capability for Exhibit3 Staged mode
+// and instead requiring data be pre-uploaded to avoid the inherrent security problems
+// but are still reusing much of the code.
+
+// Originally, HostedDatabase was a single standalone disk based Sesame repository,
+// but as all supported modes are now "hosted", this class has been changed to
+// provide a generic abstraction for both individual in-memory and on-disk stores.
+
+public abstract class HostedDatabase extends Database {
+ final private DataLink _dataLink;
+
+ private int _referenceCount;
+
+ public HostedDatabase(DataLink dataLink) {
+ _dataLink = dataLink;
+ }
+
+ public DataLink getDataLink() {
+ return _dataLink;
+ }
+
+ public int getReferenceCount() {
+ return _referenceCount;
+ }
- public HostedDatabase(File databaseDir) {
- _repository = DataLoadingUtilities.createNativeRepository(databaseDir);
- _sail = ((SailRepository) _repository).getSail();
- }
-
- @Override
- public Repository getRepository() {
- return _repository;
- }
+ public void addReference() {
+ _referenceCount++;
+ }
+
+ public void removeReference() {
+ _referenceCount--;
+ }
+
+ protected abstract DataLoadingUtilities.RepoSailTuple createRepo(File f);
+
+ protected abstract String getRepoType();
+
+ synchronized public Repository getRepository() {
+ if (_repository == null) {
+ String dbDir = System.getProperty("backstage.databaseDir","databases");
+ String dbUrl = _dataLink.url.toString();
+ String dbName = dbUrl.substring(dbUrl.lastIndexOf("/")+1);
+
+ File dbFile = new File(new File(dbDir,getRepoType()),dbName);
+
+ DataLoadingUtilities.RepoSailTuple rs = createRepo(dbFile);
+ _repository = rs.repository;
+ _sail = rs.sail; // set inherited internal sail
+ rs = null;
+ }
+ return _repository;
+ }
}
View
69 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/model/data/InMemHostedDatabase.java
@@ -8,71 +8,20 @@
import org.openrdf.sail.Sail;
import org.openrdf.sail.memory.MemoryStore;
-import edu.mit.simile.backstage.ExhibitIdentity;
-import edu.mit.simile.backstage.data.InMemHostedDataLink;
+import edu.mit.simile.backstage.data.DataLink;
+import edu.mit.simile.backstage.util.DataLoadingUtilities;
-// In the original Backstage, the term "InMemHosted" indicated that the data was loaded
-// just-in-time from an URL. We're removing that capability for Exhibit3 Staged mode
-// and instead requiring data be pre-uploaded to avoid the inherrent security problems
-// with that approach, but are still reusing much of the code. There is a "Hosted"
-// mode but it uses a global database and the implications of that aren't understood
-// at this time.
+public class InMemHostedDatabase extends HostedDatabase {
-public class InMemHostedDatabase extends Database {
- final private InMemHostedDataLink _dataLink;
-
- private int _referenceCount;
-
- public InMemHostedDatabase(InMemHostedDataLink dataLink) {
- _dataLink = dataLink;
+ public InMemHostedDatabase(DataLink datalink) {
+ super(datalink);
}
-
- public InMemHostedDataLink getDataLink() {
- return _dataLink;
- }
-
- public int getReferenceCount() {
- return _referenceCount;
- }
-
- public void addReference() {
- _referenceCount++;
- }
-
- public void removeReference() {
- _referenceCount--;
- }
-
- synchronized public void setRepository(Repository repo) {
- // break encapsulation to simplify data upload integration.
- // defined here instead of Database since the breakage would
- // be a lot more serious for a HostedDatabase
- _repository = repo;
- // all repos should be sail repos, but our Java master must be served
- try {
- SailRepository sr = (SailRepository)repo;
- _sail = sr.getSail();
- } catch (ClassCastException e) {
- // pass
- }
+ protected String getRepoType() {
+ return "mem";
}
- synchronized public Repository getRepository() {
- if (_repository == null) {
- String dbDir = System.getProperty("backstage.databaseDir","databases");
- String dbUrl = _dataLink.url.toString();
- String dbName = dbUrl.substring(dbUrl.lastIndexOf("/")+1);
- File dbFile = new File(dbDir,dbName);
-
- _sail = new MemoryStore(dbFile);
- _repository = new SailRepository(_sail);
- try {
- _repository.initialize();
- } catch (RepositoryException e) {
- _logger.error("Failed to initialize repository", e);
- }
- }
- return _repository;
+ synchronized protected DataLoadingUtilities.RepoSailTuple createRepo(File dbFile) {
+ return DataLoadingUtilities.createMemoryRepository(dbFile);
}
}
View
27 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/model/data/OnDiskHostedDatabase.java
@@ -0,0 +1,27 @@
+package edu.mit.simile.backstage.model.data;
+
+import java.io.File;
+
+import org.openrdf.repository.Repository;
+import org.openrdf.repository.RepositoryException;
+import org.openrdf.repository.sail.SailRepository;
+import org.openrdf.sail.Sail;
+import org.openrdf.sail.memory.MemoryStore;
+
+import edu.mit.simile.backstage.data.DataLink;
+import edu.mit.simile.backstage.util.DataLoadingUtilities;
+
+public class OnDiskHostedDatabase extends HostedDatabase {
+
+ public OnDiskHostedDatabase(DataLink datalink) {
+ super(datalink);
+ }
+
+ protected String getRepoType() {
+ return "disk";
+ }
+
+ synchronized protected DataLoadingUtilities.RepoSailTuple createRepo(File dbFile) {
+ return DataLoadingUtilities.createNativeRepository(dbFile);
+ }
+}
View
26 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/model/data/StandaloneDiskHostedDatabase.java
@@ -0,0 +1,26 @@
+package edu.mit.simile.backstage.model.data;
+
+import java.io.File;
+
+import org.openrdf.repository.Repository;
+import org.openrdf.repository.sail.SailRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import edu.mit.simile.backstage.util.DataLoadingUtilities;
+
+public class StandaloneDiskHostedDatabase extends Database {
+ protected static Logger _logger = LoggerFactory.getLogger("backstage.hosted-database");
+
+ public StandaloneDiskHostedDatabase(File databaseDir) {
+ DataLoadingUtilities.RepoSailTuple rs = DataLoadingUtilities.createNativeRepository(databaseDir);
+ _repository = rs.repository;
+ _sail = rs.sail;
+ rs = null;
+ }
+
+ @Override
+ public Repository getRepository() {
+ return _repository;
+ }
+}
View
36 modules/backstage/MOD-INF/src/edu/mit/simile/backstage/util/DataLoadingUtilities.java
@@ -46,28 +46,47 @@
protected static Logger _logger = LoggerFactory.getLogger("backstage.data-loading");
final static public String s_bnodePrefix = "urn:bnode:";
+
+ static public class RepoSailTuple {
+ public RepoSailTuple(Repository r, Sail s) {
+ repository = r;
+ sail = s;
+ }
+
+ public Repository repository;
+ public Sail sail;
+ }
- static public Repository createMemoryRepository() {
+ static public RepoSailTuple createMemoryRepository(File dir) {
try {
- Repository r = new SailRepository(new MemoryStore());
+ Repository r = null;
+ Sail s = null;
+ if (dir != null) {
+ s = new MemoryStore(dir);
+ r = new SailRepository(s);
+ } else {
+ s = new MemoryStore();
+ r = new SailRepository(new MemoryStore());
+ }
r.initialize();
- return r;
+
+ return new RepoSailTuple(r,s);
} catch (Exception e) {
- _logger.error("Exception caught while creating memory repository", e);
+ _logger.error("Exception caught while creating Sesame in-memory repository", e);
return null;
}
}
- static public Repository createNativeRepository(File dir) {
+ static public RepoSailTuple createNativeRepository(File dir) {
try {
Sail sail = new NativeStore();
sail.setDataDir(dir);
((NativeStore) sail).setTripleIndexes("spoc,posc,opsc");
Repository r = new SailRepository(sail);
r.initialize();
- return r;
+ return new RepoSailTuple(r,sail);
} catch (Exception e) {
- _logger.error("Exception caught while creating Sesame repository", e);
+ _logger.error("Exception caught while creating Sesame native repository", e);
return null;
}
}
@@ -251,7 +270,8 @@ static public void setRequestHeaders(URLConnection conn, String ua) {
}
static public void loadDataFromStream(InputStream stream, String sourceURL, String lang, Sail sail) throws Exception {
- Repository r = createMemoryRepository();
+ RepoSailTuple rs = createMemoryRepository(null);
+ Repository r = rs.repository;
lang = lang.toLowerCase();
if ("exhibit/json".equals(lang)) {
Please sign in to comment.
Something went wrong with that request. Please try again.