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.ldap.replication;
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.entry.DefaultEntry;
33  import org.apache.directory.api.ldap.model.entry.Entry;
34  import org.apache.directory.api.ldap.model.message.controls.ChangeType;
35  import org.apache.directory.api.ldap.model.name.Dn;
36  import org.apache.directory.api.ldap.model.schema.SchemaManager;
37  
38  
39  /**
40   * A ReplicaEventMessage serializer/deserializer.
41   * 
42   * A modification is serialized following this format : <br>
43   * <ul>
44   * <li>byte : EventType</li>
45   * <li>byte[] : the serialized DN</li>
46   * <li>byte[] : the serialized entry</li>
47   * </ul>
48   * 
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  public class ReplicaEventMessageSerializer implements Serializer
52  {
53      /** The serialVersionUID */
54      private static final long serialVersionUID = 1L;
55  
56      /** The schemaManager */
57      private transient SchemaManager schemaManager;
58  
59  
60      /**
61       * Creates a new instance of ReplicaEventMessageSerializer.
62       *
63       * @param schemaManager The reference to the global schemaManager
64       */
65      public ReplicaEventMessageSerializer( SchemaManager schemaManager )
66      {
67          this.schemaManager = schemaManager;
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      public byte[] serialize( Object object ) throws IOException
75      {
76          ReplicaEventMessagee/directory/server/ldap/replication/ReplicaEventMessage.html#ReplicaEventMessage">ReplicaEventMessage replicaEventMessage = ( ReplicaEventMessage ) object;
77  
78          Entry entry = replicaEventMessage.getEntry();
79          ChangeType changeType = replicaEventMessage.getChangeType();
80  
81          try ( ByteArrayOutputStream baos = new ByteArrayOutputStream();
82              ObjectOutput out = new ObjectOutputStream( baos ) )
83          {
84  
85              // The change type first
86              out.writeByte( changeType.getValue() );
87  
88              // The entry DN
89              entry.getDn().writeExternal( out );
90  
91              // The entry
92              entry.writeExternal( out );
93  
94              out.flush();
95  
96              return baos.toByteArray();
97          }
98      }
99  
100 
101     /**
102      *  Deserialize a ReplicaEventMessage.
103      *  
104      *  @param bytes the byte array containing the serialized ReplicaEventMessage
105      *  @return An instance of a ReplicaEventMessage object 
106      *  @throws IOException if we can't deserialize the ReplicaEventMessage
107      */
108     public Object deserialize( byte[] bytes ) throws IOException
109     {
110         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bytes ) );
111 
112         ReplicaEventMessage replicaEventMessage = null;
113 
114         try
115         {
116             // The changeType
117             byte type = in.readByte();
118             ChangeType changeType = ChangeType.getChangeType( type );
119 
120             // The Entry's DN
121             Dn entryDn = new Dn( schemaManager );
122             entryDn.readExternal( in );
123 
124             // The Entry
125             Entry entry = new DefaultEntry( schemaManager );
126             entry.readExternal( in );
127             entry.setDn( entryDn );
128 
129             // And create a ReplicaEventMessage
130             replicaEventMessage = new ReplicaEventMessage( changeType, entry );
131         }
132         catch ( ClassNotFoundException cnfe )
133         {
134             // there is nothing we can do here...
135         }
136 
137         return replicaEventMessage;
138     }
139 }