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.jdbm;
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
30 import jdbm.helper.Serializer;
31
32 import org.apache.directory.api.ldap.model.name.Dn;
33 import org.apache.directory.api.ldap.model.schema.SchemaManager;
34 import org.apache.directory.server.i18n.I18n;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45
46 public class DnSerializer implements Serializer
47 {
48
49 private static final long serialVersionUID = 1L;
50
51
52 private static final Logger LOG = LoggerFactory.getLogger( DnSerializer.class );
53
54
55
56
57 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
58
59
60 private transient SchemaManager schemaManager;
61
62
63
64
65
66
67
68 public DnSerializer( SchemaManager schemaManager )
69 {
70 this.schemaManager = schemaManager;
71 }
72
73
74
75
76
77
78
79
80
81 public byte[] serialize( Object object ) throws IOException
82 {
83 Dn dn = ( Dn ) object;
84
85 try ( ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 ObjectOutput out = new ObjectOutputStream( baos ) )
87 {
88
89
90 dn.writeExternal( out );
91
92 out.flush();
93
94 if ( IS_DEBUG )
95 {
96 LOG.debug( ">------------------------------------------------" );
97 LOG.debug( "Serialized {}", dn );
98 }
99
100 return baos.toByteArray();
101 }
102 }
103
104
105
106
107
108
109
110
111
112 public Object deserialize( byte[] bytes ) throws IOException
113 {
114 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bytes ) );
115
116 try
117 {
118 Dn dn = new Dn();
119
120 dn.readExternal( in );
121
122 return dn;
123 }
124 catch ( ClassNotFoundException cnfe )
125 {
126 LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
127 throw new IOException( cnfe.getLocalizedMessage() );
128 }
129 }
130 }