1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mitosis.operation;
21
22
23 import java.io.Externalizable;
24 import java.io.IOException;
25 import java.io.ObjectInput;
26 import java.io.ObjectOutput;
27
28 import javax.naming.NamingException;
29 import javax.naming.OperationNotSupportedException;
30
31 import org.apache.directory.mitosis.common.CSN;
32 import org.apache.directory.mitosis.common.Constants;
33 import org.apache.directory.mitosis.store.ReplicationStore;
34 import org.apache.directory.server.core.CoreSession;
35 import org.apache.directory.server.core.entry.DefaultServerAttribute;
36 import org.apache.directory.server.core.entry.DefaultServerEntry;
37 import org.apache.directory.server.core.partition.Partition;
38 import org.apache.directory.server.core.partition.PartitionNexus;
39 import org.apache.directory.server.schema.registries.Registries;
40 import org.apache.directory.shared.ldap.name.LdapDN;
41 import org.apache.directory.shared.ldap.name.LdapDNSerializer;
42 import org.apache.directory.shared.ldap.schema.AttributeType;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class Operation implements Externalizable
63 {
64
65
66
67
68
69
70
71 private static final long serialVersionUID = 1L;
72
73
74 protected CSN csn;
75
76
77 protected OperationType operationType;
78
79
80 protected Registries registries;
81
82
83
84
85
86
87
88
89
90
91
92
93 {
94 this.registries = registries;
95 this.operationType = operationType;
96 }
97
98
99
100
101
102
103
104
105
106
107 protected Operation( Registries registries, OperationType operationType, CSN csn )
108 {
109 assert csn != null;
110 this.registries = registries;
111 this.csn = csn;
112 this.operationType = operationType;
113 }
114
115
116
117
118
119 public CSN getCSN()
120 {
121 return csn;
122 }
123
124
125
126
127
128
129
130
131
132 public final void execute( PartitionNexus nexus, ReplicationStore store, CoreSession coreSession )
133 throws Exception
134 {
135 synchronized ( nexus )
136 {
137 execute0( nexus, store, coreSession );
138 store.putLog( this );
139 }
140 }
141
142
143
144
145
146
147
148
149
150 protected void execute0( PartitionNexus nexus, ReplicationStore store, CoreSession coreSession )
151 throws Exception
152 {
153 throw new OperationNotSupportedException( nexus.getSuffixDn().toString() );
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167 private static Operation readAttributeOperation( ObjectInput in, Registries registries,
168 Operation operation ) throws ClassNotFoundException, IOException
169 {
170 AttributeOperation attributeOperation = (AttributeOperation)operation;
171
172 LdapDN dn = LdapDNSerializer.deserialize( in );
173
174
175 String id = in.readUTF();
176
177 try
178 {
179
180 AttributeType at = registries.getAttributeTypeRegistry().lookup( id );
181
182
183 DefaultServerAttribute attribute = new DefaultServerAttribute( id, at );
184 attribute.deserialize( in );
185
186
187 attributeOperation.dn = dn;
188 attributeOperation.attribute = attribute;
189
190 return operation;
191 }
192 catch ( NamingException ne )
193 {
194 throw new IOException( "Cannot find the '" + id + "' attributeType" );
195 }
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209 public static Operation deserialize( Registries registries, ObjectInput in ) throws ClassNotFoundException, IOException
210 {
211
212 int opTypeValue = in.readInt();
213 OperationType opType = OperationType.get( opTypeValue );
214
215
216 CSN csn = (CSN)in.readObject();
217
218 Operation operation = null;
219
220 switch ( opType )
221 {
222 case ADD_ATTRIBUTE :
223
224 operation = new AddAttributeOperation( registries );
225
226
227 operation.csn = csn;
228
229
230 readAttributeOperation( in, registries, operation );
231
232 return operation;
233
234 case DELETE_ATTRIBUTE :
235
236 operation = new DeleteAttributeOperation( registries );
237
238
239 operation.csn = csn;
240
241
242 readAttributeOperation( in, registries, operation );
243
244 return operation;
245
246 case REPLACE_ATTRIBUTE :
247
248 operation = new ReplaceAttributeOperation( registries );
249
250
251 operation.csn = csn;
252
253
254 readAttributeOperation( in, registries, operation );
255
256 return operation;
257
258 case ADD_ENTRY :
259
260 operation = new AddEntryOperation( registries );
261
262
263 operation.csn = csn;
264
265 DefaultServerEntry entry = new DefaultServerEntry( registries );
266 entry.deserialize( in );
267 ((AddEntryOperation)operation).setEntry( entry );
268
269 return operation;
270
271 case COMPOSITE_OPERATION :
272
273 operation = new CompositeOperation( registries );
274
275
276 operation.csn = csn;
277
278
279 int nbOperations = in.readInt();
280
281 for ( int i = 0; i < nbOperations; i++ )
282 {
283 Operation child = deserialize( registries, in );
284 child.csn = csn;
285 ((CompositeOperation)operation).add( child );
286 }
287
288 return operation;
289
290 default :
291 throw new IOException( "Cannot read the unkown operation" );
292 }
293 }
294
295
296
297
298
299
300
301
302
303
304
305 public static void serialize( Operation operation, ObjectOutput out ) throws ClassNotFoundException, IOException
306 {
307 OperationType opType = operation.operationType;
308
309
310 out.writeInt( opType.ordinal() );
311
312
313 out.writeObject( operation.csn );
314
315 switch ( opType )
316 {
317 case REPLACE_ATTRIBUTE :
318 case DELETE_ATTRIBUTE :
319 case ADD_ATTRIBUTE :
320 AttributeOperation attrOp = (AttributeOperation)operation;
321
322
323 LdapDNSerializer.serialize( attrOp.dn, out );
324
325
326 out.writeUTF( ((AttributeOperation)operation).attribute.getId() );
327
328
329 DefaultServerAttribute attr = (DefaultServerAttribute)(attrOp.attribute);
330 attr.serialize( out );
331 return;
332
333 case ADD_ENTRY :
334 ((DefaultServerEntry)((AddEntryOperation)operation).getEntry()).serialize( out );
335 return;
336
337 case COMPOSITE_OPERATION :
338 out.writeInt( ((CompositeOperation)operation).size() );
339
340
341 for ( Operation child:((CompositeOperation)operation).getChildren() )
342 {
343 serialize( child, out );
344 }
345
346 return;
347 }
348 }
349
350
351
352
353
354
355
356
357
358 public void readExternal( ObjectInput in ) throws ClassNotFoundException, IOException
359 {
360 csn = (CSN)in.readObject();
361 }
362
363
364
365
366
367
368
369
370 public void writeExternal( ObjectOutput out) throws IOException
371 {
372 out.writeObject( csn );
373 }
374
375
376
377
378
379 public String toString()
380 {
381 return csn.toString();
382 }
383 }