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
45
46
47
48
49
50
51 public static final String SYSTEM_PROP_CONFIG_FILE = "aether.syncContext.named.redisson.configFile";
52
53 protected final RedissonClient redissonClient;
54
55 public RedissonNamedLockFactorySupport() {
56 this.redissonClient = createRedissonClient();
57 }
58
59 @Override
60 public void shutdown() {
61 logger.trace("Shutting down Redisson client with id '{}'", redissonClient.getId());
62 redissonClient.shutdown();
63 }
64
65 private RedissonClient createRedissonClient() {
66 Path configFilePath = null;
67
68 String configFile = System.getProperty(SYSTEM_PROP_CONFIG_FILE);
69 if (configFile != null && !configFile.isEmpty()) {
70 configFilePath = Paths.get(configFile);
71 if (Files.notExists(configFilePath)) {
72 throw new IllegalArgumentException(
73 "The specified Redisson config file does not exist: " + configFilePath);
74 }
75 }
76
77 if (configFilePath == null) {
78 String mavenConf = System.getProperty("maven.conf");
79 if (mavenConf != null && !mavenConf.isEmpty()) {
80 configFilePath = Paths.get(mavenConf, DEFAULT_CONFIG_FILE_NAME);
81 if (Files.notExists(configFilePath)) {
82 configFilePath = null;
83 }
84 }
85 }
86
87 Config config;
88
89 if (configFilePath != null) {
90 logger.trace("Reading Redisson config file from '{}'", configFilePath);
91 try (InputStream is = Files.newInputStream(configFilePath)) {
92 config = Config.fromYAML(is);
93 } catch (IOException e) {
94 throw new IllegalStateException("Failed to read Redisson config file: " + configFilePath, e);
95 }
96 } else {
97 config = new Config();
98 config.useSingleServer().setAddress(DEFAULT_REDIS_ADDRESS).setClientName(DEFAULT_CLIENT_NAME);
99 }
100
101 RedissonClient redissonClient = Redisson.create(config);
102 logger.trace("Created Redisson client with id '{}'", redissonClient.getId());
103
104 return redissonClient;
105 }
106 }