org.openprovenance.prov.sql.PersistenceUtility Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov-sql Show documentation
Show all versions of prov-sql Show documentation
Java Objects representing the PROV model, XML serialiser/deserialiser for them, and ORM mapping generated by HyperJaxb.
The newest version!
package org.openprovenance.prov.sql;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.openprovenance.prov.model.QualifiedName;
import org.openprovenance.prov.model.ProvUtilities;
public class PersistenceUtility {
static Logger logger = LogManager.getLogger(PersistenceUtility.class);
static private EntityManagerFactory emf;
static private EntityManager entityManager;
static Hashtable table;
public PersistenceUtility() {
}
public void setUp() {
if (emf==null) emf=createEntityManagerFactory();
if (entityManager==null) entityManager=createEntityManager();
if (table==null) table=new Hashtable();
}
protected EntityManagerFactory createEntityManagerFactory() {
try {
final Enumeration resources = getClass().getClassLoader().getResources("META-INF/persistence.xml");
while (resources.hasMoreElements()) {
final URL resource = resources.nextElement();
//logger.debug("Detected [" + resource + "].");
}
}
catch (IOException ignored) {
}
final Properties properties = getEntityManagerFactoryProperties();
if (properties == null) {
return javax.persistence.Persistence.createEntityManagerFactory(getPersistenceUnitName());
}
else {
return javax.persistence.Persistence.createEntityManagerFactory(getPersistenceUnitName(), properties);
}
}
public Properties getEntityManagerFactoryProperties() {
try {
final Enumeration resources = getClass().getClassLoader().getResources(getEntityManagerFactoryPropertiesResourceName());
if (!resources.hasMoreElements()) {
logger.debug("Entity manager factory properties are not set.");
return null;
}
else {
//logger.debug("Loading entity manager factory properties.");
final Properties properties = new Properties();
while (resources.hasMoreElements()) {
final URL resource = resources.nextElement();
logger.debug("Loading entity manager factory properties from [" + resource + "].");
if (resource == null) {
return null;
}
else {
InputStream is = null;
try {
is = resource.openStream();
properties.load(is);
return properties;
}
catch (IOException ex) {
return null;
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException ex) {
// Ignore
}
}
}
}
}
return properties;
}
}
catch (IOException ex) {
return null;
}
}
public String getEntityManagerFactoryPropertiesResourceName() {
return DB_PROPERTIES_FILE;
}
String getPersistenceUnitName() {
return "org.openprovenance.prov.sql";
}
public EntityManager createEntityManager () {
EntityManager em=emf.createEntityManager();
return em;
}
/*
static Map makeProperties(String filename) {
Map properties=new HashMap();
properties.put("hibernate.ejb.cfgfile",filename);
properties.put("javax.persistence.provider","org.hibernate.ejb.HibernatePersistence");
logger.debug(properties);
return properties;
}
*/
public EntityTransaction getTransaction() {
return entityManager.getTransaction();
}
public EntityTransaction beginTransaction() {
EntityTransaction tx=entityManager.getTransaction();
tx.begin();
return tx;
}
public EntityTransaction commitTransaction() {
EntityTransaction tx=entityManager.getTransaction();
tx.commit();
return tx;
}
public void close() {
if (entityManager!=null) entityManager.close();
}
public Document persist(Document doc) {
try {
beginTransaction();
Dagify dagifier=new Dagify(entityManager,table);
ProvUtilities u=new ProvUtilities();
u.forAllStatementOrBundle(doc.getStatementOrBundle(), dagifier);
entityManager.persist(doc);
return doc;
} catch (RuntimeException re) {
re.printStackTrace();
return null; // FIXME: why not re-throw exception
}
finally {
commitTransaction();
}
}
public Document persistInTransaction(Document doc) {
try {
Dagify dagifier=new Dagify(entityManager,table);
ProvUtilities u=new ProvUtilities();
u.forAllStatementOrBundle(doc.getStatementOrBundle(), dagifier);
entityManager.persist(doc);
return doc;
} catch (RuntimeException re) {
re.printStackTrace();
return null; // FIXME: why not re-throw exception
}
}
public Dagify getDagifyer () {
return new Dagify(entityManager,table);
}
public IncrementalDocument persist(IncrementalDocument doc) {
try {
beginTransaction();
entityManager.persist(doc);
return doc;
} catch (RuntimeException re) {
re.printStackTrace();
throw re;
}
finally {
commitTransaction();
}
}
public IncrementalDocument persistInTransaction(IncrementalDocument doc) {
entityManager.persist(doc);
return doc;
}
public NamedDocument persistInTransaction(NamedDocument doc) {
entityManager.persist(doc);
return doc;
}
public PutableDocument persistInTransaction(PutableDocument doc) {
entityManager.persist(doc);
return doc;
}
public DOC find(Class cl, Long id) {
beginTransaction();
DOC doc=entityManager.find(cl, id);
commitTransaction();
return doc;
}
public DOC findInTransaction(Class cl, Long id) {
DOC doc=entityManager.find(cl, id);
return doc;
}
public Query createQuery(String q) {
return entityManager.createQuery(q);
}
public Query createNamedQuery(String q) {
return entityManager.createNamedQuery(q);
}
private static Properties getPropertiesFromClasspath(String propFileName) {
Properties props = new Properties();
InputStream inputStream = PersistenceUtility.class.getClassLoader()
.getResourceAsStream(propFileName);
if (inputStream == null) {
// throw new FileNotFoundException("property file '" + propFileName
// + "' not found in the classpath");
return null;
}
try {
props.load(inputStream);
} catch (IOException ee) {
return null;
}
return props;
}
final static private String configFilename="db.properties";
public final static String DB_PROPERTIES_FILE = getPropertiesFromClasspath(configFilename).getProperty("persistence.properties");
}