Guest User

Untitled

a guest
May 28th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. package com.netdesign.dao.cti.callback.ioc;
  2.  
  3. import static com.google.inject.name.Names.named;
  4.  
  5. import java.util.Properties;
  6.  
  7. import javax.persistence.NoResultException;
  8.  
  9. import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
  10. import org.mybatis.guice.MyBatisModule;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.slurry.cache4guice.cache.util.Cache4GuiceHelper;
  14.  
  15. import com.google.inject.Inject;
  16. import com.google.inject.Key;
  17. import com.google.inject.PrivateModule;
  18. import com.google.inject.name.Names;
  19. import com.netdesign.dao.cti.callback.CTICallBackWallBoardDataProvider;
  20. import com.netdesign.dao.cti.callback.CallBackMapper;
  21. import com.netdesign.wallboard.core.dao.CoreDaoService;
  22. import com.netdesign.wallboard.core.datastorage.Parameter;
  23. import com.netdesign.wallboard.core.interfaces.WallBoardDataProvider;
  24. import com.netdesign.wallboard.core.interfaces.WallBoardDriverModule;
  25.  
  26. public class CTICallBackModule extends PrivateModule implements
  27.         WallBoardDriverModule {
  28.  
  29.     public static final String CACHE = "Cache timeout in seconds";
  30.  
  31.     public CTICallBackModule() {
  32.  
  33.     }
  34.  
  35.     private Cache4GuiceHelper cache4GuiceHelper;
  36.  
  37.     public Cache4GuiceHelper getCache4GuiceHelper() {
  38.         return cache4GuiceHelper;
  39.     }
  40.  
  41.      @Inject
  42.     public void setCache4GuiceHelper(Cache4GuiceHelper cache4GuiceHelper) {
  43.         this.cache4GuiceHelper = cache4GuiceHelper;
  44.     }
  45.  
  46.     private Properties getConnectionProperties() {
  47.         final Properties myBatisProperties = new Properties();
  48.  
  49.         CTICallBackWallBoardDataProvider ctiCallBackWallBoardDataProvider = new CTICallBackWallBoardDataProvider();
  50.  
  51.         Parameter parameterPassword = getCoreDaoService().getParameter(
  52.                 ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
  53.                 ctiCallBackWallBoardDataProvider.PASSWORD);
  54.         Parameter parameterUsername = getCoreDaoService().getParameter(
  55.                 ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
  56.                 ctiCallBackWallBoardDataProvider.USERNAME);
  57.         Parameter parameterServerIP = getCoreDaoService().getParameter(
  58.                 ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
  59.                 ctiCallBackWallBoardDataProvider.SERVER_IP);
  60.  
  61.         myBatisProperties.setProperty("mybatis.environment.id", "production");
  62.  
  63.         myBatisProperties.setProperty("JDBC.username",
  64.                 parameterUsername.getValue());
  65.         myBatisProperties.setProperty("JDBC.password",
  66.                 parameterPassword.getValue());
  67.         myBatisProperties.setProperty("JDBC.autoCommit", "true");
  68.         setupURLAndServer(myBatisProperties, parameterPassword,
  69.                 parameterUsername, parameterServerIP);
  70.  
  71.         setupCache(ctiCallBackWallBoardDataProvider);
  72.  
  73.         return myBatisProperties;
  74.     }
  75.  
  76.     private void setupCache(
  77.             CTICallBackWallBoardDataProvider ctiCallBackWallBoardDataProvider) {
  78.         ctiCallBackWallBoardDataProvider
  79.                 .setCache4GuiceHelper(getCache4GuiceHelper());
  80.         try {
  81.             Parameter parameterCache = getCoreDaoService().getParameter(
  82.                     ctiCallBackWallBoardDataProvider.getProviderUniqueID(),
  83.                     CACHE);
  84.  
  85.             ctiCallBackWallBoardDataProvider.setupCache(Integer
  86.                     .parseInt(parameterCache.getValue()));
  87.         } catch (NoResultException e) {
  88.             logger.debug("Could not find cache settings, running with the driver the first time?");
  89.         }
  90.     }
  91.  
  92.     private CoreDaoService coreDaoService;
  93.  
  94.     public CoreDaoService getCoreDaoService() {
  95.         return coreDaoService;
  96.     }
  97.  
  98.     private Logger logger = LoggerFactory.getLogger(CTICallBackModule.class);
  99.  
  100.     @Override
  101.     public void configure() {
  102.         logger.info("Starting :" + CTICallBackWallBoardDataProvider.class);
  103.  
  104.         MyBatisModule myBatisModule = new MyBatisModule() {
  105.  
  106.             @Override
  107.             protected void initialize() {
  108.  
  109.                 Names.bindProperties(this.binder(), getConnectionProperties());
  110.  
  111.                 addMapperClass(CallBackMapper.class);
  112.  
  113.                 environmentId("production");
  114.                 bindDataSourceProviderType(CTICallBackDataSourceProvider.class);
  115.                 bindTransactionFactoryType(JdbcTransactionFactory.class);
  116.  
  117.             }
  118.         };
  119.  
  120.         install(myBatisModule);
  121.  
  122.         CTICallBackWallBoardDataProvider callBackWallBoardDataProvider = new CTICallBackWallBoardDataProvider();
  123.         bind(
  124.                 Key.get(WallBoardDataProvider.class,
  125.                         named(callBackWallBoardDataProvider
  126.                                 .getProviderMetaData().getProviderUniqueID())))
  127.                 .to(CTICallBackWallBoardDataProvider.class);
  128.  
  129.         expose(Key.get(WallBoardDataProvider.class,
  130.                 named(callBackWallBoardDataProvider.getProviderMetaData()
  131.                         .getProviderUniqueID())));
  132.  
  133.         logger.info("Started:" + CTICallBackWallBoardDataProvider.class);
  134.     }
  135.  
  136.     protected void setupURLAndServer(Properties myBatisProperties,
  137.             Parameter parameterPassword, Parameter parameterUsername,
  138.             Parameter parameterServerIP) {
  139.         myBatisProperties.setProperty("JDBC.driver",
  140.  
  141.         "com.microsoft.sqlserver.jdbc.SQLServerDriver");
  142.         myBatisProperties.setProperty("JDBC.urlA",
  143.                 "jdbc:sqlserver://" + parameterServerIP.getValue() + ";user="
  144.                         + parameterUsername.getValue() + ";password="
  145.                         + parameterPassword.getValue() + ";");
  146.         myBatisProperties.setProperty("JDBC.urlB",
  147.                 "jdbc:sqlserver://" + parameterServerIP.getValue() + ";user="
  148.                         + parameterUsername.getValue() + ";password="
  149.                         + parameterPassword.getValue() + ";");
  150.     }
  151.  
  152.     @Override
  153.     @Inject
  154.     public void setCoreDaoService(CoreDaoService coreDaoService) {
  155.         this.coreDaoService = coreDaoService;
  156.  
  157.     }
  158.  
  159. }
Add Comment
Please, Sign In to add comment