8697c374c1ab2a749d14337021d4895ca7d64ead
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / test / java / org / openecomp / appc / lockmanager / impl / sql / Synchronizer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.lockmanager.impl.sql;
26
27 public class Synchronizer {
28
29     private int participantNo;
30     private int participantCount;
31
32     public Synchronizer(int participantNo) {
33         this.participantNo = participantNo;
34     }
35
36     public int getParticipantCount() {
37         return participantCount;
38     }
39
40     public void postLoadLockRecord(String resource, String owner) {
41         synchronized(this) {
42             waitForAllParticipants(this, participantNo, ++participantCount);
43         }
44     }
45
46     public void preAddLockRecord(String resource, String owner) {
47     }
48
49     public void postAddLockRecord(String resource, String owner) {
50     }
51
52     public void preUpdateLockRecord(String resource, String owner) {
53     }
54
55     public void postUpdateLockRecord(String resource, String owner) {
56     }
57
58     public void releaseWait() {
59         synchronized(this) {
60             this.notifyAll();
61         }
62     }
63
64     protected void waitOn(Object obj, long timeout) {
65         try {
66             obj.wait(timeout);
67         } catch(InterruptedException e) {
68             throw new RuntimeException(e);
69         }
70     }
71
72     protected void waitOn(Object obj) {
73         waitOn(obj, 0);
74     }
75
76     protected void waitForAllParticipants(Object waitObj, int totalParticipantsNo, int currentParticipantsNo) {
77         if(totalParticipantsNo > currentParticipantsNo) {
78             waitOn(waitObj);
79         } else {
80             waitObj.notifyAll();
81         }
82     }
83 }