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.support.AdaptedSemaphoreNamedLock;
028import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock.AdaptedSemaphore;
029import org.eclipse.aether.named.support.NamedLockFactorySupport;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 * Factory of {@link AdaptedSemaphoreNamedLock} instances, using adapted Hazelcast {@link ISemaphore}. It delegates
035 * most the work to {@link HazelcastSemaphoreProvider} and this class just adapts the returned semaphore to named lock
036 * and caches {@link ISemaphore} instances, as recommended by Hazelcast.
037 */
038public class HazelcastSemaphoreNamedLockFactory extends NamedLockFactorySupport {
039    protected final HazelcastInstance hazelcastInstance;
040
041    protected final boolean manageHazelcast;
042
043    private final HazelcastSemaphoreProvider hazelcastSemaphoreProvider;
044
045    private final ConcurrentMap<String, ISemaphore> semaphores;
046
047    public HazelcastSemaphoreNamedLockFactory(
048            final HazelcastInstance hazelcastInstance,
049            final boolean manageHazelcast,
050            final HazelcastSemaphoreProvider hazelcastSemaphoreProvider) {
051        this.hazelcastInstance = requireNonNull(hazelcastInstance);
052        this.manageHazelcast = manageHazelcast;
053        this.hazelcastSemaphoreProvider = requireNonNull(hazelcastSemaphoreProvider);
054        this.semaphores = new ConcurrentHashMap<>();
055    }
056
057    @Override
058    protected AdaptedSemaphoreNamedLock createLock(final String name) {
059        ISemaphore semaphore = semaphores.computeIfAbsent(
060                name, k -> hazelcastSemaphoreProvider.acquireSemaphore(hazelcastInstance, name));
061        return new AdaptedSemaphoreNamedLock(name, this, new HazelcastSemaphore(semaphore));
062    }
063
064    @Override
065    protected void destroyLock(final String name) {
066        hazelcastSemaphoreProvider.releaseSemaphore(hazelcastInstance, name, semaphores.remove(name));
067    }
068
069    @Override
070    public void shutdown() {
071        if (manageHazelcast) {
072            hazelcastInstance.shutdown();
073        }
074    }
075
076    private static final class HazelcastSemaphore implements AdaptedSemaphore {
077        private final ISemaphore semaphore;
078
079        private HazelcastSemaphore(final ISemaphore semaphore) {
080            this.semaphore = semaphore;
081        }
082
083        @Override
084        public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
085            return semaphore.tryAcquire(perms, time, unit);
086        }
087
088        @Override
089        public void release(final int perms) {
090            semaphore.release(perms);
091        }
092    }
093}