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

com.jk.data.dataaccess.JKDataAccessFactory Maven / Gradle / Ivy

/*
 * Copyright 2002-2022 Dr. Jalal Kiswani. 
 * Email: [email protected]
 * Check out https://smart-api.com for more details
 * 
 * All the opensource projects of Dr. Jalal Kiswani are free for personal and academic use only, 
 * for commercial usage and support, please contact the author.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jk.data.dataaccess;

import com.jk.core.config.JKConfig;
import com.jk.core.config.JKConfigEvent;
import com.jk.core.config.JKConfigListener;
import com.jk.core.logging.JKLogger;
import com.jk.core.logging.JKLoggerFactory;
import com.jk.core.util.JK;
import com.jk.data.dataaccess.core.JKDataAccessImpl;
import com.jk.data.dataaccess.core.JKDataAccessService;
import com.jk.data.dataaccess.nosql.JKNoSqlDataAccess;
import com.jk.data.dataaccess.orm.JKObjectDataAccess;
import com.jk.data.dataaccess.orm.JKObjectDataAccessImpl;
import com.jk.data.datasource.JKDataSource;
import com.jk.data.datasource.JKDataSourceFactory;
import com.jk.data.datasource.JKNoSqlDataSource;
import com.jk.data.vendors.h2.H2DataAccess;
import com.jk.data.vendors.mongo.JKMongoDataAccess;
import com.jk.data.vendors.mysql.MysqlDataAccess;
import com.jk.data.vendors.oracle.JKOracleDataAccess;

// TODO: Auto-generated Javadoc
/**
 * A factory for creating JKDataSource objects.
 */
public class JKDataAccessFactory {

	/** The logger. */
	static JKLogger logger = JKLoggerFactory.getLogger(JKDataAccessFactory.class);

	/** The default resource manager. */
	// TODO: get this from the JKDataSourceFactory
	private static JKDataSource defaultDataSource;

	/** The default no sql data source. */
	private static JKNoSqlDataSource defaultNoSqlDataSource;
	static {
		JKConfig.addListner(new JKConfigListener() {
			
			@Override
			public void reloadConfig(JKConfigEvent event) {
				close();				
			}
			
			@Override
			public void closeConfig(JKConfigEvent event) {
				close();
			}
		});
	}

	/**
	 * Gets the default data source.
	 *
	 * @return the default data source
	 */
	/////////////////////////////////////////////////////////////////////////
	public synchronized static JKDataSource getDefaultDataSource() {
		if (defaultDataSource == null) {
			init();
		}
		return defaultDataSource;
	}

	/**
	 * Sets the default data source.
	 *
	 * @param impl the new default data source
	 */
	/////////////////////////////////////////////////////////////////////////
	public static void setDefaultDataSource(final JKDataSource impl) {
		if (defaultDataSource != null) {
			logger.debug("close old defaultDataSource");
			try {
				defaultDataSource.close();
			} catch (Exception e) {
			}
		}
		defaultDataSource = impl;
	}

	/**
	 * Inits the.
	 */
	/////////////////////////////////////////////////////////////////////////
	public static synchronized void init() {
		logger.info("Init DataAccessFactory");
		try {
//			reset();
			// Properties properties = JKConfig.get().toProperties();
//			properties.putAll(loadEnvironmentVariables());
//			System.getProperties().putAll(properties);
			setDefaultDataSource(JKDataSourceFactory.getDefaultDataSource());
		} catch (Exception e) {
			JK.throww(e);
		}
	}

	/**
	 * Load environment variables.
	 *
	 * @return the properties
	 */

