851e3172065dd71543c93b491d1bc334db3fe71b
[appc.git] /
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.pessimistic;
24
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.locks.Lock;
31 import java.util.concurrent.locks.ReentrantLock;
32
33 import org.openecomp.appc.lockmanager.api.LockRuntimeException;
34 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
35 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
36 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.LockRecord;
37 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
38
39 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
40
41         private final Map<String, LockRecord> locks = new HashMap<>();
42         private final Lock lock = new ReentrantLock();
43         private boolean useReal;
44         private Synchronizer synchronizer;
45
46         MySqlLockManagerMock(boolean useReal) {
47                 this.useReal = useReal;
48         }
49
50         @Override
51         public void setSynchronizer(Synchronizer synchronizer) {
52                 this.synchronizer = synchronizer;
53         }
54
55         @Override
56         protected Connection openDbConnection() {
57                 if(useReal) {
58                         return super.openDbConnection();
59                 }
60                 return null;
61         }
62
63         @Override
64         protected void closeDbConnection(Connection connection) {
65                 if(useReal) {
66                         super.closeDbConnection(connection);
67                 }
68         }
69
70         @Override
71         protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
72                 LockRecord res;
73                 if(useReal) {
74                         res = super.loadLockRecord(connection, resource);
75                 } else {
76                         res = locks.get(resource);
77                 }
78                 if(synchronizer != null) {
79                         synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
80                 }
81                 return res;
82         }
83
84         @Override
85         protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
86                 if(synchronizer != null) {
87                         synchronizer.preAddLockRecord(resource, owner);
88                 }
89                 try {
90                         if(useReal) {
91                                 super.addLockRecord(connection, resource, owner, timeout);
92                                 return;
93                         }
94                         LockRecord lockRecord = new LockRecord(resource);
95                         lockRecord.setOwner(owner);
96                         lockRecord.setUpdated(System.currentTimeMillis());
97                         lockRecord.setTimeout(timeout);
98                         locks.put(resource, lockRecord);
99                 } finally {
100                         if(synchronizer != null) {
101                                 synchronizer.postAddLockRecord(resource, owner);
102                         }
103                 }
104         }
105
106         @Override
107         protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
108                 if(synchronizer != null) {
109                         synchronizer.preUpdateLockRecord(resource, owner);
110                 }
111                 try {
112                         if(useReal) {
113                                 super.updateLockRecord(connection, resource, owner, timeout);
114                                 return;
115                         }
116                         LockRecord lockRecord = loadLockRecord(connection, resource);
117                         lockRecord.setOwner(owner);
118                         lockRecord.setUpdated(System.currentTimeMillis());
119                         lockRecord.setTimeout(timeout);
120                         locks.put(resource, lockRecord);
121                 } finally {
122                         if(synchronizer != null) {
123                                 synchronizer.postUpdateLockRecord(resource, owner);
124                         }
125                 }
126         }
127
128         @Override
129         protected void enterCriticalSection(Connection connection, String resource) {
130                 if(useReal) {
131                         super.enterCriticalSection(connection, resource);
132                         return;
133                 }
134                 try {
135                         if(!lock.tryLock(criticalSectionWaitTimeoutSecs, TimeUnit.SECONDS)) {
136                 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
137                         }
138                 } catch(InterruptedException e) {
139                         throw new LockRuntimeException("Cannot obtain critical section lock.", e);
140                 }
141         }
142
143         @Override
144         protected void leaveCriticalSection(Connection connection, String resource) {
145                 if(useReal) {
146                         super.leaveCriticalSection(connection, resource);
147                         return;
148                 }
149                 lock.unlock();
150         }
151 }