public class RDFBeanManager extends Object
Provides basic CRUD and dynamic proxy management functions for persisting RDFBean data objects using a RDF model stored in RDF4J repository.
RDFBeanManager is instantiated with opened RDF4J RepositoryConnection to an existing Repository object. The client code is responsible for opening and closing the repository connection, as well as initializing and shutting down the repository.
Example of usage:
Repository repo;
try {
File dataDir = new File("C:\\temp\\myRepository\\");
repo = new SailRepository(new NativeStore(dataDir));
repo.initialize();
try (RepositoryConnection con = repo.getConnection()) {
RDFBeanManager rdfBeanManager = new RDFBeanManager(con);
//... do something ...
}
finally {
repo.shutDown();
}
This class, as well as RDF4J RepositoryConnection is not thread-safe. It is recommended that each thread obtain it’s own RepositoryConnection from a shared Repository object and create a separate RDFBeanManager instance on it.
By default, RDFBeanManager methods add or delete individual RDF statements in a transaction-safe manner. A method starts new transaction on RepositoryConnection before any update and commit it automatically after updates are completed. If the method throws an exception, the entire transaction is rolled back that guarantees that all updates the method made to this point will not take effect.
The behaviour is different if the method is invoked when RepositoryConnection already has an active transaction. In this case, the method does not start new transaction, but re-uses existing one by adding new operations to it. The updates will not take effect until the transaction is committed. If an exception is thrown by the method, the transaction status is not changed (the client code is free to roll it back on it’s own).
With this explicit transaction management, one can group multiple RDFBeanManager operations and treat them as a single update, as shown in the below example:
RepositoryConnection con = rdfBeanManager.getRepositoryConnection();
// start a transaction
con.begin();
try {
// Add few RDFBean objects
rdfBeanManager.add(object1);
rdfBeanManager.add(object2);
rdfBeanManager.add(object3);
// Commit the above adds at once
con.commit();
}
catch (Throwable t) {
// Something went wrong, we roll the transaction back
con.rollback();
throw t;
}
Modifier and Type | Field and Description |
---|---|
static org.eclipse.rdf4j.model.IRI |
BINDINGCLASS_PROPERTY |
static org.eclipse.rdf4j.model.IRI |
BINDINGIFACE_PROPERTY |
static org.eclipse.rdf4j.model.ValueFactory |
valueFactory |
Constructor and Description |
---|
RDFBeanManager(org.eclipse.rdf4j.repository.RepositoryConnection conn)
Creates new RDFBeanManager instance upon the given RDF4J RepositoryConnection.
|
Modifier and Type | Method and Description |
---|---|
org.eclipse.rdf4j.model.Resource |
add(Object o)
Marshalls the state of a Java object to an RDF resource (a set of triple statements in the underlying RDF model).
|
void |
addProxyListener(ProxyListener l) |
<T> T |
create(org.eclipse.rdf4j.model.Resource r,
Class<T> iface)
Creates new dynamic proxy object implementing the specified Java interface.
|
<T> T |
create(String id,
Class<T> iface)
Creates new dynamic proxy object implementing the specified Java interface.
|
<T> Collection<T> |
createAll(Class<T> iface)
Constructs all dynamic proxy objects implementing the specified Java interface from their representations in the underlying RDF model.
|
boolean |
delete(org.eclipse.rdf4j.model.Resource uri)
Deletes the RDF resource from the underlying model.
|
void |
delete(String stringId,
Class rdfBeanClass)
Deletes an RDF resource representing an object that matches the specified RDFBean identifier and Java class from the underlying model.
|
Object |
get(org.eclipse.rdf4j.model.Resource r)
Unmarshalls an RDF resource by creating an object of auto-detected Java class.
|
<T> T |
get(org.eclipse.rdf4j.model.Resource r,
Class<T> rdfBeanClass)
Unmarshalls an RDF resource by creating an object of the specified Java class.
|
<T> T |
get(String stringId,
Class<T> rdfBeanClass)
Unmarshalls an RDF resource matching the specified RDFBean identifier by creating an object of the specified Java class.
|
<T> org.eclipse.rdf4j.common.iteration.CloseableIteration<T,Exception> |
getAll(Class<T> rdfBeanClass)
Returns an iterator over all objects of the specified Java class stored in the RDF model.
|
protected Class<?> |
getBindingClassForType(org.eclipse.rdf4j.model.IRI rdfType) |
ClassLoader |
getClassLoader()
Returns the current ClassLoader for loading RDFBean classes.
|
DatatypeMapper |
getDatatypeMapper()
Returns a current DatatypeMapper implementation.
|
List<ProxyListener> |
getProxyListeners() |
org.eclipse.rdf4j.repository.RepositoryConnection |
getRepositoryConnection() |
org.eclipse.rdf4j.model.Resource |
getResource(String stringId,
Class rdfBeanClass)
Returns an RDF resource representing an object that matches the specified RDFBean identifier and Java class.
|
boolean |
isResourceExist(org.eclipse.rdf4j.model.Resource r)
Checks whether an RDF resource exists in the underlying model.
|
boolean |
isResourceExist(org.eclipse.rdf4j.model.Resource r,
Class rdfBeanClass)
Checks whether an RDF resource exists in the underlying model and represents an object of the specified Java class.
|
void |
removeProxyListener(ProxyListener l) |
void |
setClassLoader(ClassLoader classLoader)
Sets a custom ClassLoader instance for loading RDFBean classes.
|
void |
setDatatypeMapper(DatatypeMapper datatypeMapper)
Sets a DatatypeMapper implementation.
|
org.eclipse.rdf4j.model.Resource |
update(Object o)
Marshalls the state of a Java RDFBean object to an RDF resource (a set of triple statements in the underlying RDF model).
|
public static final org.eclipse.rdf4j.model.ValueFactory valueFactory
public static final org.eclipse.rdf4j.model.IRI BINDINGCLASS_PROPERTY
public static final org.eclipse.rdf4j.model.IRI BINDINGIFACE_PROPERTY
public RDFBeanManager(org.eclipse.rdf4j.repository.RepositoryConnection conn)
Creates new RDFBeanManager instance upon the given RDF4J RepositoryConnection.
conn
- public org.eclipse.rdf4j.repository.RepositoryConnection getRepositoryConnection()
public org.eclipse.rdf4j.model.Resource add(Object o) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Marshalls the state of a Java object to an RDF resource (a set of triple statements in the underlying RDF model).
The class of the object must conform to the RDFBean specification.
If the object has a not-null property, annotated with RDFSubject
, the method returns IRI of newly created RDF resource. Otherwise (the RDFBean is anonymous), a BNode object is returned.
If an RDF representation of the given unanonymous object already exists in the model, the method immediately returns IRI without any model modifications.
Upon marshalling a first instance of every Java class, the method adds special statement to the model containing information about binding of specific RDF type to that class. This information is needed to determine which class to use for instatiation of the unmarshalled objects later.
o
- RDFBean to addRDFBeanException
- If class of the object is not a valid RDFBean classorg.eclipse.rdf4j.repository.RepositoryException
If there is an active transaction started on RepositoryConnection, all
individual updates performed by this method are added to that
transaction. This means that the updates are not effective until the
transaction is committed. Otherwise, this method will start new
transaction under the hood to commit individual triple updates at once.
,
update(Object)
public org.eclipse.rdf4j.model.Resource update(Object o) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Marshalls the state of a Java RDFBean object to an RDF resource (a set of triple statements in the underlying RDF model). If existing RDF representation of this object is found in the model, it will be overwritten.
The class of the object must conform the RDFBean specification.
If no RDF representation for the given object is found, or if the object is an anonymous RDFBean, the method works exactly like add()
.
If there is an active transaction started on RepositoryConnection, all individual updates performed by this method are added to that transaction. That means that the updates are not effective until the transaction is committed. Otherwise, this method will start new transaction under the hood to commit individual triple updates at once.
o
- RDFBean to updateRDFBeanException
- If class of the object is not a valid RDFBean classorg.eclipse.rdf4j.repository.RepositoryException
add(Object)
public <T> T get(org.eclipse.rdf4j.model.Resource r, Class<T> rdfBeanClass) throws RDFBeanException, org.eclipse.rdf4j.RDF4JException
Unmarshalls an RDF resource by creating an object of the specified Java class.
The class must conform to the RDFBean specification.
r
- Resource IRI (or BNode for anonymous RDFBean).rdfBeanClass
- Java class of RDFBeanRDFBeanException
- If the class is not a valid RDFBean class or an instance of this class cannot be createdorg.eclipse.rdf4j.RDF4JException
get(Resource)
,
get(String,Class)
,
getAll(Class)
public Object get(org.eclipse.rdf4j.model.Resource r) throws RDFBeanException, org.eclipse.rdf4j.RDF4JException
Unmarshalls an RDF resource by creating an object of auto-detected Java class.
The method tries to determine a Java class using binding class information added to the model at marshalling. If the binding class information is not found, RDFBeanException is thrown.
r
- Resource IRI (or BNode for anonymous RDFBean).RDFBeanException
- If the binding class cannot be detected, or it is not a valid RDFBean class or an instance of this class cannot be createdorg.eclipse.rdf4j.RDF4JException
get(Resource,Class)
,
get(String,Class)
,
getAll(Class)
public <T> T get(String stringId, Class<T> rdfBeanClass) throws RDFBeanException, org.eclipse.rdf4j.RDF4JException
Unmarshalls an RDF resource matching the specified RDFBean identifier by creating an object of the specified Java class.
The class must conform to the RDFBean specification.
If a namespace prefix is defined in RDFSubject
declaration for this RDFBean class, the provided identifier value is interpreted as a local part of fully qualified RDFBean name (RDF resource IRI). Otherwise, the fully qualified name is expected.
stringId
- RDFBean ID valuerdfBeanClass
- Java class of RDFBeanRDFBeanException
- If the class is not a valid RDFBean class or an instance of this class cannot be createdorg.eclipse.rdf4j.RDF4JException
get(Resource)
,
get(Resource,Class)
,
getAll(Class)
public <T> org.eclipse.rdf4j.common.iteration.CloseableIteration<T,Exception> getAll(Class<T> rdfBeanClass) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Returns an iterator over all objects of the specified Java class stored in the RDF model.
The class must conform to the RDFBean specification.
The returned Iterator performs “lazy” unmarshalling of objects (on every next()
call) with no specific order. When iterating is done, the caller must invoke the close()
method to release the resources of underlying RDF model.
rdfBeanClass
- Java class of objects to iterateRDFBeanException
- If the class is not a valid RDFBean classorg.eclipse.rdf4j.repository.RepositoryException
public boolean isResourceExist(org.eclipse.rdf4j.model.Resource r) throws org.eclipse.rdf4j.repository.RepositoryException
Checks whether an RDF resource exists in the underlying model.
r
- Resource IRI or BNodeorg.eclipse.rdf4j.repository.RepositoryException
public boolean isResourceExist(org.eclipse.rdf4j.model.Resource r, Class rdfBeanClass) throws RDFBeanValidationException, org.eclipse.rdf4j.repository.RepositoryException
Checks whether an RDF resource exists in the underlying model and represents an object of the specified Java class.
The class must conform to the RDFBean specification.
r
- Resource IRI or BNodeRDFBean
annotation of the given class.RDFBeanValidationException
- If the class is not a valid RDFBean classorg.eclipse.rdf4j.repository.RepositoryException
public org.eclipse.rdf4j.model.Resource getResource(String stringId, Class rdfBeanClass) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Returns an RDF resource representing an object that matches the specified RDFBean identifier and Java class.
The class must conform to the RDFBean specification.
If a namespace prefix is defined in RDFSubject
declaration for this RDFBean class, the provided identifier value is interpreted as a local part of fully qualified RDFBean name (RDF resource IRI). Otherwise, the fully qualified name is expected.
stringId
- RDFBean ID valuerdfBeanClass
- Java class of RDFBeanRDFBeanException
- If the class is not a valid RDFBean classorg.eclipse.rdf4j.repository.RepositoryException
public boolean delete(org.eclipse.rdf4j.model.Resource uri) throws org.eclipse.rdf4j.repository.RepositoryException
Deletes the RDF resource from the underlying model.
It results in deletion of all statements where the given resource is either a subject or an object.
If there is an active transaction started on RepositoryConnection, all individual updates performed by this method are added to that transaction. That means that the updates are not effective until the transaction is committed. Otherwise, this method will start new transaction under the hood to commit individual triple updates at once.
uri
- Resource IRIorg.eclipse.rdf4j.repository.RepositoryException
delete(String,Class)
public void delete(String stringId, Class rdfBeanClass) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Deletes an RDF resource representing an object that matches the specified RDFBean identifier and Java class from the underlying model.
The class must conform to the RDFBean specification.
If the RDF resource is found, it results in deletion of all statements where it is either a subject or an object.
If a namespace prefix is defined in RDFSubject
declaration for this RDFBean class, the provided identifier value is interpreted as a local part of fully qualified RDFBean name (RDF resource IRI). Otherwise, the fully qualified name is expected.
If there is an active transaction started on RepositoryConnection, all individual updates performed by this method are added to that transaction. That means that the updates are not effective until the transaction is committed. Otherwise, this method will start new transaction under the hood to commit individual triple updates at once.
stringId
- RDFBean ID valuerdfBeanClass
- Java class of RDFBeanRDFBeanException
- If the class is not a valid RDFBean classorg.eclipse.rdf4j.repository.RepositoryException
delete(Resource)
protected Class<?> getBindingClassForType(org.eclipse.rdf4j.model.IRI rdfType) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
RDFBeanException
org.eclipse.rdf4j.repository.RepositoryException
public <T> T create(org.eclipse.rdf4j.model.Resource r, Class<T> iface) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Creates new dynamic proxy object implementing the specified Java interface. The specified RDF resource will represent the object in the underlying RDF model.
The interface must conform to the RDFBean specification.
T
- r
- Resource IRIiface
- RDFBean-compliant Java interfaceRDFBeanException
- If iface is not a valid RDFBean interfaceorg.eclipse.rdf4j.repository.RepositoryException
create(String,Class)
public <T> T create(String id, Class<T> iface) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Creates new dynamic proxy object implementing the specified Java interface. An RDF resource matching the specified RDFBean identifier will represent the object in the underlying RDF model.
The interface must conform to the RDFBean specification.
If a namespace prefix is defined in RDFSubject
declaration for this RDFBean interface, the provided identifier value is interpreted as a local part of fully qualified RDFBean name (RDF resource IRI). Otherwise, the fully qualified name is expected.
id
- RDFBean ID valueiface
- RDFBean-compliant Java interfaceRDFBeanException
- if iface is not valid RDFBean interface or the RDFBean identifier cannot be resolved to a resourceorg.eclipse.rdf4j.repository.RepositoryException
create(Resource,Class)
public <T> Collection<T> createAll(Class<T> iface) throws RDFBeanException, org.eclipse.rdf4j.repository.RepositoryException
Constructs all dynamic proxy objects implementing the specified Java interface from their representations in the underlying RDF model.
The interface must conform to the RDFBean specification.
iface
- RDFBean-compliant Java interfaceRDFBeanException
- If iface is not a valid RDFBean interfaceorg.eclipse.rdf4j.repository.RepositoryException
public ClassLoader getClassLoader()
Returns the current ClassLoader for loading RDFBean classes.
By default, the classes are loaded by the ClassLoader of this RDFBeanManager.
setClassLoader(ClassLoader)
public void setClassLoader(ClassLoader classLoader)
Sets a custom ClassLoader instance for loading RDFBean classes.
By default, the classes are loaded by the ClassLoader of this RDFBeanManager.
classLoader
- the ClassLoader instance to setgetClassLoader()
public DatatypeMapper getDatatypeMapper()
Returns a current DatatypeMapper implementation.
setDatatypeMapper(DatatypeMapper)
public void setDatatypeMapper(DatatypeMapper datatypeMapper)
Sets a DatatypeMapper implementation.
datatypeMapper
- the datatypeMapper to setgetDatatypeMapper()
public void addProxyListener(ProxyListener l)
public void removeProxyListener(ProxyListener l)
public List<ProxyListener> getProxyListeners()
Copyright © 2017. All rights reserved.