View Javadoc
1   package org.eclipse.aether.named.hazelcast;
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 com.hazelcast.core.HazelcastInstance;
23  import com.hazelcast.cp.ISemaphore;
24  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
25  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore;
26  import org.eclipse.aether.named.support.NamedLockFactorySupport;
27  
28  import java.util.concurrent.ConcurrentHashMap;
29  import java.util.concurrent.ConcurrentMap;
30  import java.util.concurrent.TimeUnit;
31  import java.util.function.BiFunction;
32  
33  /**
34   * Factory of {@link AdaptedSemaphoreNamedLock} instances using adapted Hazelcast {@link ISemaphore}. This class may
35   * use {@link HazelcastInstance} backed by Hazelcast Server or Hazelcast Client.
36   */
37  public class HazelcastSemaphoreNamedLockFactory
38      extends NamedLockFactorySupport
39  {
40      protected static final String NAME_PREFIX = "maven:resolver:";
41  
42      private final HazelcastInstance hazelcastInstance;
43  
44      private final BiFunction<HazelcastInstance, String, ISemaphore> semaphoreFunction;
45  
46      private final boolean destroySemaphore;
47  
48      private final boolean manageHazelcast;
49  
50      private final ConcurrentMap<String, ISemaphore> semaphores;
51  
52      public HazelcastSemaphoreNamedLockFactory(
53          final HazelcastInstance hazelcastInstance,
54          final BiFunction<HazelcastInstance, String, ISemaphore> semaphoreFunction,
55          final boolean destroySemaphore,
56          final boolean manageHazelcast
57      )
58      {
59          this.hazelcastInstance = hazelcastInstance;
60          this.semaphoreFunction = semaphoreFunction;
61          this.destroySemaphore = destroySemaphore;
62          this.manageHazelcast = manageHazelcast;
63          this.semaphores = new ConcurrentHashMap<>();
64      }
65  
66      @Override
67      protected AdaptedSemaphoreNamedLock createLock( final String name )
68      {
69          ISemaphore semaphore = semaphores.computeIfAbsent( name, k ->
70          {
71              ISemaphore result = semaphoreFunction.apply( hazelcastInstance, k );
72              result.init( Integer.MAX_VALUE );
73              return result;
74          } );
75          return new AdaptedSemaphoreNamedLock( name, this, new HazelcastSemaphore( semaphore ) );
76      }
77  
78      @Override
79      public void shutdown()
80      {
81          if ( manageHazelcast )
82          {
83              hazelcastInstance.shutdown();
84          }
85      }
86  
87      @Override
88      protected void destroyLock( final String name )
89      {
90          ISemaphore semaphore = semaphores.remove( name );
91          if ( destroySemaphore )
92          {
93              if ( semaphore == null )
94              {
95                  throw new IllegalStateException( "Semaphore expected but does not exist: " + name );
96              }
97              semaphore.destroy();
98          }
99      }
100 
101     private static final class HazelcastSemaphore implements AdaptedSemaphore
102     {
103         private final ISemaphore semaphore;
104 
105         private HazelcastSemaphore( final ISemaphore semaphore )
106         {
107             this.semaphore = semaphore;
108         }
109 
110         @Override
111         public boolean tryAcquire( final int perms, final long time, final TimeUnit unit )
112             throws InterruptedException
113         {
114             return semaphore.tryAcquire( perms, time, unit );
115         }
116 
117         @Override
118         public void release( final int perms )
119         {
120             semaphore.release( perms );
121         }
122     }
123 }