View Javadoc
1   package org.eclipse.aether.internal.impl.synccontext;
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 java.util.HashMap;
23  import java.util.Map;
24  import java.util.concurrent.TimeUnit;
25  
26  import javax.inject.Inject;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  
30  import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
31  import org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper;
32  import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
33  import org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper;
34  import org.eclipse.aether.named.NamedLockFactory;
35  import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
36  import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
37  import org.eclipse.aether.named.providers.NoopNamedLockFactory;
38  
39  /**
40   * Selector for {@link NamedLockFactory} and {@link NameMapper} that selects and exposes selected ones. Essentially
41   * all the named locks configuration is here.
42   */
43  @Singleton
44  @Named
45  public final class NamedLockFactorySelector
46  {
47      public static final long TIME = Long.getLong(
48          "aether.syncContext.named.time", 30L
49      );
50  
51      public static final TimeUnit TIME_UNIT = TimeUnit.valueOf( System.getProperty(
52          "aether.syncContext.named.time.unit", TimeUnit.SECONDS.name()
53      ) );
54  
55      private static final String FACTORY_NAME = System.getProperty(
56          "aether.syncContext.named.factory", LocalReadWriteLockNamedLockFactory.NAME
57      );
58  
59      private static final String NAME_MAPPER_NAME = System.getProperty(
60          "aether.syncContext.named.nameMapper", GAVNameMapper.NAME
61      );
62  
63      private final NamedLockFactory namedLockFactory;
64  
65      private final NameMapper nameMapper;
66  
67      /**
68       * Constructor used with DI, where factories are injected and selected based on key.
69       */
70      @Inject
71      public NamedLockFactorySelector( final Map<String, NamedLockFactory> factories,
72                                       final Map<String, NameMapper> nameMappers )
73      {
74          this.namedLockFactory = selectNamedLockFactory( factories );
75          this.nameMapper = selectNameMapper( nameMappers );
76      }
77  
78      /**
79       * Default constructor for ServiceLocator.
80       */
81      public NamedLockFactorySelector()
82      {
83          Map<String, NamedLockFactory> factories = new HashMap<>();
84          factories.put( NoopNamedLockFactory.NAME, new NoopNamedLockFactory() );
85          factories.put( LocalReadWriteLockNamedLockFactory.NAME, new LocalReadWriteLockNamedLockFactory() );
86          factories.put( LocalSemaphoreNamedLockFactory.NAME, new LocalSemaphoreNamedLockFactory() );
87          this.namedLockFactory = selectNamedLockFactory( factories );
88  
89          Map<String, NameMapper> nameMappers = new HashMap<>();
90          nameMappers.put( StaticNameMapper.NAME, new StaticNameMapper() );
91          nameMappers.put( GAVNameMapper.NAME, new GAVNameMapper() );
92          nameMappers.put( DiscriminatingNameMapper.NAME, new DiscriminatingNameMapper( new GAVNameMapper() ) );
93          this.nameMapper = selectNameMapper( nameMappers );
94      }
95  
96      /**
97       * Returns the selected {@link NamedLockFactory}, never null.
98       */
99      public NamedLockFactory getSelectedNamedLockFactory()
100     {
101         return namedLockFactory;
102     }
103 
104     /**
105      * Returns the selected {@link NameMapper}, never null.
106      */
107     public NameMapper getSelectedNameMapper()
108     {
109         return nameMapper;
110     }
111 
112     private static NamedLockFactory selectNamedLockFactory( final Map<String, NamedLockFactory> factories )
113     {
114         NamedLockFactory factory = factories.get( FACTORY_NAME );
115         if ( factory == null )
116         {
117             throw new IllegalArgumentException( "Unknown NamedLockFactory name: " + FACTORY_NAME
118                 + ", known ones: " + factories.keySet() );
119         }
120         return factory;
121     }
122 
123     private static NameMapper selectNameMapper( final Map<String, NameMapper> nameMappers )
124     {
125         NameMapper nameMapper = nameMappers.get( NAME_MAPPER_NAME );
126         if ( nameMapper == null )
127         {
128             throw new IllegalArgumentException( "Unknown NameMapper name: " + NAME_MAPPER_NAME
129                 + ", known ones: " + nameMappers.keySet() );
130         }
131         return nameMapper;
132     }
133 }