Docker update and POM fix
[music.git] / src / main / java / org / onap / music / lockingservice / MusicLockState.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22 package org.onap.music.lockingservice;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectInput;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutput;
30 import java.io.ObjectOutputStream;
31 import java.io.Serializable;
32 import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.eelf.logging.format.AppMessages;
34 import org.onap.music.eelf.logging.format.ErrorSeverity;
35 import org.onap.music.eelf.logging.format.ErrorTypes;
36
37 // the state variable that will be stored in zookeeper, capturing the transitions of
38 public class MusicLockState implements Serializable {
39     public enum LockStatus {
40         UNLOCKED, BEING_LOCKED, LOCKED
41     };// captures the state of the lock
42
43     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicLockState.class);
44     LockStatus lockStatus;
45     boolean needToSyncQuorum = false;
46     String lockHolder;
47     long leasePeriod = Long.MAX_VALUE, leaseStartTime = -1;
48     
49     private String errorMessage = null;
50     
51     public MusicLockState(String errorMessage) {
52         this.errorMessage = errorMessage;
53     }
54
55     public MusicLockState(LockStatus lockStatus, String lockHolder) {
56         this.lockStatus = lockStatus;
57         this.lockHolder = lockHolder;
58     }
59
60     public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
61         this.lockStatus = lockStatus;
62         this.lockHolder = lockHolder;
63         this.needToSyncQuorum = needToSyncQuorum;
64     }
65
66
67     public long getLeasePeriod() {
68         return leasePeriod;
69     }
70
71     public boolean isNeedToSyncQuorum() {
72         return needToSyncQuorum;
73     }
74
75
76
77     public void setLeasePeriod(long leasePeriod) {
78         this.leasePeriod = leasePeriod;
79     }
80
81
82     public long getLeaseStartTime() {
83         return leaseStartTime;
84     }
85
86
87     public void setLeaseStartTime(long leaseStartTime) {
88         this.leaseStartTime = leaseStartTime;
89     }
90
91
92
93     public LockStatus getLockStatus() {
94         return lockStatus;
95     }
96
97     public void setLockStatus(LockStatus lockStatus) {
98         this.lockStatus = lockStatus;
99     }
100
101     public String getLockHolder() {
102         return lockHolder;
103     }
104
105     public void setLockHolder(String lockHolder) {
106         this.lockHolder = lockHolder;
107     }
108
109     public String getErrorMessage() {
110                 return errorMessage;
111         }
112     
113     public byte[] serialize() {
114         ByteArrayOutputStream bos = new ByteArrayOutputStream();
115         ObjectOutput out = null;
116         try {
117             out = new ObjectOutputStream(bos);
118             out.writeObject(this);
119         } catch (IOException e) {
120                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
121         }
122         return bos.toByteArray();
123     }
124
125     public static MusicLockState deSerialize(byte[] data) {
126         ByteArrayInputStream bis = new ByteArrayInputStream(data);
127         Object o = null;
128         ObjectInput in = null;
129         try {
130             in = new ObjectInputStream(bis);
131             o = in.readObject();
132         } catch (ClassNotFoundException | IOException e) {
133                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
134         }
135         return (MusicLockState) o;
136     }
137 }