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.Rdn;
33  import org.apache.directory.api.ldap.model.schema.SchemaManager;
34  import org.apache.directory.server.i18n.I18n;
35  import org.apache.directory.server.xdbm.ParentIdAndRdn;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * Serialize and deserialize a ParentidAndRdn.
42   * <br><br>
43   * <b>This class must *not* be used outside of the server.</b>
44   *  
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  public class ParentIdAndRdnSerializer implements Serializer
48  {
49      /** The serialVersionUID */
50      private static final long serialVersionUID = 1L;
51  
52      /** the logger for this class */
53      private static final Logger LOG = LoggerFactory.getLogger( ParentIdAndRdnSerializer.class );
54  
55      /**
56       * Speedup for logs
57       */
58      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
59  
60      /** The schemaManager reference */
61      private transient SchemaManager schemaManager;
62  
63  
64      /**
65       * Creates a new instance of ParentIdAndRdnSerializer.
66       * 
67       * @param schemaManager The reference to the global schemaManager
68       */
69      public ParentIdAndRdnSerializer( SchemaManager schemaManager )
70      {
71          this.schemaManager = schemaManager;
72      }
73  
74  
75      /**
76       * This is the place where we serialize ParentIdAndRdn
77       * 
78       * @param object The element to serialize
79       * @return the byte] containing the serialized element
80       * @throws IOException If the serialization failed
81       */
82      public byte[] serialize( Object object ) throws IOException
83      {
84          ParentIdAndRdn/../../../org/apache/directory/server/xdbm/ParentIdAndRdn.html#ParentIdAndRdn">ParentIdAndRdn parentIdAndRdn = ( ParentIdAndRdn ) object;
85  
86          try ( ByteArrayOutputStream baos = new ByteArrayOutputStream();
87              ObjectOutput out = new ObjectOutputStream( baos ) )
88          {
89  
90              // First, the Dn
91              Rdn[] rdns = parentIdAndRdn.getRdns();
92  
93              // Write the Rdn of the Dn
94              if ( ( rdns == null ) || ( rdns.length == 0 ) )
95              {
96                  out.writeByte( 0 );
97              }
98              else
99              {
100                 out.writeByte( rdns.length );
101 
102                 for ( Rdn rdn : rdns )
103                 {
104                     rdn.writeExternal( out );
105                 }
106             }
107 
108             // Then the parentId.
109             out.writeUTF( parentIdAndRdn.getParentId() );
110 
111             // The number of children
112             out.writeInt( parentIdAndRdn.getNbChildren() );
113 
114             // The number of descendants
115             out.writeInt( parentIdAndRdn.getNbDescendants() );
116 
117             out.flush();
118 
119             if ( IS_DEBUG )
120             {
121                 LOG.debug( ">------------------------------------------------" );
122                 LOG.debug( "Serialize {}", parentIdAndRdn );
123             }
124 
125             return baos.toByteArray();
126         }
127     }
128 
129 
130     /**
131      *  Deserialize a ParentIdAndRdn.
132      *  
133      *  @param bytes the byte array containing the serialized ParentIdAndRdn
134      *  @return An instance of a ParentIdAndRdn object 
135      *  @throws IOException if we can't deserialize the ParentIdAndRdn
136      */
137     public Object deserialize( byte[] bytes ) throws IOException
138     {
139         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bytes ) );
140 
141         try
142         {
143             ParentIdAndRdndbm/ParentIdAndRdn.html#ParentIdAndRdn">ParentIdAndRdn parentIdAndRdn = new ParentIdAndRdn();
144 
145             // Read the number of rdns, if any
146             byte nbRdns = in.readByte();
147 
148             if ( nbRdns == 0 )
149             {
150                 parentIdAndRdn.setRdns( new Rdn[0] );
151             }
152             else
153             {
154                 Rdn[] rdns = new Rdn[nbRdns];
155 
156                 for ( int i = 0; i < nbRdns; i++ )
157                 {
158                     Rdn rdn = new Rdn( schemaManager );
159                     rdn.readExternal( in );
160                     rdns[i] = rdn;
161                 }
162 
163                 parentIdAndRdn.setRdns( rdns );
164             }
165 
166             // Read the parent ID
167             String uuid = in.readUTF();
168 
169             parentIdAndRdn.setParentId( uuid );
170 
171             // Read the nulber of children and descendants
172             int nbChildren = in.readInt();
173             int nbDescendants = in.readInt();
174 
175             parentIdAndRdn.setNbChildren( nbChildren );
176             parentIdAndRdn.setNbDescendants( nbDescendants );
177 
178             return parentIdAndRdn;
179         }
180         catch ( ClassNotFoundException cnfe )
181         {
182             LOG.error( I18n.err( I18n.ERR_134, cnfe.getLocalizedMessage() ) );
183             throw new IOException( cnfe.getLocalizedMessage() );
184         }
185     }
186 }