View Javadoc
1   package org.eclipse.aether.named.redisson;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Support class for factories using {@link RedissonClient}.
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 }