use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.
the class SesameYard method store.
protected final Iterable<Representation> store(Iterable<Representation> representations, boolean allowCreate) throws IllegalArgumentException, YardException {
RepositoryConnection con = null;
try {
con = repository.getConnection();
con.begin();
ArrayList<Representation> added = new ArrayList<Representation>();
for (Representation representation : representations) {
if (representation != null) {
// reassign
Representation stored = store(con, representation, allowCreate, false);
// to check if the store was successful
if (stored != null) {
added.add(stored);
} else {
// can only be the case if allowCreate==false (update was called)
log.warn(String.format("Unable to update Representation %s in Yard %s because it is not present!", representation.getId(), getId()));
}
}
// ignore null values in the parsed Iterable!
}
con.commit();
return added;
} catch (RepositoryException e) {
throw new YardException("Unable to remove parsed Representations", e);
} catch (IllegalArgumentException e) {
try {
// to avoid Exception logs in case store(..) throws an Exception
// in the case allowCreate and canNotCreateIsError do not allow
// the store operation
con.rollback();
} catch (RepositoryException ignore) {
}
throw e;
} finally {
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
}
}
}
}
use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.
the class SesameYard method remove.
@Override
public void remove(String id) throws YardException, IllegalArgumentException {
if (id == null) {
throw new IllegalArgumentException("The parsed Representation id MUST NOT be NULL!");
}
RepositoryConnection con = null;
try {
con = repository.getConnection();
con.begin();
remove(con, sesameFactory.createURI(id));
con.commit();
} catch (RepositoryException e) {
throw new YardException("Unable to remove for Representation " + id, e);
} finally {
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
}
}
}
}
use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.
the class SesameContextTest method initYard.
@BeforeClass
public static final void initYard() throws RepositoryException {
repo.initialize();
// create the graphs in Clerezza
// init the ClerezzaYards for the created Clerezza graphs
SesameYardConfig yard1config = new SesameYardConfig("context 1 yard");
yard1config.setName("Yard over context 1");
yard1config.setContextEnabled(true);
yard1config.setContexts(new String[] { CONTEXT1.stringValue() });
yard1 = new SesameYard(repo, yard1config);
SesameYardConfig yard2config = new SesameYardConfig("context 2 yard");
yard2config.setName("Yard over context 2");
yard2config.setContextEnabled(true);
yard2config.setContexts(new String[] { CONTEXT2.stringValue() });
yard2 = new SesameYard(repo, yard2config);
SesameYardConfig unionYardConfig = new SesameYardConfig("union yard");
unionYardConfig.setName("Union Yard");
unionYard = new SesameYard(repo, unionYardConfig);
yards = Arrays.asList(yard1, yard2, unionYard);
// add the test data (to the Repository to also test pre-existing data)
RepositoryConnection con = repo.getConnection();
con.begin();
URI entity1 = sesameFactory.createURI("http://www.test.org/entity1");
con.add(entity1, rdfType, skosConcept, CONTEXT1);
con.add(entity1, skosPrefLabel, sesameFactory.createLiteral("test context one", EN), CONTEXT1);
con.add(entity1, skosPrefLabel, sesameFactory.createLiteral("Test Context Eins", DE), CONTEXT1);
expectedEntities.put(entity1, Arrays.asList(yard1, unionYard));
URI entity2 = sesameFactory.createURI("http://www.test.org/entity2");
con.add(entity2, rdfType, skosConcept, CONTEXT2);
con.add(entity2, skosPrefLabel, sesameFactory.createLiteral("test context two", EN), CONTEXT2);
con.add(entity2, skosPrefLabel, sesameFactory.createLiteral("Test Context Zwei", DE), CONTEXT2);
expectedEntities.put(entity2, Arrays.asList(yard2, unionYard));
con.commit();
con.close();
}
use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.
the class RdfResourceImporter method importResource.
@Override
public ResourceState importResource(InputStream is, String resourceName) throws IOException {
log.info("> importing {}:", resourceName);
RDFFormat rdfFormat = Rio.getParserFormatForFileName(resourceName);
if (rdfFormat == null) {
log.info(" ... unable to detect RDF format for {}", resourceName);
log.info(" ... resource '{}' will not be imported", resourceName);
return ResourceState.IGNORED;
} else {
RepositoryConnection con = null;
try {
con = repository.getConnection();
con.begin();
con.add(new InputStreamReader(is, UTF8), baseUri, rdfFormat, contexts);
con.commit();
return ResourceState.LOADED;
} catch (RDFParseException e) {
log.error(" ... unable to parser RDF file " + resourceName + " (format: " + rdfFormat + ")", e);
return ResourceState.ERROR;
} catch (RepositoryException e) {
throw new IllegalArgumentException("Repository Exception while " + resourceName + "!", e);
} finally {
if (con != null) {
try {
con.close();
} catch (RepositoryException e1) {
/* ignore */
}
}
}
}
}
use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.
the class RdfIndexingSource method loadRepositoryConfig.
/**
* @param repoConfigFile
* @return
*/
private RepositoryConfig loadRepositoryConfig(File repoConfigFile) {
Repository configRepo = new SailRepository(new MemoryStore());
RepositoryConnection con = null;
try {
configRepo.initialize();
con = configRepo.getConnection();
// We need to load the configuration into a context
org.openrdf.model.URI configContext = con.getValueFactory().createURI("urn:stanbol.entityhub:indexing.source.sesame:config.context");
RDFFormat format = Rio.getParserFormatForFileName(repoConfigFile.getName());
try {
con.add(new InputStreamReader(new FileInputStream(repoConfigFile), Charset.forName("UTF-8")), baseUri, format, configContext);
} catch (RDFParseException e) {
throw new IllegalArgumentException("Unable to parsed '" + repoConfigFile + "' using RDF format '" + format + "'!", e);
} catch (IOException e) {
throw new IllegalArgumentException("Unable to access '" + repoConfigFile + "'!", e);
}
con.commit();
} catch (RepositoryException e) {
throw new IllegalStateException("Unable to load '" + repoConfigFile + "' to inmemory Sail!", e);
} finally {
if (con != null) {
try {
con.close();
} catch (RepositoryException e) {
/* ignore */
}
}
}
Set<String> repoNames;
RepositoryConfig repoConfig;
try {
repoNames = RepositoryConfigUtil.getRepositoryIDs(configRepo);
if (repoNames.size() == 1) {
repoConfig = RepositoryConfigUtil.getRepositoryConfig(configRepo, repoNames.iterator().next());
repoConfig.validate();
} else if (repoNames.size() > 1) {
throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' MUST only contain a single repository configuration!");
} else {
throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' DOES NOT contain a repository configuration!");
}
} catch (RepositoryException e) {
throw new IllegalStateException("Unable to read RepositoryConfiguration form the " + "in-memory Sail!", e);
} catch (RepositoryConfigException e) {
throw new IllegalArgumentException("Repository Configuration in '" + repoConfigFile + "is not valid!", e);
} finally {
try {
configRepo.shutDown();
} catch (RepositoryException e) {
/* ignore */
}
}
if (repoConfig.getRepositoryImplConfig() == null) {
throw new IllegalArgumentException("Missing RepositoryImpl config for " + "config " + repoConfig.getID() + " of file " + repoConfigFile + "!");
}
return repoConfig;
}
Aggregations