	/**
	 * Reset.
	 */
	/////////////////////////////////////////////////////////////////////////
	protected static synchronized void close() {
		if (defaultDataSource != null) {
			logger.debug("close old defaultDataSource");
			try {
				defaultDataSource.close();
			} catch (Exception e) {
				JK.fixMe("check the appropriate behaviour for this");
			}
			defaultDataSource = null;
		}
		if (defaultNoSqlDataSource != null) {
			logger.debug("close old defaultNoSqlDataSource");
			try {
				defaultNoSqlDataSource.close();
			} catch (Exception e) {
				JK.fixMe("check the appropriate behaviour for this");
			}
			defaultNoSqlDataSource = null;
		}
	}

	/**
	 * Gets the data access service.
	 *
	 * @return the data access service
	 */
	/////////////////////////////////////////////////////////////////////////
	public static JKDataAccessService getDataAccessService() {
		return getDataAccessService(getDefaultDataSource());
	}

	/**
	 * Gets the object data access service.
	 *
	 * @param databasePrefix the database prefix
	 * @return the object data access service
	 */
	public static JKObjectDataAccess getObjectDataAccessService(String databasePrefix) {
		return getObjectDataAccessService(JKDataSourceFactory.getDataSource(databasePrefix));
	}

	/**
	 * Gets the object data access service.
	 *
	 * @param dataSource the data source
	 * @return the object data access service
	 */
	public static JKObjectDataAccess getObjectDataAccessService(JKDataSource dataSource) {
		return new JKObjectDataAccessImpl(dataSource);
	}

	/**
	 * Gets the object data access service.
	 *
	 * @return the object data access service
	 */
	/////////////////////////////////////////////////////////////////////////
	public static JKObjectDataAccess getObjectDataAccessService() {
		return getObjectDataAccessService(getDefaultDataSource());
	}

	/**
	 * Gets the data access service.
	 *
	 * @param databasePrefix the database prefix
	 * @return the data access service
	 */
	public static JKDataAccessService getDataAccessService(String databasePrefix) {
		return getDataAccessService(JKDataSourceFactory.getDataSource(databasePrefix));
	}

	/**
	 * Gets the data access service.
	 *
	 * @param dataSource the data source
	 * @return the data access service
	 */
	/////////////////////////////////////////////////////////////////////////
	public static JKDataAccessService getDataAccessService(JKDataSource dataSource) {
		if (dataSource == null) {
			dataSource = getDefaultDataSource();
		}
		switch (dataSource.getDatabaseType()) {
		case ORACLE:
			return new JKOracleDataAccess(dataSource);
		case MYSQL:
			return new MysqlDataAccess(dataSource);
		case H2:
			return new H2DataAccess(dataSource);
		default:
			return new JKDataAccessImpl(dataSource);
		}
	}

//	/**
//	 * Gets the object data access.
//	 *
//	 * @return the object data access
//	 */
//	/////////////////////////////////////////////////////////////////////////
//	public static JKObjectDataAccess getObjectDataAccess() {
//		return new JKObjectDataAccessImpl();
//	}

	/**
	 * Checks if is entity available.
	 *
	 * @param clas the clas
	 * @return true, if is entity available
	 */
	/////////////////////////////////////////////////////////////////////////
	public static boolean isEntityAvailable(Class clas) {
		return getDefaultDataSource().isEntityAvailable(clas);
	}

	/**
	 * Gets the no sql data source.
	 *
	 * @return the no sql data source
	 */
	/////////////////////////////////////////////////////////////////////////
	public static synchronized JKNoSqlDataSource getNoSqlDataSource() {
		if (defaultNoSqlDataSource == null) {
			defaultNoSqlDataSource = new JKNoSqlDataSource();
			Runtime.getRuntime().addShutdownHook(new Thread() {
				@Override
				public void run() {
					defaultNoSqlDataSource.close();
				}
			});
		}
		return defaultNoSqlDataSource;
	}

	/**
	 * Gets the no sql data access.
	 *
	 * @return the no sql data access
	 */
	/////////////////////////////////////////////////////////////////////////
	public static JKNoSqlDataAccess getNoSqlDataAccess() {
		return new JKMongoDataAccess();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy