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;
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;
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
43 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicLockState.class);
44 LockStatus lockStatus;
45 boolean needToSyncQuorum = false;
47 long leasePeriod = Long.MAX_VALUE, leaseStartTime = -1;
49 private String errorMessage = null;
51 public MusicLockState(String errorMessage) {
52 this.errorMessage = errorMessage;
55 public MusicLockState(LockStatus lockStatus, String lockHolder) {
56 this.lockStatus = lockStatus;
57 this.lockHolder = lockHolder;
60 public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
61 this.lockStatus = lockStatus;
62 this.lockHolder = lockHolder;
63 this.needToSyncQuorum = needToSyncQuorum;
67 public long getLeasePeriod() {
71 public boolean isNeedToSyncQuorum() {
72 return needToSyncQuorum;
77 public void setLeasePeriod(long leasePeriod) {
78 this.leasePeriod = leasePeriod;
82 public long getLeaseStartTime() {
83 return leaseStartTime;
87 public void setLeaseStartTime(long leaseStartTime) {
88 this.leaseStartTime = leaseStartTime;
93 public LockStatus getLockStatus() {
97 public void setLockStatus(LockStatus lockStatus) {
98 this.lockStatus = lockStatus;
101 public String getLockHolder() {
105 public void setLockHolder(String lockHolder) {
106 this.lockHolder = lockHolder;
109 public String getErrorMessage() {
113 public byte[] serialize() {
114 ByteArrayOutputStream bos = new ByteArrayOutputStream();
115 ObjectOutput out = null;
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);
122 return bos.toByteArray();
125 public static MusicLockState deSerialize(byte[] data) {
126 ByteArrayInputStream bis = new ByteArrayInputStream(data);
128 ObjectInput in = null;
130 in = new ObjectInputStream(bis);
132 } catch (ClassNotFoundException | IOException e) {
133 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
135 return (MusicLockState) o;