From e75a8ef2372722c0b22669fb427d47bacc5b8d5e Mon Sep 17 00:00:00 2001 From: HuabingZhao Date: Thu, 7 Sep 2017 14:33:18 +0800 Subject: Fix java check style warning Change-Id: I98a6d7237a213d007ad4d954989cb0b0fa150a10 Issue-Id: MSB-67 Signed-off-by: HuabingZhao --- .../onap/msb/apiroute/wrapper/util/JedisUtil.java | 273 ++++++++++----------- 1 file changed, 133 insertions(+), 140 deletions(-) (limited to 'apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/JedisUtil.java') diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/JedisUtil.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/JedisUtil.java index 0c238d8..96bccad 100644 --- a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/JedisUtil.java +++ b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/JedisUtil.java @@ -1,17 +1,15 @@ /******************************************************************************* * Copyright 2016-2017 ZTE, Inc. and others. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. ******************************************************************************/ package org.onap.msb.apiroute.wrapper.util; @@ -24,7 +22,6 @@ import java.util.PropertyResourceBundle; import java.util.ResourceBundle; import org.apache.commons.lang3.StringUtils; -import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,173 +32,169 @@ import redis.clients.jedis.JedisPoolConfig; public class JedisUtil { - private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class); - private static String host = "127.0.0.1"; - private static int port = 6379; - private static int connectionTimeout = 2000; - private static int DEFAULT_DB_INDEX = 0; + private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class); + private static String host = "127.0.0.1"; + private static int port = 6379; + private static int connectionTimeout = 2000; + private static int DEFAULT_DB_INDEX = 0; - private volatile static JedisPool jedisPool = null; + private volatile static JedisPool jedisPool = null; - public static String propertiesName = "redis.properties"; - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + public static String propertiesName = "redis.properties"; + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - private JedisUtil() { - // private constructor + private JedisUtil() { + // private constructor - } + } - private synchronized static JedisPool initialPool() throws IOException { + private synchronized static JedisPool initialPool() throws IOException { - JedisPoolConfig config = new JedisPoolConfig(); - config.setMaxTotal(50); - config.setMaxIdle(30); - config.setMaxWaitMillis(5000); - config.setTestOnBorrow(false); - config.setTestOnReturn(true); + JedisPoolConfig config = new JedisPoolConfig(); + config.setMaxTotal(50); + config.setMaxIdle(30); + config.setMaxWaitMillis(5000); + config.setTestOnBorrow(false); + config.setTestOnReturn(true); - URL urlPath = JedisUtil.class.getResource("/ext/redisConf/redis.properties"); - if (urlPath != null) { - String propertiesPath = urlPath.getPath(); + URL urlPath = JedisUtil.class.getResource("/ext/redisConf/redis.properties"); + if (urlPath != null) { + String propertiesPath = urlPath.getPath(); - File propertiesFile = new File(propertiesPath); + File propertiesFile = new File(propertiesPath); - if (propertiesFile.exists()) { + if (propertiesFile.exists()) { - BufferedInputStream inputStream = - new BufferedInputStream(new FileInputStream(propertiesPath)); - ResourceBundle bundle = new PropertyResourceBundle(inputStream); + BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(propertiesPath)); + ResourceBundle bundle = new PropertyResourceBundle(inputStream); - if (bundle == null) { - throw new IllegalArgumentException("[redis.properties] is not found!"); - } + if (bundle == null) { + throw new IllegalArgumentException("[redis.properties] is not found!"); + } - // Set up the connection pool basic information - String strHost = bundle.getString("redis.host"); - if (StringUtils.isNotEmpty(strHost)) { - host = strHost; - } + // Set up the connection pool basic information + String strHost = bundle.getString("redis.host"); + if (StringUtils.isNotEmpty(strHost)) { + host = strHost; + } - // redis port: first read from env - if (StringUtils.isNotBlank(System.getenv("APIGATEWAY_REDIS_PORT"))) { - port = Integer.parseInt(System.getenv("APIGATEWAY_REDIS_PORT")); - } else { - String strPort = bundle.getString("redis.port"); - if (StringUtils.isNotEmpty(strPort)) { - port = Integer.parseInt(strPort); - } - } + // redis port: first read from env + if (StringUtils.isNotBlank(System.getenv("APIGATEWAY_REDIS_PORT"))) { + port = Integer.parseInt(System.getenv("APIGATEWAY_REDIS_PORT")); + } else { + String strPort = bundle.getString("redis.port"); + if (StringUtils.isNotEmpty(strPort)) { + port = Integer.parseInt(strPort); + } + } - String strTimeout = bundle.getString("redis.connectionTimeout"); - if (StringUtils.isNotEmpty(strTimeout)) { - connectionTimeout = Integer.parseInt(strTimeout); - } + String strTimeout = bundle.getString("redis.connectionTimeout"); + if (StringUtils.isNotEmpty(strTimeout)) { + connectionTimeout = Integer.parseInt(strTimeout); + } - String strDbIndex = bundle.getString("redis.db_index"); - if (StringUtils.isNotEmpty(strDbIndex)) { - DEFAULT_DB_INDEX = Integer.parseInt(strDbIndex); - } + String strDbIndex = bundle.getString("redis.db_index"); + if (StringUtils.isNotEmpty(strDbIndex)) { + DEFAULT_DB_INDEX = Integer.parseInt(strDbIndex); + } - String strMaxTotal = bundle.getString("redis.pool.maxTotal"); - if (StringUtils.isNotEmpty(strMaxTotal)) { - config.setMaxTotal(Integer.parseInt(strMaxTotal)); - } + String strMaxTotal = bundle.getString("redis.pool.maxTotal"); + if (StringUtils.isNotEmpty(strMaxTotal)) { + config.setMaxTotal(Integer.parseInt(strMaxTotal)); + } - String strMaxIdle = bundle.getString("redis.pool.maxIdle"); - if (StringUtils.isNotEmpty(strMaxIdle)) { - config.setMaxIdle(Integer.parseInt(strMaxIdle)); - } + String strMaxIdle = bundle.getString("redis.pool.maxIdle"); + if (StringUtils.isNotEmpty(strMaxIdle)) { + config.setMaxIdle(Integer.parseInt(strMaxIdle)); + } - String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis"); - if (StringUtils.isNotEmpty(strMaxWaitMillis)) { - config.setMaxWaitMillis(Long.parseLong(strMaxWaitMillis)); - } + String strMaxWaitMillis = bundle.getString("redis.pool.maxWaitMillis"); + if (StringUtils.isNotEmpty(strMaxWaitMillis)) { + config.setMaxWaitMillis(Long.parseLong(strMaxWaitMillis)); + } - String strTestOnBorrow = bundle.getString("redis.pool.testOnBorrow"); - if (StringUtils.isNotEmpty(strTestOnBorrow)) { - config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow)); - } + String strTestOnBorrow = bundle.getString("redis.pool.testOnBorrow"); + if (StringUtils.isNotEmpty(strTestOnBorrow)) { + config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow)); + } + + String strTestOnReturn = bundle.getString("redis.pool.testOnReturn"); + if (StringUtils.isNotEmpty(strTestOnReturn)) { + config.setTestOnReturn(Boolean.valueOf(strTestOnReturn)); + } - String strTestOnReturn = bundle.getString("redis.pool.testOnReturn"); - if (StringUtils.isNotEmpty(strTestOnReturn)) { - config.setTestOnReturn(Boolean.valueOf(strTestOnReturn)); + } } - } - } + StringBuffer redisinfo = new StringBuffer(); + redisinfo.append("------redis.properties------").append(LINE_SEPARATOR); + redisinfo.append("redis.host: ").append(host).append(":").append(port).append(LINE_SEPARATOR); + redisinfo.append("redis.connectionTimeout: ").append(connectionTimeout).append(LINE_SEPARATOR); + redisinfo.append("redis.pool.maxTotal: ").append(config.getMaxTotal()).append(LINE_SEPARATOR); + redisinfo.append("redis.pool.maxIdle: ").append(config.getMaxIdle()).append(LINE_SEPARATOR); + redisinfo.append("redis.pool.maxWaitMillis: ").append(config.getMaxWaitMillis()).append(LINE_SEPARATOR); + redisinfo.append("redis.pool.testOnBorrow: ").append(config.getTestOnBorrow()).append(LINE_SEPARATOR); + redisinfo.append("redis.pool.testOnReturn: ").append(config.getTestOnReturn()).append(LINE_SEPARATOR); + + + LOGGER.info(redisinfo.toString()); + return new JedisPool(config, host, port, connectionTimeout); - StringBuffer redisinfo = new StringBuffer(); - redisinfo.append("------redis.properties------").append(LINE_SEPARATOR); - redisinfo.append("redis.host: ").append(host).append(":").append(port).append(LINE_SEPARATOR); - redisinfo.append("redis.connectionTimeout: ").append(connectionTimeout).append(LINE_SEPARATOR); - redisinfo.append("redis.pool.maxTotal: ").append(config.getMaxTotal()).append(LINE_SEPARATOR); - redisinfo.append("redis.pool.maxIdle: ").append(config.getMaxIdle()).append(LINE_SEPARATOR); - redisinfo.append("redis.pool.maxWaitMillis: ").append(config.getMaxWaitMillis()) - .append(LINE_SEPARATOR); - redisinfo.append("redis.pool.testOnBorrow: ").append(config.getTestOnBorrow()) - .append(LINE_SEPARATOR); - redisinfo.append("redis.pool.testOnReturn: ").append(config.getTestOnReturn()) - .append(LINE_SEPARATOR); - - - LOGGER.info(redisinfo.toString()); - return new JedisPool(config, host, port, connectionTimeout); - - } - - /** - * From the connection pool to obtain jedis instance, use the default database index number 0 - * - * @return - * @throws Exception - */ - public static Jedis borrowJedisInstance() throws Exception { - return borrowJedisInstance(DEFAULT_DB_INDEX); - } - - /** - * From the connection pool to obtain jedis instance, using the specified database index number - * - * @return - * @throws Exception - */ - - public static Jedis borrowJedisInstance(final int dbIndex) throws Exception { - if (jedisPool == null) { - synchronized (JedisUtil.class) { - if (jedisPool == null) { - jedisPool = initialPool(); - } - } } - Jedis resource = jedisPool.getResource(); - if (resource == null) { - throw new Exception("fetch from jedis pool failed,null object!"); + /** + * From the connection pool to obtain jedis instance, use the default database index number 0 + * + * @return + * @throws Exception + */ + public static Jedis borrowJedisInstance() throws Exception { + return borrowJedisInstance(DEFAULT_DB_INDEX); } - resource.select(dbIndex); - return resource; + /** + * From the connection pool to obtain jedis instance, using the specified database index number + * + * @return + * @throws Exception + */ + + public static Jedis borrowJedisInstance(final int dbIndex) throws Exception { + if (jedisPool == null) { + synchronized (JedisUtil.class) { + if (jedisPool == null) { + jedisPool = initialPool(); + } + } + } + Jedis resource = jedisPool.getResource(); + + if (resource == null) { + throw new Exception("fetch from jedis pool failed,null object!"); + } - } + resource.select(dbIndex); + return resource; - /** - * returned to the pool jedis instance - * - * @param jedis - */ - public static void returnJedisInstance(final Jedis jedis) { - if (jedis != null) { - jedis.close(); } - } + + /** + * returned to the pool jedis instance + * + * @param jedis + */ + public static void returnJedisInstance(final Jedis jedis) { + if (jedis != null) { + jedis.close(); + } + } } -- cgit 1.2.3-korg