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 */ 040public class HazelcastSemaphoreNamedLockFactory extends NamedLockFactorySupport { 041 protected final HazelcastInstance hazelcastInstance; 042 043 protected final boolean manageHazelcast; 044 045 private final HazelcastSemaphoreProvider hazelcastSemaphoreProvider; 046 047 private final ConcurrentMap<NamedLockKey, ISemaphore> semaphores; 048 049 public HazelcastSemaphoreNamedLockFactory( 050 final HazelcastInstance hazelcastInstance, 051 final boolean manageHazelcast, 052 final HazelcastSemaphoreProvider hazelcastSemaphoreProvider) { 053 this.hazelcastInstance = requireNonNull(hazelcastInstance); 054 this.manageHazelcast = manageHazelcast; 055 this.hazelcastSemaphoreProvider = requireNonNull(hazelcastSemaphoreProvider); 056 this.semaphores = new ConcurrentHashMap<>(); 057 } 058 059 @Override 060 protected AdaptedSemaphoreNamedLock createLock(final NamedLockKey key) { 061 ISemaphore semaphore = semaphores.computeIfAbsent( 062 key, k -> hazelcastSemaphoreProvider.acquireSemaphore(hazelcastInstance, key)); 063 return new AdaptedSemaphoreNamedLock(key, this, new HazelcastSemaphore(semaphore)); 064 } 065 066 @Override 067 protected void destroyLock(final NamedLock namedLock) { 068 if (namedLock instanceof AdaptedSemaphoreNamedLock) { 069 final NamedLockKey key = namedLock.key(); 070 hazelcastSemaphoreProvider.releaseSemaphore(hazelcastInstance, key, semaphores.remove(key)); 071 } 072 } 073 074 @Override 075 protected void doShutdown() { 076 if (manageHazelcast) { 077 hazelcastInstance.shutdown(); 078 } 079 } 080 081 private static final class HazelcastSemaphore implements AdaptedSemaphore { 082 private final ISemaphore semaphore; 083 084 private HazelcastSemaphore(final ISemaphore semaphore) { 085 this.semaphore = semaphore; 086 } 087 088 @Override 089 public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException { 090 return semaphore.tryAcquire(perms, time, unit); 091 } 092 093 @Override 094 public void release(final int perms) { 095 semaphore.release(perms); 096 } 097 } 098}