2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.lockmanager.impl.sql.pessimistic;
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.util.HashMap;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.locks.Lock;
33 import java.util.concurrent.locks.ReentrantLock;
35 import org.onap.appc.lockmanager.api.LockRuntimeException;
36 import org.onap.appc.lockmanager.impl.sql.Synchronizer;
37 import org.onap.appc.lockmanager.impl.sql.SynchronizerReceiver;
38 import org.onap.appc.lockmanager.impl.sql.pessimistic.LockRecord;
39 import org.onap.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
41 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
43 private final Map<String, LockRecord> locks = new HashMap<>();
44 private final Lock lock = new ReentrantLock();
45 private boolean useReal;
46 private Synchronizer synchronizer;
48 MySqlLockManagerMock(boolean useReal) {
49 this.useReal = useReal;
53 public void setSynchronizer(Synchronizer synchronizer) {
54 this.synchronizer = synchronizer;
58 protected Connection openDbConnection() {
60 return super.openDbConnection();
66 protected void closeDbConnection(Connection connection) {
68 super.closeDbConnection(connection);
73 protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
76 res = super.loadLockRecord(connection, resource);
78 res = locks.get(resource);
80 if(synchronizer != null) {
81 synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
87 protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
88 if(synchronizer != null) {
89 synchronizer.preAddLockRecord(resource, owner);
93 super.addLockRecord(connection, resource, owner, timeout);
96 LockRecord lockRecord = new LockRecord(resource);
97 lockRecord.setOwner(owner);
98 lockRecord.setUpdated(System.currentTimeMillis());
99 lockRecord.setTimeout(timeout);
100 locks.put(resource, lockRecord);
102 if(synchronizer != null) {
103 synchronizer.postAddLockRecord(resource, owner);
109 protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
110 if(synchronizer != null) {
111 synchronizer.preUpdateLockRecord(resource, owner);
115 super.updateLockRecord(connection, resource, owner, timeout);
118 LockRecord lockRecord = loadLockRecord(connection, resource);
119 lockRecord.setOwner(owner);
120 lockRecord.setUpdated(System.currentTimeMillis());
121 lockRecord.setTimeout(timeout);
122 locks.put(resource, lockRecord);
124 if(synchronizer != null) {
125 synchronizer.postUpdateLockRecord(resource, owner);
131 protected void enterCriticalSection(Connection connection, String resource) {
133 super.enterCriticalSection(connection, resource);
137 if(!lock.tryLock(criticalSectionWaitTimeoutSecs, TimeUnit.SECONDS)) {
138 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
140 } catch(InterruptedException e) {
141 throw new LockRuntimeException("Cannot obtain critical section lock.", e);
146 protected void leaveCriticalSection(Connection connection, String resource) {
148 super.leaveCriticalSection(connection, resource);