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.common;
21
22
23 import java.net.InetSocketAddress;
24
25 import org.apache.directory.shared.ldap.util.StringTools;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34
35 public class Replica
36 {
37
38 private static Logger log = LoggerFactory.getLogger( Replica.class );
39
40
41 private static final boolean IS_DEBUG = log.isDebugEnabled();
42
43
44 private final String id;
45
46
47 private final InetSocketAddress address;
48
49
50
51
52
53
54
55
56
57
58
59 public Replica( String replica )
60 {
61 if ( StringTools.isEmpty( replica ) )
62 {
63 log.error( "Null or empty replica are not allowed" );
64 throw new IllegalArgumentException( "Null or empty Replica " );
65 }
66
67 replica = replica.trim();
68
69 int atPos = replica.indexOf( '@' );
70
71 if ( atPos <= 0 )
72 {
73 log.error( "The ReplicaId '@' element is missing in {}", replica );
74 throw new IllegalArgumentException( "Replica ID not found: " + replica );
75 }
76
77 int colonPos = replica.indexOf( ':', atPos );
78
79 if ( colonPos < 0 )
80 {
81 log.error( "Replica port not found in {}", replica );
82 throw new IllegalArgumentException( "Port number not found in replica : " + replica );
83 }
84
85 id = replica.substring( 0, atPos );
86 String server = replica.substring( atPos + 1, colonPos );
87 int port = -1;
88
89 try
90 {
91 port = Integer.parseInt( replica.substring( colonPos + 1 ) );
92 }
93 catch ( NumberFormatException nfe )
94 {
95 log.error( "The port value should be a value between 1 and 65535, port : {}", new Integer( port ) );
96 throw new IllegalArgumentException( "Bad port number : " + port );
97 }
98
99 try
100 {
101 address = new InetSocketAddress( server, port );
102 }
103 catch ( IllegalArgumentException iae )
104 {
105 log.error( "The server address/name is invalid ({}) in replica {}", server, replica );
106 throw new IllegalArgumentException( "The server address/name is invalid in replica " + replica
107 + ", error : " + iae.getMessage() );
108 }
109
110 if ( IS_DEBUG )
111 {
112 log.debug( "Created a replica {} on server {}", id, server + ':' + port );
113 }
114 }
115
116
117
118
119
120
121
122
123 public Replica( String id, InetSocketAddress address )
124 {
125 assert id != null;
126 assert address != null;
127
128 this.id = id;
129 this.address = address;
130 }
131
132
133
134
135
136 public InetSocketAddress getAddress()
137 {
138 return address;
139 }
140
141
142
143
144
145 public String getId()
146 {
147 return id;
148 }
149
150
151
152
153
154
155 public int hashCode()
156 {
157 return id.hashCode();
158 }
159
160
161 public boolean equals( Object o )
162 {
163 if ( o == this )
164 {
165 return true;
166 }
167
168 if ( o instanceof Replica )
169 {
170 return this.id.equals( ( ( Replica ) o ).id );
171 }
172 else
173 {
174 return false;
175 }
176 }
177
178
179
180
181
182 public String toString()
183 {
184 return getId().toString() + '@' + getAddress().getAddress().getHostAddress() + ':' + getAddress().getPort();
185 }
186 }