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