2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.adaptors.lock.comp;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.List;
30 import org.onap.ccsdk.sli.adaptors.lock.dao.ResourceLockDao;
31 import org.onap.ccsdk.sli.adaptors.lock.data.ResourceLock;
33 public class LockHelperImpl implements LockHelper {
35 private ResourceLockDao resourceLockDao;
36 private int retryCount = 10;
37 private int lockWait = 5; // Seconds
40 public void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */) {
41 lock(Collections.singleton(resourceName), lockRequester, lockTimeout);
45 public void unlock(String resourceName, boolean force) {
46 unlock(Collections.singleton(resourceName), force);
50 public void lock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */) {
51 for (int i = 0; true; i++) {
53 tryLock(resourceNameList, lockRequester, lockTimeout);
55 } catch (ResourceLockedException e) {
59 Thread.sleep(lockWait * 1000);
60 } catch (InterruptedException ex) {
67 public void unlock(Collection<String> lockNames, boolean force) {
68 if (lockNames == null || lockNames.size() == 0)
71 resourceLockDao.lockTable();
74 for (String name : lockNames) {
75 ResourceLock l = resourceLockDao.getByResourceName(name);
77 if (force || l.lockCount == 1)
78 resourceLockDao.delete(l.id);
80 resourceLockDao.decrementLockCount(l.id);
83 resourceLockDao.unlockTable();
87 public void tryLock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */) {
88 if (resourceNameList == null || resourceNameList.size() == 0)
91 lockRequester = generateLockRequester(lockRequester, 100);
93 resourceLockDao.lockTable();
96 // First check if all requested records are available to lock
98 Date now = new Date();
100 List<ResourceLock> dbLockList = new ArrayList<ResourceLock>();
101 List<String> insertLockNameList = new ArrayList<String>();
102 for (String name : resourceNameList) {
103 ResourceLock l = resourceLockDao.getByResourceName(name);
106 l == null || now.getTime() > l.expirationTime.getTime() || lockRequester != null &&
107 lockRequester.equals(l.lockHolder) || l.lockCount <= 0;
109 throw new ResourceLockedException(l.resourceName, l.lockHolder, lockRequester);
114 insertLockNameList.add(name);
117 // Update the lock info in DB
118 for (ResourceLock l : dbLockList)
119 resourceLockDao.update(l.id, now, new Date(now.getTime() + lockTimeout * 1000), l.lockCount + 1);
121 // Insert records for those that are not yet there
122 for (String lockName : insertLockNameList) {
123 ResourceLock l = new ResourceLock();
124 l.resourceName = lockName;
125 l.lockHolder = lockRequester;
127 l.expirationTime = new Date(now.getTime() + lockTimeout * 1000);
129 resourceLockDao.add(l);
132 resourceLockDao.unlockTable();
136 private static String generateLockRequester(String name, int maxLength) {
139 int l1 = name.length();
140 String tname = Thread.currentThread().getName();
141 int l2 = tname.length();
142 if (l1 + l2 + 1 > maxLength) {
143 int maxl1 = maxLength / 2;
145 name = name.substring(0, maxl1);
148 int maxl2 = maxLength - l1 - 1;
150 tname = tname.substring(0, 6) + "..." + tname.substring(l2 - maxl2 + 9);
152 return tname + '-' + name;
155 public void setResourceLockDao(ResourceLockDao resourceLockDao) {
156 this.resourceLockDao = resourceLockDao;
159 public void setRetryCount(int retryCount) {
160 this.retryCount = retryCount;
163 public void setLockWait(int lockWait /* Seconds */) {
164 this.lockWait = lockWait;