001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.redis;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XObject;
026import redis.clients.jedis.Jedis;
027import redis.clients.jedis.JedisPool;
028import redis.clients.jedis.JedisPoolConfig;
029import redis.clients.jedis.Protocol;
030import redis.clients.jedis.exceptions.JedisException;
031
032/**
033 * Descriptor for a Redis configuration.
034 *
035 * @since 5.8
036 */
037@XObject("server")
038public class RedisServerDescriptor extends RedisPoolDescriptor {
039
040    private static final Log log = LogFactory.getLog(RedisServerDescriptor.class);
041
042    @XNode("host")
043    public String host;
044
045    @XNode("port")
046    public int port = Protocol.DEFAULT_PORT;
047
048    @XNode("failoverTimeout")
049    public int failoverTimeout = 300;
050
051    protected boolean canConnect(String name, int port) {
052        try (Jedis jedis = new Jedis(name, port)) {
053            if (StringUtils.isNotBlank(password)) {
054                jedis.auth(password);
055            }
056            return canPing(jedis);
057        }
058    }
059
060    protected boolean canPing(Jedis jedis) {
061        try {
062            String pong = jedis.ping();
063            return "PONG".equals(pong);
064        } catch (JedisException cause) {
065            log.debug("Exception during ping", cause);
066            return false;
067        }
068    }
069
070    @Override
071    public RedisExecutor newExecutor() {
072        if (!canConnect(host, port)) {
073            throw new RuntimeException("Cannot connect to Redis host: " + host + ":" + port);
074        }
075        JedisPoolConfig conf = new JedisPoolConfig();
076        conf.setMaxTotal(maxTotal);
077        conf.setMaxIdle(maxIdle);
078        RedisExecutor base = new RedisPoolExecutor(new JedisPool(conf, host, port, timeout,
079                StringUtils.defaultIfBlank(password, null), database));
080        return new RedisFailoverExecutor(failoverTimeout, base);
081    }
082
083}