Adding tools module to apex-pdp
[policy/apex-pdp.git] / plugins / plugins-context / context-locking / 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
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     // The Lock ID
41     private final String lockId;
42
43     // The mutex used for Curator locking
44     private final InterProcessMutex lockMutex;
45
46     /**
47      * Create the lock Facade.
48      *
49      * @param lockMutex The lock mutex behind the facade
50      * @param lockId The ID of the lock
51      */
52     public CuratorLockFacade(final InterProcessMutex lockMutex, final String lockId) {
53         this.lockId = lockId;
54         this.lockMutex = lockMutex;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see java.util.concurrent.locks.Lock#lock()
61      */
62     @Override
63     public void lock() {
64         try {
65             lockMutex.acquire();
66         } catch (final Exception e) {
67             LOGGER.warn("failed to acquire lock for \"" + lockId, e);
68         }
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see java.util.concurrent.locks.Lock#lockInterruptibly()
75      */
76     @Override
77     public void lockInterruptibly() throws InterruptedException {
78         LOGGER.warn("lockInterruptibly() not supported for \"" + lockId);
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see java.util.concurrent.locks.Lock#tryLock()
85      */
86     @Override
87     public boolean tryLock() {
88         try {
89             lockMutex.acquire();
90             return true;
91         } catch (final Exception e) {
92             LOGGER.warn("failed to acquire lock for \"" + lockId, e);
93             return false;
94         }
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see java.util.concurrent.locks.Lock#tryLock(long, java.util.concurrent.TimeUnit)
101      */
102     @Override
103     public boolean tryLock(final long time, final TimeUnit unit) throws InterruptedException {
104         try {
105             lockMutex.acquire(time, unit);
106             return true;
107         } catch (final Exception e) {
108             LOGGER.warn("failed to acquire lock for \"" + lockId, e);
109             return false;
110         }
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see java.util.concurrent.locks.Lock#unlock()
117      */
118     @Override
119     public void unlock() {
120         try {
121             lockMutex.release();
122         } catch (final Exception e) {
123             LOGGER.warn("failed to release lock for \"" + lockId, e);
124         }
125     }
126
127     /*
128      * (non-Javadoc)
129      *
130      * @see java.util.concurrent.locks.Lock#newCondition()
131      */
132     @Override
133     public Condition newCondition() {
134         LOGGER.warn("newCondition() not supported for \"" + lockId);
135         return null;
136     }
137 }