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.nio.file.Files;
22  import java.nio.file.Paths;
23  import java.util.concurrent.ConcurrentHashMap;
24  import java.util.concurrent.ConcurrentMap;
25  import java.util.concurrent.TimeUnit;
26  
27  import com.hazelcast.core.HazelcastInstance;
28  import com.hazelcast.cp.ISemaphore;
29  import org.eclipse.aether.named.NamedLock;
30  import org.eclipse.aether.named.NamedLockKey;
31  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
32  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore;
33  import org.eclipse.aether.named.support.NamedLockFactorySupport;
34  
35  import static java.util.Objects.requireNonNull;
36  
37  /**
38   * Factory of {@link AdaptedSemaphoreNamedLock} instances, using adapted Hazelcast {@link ISemaphore}. It delegates
39   * most the work to {@link HazelcastSemaphoreProvider} and this class just adapts the returned semaphore to named lock
40   * and caches {@link ISemaphore} instances, as recommended by Hazelcast.
41   *
42   * @deprecated Hazelcast support will be dropped.
43   */
44  @Deprecated
45  public class HazelcastSemaphoreNamedLockFactory extends NamedLockFactorySupport {
46      protected final HazelcastInstance hazelcastInstance;
47  
48      protected final boolean manageHazelcast;
49  
50      private final HazelcastSemaphoreProvider hazelcastSemaphoreProvider;
51  
52      private final ConcurrentMap<NamedLockKey, ISemaphore> semaphores;
53  
54      public HazelcastSemaphoreNamedLockFactory(
55              final HazelcastInstance hazelcastInstance,
56              final boolean manageHazelcast,
57              final HazelcastSemaphoreProvider hazelcastSemaphoreProvider) {
58          this.hazelcastInstance = requireNonNull(hazelcastInstance);
59          this.manageHazelcast = manageHazelcast;
60          this.hazelcastSemaphoreProvider = requireNonNull(hazelcastSemaphoreProvider);
61          this.semaphores = new ConcurrentHashMap<>();
62      }
63  
64      /**
65       * Guard used by the default constructors of subclasses before they create a Hazelcast member or client via
66       * standard Hazelcast configuration discovery: refuses to proceed when no explicit operator-provided
67       * configuration is discoverable. Without one, Hazelcast falls back to its built-in defaults (default cluster
68       * name, auto-discovery join, no authentication or TLS in open-source Hazelcast), which lets any peer able to
69       * reach the network segment join the cluster and release the semaphore permits that guard local repository
70       * writes.
71       *
72       * @param configSystemProperty the Hazelcast system property naming an explicit configuration file
73       * @param classLoader the class loader to probe for configuration resources
74       * @param configResourceNames configuration file names probed on the classpath and in the working directory
75       * @throws IllegalStateException if no explicit configuration is discoverable
76       */
77      static void requireExplicitHazelcastConfig(
78              final String configSystemProperty, final ClassLoader classLoader, final String... configResourceNames) {
79          if (System.getProperty(configSystemProperty) != null) {
80              return;
81          }
82          for (String configResourceName : configResourceNames) {
83              if (classLoader.getResource(configResourceName) != null
84                      || Files.isRegularFile(Paths.get(configResourceName))) {
85                  return;
86              }
87          }
88          throw new IllegalStateException(
89                  "Refusing to create a Hazelcast instance from built-in defaults (default cluster name,"
90                          + " auto-discovery join, no authentication): distributed lock state guarding local"
91                          + " repository writes would be modifiable by unauthenticated network peers. Provide an"
92                          + " explicit Hazelcast configuration that secures or isolates the cluster, via the '"
93                          + configSystemProperty + "' system property or one of "
94                          + String.join(", ", configResourceNames)
95                          + " on the classpath or in the working directory.");
96      }
97  
98      @Override
99      protected AdaptedSemaphoreNamedLock createLock(final NamedLockKey key) {
100         ISemaphore semaphore = semaphores.computeIfAbsent(
101                 key, k -> hazelcastSemaphoreProvider.acquireSemaphore(hazelcastInstance, key));
102         return new AdaptedSemaphoreNamedLock(key, this, new HazelcastSemaphore(semaphore));
103     }
104 
105     @Override
106     protected void destroyLock(final NamedLock namedLock) {
107         if (namedLock instanceof AdaptedSemaphoreNamedLock) {
108             final NamedLockKey key = namedLock.key();
109             hazelcastSemaphoreProvider.releaseSemaphore(hazelcastInstance, key, semaphores.remove(key));
110         }
111     }
112 
113     @Override
114     protected void doShutdown() {
115         if (manageHazelcast) {
116             hazelcastInstance.shutdown();
117         }
118     }
119 
120     private static final class HazelcastSemaphore implements AdaptedSemaphore {
121         private final ISemaphore semaphore;
122 
123         private HazelcastSemaphore(final ISemaphore semaphore) {
124             this.semaphore = semaphore;
125         }
126 
127         @Override
128         public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
129             return semaphore.tryAcquire(perms, time, unit);
130         }
131 
132         @Override
133         public void release(final int perms) {
134             semaphore.release(perms);
135         }
136     }
137 }