Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / JedisUtil.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.msb.apiroute.wrapper.util;
18
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.net.URL;
24 import java.util.PropertyResourceBundle;
25 import java.util.ResourceBundle;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import redis.clients.jedis.Jedis;
33 import redis.clients.jedis.JedisPool;
34 import redis.clients.jedis.JedisPoolConfig;
35
36
37
38 public class JedisUtil {
39   private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class);
40   private static String host = "127.0.0.1";
41   private static int port = 6379;
42   private static int connectionTimeout = 2000;
43   private static int DEFAULT_DB_INDEX = 0;
44
45   private volatile static JedisPool jedisPool = null;
46
47
48   public static String propertiesName = "redis.properties";
49   private static final String LINE_SEPARATOR = System.getProperty("line.separator");
50
51
52   private JedisUtil() {
53     // private constructor
54
55   }
56
57   private synchronized static JedisPool initialPool() throws IOException {
58
59     JedisPoolConfig config = new JedisPoolConfig();
60     config.setMaxTotal(50);
61     config.setMaxIdle(30);
62     config.setMaxWaitMillis(5000);
63     config.setTestOnBorrow(false);
64     config.setTestOnReturn(true);
65
66     URL urlPath = JedisUtil.class.getResource("/ext/redisConf/redis.properties");
67     if (urlPath != null) {
68       String propertiesPath = urlPath.getPath();
69
70
71       File propertiesFile = new File(propertiesPath);
72
73       if (propertiesFile.exists()) {
74
75
76         BufferedInputStream inputStream =
77             new BufferedInputStream(new FileInputStream(propertiesPath));
78         ResourceBundle bundle = new PropertyResourceBundle(inputStream);
79
80         if (bundle == null) {
81           throw new IllegalArgumentException("[redis.properties] is not found!");
82         }
83
84
85         // Set up the connection pool basic information
86         String strHost = bundle.getString("redis.host");
87         if (StringUtils.isNotEmpty(strHost)) {
88           host = strHost;
89         }
90
91         // redis port: first read from env
92         if (StringUtils.isNotBlank(System.getenv("APIGATEWAY_REDIS_PORT"))) {
93           port = Integer.parseInt(System.getenv("APIGATEWAY_REDIS_PORT"));
94         } else {
95           String strPort = bundle.getString("redis.port");
96           if (StringUtils.isNotEmpty(strPort)) {
97             port = Integer.parseInt(strPort);
98           }
99         }
100
101
102         String strTimeout = bundle.getString("redis.connectionTimeout");
103         if (StringUtils.isNotEmpty(strTimeout)) {
104           connectionTimeout = Integer.parseInt(strTimeout);
105         }
106
107
108         String strDbIndex = bundle.getString("redis.db_index");
109         if (StringUtils.isNotEmpty(strDbIndex)) {
110           DEFAULT_DB_INDEX = Integer.parseInt(strDbIndex);
111         }
112
113         String strMaxTotal = bundle.getString("redis.pool.maxTotal");
114         if (StringUtils.isNotEmpty(strMaxTotal)) {
115           config.setMaxTotal(Integer.parseInt(strMaxTotal));
116         }
117
118         String strMaxIdle = bundle.getString("redis.pool.maxIdle");
119         if (StringUtils.isNotEmpty(strMaxIdle)) {
120           config.setMaxIdle(Integer.parseInt(strMaxIdle));
121         }
122
123         String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis");
124         if (StringUtils.isNotEmpty(strMaxWaitMillis)) {
125           config.setMaxWaitMillis(Long.parseLong(strMaxWaitMillis));
126         }
127
128         String strTestOnBorrow = bundle.getString("redis.pool.testOnBorrow");
129         if (StringUtils.isNotEmpty(strTestOnBorrow)) {
130           config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
131         }
132
133         String strTestOnReturn = bundle.getString("redis.pool.testOnReturn");
134         if (StringUtils.isNotEmpty(strTestOnReturn)) {
135           config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
136         }
137
138       }
139     }
140
141     StringBuffer redisinfo = new StringBuffer();
142     redisinfo.append("------redis.properties------").append(LINE_SEPARATOR);
143     redisinfo.append("redis.host: ").append(host).append(":").append(port).append(LINE_SEPARATOR);
144     redisinfo.append("redis.connectionTimeout: ").append(connectionTimeout).append(LINE_SEPARATOR);
145     redisinfo.append("redis.pool.maxTotal: ").append(config.getMaxTotal()).append(LINE_SEPARATOR);
146     redisinfo.append("redis.pool.maxIdle: ").append(config.getMaxIdle()).append(LINE_SEPARATOR);
147     redisinfo.append("redis.pool.maxWaitMillis: ").append(config.getMaxWaitMillis())
148         .append(LINE_SEPARATOR);
149     redisinfo.append("redis.pool.testOnBorrow: ").append(config.getTestOnBorrow())
150         .append(LINE_SEPARATOR);
151     redisinfo.append("redis.pool.testOnReturn: ").append(config.getTestOnReturn())
152         .append(LINE_SEPARATOR);
153
154
155     LOGGER.info(redisinfo.toString());
156     return new JedisPool(config, host, port, connectionTimeout);
157
158   }
159
160   /**
161    * From the connection pool to obtain jedis instance, use the default database index number 0
162    * 
163    * @return
164    * @throws Exception
165    */
166   public static Jedis borrowJedisInstance() throws Exception {
167     return borrowJedisInstance(DEFAULT_DB_INDEX);
168   }
169
170   /**
171    * From the connection pool to obtain jedis instance, using the specified database index number
172    * 
173    * @return
174    * @throws Exception
175    */
176
177   public static Jedis borrowJedisInstance(final int dbIndex) throws Exception {
178     if (jedisPool == null) {
179       synchronized (JedisUtil.class) {
180         if (jedisPool == null) {         
181             jedisPool = initialPool();         
182         }
183       }
184     }
185     Jedis resource = jedisPool.getResource();
186
187     if (resource == null) {
188       throw new Exception("fetch from jedis pool failed,null object!");
189     }
190
191     resource.select(dbIndex);
192     return resource;
193
194   }
195
196   /**
197    * returned to the pool jedis instance
198    * 
199    * @param jedis
200    */
201   public static void returnJedisInstance(final Jedis jedis) {
202     if (jedis != null) {
203       jedis.close();
204     }
205   }
206
207
208 }