View Javadoc
1   package org.eclipse.aether.internal.impl.synccontext.named;
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.NamedLockFactory;
23  
24  import java.util.Map;
25  
26  /**
27   * Selector implementation support class: by extending this class one may override defaults, or provide completely
28   * alternative way of configuration. This implementation uses Java System properties to select factory and name mapper.
29   *
30   * @since 1.7.3
31   */
32  public abstract class NamedLockFactorySelectorSupport
33      implements NamedLockFactorySelector
34  {
35      public static final String FACTORY_KEY = "aether.syncContext.named.factory";
36  
37      public static final String NAME_MAPPER_KEY = "aether.syncContext.named.nameMapper";
38  
39      private final NamedLockFactory namedLockFactory;
40  
41      private final NameMapper nameMapper;
42  
43      public NamedLockFactorySelectorSupport( final Map<String, NamedLockFactory> factories,
44                                              final String defaultFactoryName,
45                                              final Map<String, NameMapper> nameMappers,
46                                              final String defaultNameMapperName )
47      {
48          this.namedLockFactory = selectNamedLockFactory( factories, getFactoryName( defaultFactoryName ) );
49          this.nameMapper = selectNameMapper( nameMappers, getNameMapperName( defaultNameMapperName ) );
50      }
51  
52      /**
53       * Returns the selected {@link NamedLockFactory}, never null.
54       */
55      @Override
56      public NamedLockFactory getSelectedNamedLockFactory()
57      {
58          return namedLockFactory;
59      }
60  
61      /**
62       * Returns the selected {@link NameMapper}, never null.
63       */
64      @Override
65      public NameMapper getSelectedNameMapper()
66      {
67          return nameMapper;
68      }
69  
70      /**
71       * Returns selected factory name (or passed in default) using System property value of {@link #FACTORY_KEY}.
72       */
73      protected String getFactoryName( final String defaultFactoryName )
74      {
75          return System.getProperty( FACTORY_KEY, defaultFactoryName );
76      }
77  
78      /**
79       * Returns selected name mapper name (or passed in default) using System property value of {@link #NAME_MAPPER_KEY}.
80       */
81      protected String getNameMapperName( final String defaultNameMapperName )
82      {
83          return System.getProperty( NAME_MAPPER_KEY, defaultNameMapperName );
84      }
85  
86      private NamedLockFactory selectNamedLockFactory( final Map<String, NamedLockFactory> factories,
87                                                       final String factoryName )
88      {
89          NamedLockFactory factory = factories.get( factoryName );
90          if ( factory == null )
91          {
92              throw new IllegalArgumentException( "Unknown NamedLockFactory name: " + factoryName
93                  + ", known ones: " + factories.keySet() );
94          }
95          return factory;
96      }
97  
98      private NameMapper selectNameMapper( final Map<String, NameMapper> nameMappers,
99                                           final String mapperName )
100     {
101         NameMapper nameMapper = nameMappers.get( mapperName );
102         if ( nameMapper == null )
103         {
104             throw new IllegalArgumentException( "Unknown NameMapper name: " + mapperName
105                 + ", known ones: " + nameMappers.keySet() );
106         }
107         return nameMapper;
108     }
109 }