View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.named.hazelcast;
20  
21  import java.util.concurrent.ConcurrentHashMap;
22  import java.util.concurrent.ConcurrentMap;
23  import java.util.concurrent.TimeUnit;
24  
25  import com.hazelcast.core.HazelcastInstance;
26  import com.hazelcast.cp.ISemaphore;
27  import org.eclipse.aether.named.NamedLock;
28  import org.eclipse.aether.named.NamedLockKey;
29  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
30  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore;
31  import org.eclipse.aether.named.support.NamedLockFactorySupport;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * Factory of {@link AdaptedSemaphoreNamedLock} instances, using adapted Hazelcast {@link ISemaphore}. It delegates
37   * most the work to {@link HazelcastSemaphoreProvider} and this class just adapts the returned semaphore to named lock
38   * and caches {@link ISemaphore} instances, as recommended by Hazelcast.
39   *
40   * @deprecated Hazelcast support will be dropped.
41   */
42  @Deprecated
43  public class HazelcastSemaphoreNamedLockFactory extends NamedLockFactorySupport {
44      protected final HazelcastInstance hazelcastInstance;
45  
46      protected final boolean manageHazelcast;
47  
48      private final HazelcastSemaphoreProvider hazelcastSemaphoreProvider;
49  
50      private final ConcurrentMap<NamedLockKey, ISemaphore> semaphores;
51  
52      public HazelcastSemaphoreNamedLockFactory(
53              final HazelcastInstance hazelcastInstance,
54              final boolean manageHazelcast,
55              final HazelcastSemaphoreProvider hazelcastSemaphoreProvider) {
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 NamedLockKey key) {
64          ISemaphore semaphore = semaphores.computeIfAbsent(
65                  key, k -> hazelcastSemaphoreProvider.acquireSemaphore(hazelcastInstance, key));
66          return new AdaptedSemaphoreNamedLock(key, this, new HazelcastSemaphore(semaphore));
67      }
68  
69      @Override
70      protected void destroyLock(final NamedLock namedLock) {
71          if (namedLock instanceof AdaptedSemaphoreNamedLock) {
72              final NamedLockKey key = namedLock.key();
73              hazelcastSemaphoreProvider.releaseSemaphore(hazelcastInstance, key, semaphores.remove(key));
74          }
75      }
76  
77      @Override
78      protected void doShutdown() {
79          if (manageHazelcast) {
80              hazelcastInstance.shutdown();
81          }
82      }
83  
84      private static final class HazelcastSemaphore implements AdaptedSemaphore {
85          private final ISemaphore semaphore;
86  
87          private HazelcastSemaphore(final ISemaphore semaphore) {
88              this.semaphore = semaphore;
89          }
90  
91          @Override
92          public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
93              return semaphore.tryAcquire(perms, time, unit);
94          }
95  
96          @Override
97          public void release(final int perms) {
98              semaphore.release(perms);
99          }
100     }
101 }