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