001/* 002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.ecm.platform.importer.mqueues.message; 020 021import java.io.IOException; 022import java.io.ObjectInput; 023import java.io.ObjectOutput; 024import java.util.Arrays; 025import java.util.Objects; 026 027/** 028 * Simple message with an Id and data payload. 029 * 030 * @since 9.1 031 */ 032public class IdMessage implements Message { 033 public static IdMessage POISON_PILL = new IdMessage("_POISON_PILL_", null, true, false); 034 private String id; 035 private byte[] data; 036 private boolean poisonPill = false; 037 private boolean forceBatch = false; 038 039 protected IdMessage(String id, byte[] data, boolean poisonPill, boolean forceBatch) { 040 this.id = Objects.requireNonNull(id); 041 this.data = data; 042 this.poisonPill = poisonPill; 043 this.forceBatch = forceBatch; 044 } 045 046 static public IdMessage of(String id, byte[] data) { 047 return new IdMessage(id, data, false, false); 048 } 049 050 static public IdMessage of(String id) { 051 return new IdMessage(id, null, false, false); 052 } 053 054 /** 055 * A message that force the batch. 056 */ 057 static public IdMessage ofForceBatch(String id, byte[] data) { 058 return new IdMessage(id, data, false, true); 059 } 060 061 static public IdMessage ofForceBatch(String id) { 062 return new IdMessage(id, null, false, true); 063 } 064 065 public byte[] getData() { 066 return data; 067 } 068 069 @Override 070 public String getId() { 071 return id; 072 } 073 074 @Override 075 public boolean poisonPill() { 076 return poisonPill; 077 } 078 079 @Override 080 public boolean forceBatch() { 081 return forceBatch; 082 } 083 084 @Override 085 public void writeExternal(ObjectOutput out) throws IOException { 086 out.writeObject(id); 087 out.writeBoolean(poisonPill); 088 out.writeBoolean(forceBatch); 089 if (data == null || data.length == 0) { 090 out.writeInt(0); 091 } else { 092 out.writeInt(data.length); 093 out.write(data); 094 } 095 } 096 097 @Override 098 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 099 this.id = (String) in.readObject(); 100 this.poisonPill = in.readBoolean(); 101 this.forceBatch = in.readBoolean(); 102 int dataLength = in.readInt(); 103 if (dataLength == 0) { 104 this.data = null; 105 } else { 106 int read = in.read(this.data, 0, dataLength); 107 assert(read == dataLength); 108 } 109 } 110 111 @Override 112 public boolean equals(Object o) { 113 if (this == o) return true; 114 if (o == null || getClass() != o.getClass()) return false; 115 116 IdMessage idMessage = (IdMessage) o; 117 118 if (poisonPill != idMessage.poisonPill) return false; 119 if (forceBatch != idMessage.forceBatch) return false; 120 return id != null ? id.equals(idMessage.id) : idMessage.id == null; 121 122 } 123 124 @Override 125 public int hashCode() { 126 int result = id.hashCode(); 127 result = 31 * result + Arrays.hashCode(data); 128 result = 31 * result + (poisonPill ? 1 : 0); 129 result = 31 * result + (forceBatch ? 1 : 0); 130 return result; 131 } 132 133 @Override 134 public String toString() { 135 return String.format("IdMessage(%s, %d, %b, %b", id, (data != null) ? data.length : 0, poisonPill, forceBatch); 136 } 137}