001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.server.core.partition.impl.btree.mavibot;
021
022
023import java.io.ByteArrayInputStream;
024import java.io.ByteArrayOutputStream;
025import java.io.IOException;
026import java.io.ObjectInputStream;
027import java.io.ObjectOutput;
028import java.io.ObjectOutputStream;
029import java.nio.ByteBuffer;
030import java.util.Comparator;
031
032import org.apache.directory.api.ldap.model.name.Dn;
033import org.apache.directory.api.ldap.model.schema.comparators.DnComparator;
034import org.apache.directory.mavibot.btree.serializer.AbstractElementSerializer;
035import org.apache.directory.mavibot.btree.serializer.BufferHandler;
036import org.apache.directory.server.i18n.I18n;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040
041/**
042 * Serialize and deserialize a Dn.
043 * <br><br>
044 * <b>This class must *not* be used outside of the server.</b>
045 *  
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 */
048public class DnSerializer extends AbstractElementSerializer<Dn>
049{
050    /** The serialVersionUID */
051    private static final long serialVersionUID = 1L;
052
053    /** the logger for this class */
054    private static final Logger LOG = LoggerFactory.getLogger( DnSerializer.class );
055
056    /**
057     * Speedup for logs
058     */
059    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
060
061    private static Comparator<Dn> comp = new Comparator<Dn>()
062    {
063        DnComparator comparator = new DnComparator( null );
064        
065        @Override
066        public int compare( Dn dn1, Dn dn2 )
067        {
068            return comparator.compare( dn1,  dn2 );
069        }
070    };
071
072
073    /**
074     * Creates a new instance of DnSerializer.
075     */
076    public DnSerializer()
077    {
078        super( comp );
079    }
080
081
082    /**
083     * This is the place where we serialize Dn
084     * 
085     * @param dn The Dn to serialize
086     * @return The byte[] containing the serialized Dn
087     */
088    public byte[] serialize( Dn dn )
089    {
090        try
091        {
092            ByteArrayOutputStream baos = new ByteArrayOutputStream();
093            ObjectOutput out = new ObjectOutputStream( baos );
094
095            // First, the Dn
096            dn.writeExternal( out );
097
098            out.flush();
099
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}