Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.netdesign.dao.cti.callback.ioc;
- import static com.google.inject.name.Names.named;
- import java.util.Properties;
- import javax.persistence.NoResultException;
- import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
- import org.mybatis.guice.MyBatisModule;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.slurry.cache4guice.cache.util.Cache4GuiceHelper;
- import com.google.inject.Inject;
- import com.google.inject.Key;
- import com.google.inject.PrivateModule;
- import com.google.inject.name.Names;
- import com.netdesign.dao.cti.callback.CTICallBackWallBoardDataProvider;
- import com.netdesign.dao.cti.callback.CallBackMapper;
- import com.netdesign.wallboard.core.dao.CoreDaoService;
- import com.netdesign.wallboard.core.datastorage.Parameter;
- import com.netdesign.wallboard.core.interfaces.WallBoardDataProvider;
- import com.netdesign.wallboard.core.interfaces.WallBoardDriverModule;
- public class CTICallBackModule extends PrivateModule implements
- WallBoardDriverModule {
- public static final String CACHE = "Cache timeout in seconds";
- public CTICallBackModule() {
- }
- private Cache4GuiceHelper cache4GuiceHelper;
- public Cache4GuiceHelper getCache4GuiceHelper() {
- return cache4GuiceHelper;
- }
- @Inject
- public void setCache4GuiceHelper(Cache4GuiceHelper cache4GuiceHelper) {
- this.cache4GuiceHelper = cache4GuiceHelper;
- }
- private Properties getConnectionProperties() {
- final Properties myBatisProperties = new Properties();
- CTICallBackWallBoardDataProvider ctiCallBackWallBoardDataProvider = new CTICallBackWallBoardDataProvider();
- Parameter parameterPassword = getCoreDaoService().getParameter(
- ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
- ctiCallBackWallBoardDataProvider.PASSWORD);
- Parameter parameterUsername = getCoreDaoService().getParameter(
- ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
- ctiCallBackWallBoardDataProvider.USERNAME);
- Parameter parameterServerIP = getCoreDaoService().getParameter(
- ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
- ctiCallBackWallBoardDataProvider.SERVER_IP);
- myBatisProperties.setProperty("mybatis.environment.id", "production");
- myBatisProperties.setProperty("JDBC.username",
- parameterUsername.getValue());
- myBatisProperties.setProperty("JDBC.password",
- parameterPassword.getValue());
- myBatisProperties.setProperty("JDBC.autoCommit", "true");
- setupURLAndServer(myBatisProperties, parameterPassword,
- parameterUsername, parameterServerIP);
- setupCache(ctiCallBackWallBoardDataProvider);
- return myBatisProperties;
- }
- private void setupCache(
- CTICallBackWallBoardDataProvider ctiCallBackWallBoardDataProvider) {
- ctiCallBackWallBoardDataProvider
- .setCache4GuiceHelper(getCache4GuiceHelper());
- try {
- Parameter parameterCache = getCoreDaoService().getParameter(
- ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
- CACHE);
- ctiCallBackWallBoardDataProvider.setupCache(Integer
- .parseInt(parameterCache.getValue()));
- } catch (NoResultException e) {
- logger.debug("Could not find cache settings, running with the driver the first time?");
- }
- }
- private CoreDaoService coreDaoService;
- public CoreDaoService getCoreDaoService() {
- return coreDaoService;
- }
- private Logger logger = LoggerFactory.getLogger(CTICallBackModule.class);
- @Override
- public void configure() {
- logger.info("Starting :" + CTICallBackWallBoardDataProvider.class);
- MyBatisModule myBatisModule = new MyBatisModule() {
- @Override
- protected void initialize() {
- Names.bindProperties(this.binder(), getConnectionProperties());
- addMapperClass(CallBackMapper.class);
- environmentId("production");
- bindDataSourceProviderType(CTICallBackDataSourceProvider.class);
- bindTransactionFactoryType(JdbcTransactionFactory.class);
- }
- };
- install(myBatisModule);
- CTICallBackWallBoardDataProvider callBackWallBoardDataProvider = new CTICallBackWallBoardDataProvider();
- bind(
- Key.get(WallBoardDataProvider.class,
- named(callBackWallBoardDataProvider
- .getProviderMetaData().getProviderUniqueID())))
- .to(CTICallBackWallBoardDataProvider.class);
- expose(Key.get(WallBoardDataProvider.class,
- named(callBackWallBoardDataProvider.getProviderMetaData()
- .getProviderUniqueID())));
- logger.info("Started:" + CTICallBackWallBoardDataProvider.class);
- }
- protected void setupURLAndServer(Properties myBatisProperties,
- Parameter parameterPassword, Parameter parameterUsername,
- Parameter parameterServerIP) {
- myBatisProperties.setProperty("JDBC.driver",
- "com.microsoft.sqlserver.jdbc.SQLServerDriver");
- myBatisProperties.setProperty("JDBC.urlA",
- "jdbc:sqlserver://" + parameterServerIP.getValue() + ";user="
- + parameterUsername.getValue() + ";password="
- + parameterPassword.getValue() + ";");
- myBatisProperties.setProperty("JDBC.urlB",
- "jdbc:sqlserver://" + parameterServerIP.getValue() + ";user="
- + parameterUsername.getValue() + ";password="
- + parameterPassword.getValue() + ";");
- }
- @Override
- @Inject
- public void setCoreDaoService(CoreDaoService coreDaoService) {
- this.coreDaoService = coreDaoService;
- }
- }
Add Comment
Please, Sign In to add comment