All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.unicore.persist.PersistenceFactory Maven / Gradle / Ivy

package eu.unicore.persist;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import eu.unicore.persist.impl.ClassScanner;
import eu.unicore.persist.impl.H2Persist;
import eu.unicore.util.configuration.ConfigurationException;


/**
 * create persistence handlers
 * 
 * @author schuller
 */
public class PersistenceFactory {

	private static final Logger logger = LogManager.getLogger("unicore.persistence,PersistenceFactory"); 
	
	private final PersistenceProperties config;
	
	private PersistenceFactory(PersistenceProperties config){
		this.config = config;
	}
	
	public PersistenceProperties getConfig() {
		return config;
	}

	public static synchronized PersistenceFactory get(PersistenceProperties config){
		PersistenceProperties realConfig = null;
		//check if we must merge properties from an additional config file
		File cFile=config==null? null : config.getFileValue(PersistenceProperties.FILE, false);
		if(cFile!=null){
			try(InputStream in = new FileInputStream(cFile)){
				Properties props = new Properties();
				props.load(in);
				props.putAll(config.getRawProperties());
				realConfig = new PersistenceProperties(props);
				logger.info("Reading persistence configuration from <"+cFile+">");
			}catch(IOException ex){
				throw new ConfigurationException("Can't read properties file <"+cFile+">",ex);
			}
		}else{
			realConfig = config;
		}
		return new PersistenceFactory(realConfig);
	}

	/**
	 * create a persistence handler for the given class
	 *
	 * @param  - the type of java class to be persisted
	 * @param daoClass - the Java class to be persisted
	 * @return {@link Persist} implementation
	 * @throws PersistenceException
	 */
	public  Persist getPersist(Class daoClass) throws PersistenceException{
		return getPersist(daoClass, null);
	}

	/**
	 * create a persistence handler for the given class storing data in the named table
	 *
	 * @param  - the type of java class to be persisted
	 * @param daoClass - the Java class to be persisted
	 * @param tableName - if null, it will be inferred from the daoClass
	 * @return {@link Persist} implementation
	 * @throws PersistenceException
	 */
	public  Persist getPersist(Class daoClass, String tableName) throws PersistenceException{
		try{
			return configurePersist(daoClass, getPersistClass(daoClass), tableName);
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}

	/**
	 * create an instance of the persistence class
	 * 
	 * @param daoClass
	 * @param implementation
	 * @param pd
	 * @throws Exception
	 */
	public  Persist configurePersist(Class daoClass, Class> implementation, String tableName)
			throws Exception {
		Persistp = implementation.getConstructor(Class.class, String.class).newInstance(daoClass, tableName);
		p.setConfigSource(config);
		p.init();
		return p;
	}

	@SuppressWarnings("unchecked")
	protected  Class> getPersistClass(Class daoClass)throws ClassNotFoundException{
		String tableName = ClassScanner.getTableName(daoClass);
		String clazz = config.getSubkeyValue(PersistenceProperties.DB_IMPL,tableName);
		if(clazz==null){
			clazz = H2Persist.class.getName();
		}
		if(clazz.startsWith("de.fzj.unicore.persist")) {
			String old = clazz;
			clazz = clazz.replace("de.fzj.unicore", "eu.unicore");
			logger.warn("DEPRECATED class name: {}, use {}", old, clazz);
		}
		return (Class>)Class.forName(clazz);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy