2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.locking.curator;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.locks.Condition;
25 import java.util.concurrent.locks.Lock;
27 import org.apache.curator.framework.recipes.locks.InterProcessMutex;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class provides a facade over the {@link Lock} interface for Curator locks.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
36 public class CuratorLockFacade implements Lock {
37 // Logger for this class
38 private static final XLogger LOGGER = XLoggerFactory.getXLogger(CuratorLockFacade.class);
40 // Recurring string constants
41 private static final String FAILED_TO_ACQUIRE_LOCK_TAG = "failed to acquire lock for \"{}\"";
44 private final String lockId;
46 // The mutex used for Curator locking
47 private final InterProcessMutex lockMutex;
50 * Create the lock Facade.
52 * @param lockMutex The lock mutex behind the facade
53 * @param lockId The ID of the lock
55 public CuratorLockFacade(final InterProcessMutex lockMutex, final String lockId) {
57 this.lockMutex = lockMutex;
67 } catch (final Exception e) {
68 LOGGER.warn(FAILED_TO_ACQUIRE_LOCK_TAG, lockId, e);
76 public void lockInterruptibly() throws InterruptedException {
77 LOGGER.warn("lockInterruptibly() not supported for \"{}\"", lockId);
84 public boolean tryLock() {
88 } catch (final Exception e) {
89 LOGGER.warn("failed to acquire lock for \"" + lockId, e);
98 public boolean tryLock(final long time, final TimeUnit unit) throws InterruptedException {
100 lockMutex.acquire(time, unit);
102 } catch (final Exception e) {
103 LOGGER.warn("failed to acquire lock for \"" + lockId, e);
112 public void unlock() {
115 } catch (final Exception e) {
116 LOGGER.warn("failed to release lock for \"" + lockId, e);
124 public Condition newCondition() {
125 LOGGER.warn("newCondition() not supported for \"{} \"", lockId);