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.ByteArrayOutputStream;
030    import java.io.IOException;
031    import java.io.Serializable;
032    import java.io.UnsupportedEncodingException;
033    import java.util.zip.DataFormatException;
034    
035    
036    /**
037     * This message is used by an LDAP server to communicate to the topology
038     * that the generation must be reset for the domain.
039     */
040    public class ResetGenerationId extends ReplicationMessage implements
041        Serializable
042    {
043      private static final long serialVersionUID = 7657049716115572226L;
044      private long generationId;
045    
046      /**
047       * Creates a new message.
048       * @param generationId The new reference value of the generationID.
049       */
050      public ResetGenerationId(long generationId)
051      {
052        this.generationId = generationId;
053      }
054    
055      /**
056       * Creates a new GenerationIdMessage from its encoded form.
057       *
058       * @param in The byte array containing the encoded form of the
059       *           WindowMessage.
060       * @throws DataFormatException If the byte array does not contain a valid
061       *                             encoded form of the WindowMessage.
062       */
063      public ResetGenerationId(byte[] in) throws DataFormatException
064      {
065        try
066        {
067          if (in[0] != MSG_TYPE_RESET_GENERATION_ID)
068            throw new
069            DataFormatException("input is not a valid GenerationId Message");
070    
071          int pos = 1;
072    
073          /* read the generationId */
074          int length = getNextLength(in, pos);
075          generationId = Long.valueOf(new String(in, pos, length,
076          "UTF-8"));
077          pos += length +1;
078        } catch (UnsupportedEncodingException e)
079        {
080          throw new DataFormatException("UTF-8 is not supported by this jvm.");
081        }
082    
083      }
084    
085      /**
086       * {@inheritDoc}
087       */
088      @Override
089      public byte[] getBytes()
090      {
091        try
092        {
093          ByteArrayOutputStream oStream = new ByteArrayOutputStream();
094    
095          /* Put the message type */
096          oStream.write(MSG_TYPE_RESET_GENERATION_ID);
097    
098          // Put the generationId
099          oStream.write(String.valueOf(generationId).getBytes("UTF-8"));
100          oStream.write(0);
101    
102          return oStream.toByteArray();
103        }
104        catch (IOException e)
105        {
106          // never happens
107          return null;
108        }
109      }
110    
111      /**
112       * Returns the generation Id set in this message.
113       * @return the value of the generation ID.
114       *
115       */
116      public long getGenerationId()
117      {
118        return this.generationId;
119      }
120    }