1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.named.redisson;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26
27 import org.eclipse.aether.named.support.NamedLockFactorySupport;
28 import org.redisson.Redisson;
29 import org.redisson.api.RedissonClient;
30 import org.redisson.config.Config;
31
32
33
34
35 public abstract class RedissonNamedLockFactorySupport extends NamedLockFactorySupport {
36 protected static final String NAME_PREFIX = "maven:resolver:";
37
38 private static final String DEFAULT_CONFIG_FILE_NAME = "maven-resolver-redisson.yaml";
39
40 private static final String DEFAULT_REDIS_ADDRESS = "redis://localhost:6379";
41
42 private static final String DEFAULT_CLIENT_NAME = "maven-resolver";
43
44 private static final String CONFIG_PROP_CONFIG_FILE = "aether.syncContext.named.redisson.configFile";
45
46 protected final RedissonClient redissonClient;
47
48 public RedissonNamedLockFactorySupport() {
49 this.redissonClient = createRedissonClient();
50 }
51
52 @Override
53 public void shutdown() {
54 logger.trace("Shutting down Redisson client with id '{}'", redissonClient.getId());
55 redissonClient.shutdown();
56 }
57
58 private RedissonClient createRedissonClient() {
59 Path configFilePath = null;
60
61 String configFile = System.getProperty(CONFIG_PROP_CONFIG_FILE);
62 if (configFile != null && !configFile.isEmpty()) {
63 configFilePath = Paths.get(configFile);
64 if (Files.notExists(configFilePath)) {
65 throw new IllegalArgumentException(
66 "The specified Redisson config file does not exist: " + configFilePath);
67 }
68 }
69
70 if (configFilePath == null) {
71 String mavenConf = System.getProperty("maven.conf");
72 if (mavenConf != null && !mavenConf.isEmpty()) {
73 configFilePath = Paths.get(mavenConf, DEFAULT_CONFIG_FILE_NAME);
74 if (Files.notExists(configFilePath)) {
75 configFilePath = null;
76 }
77 }
78 }
79
80 Config config;
81
82 if (configFilePath != null) {
83 logger.trace("Reading Redisson config file from '{}'", configFilePath);
84 try (InputStream is = Files.newInputStream(configFilePath)) {
85 config = Config.fromYAML(is);
86 } catch (IOException e) {
87 throw new IllegalStateException("Failed to read Redisson config file: " + configFilePath, e);
88 }
89 } else {
90 config = new Config();
91 config.useSingleServer().setAddress(DEFAULT_REDIS_ADDRESS).setClientName(DEFAULT_CLIENT_NAME);
92 }
93
94 RedissonClient redissonClient = Redisson.create(config);
95 logger.trace("Created Redisson client with id '{}'", redissonClient.getId());
96
97 return redissonClient;
98 }
99 }