001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.named.redisson;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.nio.file.Files;
024import java.nio.file.Path;
025import java.nio.file.Paths;
026
027import org.eclipse.aether.named.support.NamedLockFactorySupport;
028import org.redisson.Redisson;
029import org.redisson.api.RedissonClient;
030import org.redisson.config.Config;
031
032/**
033 * Support class for factories using {@link RedissonClient}.
034 */
035public abstract class RedissonNamedLockFactorySupport extends NamedLockFactorySupport {
036    protected static final String NAME_PREFIX = "maven:resolver:";
037
038    private static final String DEFAULT_CONFIG_FILE_NAME = "maven-resolver-redisson.yaml";
039
040    private static final String DEFAULT_REDIS_ADDRESS = "redis://localhost:6379";
041
042    private static final String DEFAULT_CLIENT_NAME = "maven-resolver";
043
044    /**
045     * Path to a Redisson configuration file in YAML format. Read official documentation for details.
046     *
047     * @since 1.7.0
048     * @configurationSource {@link System#getProperty(String, String)}
049     * @configurationType {@link java.lang.String}
050     */
051    public static final String SYSTEM_PROP_CONFIG_FILE = "aether.syncContext.named.redisson.configFile";
052
053    protected final RedissonClient redissonClient;
054
055    public RedissonNamedLockFactorySupport() {
056        this.redissonClient = createRedissonClient();
057    }
058
059    @Override
060    public void shutdown() {
061        logger.trace("Shutting down Redisson client with id '{}'", redissonClient.getId());
062        redissonClient.shutdown();
063    }
064
065    private RedissonClient createRedissonClient() {
066        Path configFilePath = null;
067
068        String configFile = System.getProperty(SYSTEM_PROP_CONFIG_FILE);
069        if (configFile != null && !configFile.isEmpty()) {
070            configFilePath = Paths.get(configFile);
071            if (Files.notExists(configFilePath)) {
072                throw new IllegalArgumentException(
073                        "The specified Redisson config file does not exist: " + configFilePath);
074            }
075        }
076
077        if (configFilePath == null) {
078            String mavenConf = System.getProperty("maven.conf");
079            if (mavenConf != null && !mavenConf.isEmpty()) {
080                configFilePath = Paths.get(mavenConf, DEFAULT_CONFIG_FILE_NAME);
081                if (Files.notExists(configFilePath)) {
082                    configFilePath = null;
083                }
084            }
085        }
086
087        Config config;
088
089        if (configFilePath != null) {
090            logger.trace("Reading Redisson config file from '{}'", configFilePath);
091            try (InputStream is = Files.newInputStream(configFilePath)) {
092                config = Config.fromYAML(is);
093            } catch (IOException e) {
094                throw new IllegalStateException("Failed to read Redisson config file: " + configFilePath, e);
095            }
096        } else {
097            config = new Config();
098            config.useSingleServer().setAddress(DEFAULT_REDIS_ADDRESS).setClientName(DEFAULT_CLIENT_NAME);
099        }
100
101        RedissonClient redissonClient = Redisson.create(config);
102        logger.trace("Created Redisson client with id '{}'", redissonClient.getId());
103
104        return redissonClient;
105    }
106}