1121N/A/*
1121N/A * CDDL HEADER START
1121N/A *
1121N/A * The contents of this file are subject to the terms of the
1121N/A * Common Development and Distribution License, Version 1.0 only
1121N/A * (the "License"). You may not use this file except in compliance
1121N/A * with the License.
1121N/A *
1121N/A * You can obtain a copy of the license at
1121N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
1121N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
1121N/A * See the License for the specific language governing permissions
1121N/A * and limitations under the License.
1121N/A *
1121N/A * When distributing Covered Code, include this CDDL HEADER in each
1121N/A * file and include the License file at
1121N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
1121N/A * add the following below this CDDL HEADER, with the fields enclosed
1121N/A * by brackets "[]" replaced with your own identifying information:
1121N/A * Portions Copyright [yyyy] [name of copyright owner]
1121N/A *
1121N/A * CDDL HEADER END
1121N/A *
1121N/A *
5086N/A * Copyright 2006-2010 Sun Microsystems, Inc.
6343N/A * Portions copyright 2013 ForgeRock AS.
1121N/A */
1182N/Apackage org.opends.server.replication.protocol;
2086N/Aimport org.opends.messages.Message;
1121N/A
2350N/Aimport static org.opends.server.loggers.debug.DebugLogger.debugEnabled;
2350N/Aimport static org.opends.server.loggers.debug.DebugLogger.getTracer;
5086N/Aimport static org.opends.server.util.StaticUtils.stackTraceToSingleLineString;
2350N/A
1121N/Aimport java.io.UnsupportedEncodingException;
1121N/Aimport java.util.zip.DataFormatException;
1121N/A
2350N/Aimport org.opends.server.loggers.debug.DebugTracer;
2350N/A
1121N/A/**
1183N/A * This message is part of the replication protocol.
1191N/A * This message is sent by a server or a replication server when an error
1121N/A * is detected in the context of a total update.
1121N/A */
3853N/Apublic class ErrorMsg extends RoutableMsg
1121N/A{
2350N/A // The tracer object for the debug logger
2350N/A private static final DebugTracer TRACER = getTracer();
2350N/A
2811N/A // Specifies the messageID built from the error that was detected
1121N/A private int msgID;
2086N/A
1121N/A // Specifies the complementary details about the error that was detected
2086N/A private Message details = null;
1121N/A
5086N/A // The time of creation of this message.
5086N/A // protocol version previous to V4
5086N/A private Long creationTime = System.currentTimeMillis();
5086N/A
1121N/A /**
3853N/A * Creates an ErrorMsg providing the destination server.
2350N/A *
1121N/A * @param sender The server ID of the server that send this message.
1121N/A * @param destination The destination server or servers of this message.
2350N/A * @param details The message containing the details of the error.
1121N/A */
4802N/A public ErrorMsg(int sender, int destination,
2086N/A Message details)
1121N/A {
1121N/A super(sender, destination);
2086N/A this.msgID = details.getDescriptor().getId();
1121N/A this.details = details;
5086N/A this.creationTime = System.currentTimeMillis();
2350N/A
2350N/A if (debugEnabled())
5086N/A TRACER.debugInfo(" Creating error message" + this.toString()
5086N/A + " " + stackTraceToSingleLineString(new Exception("trace")));
1121N/A }
1121N/A
1121N/A /**
3853N/A * Creates an ErrorMsg.
1121N/A *
4802N/A * @param i replication server id
1121N/A * @param details details of the error
1121N/A */
4802N/A public ErrorMsg(int i, Message details)
1121N/A {
4802N/A super(-2, i);
2086N/A this.msgID = details.getDescriptor().getId();
1121N/A this.details = details;
5086N/A this.creationTime = System.currentTimeMillis();
2350N/A
2350N/A if (debugEnabled())
2350N/A TRACER.debugInfo(this.toString());
1121N/A }
1121N/A
1121N/A /**
3853N/A * Creates a new ErrorMsg by decoding the provided byte array.
2350N/A *
2350N/A * @param in A byte array containing the encoded information for the Message
5086N/A * @param version The protocol version to use to decode the msg.
1121N/A * @throws DataFormatException If the in does not contain a properly
2350N/A * encoded message.
1121N/A */
5086N/A public ErrorMsg(byte[] in, short version)
5086N/A throws DataFormatException
1121N/A {
1121N/A super();
1121N/A try
1121N/A {
1121N/A /* first byte is the type */
1121N/A if (in[0] != MSG_TYPE_ERROR)
5086N/A throw new DataFormatException("input is not a valid " +
5086N/A this.getClass().getCanonicalName());
1121N/A int pos = 1;
1121N/A
1121N/A // sender
1121N/A int length = getNextLength(in, pos);
1121N/A String senderString = new String(in, pos, length, "UTF-8");
4802N/A senderID = Integer.valueOf(senderString);
1121N/A pos += length +1;
1121N/A
1121N/A // destination
1121N/A length = getNextLength(in, pos);
1121N/A String serverIdString = new String(in, pos, length, "UTF-8");
4802N/A destination = Integer.valueOf(serverIdString);
1121N/A pos += length +1;
1121N/A
1121N/A // MsgID
1121N/A length = getNextLength(in, pos);
1121N/A String msgIdString = new String(in, pos, length, "UTF-8");
1121N/A msgID = Integer.valueOf(msgIdString);
1121N/A pos += length +1;
1121N/A
1121N/A // Details
1121N/A length = getNextLength(in, pos);
2086N/A details = Message.raw(new String(in, pos, length, "UTF-8"));
1121N/A pos += length +1;
1121N/A
5086N/A if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4)
5086N/A {
5086N/A // Creation Time
5086N/A length = getNextLength(in, pos);
5086N/A String creationTimeString = new String(in, pos, length, "UTF-8");
5086N/A creationTime = Long.valueOf(creationTimeString);
5086N/A pos += length +1;
5086N/A }
1121N/A }
1121N/A catch (UnsupportedEncodingException e)
1121N/A {
1121N/A throw new DataFormatException("UTF-8 is not supported by this jvm.");
1121N/A }
1121N/A }
1121N/A
1121N/A /**
5086N/A * Get the details from this message.
1121N/A *
5086N/A * @return the details from this message.
1121N/A */
2086N/A public Message getDetails()
1121N/A {
1121N/A return details;
1121N/A }
1121N/A
1121N/A /**
5086N/A * Get the msgID from this message.
1121N/A *
5086N/A * @return the msgID from this message.
1121N/A */
1121N/A public int getMsgID()
1121N/A {
1121N/A return msgID;
1121N/A }
1121N/A
5086N/A // ============
5086N/A // Msg encoding
5086N/A // ============
5086N/A
1121N/A /**
1121N/A * {@inheritDoc}
1121N/A */
1121N/A @Override
5086N/A public byte[] getBytes(short version)
1121N/A {
1121N/A try {
1121N/A byte[] byteSender = String.valueOf(senderID).getBytes("UTF-8");
1121N/A byte[] byteDestination = String.valueOf(destination).getBytes("UTF-8");
1121N/A byte[] byteErrMsgId = String.valueOf(msgID).getBytes("UTF-8");
2086N/A byte[] byteDetails = details.toString().getBytes("UTF-8");
5086N/A byte[] byteCreationTime = null;
1121N/A
1121N/A int length = 1 + byteSender.length + 1
1121N/A + byteDestination.length + 1
1121N/A + byteErrMsgId.length + 1
1121N/A + byteDetails.length + 1;
1121N/A
5086N/A if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4)
5086N/A {
5086N/A byteCreationTime = creationTime.toString().getBytes("UTF-8");
5086N/A length += byteCreationTime.length + 1;
5086N/A }
5086N/A
1121N/A byte[] resultByteArray = new byte[length];
1121N/A
1121N/A // put the type of the operation
1121N/A resultByteArray[0] = MSG_TYPE_ERROR;
1121N/A int pos = 1;
1121N/A
1121N/A // sender
1121N/A pos = addByteArray(byteSender, resultByteArray, pos);
1121N/A
1121N/A // destination
1121N/A pos = addByteArray(byteDestination, resultByteArray, pos);
1121N/A
1121N/A // MsgId
1121N/A pos = addByteArray(byteErrMsgId, resultByteArray, pos);
1121N/A
1121N/A // details
1121N/A pos = addByteArray(byteDetails, resultByteArray, pos);
1121N/A
5086N/A if (version >= ProtocolVersion.REPLICATION_PROTOCOL_V4)
5086N/A {
5086N/A // creation time
5086N/A pos = addByteArray(byteCreationTime, resultByteArray, pos);
5086N/A }
5086N/A
1121N/A return resultByteArray;
1121N/A }
1121N/A catch (UnsupportedEncodingException e)
1121N/A {
1121N/A return null;
1121N/A }
1121N/A }
2350N/A
2350N/A /**
2350N/A * Returns a string representation of the message.
2350N/A *
2350N/A * @return the string representation of this message.
2350N/A */
2350N/A public String toString()
2350N/A {
2350N/A return "ErrorMessage=["+
2350N/A " sender=" + this.senderID +
2350N/A " destination=" + this.destination +
2350N/A " msgID=" + this.msgID +
5086N/A " details=" + this.details +
5086N/A " creationTime=" + this.creationTime + "]";
2350N/A }
5086N/A
5086N/A /**
5086N/A * Get the creation time of this message.
5086N/A * When several attempts of initialization are done sequentially, it helps
5086N/A * sorting the good ones, from the ones that relate to ended initialization
5086N/A * when they are received.
5086N/A *
5086N/A * @return the creation time of this message.
5086N/A */
5086N/A public Long getCreationTime()
5086N/A {
5086N/A return creationTime;
5086N/A }
5086N/A
5086N/A /**
5086N/A * Get the creation time of this message.
5086N/A * @param creationTime the creation time of this message.
5086N/A */
5086N/A public void setCreationTime(long creationTime)
5086N/A {
5086N/A this.creationTime = creationTime;
5086N/A }
5086N/A
1121N/A}