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