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