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    private static final String CONFIG_PROP_CONFIG_FILE = "aether.syncContext.named.redisson.configFile";
045
046    protected final RedissonClient redissonClient;
047
048    public RedissonNamedLockFactorySupport() {
049        this.redissonClient = createRedissonClient();
050    }
051
052    @Override
053    public void shutdown() {
054        logger.trace("Shutting down Redisson client with id '{}'", redissonClient.getId());
055        redissonClient.shutdown();
056    }
057
058    private RedissonClient createRedissonClient() {
059        Path configFilePath = null;
060
061        String configFile = System.getProperty(CONFIG_PROP_CONFIG_FILE);
062        if (configFile != null && !configFile.isEmpty()) {
063            configFilePath = Paths.get(configFile);
064            if (Files.notExists(configFilePath)) {
065                throw new IllegalArgumentException(
066                        "The specified Redisson config file does not exist: " + configFilePath);
067            }
068        }
069
070        if (configFilePath == null) {
071            String mavenConf = System.getProperty("maven.conf");
072            if (mavenConf != null && !mavenConf.isEmpty()) {
073                configFilePath = Paths.get(mavenConf, DEFAULT_CONFIG_FILE_NAME);
074                if (Files.notExists(configFilePath)) {
075                    configFilePath = null;
076                }
077            }
078        }
079
080        Config config;
081
082        if (configFilePath != null) {
083            logger.trace("Reading Redisson config file from '{}'", configFilePath);
084            try (InputStream is = Files.newInputStream(configFilePath)) {
085                config = Config.fromYAML(is);
086            } catch (IOException e) {
087                throw new IllegalStateException("Failed to read Redisson config file: " + configFilePath, e);
088            }
089        } else {
090            config = new Config();
091            config.useSingleServer().setAddress(DEFAULT_REDIS_ADDRESS).setClientName(DEFAULT_CLIENT_NAME);
092        }
093
094        RedissonClient redissonClient = Redisson.create(config);
095        logger.trace("Created Redisson client with id '{}'", redissonClient.getId());
096
097        return redissonClient;
098    }
099}