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.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   * The class stores a Replica, which is composed of an Id, a server and a port.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class Replica
36  {
37      /** The logger */
38      private static Logger log = LoggerFactory.getLogger( Replica.class );
39  
40      /** A speedup for logger */
41      private static final boolean IS_DEBUG = log.isDebugEnabled();
42  
43      /** The replicaId */
44      private final String id;
45  
46      /** The server address */
47      private final InetSocketAddress address;
48  
49  
50      /**
51       * Creates a new instance of Replica, from a String.
52       * 
53       * The String format is the following :
54       * 
55       * <replicaId>@<server>:<port>
56       * 
57       * @param replica The replica to create
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      * Creates a new instance of Replica, from a valid Id and a valid address.
119      *
120      * @param id The Replica Id
121      * @param address The server address.
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      * @return the replica address
135      */
136     public InetSocketAddress getAddress()
137     {
138         return address;
139     }
140 
141 
142     /**
143      * @return the replica Id
144      */
145     public String getId()
146     {
147         return id;
148     }
149 
150 
151     /**
152      * Compute the instance's hash code
153      * @return the instance's hash code 
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      * @return The replica. The format is &lt;replica id> '@' &lt;server> ':' &lt;port>
181      */
182     public String toString()
183     {
184         return getId().toString() + '@' + getAddress().getAddress().getHostAddress() + ':' + getAddress().getPort();
185     }
186 }