2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.lockingservice;
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;
34 // the state variable that will be stored in zookeeper, capturing the transitions of
35 public class MusicLockState implements Serializable {
36 public enum LockStatus {
37 UNLOCKED, BEING_LOCKED, LOCKED
38 };// captures the state of the lock
40 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicLockState.class);
41 LockStatus lockStatus;
42 boolean needToSyncQuorum = false;
44 long leasePeriod = Long.MAX_VALUE, leaseStartTime = -1;
46 public MusicLockState(LockStatus lockStatus, String lockHolder) {
47 this.lockStatus = lockStatus;
48 this.lockHolder = lockHolder;
51 public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
52 this.lockStatus = lockStatus;
53 this.lockHolder = lockHolder;
54 this.needToSyncQuorum = needToSyncQuorum;
58 public long getLeasePeriod() {
62 public boolean isNeedToSyncQuorum() {
63 return needToSyncQuorum;
68 public void setLeasePeriod(long leasePeriod) {
69 this.leasePeriod = leasePeriod;
73 public long getLeaseStartTime() {
74 return leaseStartTime;
78 public void setLeaseStartTime(long leaseStartTime) {
79 this.leaseStartTime = leaseStartTime;
84 public LockStatus getLockStatus() {
88 public void setLockStatus(LockStatus lockStatus) {
89 this.lockStatus = lockStatus;
92 public String getLockHolder() {
96 public void setLockHolder(String lockHolder) {
97 this.lockHolder = lockHolder;
100 public byte[] serialize() {
101 ByteArrayOutputStream bos = new ByteArrayOutputStream();
102 ObjectOutput out = null;
104 out = new ObjectOutputStream(bos);
105 out.writeObject(this);
106 } catch (IOException e) {
107 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
109 return bos.toByteArray();
112 public static MusicLockState deSerialize(byte[] data) {
113 ByteArrayInputStream bis = new ByteArrayInputStream(data);
115 ObjectInput in = null;
117 in = new ObjectInputStream(bis);
119 } catch (ClassNotFoundException | IOException e) {
120 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
122 return (MusicLockState) o;