View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.partition.impl.btree.mavibot;
21  
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutput;
28  import java.io.ObjectOutputStream;
29  import java.nio.ByteBuffer;
30  import java.util.Comparator;
31  
32  import org.apache.directory.api.ldap.model.name.Dn;
33  import org.apache.directory.api.ldap.model.schema.comparators.DnComparator;
34  import org.apache.directory.mavibot.btree.serializer.AbstractElementSerializer;
35  import org.apache.directory.mavibot.btree.serializer.BufferHandler;
36  import org.apache.directory.server.i18n.I18n;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  
41  /**
42   * Serialize and deserialize a Dn.
43   * <br><br>
44   * <b>This class must *not* be used outside of the server.</b>
45   *  
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  public class DnSerializer extends AbstractElementSerializer<Dn>
49  {
50      /** The serialVersionUID */
51      private static final long serialVersionUID = 1L;
52  
53      /** the logger for this class */
54      private static final Logger LOG = LoggerFactory.getLogger( DnSerializer.class );
55  
56      /**
57       * Speedup for logs
58       */
59      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
60  
61      private static Comparator<Dn> comp = new Comparator<Dn>()
62      {
63          DnComparator comparator = new DnComparator( null );
64          
65          @Override
66          public int compare( Dn dn1, Dn dn2 )
67          {
68              return comparator.compare( dn1,  dn2 );
69          }
70      };
71  
72  
73      /**
74       * Creates a new instance of DnSerializer.
75       */
76      public DnSerializer()
77      {
78          super( comp );
79      }
80  
81  
82      /**
83       * This is the place where we serialize Dn
84       * 
85       * @param dn The Dn to serialize
86       * @return The byte[] containing the serialized Dn
87       */
88      public byte[] serialize( Dn dn )
89      {
90          try
91          {
92              ByteArrayOutputStream baos = new ByteArrayOutputStream();
93              ObjectOutput out = new ObjectOutputStream( baos );
94  
95              // First, the Dn
96              dn.writeExternal( out );
97  
98              out.flush();
99  
100             if ( IS_DEBUG )
101             {
102                 LOG.debug( ">------------------------------------------------" );
103                 LOG.debug( "Serialized {}", dn );
104             }
105 
106             return baos.toByteArray();
107         }
108         catch ( IOException e )
109         {
110             throw new RuntimeException( e );
111         }
112     }
113 
114 
115     /**
116      *  Deserialize a Dn.
117      *  
118      *  @param buffer the buffer containing the serialized Dn
119      *  @return An instance of a Dn object 
120      *  @throws IOException if we can't deserialize the Dn
121      */
122     @Override
123     public Dn deserialize( ByteBuffer buffer ) throws IOException
124     {
125         return deserialize( new BufferHandler( buffer.array() ) );
126     }
127 
128 
129     @Override
130     public Dn deserialize( BufferHandler bufferHandler ) throws IOException
131     {
132         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bufferHandler.getBuffer() ) );
133 
134         try
135         {
136             Dn dn = new Dn();
137 
138             dn.readExternal( in );
139 
140             return dn;
141         }
142         catch ( ClassNotFoundException cnfe )
143         {
144             LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
145             throw new IOException( cnfe.getLocalizedMessage() );
146         }
147     }
148 
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public Dn fromBytes( byte[] buffer ) throws IOException
155     {
156         return fromBytes( buffer, 0 );
157     }
158 
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public Dn fromBytes( byte[] buffer, int pos ) throws IOException
165     {
166         int length = buffer.length - pos;
167         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( buffer, pos, length ) );
168 
169         try
170         {
171             Dn dn = new Dn();
172 
173             dn.readExternal( in );
174 
175             return dn;
176         }
177         catch ( ClassNotFoundException cnfe )
178         {
179             LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
180             throw new IOException( cnfe.getLocalizedMessage() );
181         }
182     }
183 
184 
185     /**
186      * {@inheritDoc}
187      */
188     @Override
189     public Class<?> getType()
190     {
191         return Dn.class;
192     }
193 }