Changes for checkstyle 8.32
[policy/apex-pdp.git] / plugins / plugins-context / plugins-context-locking / plugins-context-locking-curator / src / main / java / org / onap / policy / apex / plugins / context / locking / curator / CuratorLockFacade.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.locking.curator;
22
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.locks.Condition;
25 import java.util.concurrent.locks.Lock;
26 import org.apache.curator.framework.recipes.locks.InterProcessMutex;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * This class provides a facade over the {@link Lock} interface for Curator locks.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public class CuratorLockFacade implements Lock {
36     // Logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(CuratorLockFacade.class);
38
39     // Recurring string constants
40     private static final String FAILED_TO_ACQUIRE_LOCK_TAG = "failed to acquire lock for \"{}\"";
41
42     // The Lock ID
43     private final String lockId;
44
45     // The mutex used for Curator locking
46     private final InterProcessMutex lockMutex;
47
48     /**
49      * Create the lock Facade.
50      *
51      * @param lockMutex The lock mutex behind the facade
52      * @param lockId The ID of the lock
53      */
54     public CuratorLockFacade(final InterProcessMutex lockMutex, final String lockId) {
55         this.lockId = lockId;
56         this.lockMutex = lockMutex;
57     }
58
59     /**
60      * {@inheritDoc}.
61      */
62     @Override
63     public void lock() {
64         try {
65             lockMutex.acquire();
66         } catch (final Exception e) {
67             LOGGER.warn(FAILED_TO_ACQUIRE_LOCK_TAG, lockId, e);
68         }
69     }
70
71     /**
72      * {@inheritDoc}.
73      */
74     @Override
75     public void lockInterruptibly() throws InterruptedException {
76         LOGGER.warn("lockInterruptibly() not supported for \"{}\"", lockId);
77     }
78
79     /**
80      * {@inheritDoc}.
81      */
82     @Override
83     public boolean tryLock() {
84         try {
85             lockMutex.acquire();
86             return true;
87         } catch (final Exception e) {
88             LOGGER.warn("failed to acquire lock for \"" + lockId, e);
89             return false;
90         }
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public boolean tryLock(final long time, final TimeUnit unit) throws InterruptedException {
98         try {
99             lockMutex.acquire(time, unit);
100             return true;
101         } catch (final Exception e) {
102             LOGGER.warn("failed to acquire lock for \"" + lockId, e);
103             return false;
104         }
105     }
106
107     /**
108      * {@inheritDoc}.
109      */
110     @Override
111     public void unlock() {
112         try {
113             lockMutex.release();
114         } catch (final Exception e) {
115             LOGGER.warn("failed to release lock for \"" + lockId, e);
116         }
117     }
118
119     /**
120      * {@inheritDoc}.
121      */
122     @Override
123     public Condition newCondition() {
124         LOGGER.warn("newCondition() not supported for \"{} \"", lockId);
125         return null;
126     }
127 }