001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.replication.protocol;
028    
029    import java.io.Serializable;
030    import java.io.UnsupportedEncodingException;
031    import java.util.zip.DataFormatException;
032    
033    /**
034     * This message is part of the replication protocol.
035     * This message is sent by a server to one or several other servers after the
036     * last entry sent in the context of a total update and signals to the server
037     * that receives it that the export is now finished.
038     */
039    public class DoneMessage extends RoutableMessage implements
040        Serializable
041    {
042      private static final long serialVersionUID = 5216659571724730361L;
043    
044      /**
045       * Creates a message.
046       *
047       * @param sender The sender server of this message.
048       * @param destination The server or servers targetted by this message.
049       */
050      public DoneMessage(short sender, short destination)
051      {
052        super(sender, destination);
053      }
054    
055      /**
056       * Creates a new message by decoding the provided byte array.
057       * @param in A byte array containing the encoded information for the message,
058       * @throws DataFormatException If the in does not contain a properly,
059       *                             encoded message.
060       */
061      public DoneMessage(byte[] in) throws DataFormatException
062      {
063        super();
064        try
065        {
066          // First byte is the type
067          if (in[0] != MSG_TYPE_DONE)
068            throw new DataFormatException("input is not a valid DoneMessage");
069          int pos = 1;
070    
071          // sender
072          int length = getNextLength(in, pos);
073          String senderString = new String(in, pos, length, "UTF-8");
074          this.senderID = Short.valueOf(senderString);
075          pos += length +1;
076    
077          // destination
078          length = getNextLength(in, pos);
079          String destinationString = new String(in, pos, length, "UTF-8");
080          this.destination = Short.valueOf(destinationString);
081          pos += length +1;
082    
083        } catch (UnsupportedEncodingException e)
084        {
085          throw new DataFormatException("UTF-8 is not supported by this jvm.");
086        }
087      }
088    
089      /**
090       * {@inheritDoc}
091       */
092      @Override
093      public byte[] getBytes()
094      {
095        try
096        {
097          byte[] senderBytes = String.valueOf(senderID).getBytes("UTF-8");
098          byte[] destinationBytes = String.valueOf(destination).getBytes("UTF-8");
099    
100          int length = 1 + senderBytes.length + 1
101                         + destinationBytes.length + 1;
102    
103          byte[] resultByteArray = new byte[length];
104    
105          /* put the type of the operation */
106          resultByteArray[0] = MSG_TYPE_DONE;
107          int pos = 1;
108    
109          /* put the sender */
110          pos = addByteArray(senderBytes, resultByteArray, pos);
111    
112          /* put the destination */
113          pos = addByteArray(destinationBytes, resultByteArray, pos);
114    
115          return resultByteArray;
116        }
117        catch (UnsupportedEncodingException e)
118        {
119          return null;
120        }
121      }
122    }