001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.named.hazelcast; 020 021import java.util.concurrent.ConcurrentHashMap; 022import java.util.concurrent.ConcurrentMap; 023import java.util.concurrent.TimeUnit; 024 025import com.hazelcast.core.HazelcastInstance; 026import com.hazelcast.cp.ISemaphore; 027import org.eclipse.aether.named.NamedLock; 028import org.eclipse.aether.named.NamedLockKey; 029import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock; 030import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore; 031import org.eclipse.aether.named.support.NamedLockFactorySupport; 032 033import static java.util.Objects.requireNonNull; 034 035/** 036 * Factory of {@link AdaptedSemaphoreNamedLock} instances, using adapted Hazelcast {@link ISemaphore}. It delegates 037 * most the work to {@link HazelcastSemaphoreProvider} and this class just adapts the returned semaphore to named lock 038 * and caches {@link ISemaphore} instances, as recommended by Hazelcast. 039 * 040 * @deprecated Hazelcast support will be dropped. 041 */ 042@Deprecated 043public class HazelcastSemaphoreNamedLockFactory extends NamedLockFactorySupport { 044 protected final HazelcastInstance hazelcastInstance; 045 046 protected final boolean manageHazelcast; 047 048 private final HazelcastSemaphoreProvider hazelcastSemaphoreProvider; 049 050 private final ConcurrentMap<NamedLockKey, ISemaphore> semaphores; 051 052 public HazelcastSemaphoreNamedLockFactory( 053 final HazelcastInstance hazelcastInstance, 054 final boolean manageHazelcast, 055 final HazelcastSemaphoreProvider hazelcastSemaphoreProvider) { 056 this.hazelcastInstance = requireNonNull(hazelcastInstance); 057 this.manageHazelcast = manageHazelcast; 058 this.hazelcastSemaphoreProvider = requireNonNull(hazelcastSemaphoreProvider); 059 this.semaphores = new ConcurrentHashMap<>(); 060 } 061 062 @Override 063 protected AdaptedSemaphoreNamedLock createLock(final NamedLockKey key) { 064 ISemaphore semaphore = semaphores.computeIfAbsent( 065 key, k -> hazelcastSemaphoreProvider.acquireSemaphore(hazelcastInstance, key)); 066 return new AdaptedSemaphoreNamedLock(key, this, new HazelcastSemaphore(semaphore)); 067 } 068 069 @Override 070 protected void destroyLock(final NamedLock namedLock) { 071 if (namedLock instanceof AdaptedSemaphoreNamedLock) { 072 final NamedLockKey key = namedLock.key(); 073 hazelcastSemaphoreProvider.releaseSemaphore(hazelcastInstance, key, semaphores.remove(key)); 074 } 075 } 076 077 @Override 078 protected void doShutdown() { 079 if (manageHazelcast) { 080 hazelcastInstance.shutdown(); 081 } 082 } 083 084 private static final class HazelcastSemaphore implements AdaptedSemaphore { 085 private final ISemaphore semaphore; 086 087 private HazelcastSemaphore(final ISemaphore semaphore) { 088 this.semaphore = semaphore; 089 } 090 091 @Override 092 public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException { 093 return semaphore.tryAcquire(perms, time, unit); 094 } 095 096 @Override 097 public void release(final int perms) { 098 semaphore.release(perms); 099 } 100 } 101}