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    import org.opends.server.types.DN;
034    import org.opends.server.types.DirectoryException;
035    
036    /**
037     * This message is part of the replication protocol.
038     * This message is sent by a server to one or several servers as the
039     * first message of an export, before sending the entries.
040     */
041    public class InitializeTargetMessage extends RoutableMessage implements
042        Serializable
043    {
044      private static final long serialVersionUID = -2122460559739139735L;
045    
046      private String baseDN = null;
047    
048      // Specifies the number of entries expected to be exported.
049      private long entryCount;
050    
051      // Specifies the serverID of the server that requested this export
052      // to happen. It allows a server that previously sent an
053      // InitializeRequestMessage to know that the current message
054      // is related to its own request.
055      private short requestorID;
056    
057      /**
058       * Creates a InitializeDestinationMessage.
059       *
060       * @param baseDN The base DN for which the InitializeMessage is created.
061       * @param senderID The serverID of the server that sends this message.
062       * @param destination The destination of this message.
063       * @param requestorID The server that initiates this export.
064       * @param entryCount The count of entries that will be sent.
065       */
066      public InitializeTargetMessage(DN baseDN, short senderID,
067          short destination, short requestorID, long entryCount)
068      {
069        super(senderID, destination);
070        this.requestorID = requestorID;
071        this.baseDN = baseDN.toNormalizedString();
072        this.entryCount = entryCount;
073      }
074    
075      /**
076       * Creates an InitializeTargetMessage by decoding the provided byte array.
077       * @param in A byte array containing the encoded information for the Message
078       * @throws DataFormatException If the in does not contain a properly
079       *                             encoded InitializeMessage.
080       */
081      public InitializeTargetMessage(byte[] in) throws DataFormatException
082      {
083        super();
084        try
085        {
086          /* first byte is the type */
087          if (in[0] != MSG_TYPE_INITIALIZE_TARGET)
088            throw new DataFormatException(
089                "input is not a valid InitializeDestinationMessage");
090          int pos = 1;
091    
092          // destination
093          int length = getNextLength(in, pos);
094          String destinationString = new String(in, pos, length, "UTF-8");
095          this.destination = Short.valueOf(destinationString);
096          pos += length +1;
097    
098          // baseDn
099          length = getNextLength(in, pos);
100          baseDN = new String(in, pos, length, "UTF-8");
101          pos += length +1;
102    
103          // sender
104          length = getNextLength(in, pos);
105          String senderString = new String(in, pos, length, "UTF-8");
106          senderID = Short.valueOf(senderString);
107          pos += length +1;
108    
109          // requestor
110          length = getNextLength(in, pos);
111          String requestorString = new String(in, pos, length, "UTF-8");
112          requestorID = Short.valueOf(requestorString);
113          pos += length +1;
114    
115          // entryCount
116          length = getNextLength(in, pos);
117          String entryCountString = new String(in, pos, length, "UTF-8");
118          entryCount = Long.valueOf(entryCountString);
119          pos += length +1;
120    
121        }
122        catch (UnsupportedEncodingException e)
123        {
124          throw new DataFormatException("UTF-8 is not supported by this jvm.");
125        }
126      }
127    
128      /**
129       * Get the number of entries expected to be sent during the export.
130       * @return the entry count
131       */
132      public long getEntryCount()
133      {
134        return this.entryCount;
135      }
136    
137      /**
138       * Get the serverID of the server that initiated the export.
139       * @return the serverID
140       */
141      public long getRequestorID()
142      {
143        return this.requestorID;
144      }
145    
146      /**
147       * Get the base DN of the domain.
148       *
149       * @return the base DN
150       */
151      public DN getBaseDN()
152      {
153        if (baseDN == null)
154          return null;
155        try
156        {
157          return DN.decode(baseDN);
158        } catch (DirectoryException e)
159        {
160          return null;
161        }
162      }
163    
164      /**
165       * {@inheritDoc}
166       */
167      @Override
168      public byte[] getBytes()
169      {
170        try
171        {
172          byte[] byteDestination = String.valueOf(destination).getBytes("UTF-8");
173          byte[] byteDn = baseDN.getBytes("UTF-8");
174          byte[] byteSender = String.valueOf(senderID).getBytes("UTF-8");
175          byte[] byteRequestor = String.valueOf(requestorID).getBytes("UTF-8");
176          byte[] byteEntryCount = String.valueOf(entryCount).getBytes("UTF-8");
177    
178          int length = 1 + byteDestination.length + 1
179                         + byteDn.length + 1
180                         + byteSender.length + 1
181                         + byteRequestor.length + 1
182                         + byteEntryCount.length + 1;
183    
184          byte[] resultByteArray = new byte[length];
185    
186          /* put the type of the operation */
187          resultByteArray[0] = MSG_TYPE_INITIALIZE_TARGET;
188          int pos = 1;
189    
190          /* put the destination */
191          pos = addByteArray(byteDestination, resultByteArray, pos);
192    
193          /* put the baseDN and a terminating 0 */
194          pos = addByteArray(byteDn, resultByteArray, pos);
195    
196          /* put the sender */
197          pos = addByteArray(byteSender, resultByteArray, pos);
198    
199          /* put the requestorID */
200          pos = addByteArray(byteRequestor, resultByteArray, pos);
201    
202          /* put the entryCount */
203          pos = addByteArray(byteEntryCount, resultByteArray, pos);
204    
205          return resultByteArray;
206        }
207        catch (UnsupportedEncodingException e)
208        {
209          return null;
210        }
211      }
212    }