View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Support class for factories using {@link RedissonClient}.
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_CLIENT_NAME = "maven-resolver";
41  
42      /**
43       * Path to a Redisson configuration file in YAML format. Read official documentation for details.
44       *
45       * @since 1.7.0
46       * @configurationSource {@link System#getProperty(String, String)}
47       * @configurationType {@link java.lang.String}
48       */
49      public static final String SYSTEM_PROP_CONFIG_FILE = "aether.syncContext.named.redisson.configFile";
50  
51      /**
52       * Address of the Redis instance. Optional.
53       *
54       * @since 2.0.0
55       * @configurationSource {@link System#getProperty(String, String)}
56       * @configurationType {@link java.lang.String}
57       * @configurationDefaultValue {@link #DEFAULT_REDIS_ADDRESS}
58       */
59      public static final String SYSTEM_PROP_REDIS_ADDRESS = "aether.syncContext.named.redisson.address";
60  
61      public static final String DEFAULT_REDIS_ADDRESS = "redis://localhost:6379";
62  
63      protected final RedissonClient redissonClient;
64  
65      public RedissonNamedLockFactorySupport() {
66          this.redissonClient = createRedissonClient();
67      }
68  
69      @Override
70      protected void doShutdown() {
71          logger.trace("Shutting down Redisson client with id '{}'", redissonClient.getId());
72          redissonClient.shutdown();
73      }
74  
75      private RedissonClient createRedissonClient() {
76          Path configFilePath = null;
77  
78          String configFile = System.getProperty(SYSTEM_PROP_CONFIG_FILE);
79          if (configFile != null && !configFile.isEmpty()) {
80              configFilePath = Paths.get(configFile);
81              if (Files.notExists(configFilePath)) {
82                  throw new IllegalArgumentException(
83                          "The specified Redisson config file does not exist: " + configFilePath);
84              }
85          }
86  
87          if (configFilePath == null) {
88              String mavenConf = System.getProperty("maven.conf");
89              if (mavenConf != null && !mavenConf.isEmpty()) {
90                  configFilePath = Paths.get(mavenConf, DEFAULT_CONFIG_FILE_NAME);
91                  if (Files.notExists(configFilePath)) {
92                      configFilePath = null;
93                  }
94              }
95          }
96  
97          Config config;
98  
99          if (configFilePath != null) {
100             logger.trace("Reading Redisson config file from '{}'", configFilePath);
101             try (InputStream is = Files.newInputStream(configFilePath)) {
102                 config = Config.fromYAML(is);
103             } catch (IOException e) {
104                 throw new IllegalStateException("Failed to read Redisson config file: " + configFilePath, e);
105             }
106         } else {
107             config = new Config();
108             String defaultRedisAddress = System.getProperty(SYSTEM_PROP_REDIS_ADDRESS, DEFAULT_REDIS_ADDRESS);
109             config.useSingleServer().setAddress(defaultRedisAddress).setClientName(DEFAULT_CLIENT_NAME);
110         }
111 
112         RedissonClient redissonClient = Redisson.create(config);
113         logger.trace("Created Redisson client with id '{}'", redissonClient.getId());
114 
115         return redissonClient;
116     }
117 }