Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tirant.minemath.config
- import com.tirant.minemath.MongoConnection
- import com.mongodb.ReadPreference;
- import redis.clients.jedis.Jedis
- class MineMathematicianConfig {
- Map base
- Map config
- String zone
- def mongoMineMathDB
- def mongoMineMathPrimaryDB
- Jedis redisMineMathDB
- boolean debug=false
- def mysqlLeafDB
- public MineMathematicianConfig() {
- }
- public MineMathematicianConfig(zone) {
- this.base = [:]
- this.config = [:]
- init(zone)
- }
- public MineMathematicianConfig(base, config, zone) {
- this.base = base
- this.config = config
- init(zone)
- }
- def init(zone) {
- this.zone = zone
- if (!this.config[zone]) this.config[zone]=[:]
- mergeCommonMongo()
- mergeCommonRedis()
- mergeCommonMysql()
- }
- /*
- * Dada la configuracion propia y la common del mongo, acaba insertando los valores common necesarios
- */
- def mergeCommonMongo() {
- Map commonMongo = [
- 'common':[
- 'mongo_mine_math_db':'mine_math'
- ],
- 'zona3':[
- 'mongo_mine_math_host':'march.tirant.net:27017,ausias.tirant.net:27017,bazan.tirant.net:27017,espriu.tirant.net:27017'
- ],
- 'dev':[
- 'mongo_mine_math_host':'vargas3.tirant.net:27017',
- 'mongo_mine_math_db':'themine'
- ],
- 'test': [
- 'mongo_mine_math_host':'localhost:27017',
- 'mongo_mine_math_db':'themine_test'
- ]
- ]
- mergeConfig(commonMongo[this.zone])
- mergeConfig(commonMongo['common'])
- }
- def mergeCommonRedis() {
- Map commonRedis = [
- 'common':[
- 'redis_mine_math_port':6379
- ],
- 'dev':[
- 'redis_mine_math_host':'localhost'
- ],
- 'zona3':[
- 'redis_mine_math_host':'aleixandre.tirant.net'
- ]
- ]
- mergeConfig(commonRedis[this.zone])
- mergeConfig(commonRedis['common'])
- }
- def mergeCommonMysql() {
- Map commonMysql = [
- // Comun
- 'common':[:],
- 'test':[
- 'mysql_leaf_host': 'gustavo.tirant.net/mine_math', 'mysql_leaf_user':'tirant', 'mysql_leaf_password':'trnt,47'
- ],
- 'dev':[
- 'mysql_leaf_host': 'gustavo.tirant.net/mine_math', 'mysql_leaf_user':'tirant', 'mysql_leaf_password':'trnt,47'
- ]
- ]
- mergeConfig(commonMysql[this.zone])
- mergeConfig(commonMysql['common'])
- }
- /**
- * Hace el merge entre los parametros propios del configurador (que son prioritarios) y los que vienen en un Map generico
- */
- def mergeConfig(mapToMerge) {
- mapToMerge?.each { key, value ->
- if (!getConfigValue(key)) {
- this.config["${this.zone}"][key] = value
- }
- }
- }
- /**
- * Anade de forma segura a this.config si no existe el valor
- */
- def addToConfigIfNotExists(domainzone, property, value) {
- if (!this.config[domainzone])
- this.config[domainzone]=[:]
- if (!this.config[domainzone][property])
- this.config[domainzone][property] = value
- }
- /**
- * Busca el valor de una key primero en el Map config y si no lo encuentra en el Map base.
- * En el valor encontrado cambia los literales #ZONE#
- */
- def getConfigValue(key) {
- def result = ( config[zone][key]!=null ? config[zone][key] : base[key] )
- if (result) {
- if (result instanceof String)
- result = result.replaceAll('#ZONE#', zone)
- }
- return result
- }
- /**
- * True si el zone esta configurado
- */
- boolean checkzone() {
- return ( config[this.zone]!=null )
- }
- /**
- * Obtiene conexion al Mongo themine
- */
- def getMongoMineMathDB(preference=ReadPreference.nearest()) {
- if (this.mongoMineMathDB==null) {
- this.mongoMineMathDB = MongoConnection.getMongoDB(
- getConfigValue('mongo_mine_math_host'), getConfigValue('mongo_mine_math_db'), preference)
- }
- this.mongoMineMathDB
- }
- /**
- * Obtiene conexion al Mongo themine Primary
- */
- def getMongoMineMathPrimaryDB() {
- if (this.mongoMineMathPrimaryDB==null) {
- this.mongoMineMathPrimaryDB = MongoConnection.getMongoDB(
- getConfigValue('mongo_mine_math_host'), getConfigValue('mongo_mine_math_db'), ReadPreference.primary())
- }
- this.mongoMineMathPrimaryDB
- }
- /**
- * Obtiene conexion al Mongo themine
- */
- def getRedisMineMathDB() {
- if (this.redisMineMathDB==null) {
- this.redisMineMathDB = new Jedis(getConfigValue('redis_mine_math_host'), getConfigValue('redis_mine_math_port'));
- }
- this.redisMineMathDB
- }
- /**
- * Obtiene una conexion al Mysql del Km del dominio
- */
- def getMysqlLeafDBConnection() {
- if (this.mysqlLeafDB==null) {
- this.mysqlLeafDB = Sql.newInstance(
- "jdbc:mysql://${getConfigValue('mysql_leaf_host')}?useUnicode=true&characterEncoding=UTF-8",
- getConfigValue('mysql_leaf_user'), getConfigValue('mysql_leaf_password'), "com.mysql.jdbc.Driver")
- }
- return this.mysqlLeafDB
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement