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.api.changelog;
21  
22  
23  import java.io.IOException;
24  import java.io.ObjectInput;
25  import java.io.ObjectOutput;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.directory.api.ldap.model.ldif.LdifEntry;
30  import org.apache.directory.api.ldap.model.schema.SchemaManager;
31  import org.apache.directory.server.core.api.LdapPrincipal;
32  import org.apache.directory.server.core.api.LdapPrincipalSerializer;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * A helper class which serialize and deserialize a ChangeLogEvent.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public final class ChangeLogEventSerializer
43  {
44      /** The LoggerFactory used by this class */
45      protected static final Logger LOG = LoggerFactory.getLogger( ChangeLogEventSerializer.class );
46  
47  
48      /**
49       * Private constructor.
50       */
51      private ChangeLogEventSerializer()
52      {
53      }
54  
55  
56      /**
57       * Serializes a ChangeLogEvent instance.
58       * 
59       * @param event The ChangeLogEvent instance to serialize
60       * @param out The stream into which we will write the serialized instance
61       * @throws IOException If the stream can't be written
62       */
63      public static void serialize( ChangeLogEvent event, ObjectOutput out ) throws IOException
64      {
65          // The date the change has been created, "yyyyMMddHHmmss'Z'" 
66          out.writeUTF( event.getZuluTime() );
67  
68          // The committer's Principal
69          LdapPrincipalSerializer.serialize( event.getCommitterPrincipal(), out );
70  
71          // The revision
72          out.writeLong( event.getRevision() );
73  
74          // The forward LDIF
75          event.getForwardLdif().writeExternal( out );
76  
77          // The reverse LDIFs number
78          int nbReverses = event.getReverseLdifs().size();
79          out.writeInt( nbReverses );
80  
81          for ( LdifEntry reverseLdif : event.getReverseLdifs() )
82          {
83              reverseLdif.writeExternal( out );
84          }
85  
86          out.flush();
87      }
88  
89  
90      /**
91       * Deserializes a ChangeLogEvent instance.
92       * 
93       * @param schemaManager The SchemaManager (can be null)
94       * @param in The input stream from which the ChengaLogEvent is read
95       * @return a deserialized ChangeLogEvent
96       * @throws IOException If we had an issue processing the stream
97       */
98      public static ChangeLogEvent deserialize( SchemaManager schemaManager, ObjectInput in )
99          throws IOException
100     {
101         // The date the change has been created, "yyyyMMddHHmmss'Z'" 
102         String zuluTime = in.readUTF();
103 
104         // The committer's Principal
105         LdapPrincipal committerPrincipal = LdapPrincipalSerializer.deserialize( schemaManager, in );
106 
107         // The revision
108         long revision = in.readLong();
109 
110         // The forward LDIF
111         LdifEntry forwardEntry = new LdifEntry();
112 
113         try
114         {
115             forwardEntry.readExternal( in );
116         }
117         catch ( ClassNotFoundException cnfe )
118         {
119             IOException ioe = new IOException( cnfe.getMessage() );
120             ioe.initCause( cnfe );
121             throw ioe;
122         }
123 
124         // The reverse LDIFs number
125         int nbReverses = in.readInt();
126 
127         List<LdifEntry> reverses = new ArrayList<>( nbReverses );
128 
129         for ( int i = 0; i < nbReverses; i++ )
130         {
131             LdifEntry reverseEntry = new LdifEntry();
132 
133             try
134             {
135                 reverseEntry.readExternal( in );
136             }
137             catch ( ClassNotFoundException cnfe )
138             {
139                 IOException ioe = new IOException( cnfe.getMessage() );
140                 ioe.initCause( cnfe );
141                 throw ioe;
142             }
143 
144             reverses.add( reverseEntry );
145         }
146 
147         return new ChangeLogEvent( revision, zuluTime, committerPrincipal, forwardEntry, reverses );
148     }
149 }