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 java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ConcurrentMap;
24  import java.util.concurrent.TimeUnit;
25  
26  import com.hazelcast.core.HazelcastInstance;
27  import com.hazelcast.cp.ISemaphore;
28  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
29  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore;
30  import org.eclipse.aether.named.support.NamedLockFactorySupport;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * Factory of {@link AdaptedSemaphoreNamedLock} instances, using adapted Hazelcast {@link ISemaphore}. It delegates
36   * most the work to {@link HazelcastSemaphoreProvider} and this class just adapts the returned semaphore to named lock
37   * and caches {@link ISemaphore} instances, as recommended by Hazelcast.
38   */
39  public class HazelcastSemaphoreNamedLockFactory
40          extends NamedLockFactorySupport
41  {
42      protected final HazelcastInstance hazelcastInstance;
43  
44      protected final boolean manageHazelcast;
45  
46      private final HazelcastSemaphoreProvider hazelcastSemaphoreProvider;
47  
48      private final ConcurrentMap<String, ISemaphore> semaphores;
49  
50      public HazelcastSemaphoreNamedLockFactory(
51              final HazelcastInstance hazelcastInstance,
52              final boolean manageHazelcast,
53              final HazelcastSemaphoreProvider hazelcastSemaphoreProvider
54      )
55      {
56          this.hazelcastInstance = requireNonNull( hazelcastInstance );
57          this.manageHazelcast = manageHazelcast;
58          this.hazelcastSemaphoreProvider = requireNonNull( hazelcastSemaphoreProvider );
59          this.semaphores = new ConcurrentHashMap<>();
60      }
61  
62      @Override
63      protected AdaptedSemaphoreNamedLock createLock( final String name )
64      {
65          ISemaphore semaphore = semaphores.computeIfAbsent( name,
66                  k -> hazelcastSemaphoreProvider.acquireSemaphore( hazelcastInstance, name ) );
67          return new AdaptedSemaphoreNamedLock( name, this, new HazelcastSemaphore( semaphore ) );
68      }
69  
70      @Override
71      protected void destroyLock( final String name )
72      {
73          hazelcastSemaphoreProvider.releaseSemaphore( hazelcastInstance, name, semaphores.remove( name ) );
74      }
75  
76      @Override
77      public void shutdown()
78      {
79          if ( manageHazelcast )
80          {
81              hazelcastInstance.shutdown();
82          }
83      }
84  
85      private static final class HazelcastSemaphore implements AdaptedSemaphore
86      {
87          private final ISemaphore semaphore;
88  
89          private HazelcastSemaphore( final ISemaphore semaphore )
90          {
91              this.semaphore = semaphore;
92          }
93  
94          @Override
95          public boolean tryAcquire( final int perms, final long time, final TimeUnit unit )
96                  throws InterruptedException
97          {
98              return semaphore.tryAcquire( perms, time, unit );
99          }
100 
101         @Override
102         public void release( final int perms )
103         {
104             semaphore.release( perms );
105         }
106     }
107 }