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