Updating licenses in all files
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / test / java / org / openecomp / appc / lockmanager / impl / sql / optimistic / MySqlLockManagerMock.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.lockmanager.impl.sql.optimistic;
24
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29
30 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
31 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
32 import org.openecomp.appc.lockmanager.impl.sql.optimistic.LockRecord;
33 import org.openecomp.appc.lockmanager.impl.sql.optimistic.MySqlLockManager;
34
35 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
36
37         private final ConcurrentMap<String, LockRecord> locks = new ConcurrentHashMap<>();
38         private boolean useReal;
39         private Synchronizer synchronizer;
40
41         MySqlLockManagerMock(boolean useReal) {
42                 this.useReal = useReal;
43         }
44
45         @Override
46         public void setSynchronizer(Synchronizer synchronizer) {
47                 this.synchronizer = synchronizer;
48         }
49
50         @Override
51         protected Connection openDbConnection() {
52                 if(useReal) {
53                         return super.openDbConnection();
54                 }
55                 return null;
56         }
57
58         @Override
59         protected void closeDbConnection(Connection connection) {
60                 if(useReal) {
61                         super.closeDbConnection(connection);
62                 }
63         }
64
65         @Override
66         protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
67                 LockRecord res;
68                 if(useReal) {
69                         res = super.loadLockRecord(connection, resource);
70                 } else {
71                         res = locks.get(resource);
72                 }
73                 if(synchronizer != null) {
74                         synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
75                 }
76                 return res;
77         }
78
79         @Override
80         protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
81                 if(synchronizer != null) {
82                         synchronizer.preAddLockRecord(resource, owner);
83                 }
84                 try {
85                         if(useReal) {
86                                 super.addLockRecord(connection, resource, owner, timeout);
87                                 return;
88                         }
89                         LockRecord lockRecord = new LockRecord(resource);
90                         lockRecord.setOwner(owner);
91                         lockRecord.setUpdated(System.currentTimeMillis());
92                         lockRecord.setTimeout(timeout);
93                         lockRecord.setVer(1);
94                         LockRecord prevLockRecord = locks.putIfAbsent(resource, lockRecord);
95                         if(prevLockRecord != null) {
96                                 // simulate unique constraint violation
97                                 throw new SQLException("Duplicate PK exception", "23000", 1062);
98                         }
99                 } finally {
100                         if(synchronizer != null) {
101                                 synchronizer.postAddLockRecord(resource, owner);
102                         }
103                 }
104         }
105
106         @Override
107         protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
108                 if(synchronizer != null) {
109                         synchronizer.preUpdateLockRecord(resource, owner);
110                 }
111                 try {
112                         if(useReal) {
113                                 return super.updateLockRecord(connection, resource, owner, timeout, ver);
114                         }
115                         LockRecord lockRecord = loadLockRecord(connection, resource);
116                         synchronized(lockRecord) {
117                                 // should be atomic operation
118                                 if(ver != lockRecord.getVer()) {
119                                         return false;
120                                 }
121                                 lockRecord.setOwner(owner);
122                                 lockRecord.setUpdated(System.currentTimeMillis());
123                                 lockRecord.setTimeout(timeout);
124                                 lockRecord.setVer(ver + 1);
125                         }
126                         return true;
127                 } finally {
128                         if(synchronizer != null) {
129                                 synchronizer.postUpdateLockRecord(resource, owner);
130                         }
131                 }
132         }
133 }