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.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   * Serialize and deserialize a Dn.
41   * <br><br>
42   * <b>This class must *not* be used outside of the server.</b>
43   *  
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class DnSerializer implements Serializer
47  {
48      /** The serialVersionUID */
49      private static final long serialVersionUID = 1L;
50  
51      /** the logger for this class */
52      private static final Logger LOG = LoggerFactory.getLogger( DnSerializer.class );
53  
54      /**
55       * Speedup for logs
56       */
57      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
58  
59      /** The schemaManager reference */
60      private transient SchemaManager schemaManager;
61  
62  
63      /**
64       * Creates a new instance of DnSerializer.
65       * 
66       * @param schemaManager The reference to the global schemaManager
67       */
68      public DnSerializer( SchemaManager schemaManager )
69      {
70          this.schemaManager = schemaManager;
71      }
72  
73  
74      /**
75       * This is the place where we serialize Dn
76       * 
77       * @param object The element to serialize
78       * @return a byte[] containing the serialized element
79       * @throws IOException If teh serialization failed
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              // First, the Dn
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      *  Deserialize a Dn.
107      *  
108      *  @param bytes the byte array containing the serialized Dn
109      *  @return An instance of a Dn object 
110      *  @throws IOException if we can't deserialize the Dn
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 }