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.configuration;
21
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeSet;
30
31 import org.apache.directory.mitosis.common.CSN;
32 import org.apache.directory.mitosis.common.CSNFactory;
33 import org.apache.directory.mitosis.common.Replica;
34 import org.apache.directory.mitosis.common.DefaultCSNFactory;
35 import org.apache.directory.mitosis.service.ReplicationInterceptor;
36 import org.apache.directory.mitosis.store.ReplicationStore;
37 import org.apache.directory.mitosis.store.derby.DerbyReplicationStore;
38 import org.apache.directory.shared.ldap.util.StringTools;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48
49
50 public class ReplicationConfiguration
51 {
52 public static final int DEFAULT_LOG_MAX_AGE = 7;
53 public static final int DEFAULT_REPLICATION_INTERVAL = 5;
54 public static final int DEFAULT_RESPONSE_TIMEOUT = 60;
55 public static final int DEFAULT_SERVER_PORT = 7846;
56
57
58 private static Logger log = LoggerFactory.getLogger( ReplicationConfiguration.class );
59
60
61 private String replicaId;
62
63
64 private int serverPort = DEFAULT_SERVER_PORT;
65 private int responseTimeout = DEFAULT_RESPONSE_TIMEOUT;
66 private int replicationInterval = DEFAULT_REPLICATION_INTERVAL;
67
68
69 private final Set<Replica> peerReplicas = new HashSet<Replica>();
70
71
72 private CSNFactory csnFactory = new DefaultCSNFactory();
73 private ReplicationStore store = new DerbyReplicationStore();
74
75
76 private int logMaxAge = DEFAULT_LOG_MAX_AGE;
77
78
79
80
81
82
83
84
85
86 public ReplicationConfiguration()
87 {
88 }
89
90
91
92
93
94 public int getServerPort()
95 {
96 return serverPort;
97 }
98
99
100
101
102
103
104 public void setServerPort( int serverPort )
105 {
106 this.serverPort = serverPort;
107 }
108
109
110
111
112
113
114
115 public int getResponseTimeout()
116 {
117 return responseTimeout;
118 }
119
120
121
122
123
124
125
126 public void setResponseTimeout( int responseTimeout )
127 {
128 this.responseTimeout = responseTimeout;
129 }
130
131
132
133
134
135
136
137 public int getReplicationInterval() {
138 return replicationInterval;
139 }
140
141
142
143
144
145
146
147
148 public void setReplicationInterval( int replicationInterval )
149 {
150 if( replicationInterval < 0 )
151 {
152 replicationInterval = 0;
153 }
154
155 this.replicationInterval = replicationInterval;
156 }
157
158
159
160
161
162
163 public CSNFactory getCsnFactory()
164 {
165 return csnFactory;
166 }
167
168
169
170
171
172
173 public void setCsnFactory( CSNFactory csnFactory )
174 {
175 this.csnFactory = csnFactory;
176 }
177
178
179
180
181 public void addPeerReplica( Replica peer )
182 {
183 assert peer != null;
184 peerReplicas.add( peer );
185 }
186
187
188
189
190 public void removePeerReplica( Replica peer )
191 {
192 assert peer != null;
193 peerReplicas.remove( peer );
194 }
195
196
197
198
199 public void removeAllPeerReplicas()
200 {
201 peerReplicas.clear();
202 }
203
204
205
206
207 public Set<Replica> getPeerReplicas()
208 {
209 Set<Replica> result = new HashSet<Replica>();
210 result.addAll( peerReplicas );
211 return result;
212 }
213
214
215
216
217
218 public void setPeerReplicas( Set<Object> replicas )
219 {
220 assert replicas != null;
221
222 Set<Replica> normalizedReplicas = new HashSet<Replica>();
223
224 for ( Object replica:replicas )
225 {
226 if ( replica instanceof Replica )
227 {
228 normalizedReplicas.add( (Replica)replica );
229 }
230 else
231 {
232 normalizedReplicas.add( new Replica( replica.toString() ) );
233 }
234 }
235
236 peerReplicas.clear();
237 peerReplicas.addAll( normalizedReplicas );
238 }
239
240
241
242
243 public String getReplicaId()
244 {
245 return replicaId;
246 }
247
248
249
250
251 public void setReplicaId( String replicaId )
252 {
253 assert replicaId != null;
254 this.replicaId = replicaId;
255 }
256
257
258
259
260
261
262 public ReplicationStore getStore()
263 {
264 return store;
265 }
266
267
268
269
270
271
272 public void setStore( ReplicationStore store )
273 {
274 this.store = store;
275 }
276
277
278
279
280
281
282
283 public int getLogMaxAge()
284 {
285 return logMaxAge;
286 }
287
288
289
290
291
292
293
294 public void setLogMaxAge( int logMaxAge )
295 {
296 if ( logMaxAge <= 0 )
297 {
298 throw new ReplicationConfigurationException( "logMaxAge: " + logMaxAge );
299 }
300
301 this.logMaxAge = logMaxAge;
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 public void validate() throws ReplicationConfigurationException
322 {
323 if ( replicaId == null )
324 {
325 log.error( "The replicaId is missing" );
326 throw new ReplicationConfigurationException( "Replica ID is not specified." );
327 }
328
329 if ( ( serverPort < 0 ) || ( serverPort > 65535 ) )
330 {
331 log.error( "The replica port is not between 0 and 65535" );
332 throw new ReplicationConfigurationException( "Server port is invalid: " + serverPort );
333 }
334
335 if ( responseTimeout <= 0 )
336 {
337 log.error( "The replica responsetimeout is negative" );
338 throw new ReplicationConfigurationException( "Invalid response timeout: " + responseTimeout );
339 }
340
341 if ( csnFactory == null )
342 {
343 log.error( "The CSN factory has not been declared" );
344 throw new ReplicationConfigurationException( "CSN factory is not specified." );
345 }
346
347 if ( store == null )
348 {
349 log.error( "The store has not been declared" );
350 throw new ReplicationConfigurationException( "Replication store is not specified." );
351 }
352
353 if ( peerReplicas.size() == 0 )
354 {
355 log.error( "The replicas peer list is empty" );
356 throw new ReplicationConfigurationException( "No peer replicas" );
357 }
358
359
360
361
362 Set<String> ids = new TreeSet<String>();
363 Map<String, Integer> servers = new HashMap<String, Integer>();
364
365
366 ids.add( replicaId );
367
368
369 servers.put( "localhost", serverPort );
370 servers.put( "127.0.0.1", serverPort );
371
372 try
373 {
374 servers.put( StringTools.lowerCase( InetAddress.getByName( "127.0.0.1" ).getHostName() ), serverPort );
375 }
376 catch ( UnknownHostException uhe )
377 {
378
379 throw new ReplicationConfigurationException( "Unknown host name" );
380 }
381
382 for ( Replica peer:peerReplicas )
383 {
384 if ( ids.contains( peer.getId() ) )
385 {
386 log.error( "Peer replica ID '{}' has already been declared.", peer.getId() );
387 throw new ReplicationConfigurationException( "Peer replica ID '" + peer.getId()
388 + "' has already been declared." );
389 }
390
391
392 String replicaServer = StringTools.lowerCase( peer.getAddress().getHostName() );
393 int replicaPort = peer.getAddress().getPort();
394
395 if ( servers.containsKey( replicaServer ) )
396 {
397 int peerPort = servers.get( replicaServer );
398
399 if ( replicaPort == peerPort )
400 {
401 log.error(
402 "The replica in the peer list has already been declared on the server {} with the port {}",
403 replicaServer, peerPort );
404 throw new ReplicationConfigurationException( "Replication store is not specified." );
405 }
406 }
407
408 servers.put( replicaServer, replicaPort );
409 }
410 }
411 }