Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 5.48 KB | None | 0 0
  1. package com.tirant.minemath.config
  2.  
  3. import com.tirant.minemath.MongoConnection
  4. import com.mongodb.ReadPreference;
  5. import redis.clients.jedis.Jedis
  6.  
  7.  
  8. class MineMathematicianConfig {
  9.    
  10.     Map base
  11.     Map config
  12.     String zone
  13.     def mongoMineMathDB
  14.     def mongoMineMathPrimaryDB    
  15.     Jedis redisMineMathDB
  16.     boolean debug=false
  17.     def mysqlLeafDB
  18.        
  19.     public MineMathematicianConfig() {
  20.        
  21.     }
  22.    
  23.     public MineMathematicianConfig(zone) {
  24.         this.base = [:]
  25.         this.config = [:]
  26.         init(zone)
  27.     }    
  28.    
  29.     public MineMathematicianConfig(base, config, zone) {
  30.         this.base = base
  31.         this.config = config
  32.         init(zone)
  33.     }
  34.    
  35.     def init(zone) {
  36.         this.zone = zone        
  37.         if (!this.config[zone]) this.config[zone]=[:]
  38.         mergeCommonMongo()
  39.         mergeCommonRedis()
  40.         mergeCommonMysql()  
  41.     }
  42.    
  43.     /*
  44.     * Dada la configuracion propia y la common del mongo, acaba insertando los valores common necesarios
  45.     */
  46.     def mergeCommonMongo() {
  47.         Map commonMongo = [
  48.             'common':[
  49.                 'mongo_mine_math_db':'mine_math'    
  50.             ],
  51.             'zona3':[
  52.                 'mongo_mine_math_host':'march.tirant.net:27017,ausias.tirant.net:27017,bazan.tirant.net:27017,espriu.tirant.net:27017'
  53.             ],
  54.             'dev':[
  55.                 'mongo_mine_math_host':'vargas3.tirant.net:27017',
  56.                 'mongo_mine_math_db':'themine'
  57.             ],
  58.             'test': [
  59.                 'mongo_mine_math_host':'localhost:27017',
  60.                 'mongo_mine_math_db':'themine_test'        
  61.             ]
  62.         ]
  63.  
  64.         mergeConfig(commonMongo[this.zone])
  65.         mergeConfig(commonMongo['common'])        
  66.     }
  67.    
  68.     def mergeCommonRedis() {
  69.         Map commonRedis = [
  70.             'common':[
  71.                 'redis_mine_math_port':6379    
  72.             ],
  73.             'dev':[
  74.                 'redis_mine_math_host':'localhost'                
  75.             ],
  76.             'zona3':[
  77.                 'redis_mine_math_host':'aleixandre.tirant.net'
  78.             ]            
  79.         ]
  80.  
  81.         mergeConfig(commonRedis[this.zone])
  82.         mergeConfig(commonRedis['common'])
  83.    
  84.     }
  85.    
  86.     def mergeCommonMysql() {
  87.         Map commonMysql = [
  88.             // Comun
  89.             'common':[:],
  90.             'test':[
  91.                'mysql_leaf_host': 'gustavo.tirant.net/mine_math', 'mysql_leaf_user':'tirant', 'mysql_leaf_password':'trnt,47'                
  92.             ],     
  93.             'dev':[
  94.                'mysql_leaf_host': 'gustavo.tirant.net/mine_math', 'mysql_leaf_user':'tirant', 'mysql_leaf_password':'trnt,47'                
  95.             ]          
  96.         ]      
  97.         mergeConfig(commonMysql[this.zone])
  98.         mergeConfig(commonMysql['common'])    
  99.     }
  100.    
  101.     /**
  102.     * Hace el merge entre los parametros propios del configurador (que son prioritarios) y los que vienen en un Map generico
  103.     */
  104.     def mergeConfig(mapToMerge) {
  105.         mapToMerge?.each { key, value ->
  106.             if (!getConfigValue(key)) {
  107.                 this.config["${this.zone}"][key] = value
  108.             }
  109.         }
  110.     }
  111.    
  112.     /**
  113.     * Anade de forma segura a this.config si no existe el valor
  114.     */
  115.     def addToConfigIfNotExists(domainzone, property, value) {
  116.         if (!this.config[domainzone])
  117.             this.config[domainzone]=[:]
  118.            
  119.         if (!this.config[domainzone][property])
  120.             this.config[domainzone][property] = value
  121.     }    
  122.  
  123.     /**
  124.     * Busca el valor de una key primero en el Map config y si no lo encuentra en el Map base.
  125.     * En el valor encontrado cambia los literales #ZONE#
  126.     */
  127.     def getConfigValue(key) {
  128.         def result = ( config[zone][key]!=null ? config[zone][key] : base[key] )
  129.         if (result) {
  130.             if (result instanceof String)
  131.                 result = result.replaceAll('#ZONE#', zone)        
  132.         }
  133.         return result  
  134.     }
  135.    
  136.     /**
  137.     * True si el zone esta configurado
  138.     */
  139.     boolean checkzone() {
  140.         return ( config[this.zone]!=null )
  141.     }
  142.  
  143.  
  144.     /**
  145.     * Obtiene conexion al Mongo themine
  146.     */
  147.     def getMongoMineMathDB(preference=ReadPreference.nearest()) {
  148.         if (this.mongoMineMathDB==null) {
  149.             this.mongoMineMathDB = MongoConnection.getMongoDB(
  150.             getConfigValue('mongo_mine_math_host'), getConfigValue('mongo_mine_math_db'), preference)  
  151.         }
  152.         this.mongoMineMathDB
  153.     }  
  154.    
  155.     /**
  156.     * Obtiene conexion al Mongo themine Primary
  157.     */
  158.     def getMongoMineMathPrimaryDB() {
  159.         if (this.mongoMineMathPrimaryDB==null) {
  160.             this.mongoMineMathPrimaryDB = MongoConnection.getMongoDB(
  161.             getConfigValue('mongo_mine_math_host'), getConfigValue('mongo_mine_math_db'), ReadPreference.primary())  
  162.         }
  163.         this.mongoMineMathPrimaryDB
  164.     }      
  165.    
  166.     /**
  167.     * Obtiene conexion al Mongo themine
  168.     */
  169.     def getRedisMineMathDB() {
  170.         if (this.redisMineMathDB==null) {
  171.             this.redisMineMathDB = new Jedis(getConfigValue('redis_mine_math_host'), getConfigValue('redis_mine_math_port'));
  172.         }
  173.         this.redisMineMathDB
  174.     }  
  175.    
  176.     /**
  177.     * Obtiene una conexion al Mysql del Km del dominio
  178.     */
  179.     def getMysqlLeafDBConnection() {
  180.         if (this.mysqlLeafDB==null) {
  181.             this.mysqlLeafDB = Sql.newInstance(
  182.             "jdbc:mysql://${getConfigValue('mysql_leaf_host')}?useUnicode=true&characterEncoding=UTF-8",
  183.             getConfigValue('mysql_leaf_user'),  getConfigValue('mysql_leaf_password'), "com.mysql.jdbc.Driver")    
  184.         }
  185.         return this.mysqlLeafDB
  186.     }      
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement