1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.entry.Attribute;
33 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
34 import org.apache.directory.api.ldap.model.entry.DefaultEntry;
35 import org.apache.directory.api.ldap.model.entry.Entry;
36 import org.apache.directory.api.ldap.model.exception.LdapException;
37 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
38 import org.apache.directory.api.ldap.model.name.Dn;
39 import org.apache.directory.api.ldap.model.name.Rdn;
40 import org.apache.directory.api.ldap.model.schema.AttributeType;
41 import org.apache.directory.api.ldap.model.schema.SchemaManager;
42 import org.apache.directory.mavibot.btree.serializer.AbstractElementSerializer;
43 import org.apache.directory.mavibot.btree.serializer.BufferHandler;
44 import org.apache.directory.server.i18n.I18n;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52
53
54
55
56
57 public class MavibotEntrySerializer extends AbstractElementSerializer<Entry>
58 {
59
60 private static final long serialVersionUID = 1L;
61
62
63 private static final Logger LOG = LoggerFactory.getLogger( MavibotEntrySerializer.class );
64
65
66
67
68 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
69
70
71 private static SchemaManager schemaManager;
72
73 private static class EntryComparator implements Comparator<Entry>
74 {
75
76 @Override
77 public int compare( Entry entry1, Entry entry2 )
78 {
79 return entry1.getDn().getName().compareTo( entry1.getDn().getName() );
80 }
81
82 }
83
84 private static Comparator<Entry> comparator = new EntryComparator();
85
86
87
88
89
90
91 public MavibotEntrySerializer()
92 {
93 super( comparator );
94 }
95
96
97 @Override
98 public Comparator<Entry> getComparator()
99 {
100 return comparator;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public byte[] serialize( Entry entry )
127 {
128 try
129 {
130 ByteArrayOutputStream baos = new ByteArrayOutputStream();
131
132 ObjectOutput out = new ObjectOutputStream( baos );
133
134
135 Dn dn = entry.getDn();
136
137
138 if ( dn.isEmpty() )
139 {
140 out.writeByte( 0 );
141 }
142 else
143 {
144 out.writeByte( 1 );
145 Rdn rdn = dn.getRdn();
146 rdn.writeExternal( out );
147 }
148
149
150 out.writeInt( entry.getAttributes().size() );
151
152
153
154
155
156 for ( Attribute attribute : entry.getAttributes() )
157 {
158 AttributeType attributeType = attribute.getAttributeType();
159
160
161
162 String oid = attributeType.getOid();
163
164 out.writeUTF( oid );
165
166
167 attribute.writeExternal( out );
168 }
169
170 out.flush();
171
172
173
174
175 if ( IS_DEBUG )
176 {
177 LOG.debug( ">------------------------------------------------" );
178 LOG.debug( "Serialize {}", entry );
179 }
180
181 return baos.toByteArray();
182 }
183 catch ( Exception e )
184 {
185 throw new RuntimeException( e );
186 }
187 }
188
189
190
191
192
193
194
195
196
197 public Entry deserialize( ByteBuffer buffer ) throws IOException
198 {
199
200 int len = buffer.limit();
201
202 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( buffer.array(), buffer.position(), len ) );
203
204 try
205 {
206 Entry entry = new DefaultEntry( schemaManager );
207
208
209 byte hasDn = in.readByte();
210
211 if ( hasDn == 1 )
212 {
213 Rdn rdn = new Rdn( schemaManager );
214 rdn.readExternal( in );
215
216 try
217 {
218 entry.setDn( new Dn( schemaManager, rdn ) );
219 }
220 catch ( LdapInvalidDnException lide )
221 {
222 IOException ioe = new IOException( lide.getMessage() );
223 ioe.initCause( lide );
224 throw ioe;
225 }
226 }
227 else
228 {
229 entry.setDn( Dn.EMPTY_DN );
230 }
231
232
233 int nbAttributes = in.readInt();
234
235
236 for ( int i = 0; i < nbAttributes; i++ )
237 {
238
239 String oid = in.readUTF();
240
241 try
242 {
243 AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( oid );
244
245
246 Attribute attribute = new DefaultAttribute( attributeType );
247
248
249 attribute.readExternal( in );
250
251 entry.add( attribute );
252 }
253 catch ( LdapException ne )
254 {
255
256 throw new ClassNotFoundException( ne.getMessage(), ne );
257 }
258 }
259
260 buffer.position( buffer.position() + len );
261
262 return entry;
263 }
264 catch ( ClassNotFoundException cnfe )
265 {
266 LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
267 throw new IOException( cnfe.getLocalizedMessage() );
268 }
269 }
270
271
272 @Override
273 public Entry deserialize( BufferHandler bufferHandler ) throws IOException
274 {
275 return deserialize( ByteBuffer.wrap( bufferHandler.getBuffer() ) );
276 }
277
278
279 public static void setSchemaManager( SchemaManager schemaManager )
280 {
281 MavibotEntrySerializer.schemaManager = schemaManager;
282 }
283
284
285
286
287
288 @Override
289 public Entry fromBytes( byte[] buffer ) throws IOException
290 {
291 return fromBytes( buffer, 0 );
292 }
293
294
295
296
297
298 @Override
299 public Entry fromBytes( byte[] buffer, int pos ) throws IOException
300 {
301
302 int len = buffer.length - pos;
303
304 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( buffer, pos, len ) );
305
306 try
307 {
308 Entry entry = new DefaultEntry( schemaManager );
309
310
311 byte hasDn = in.readByte();
312
313 if ( hasDn == 1 )
314 {
315 Rdn rdn = new Rdn( schemaManager );
316 rdn.readExternal( in );
317
318 try
319 {
320 entry.setDn( new Dn( schemaManager, rdn ) );
321 }
322 catch ( LdapInvalidDnException lide )
323 {
324 IOException ioe = new IOException( lide.getMessage() );
325 ioe.initCause( lide );
326 throw ioe;
327 }
328 }
329 else
330 {
331 entry.setDn( Dn.EMPTY_DN );
332 }
333
334
335 int nbAttributes = in.readInt();
336
337
338 for ( int i = 0; i < nbAttributes; i++ )
339 {
340
341 String oid = in.readUTF();
342
343 try
344 {
345 AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( oid );
346
347
348 Attribute attribute = new DefaultAttribute( attributeType );
349
350
351 attribute.readExternal( in );
352
353 entry.add( attribute );
354 }
355 catch ( LdapException ne )
356 {
357
358 throw new ClassNotFoundException( ne.getMessage(), ne );
359 }
360 }
361
362 return entry;
363 }
364 catch ( ClassNotFoundException cnfe )
365 {
366 LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
367 throw new IOException( cnfe.getLocalizedMessage() );
368 }
369 }
370
371
372
373
374
375 @Override
376 public Class<?> getType()
377 {
378 return Entry.class;
379 }
380 }