001    /*
002     * Copyright 2009-2015 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2015 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk.unboundidds.monitors;
022    
023    
024    
025    import java.io.Serializable;
026    import java.text.SimpleDateFormat;
027    import java.util.Date;
028    
029    import com.unboundid.util.NotMutable;
030    import com.unboundid.util.ThreadSafety;
031    import com.unboundid.util.ThreadSafetyLevel;
032    
033    import static com.unboundid.util.Debug.*;
034    
035    
036    
037    /**
038     * <BLOCKQUOTE>
039     *   <B>NOTE:</B>  This class is part of the Commercial Edition of the UnboundID
040     *   LDAP SDK for Java.  It is not available for use in applications that
041     *   include only the Standard Edition of the LDAP SDK, and is not supported for
042     *   use in conjunction with non-UnboundID products.
043     * </BLOCKQUOTE>
044     * This class provides a data structure that contains information about a
045     * replication server contained in a replication summary monitor entry.
046     */
047    @NotMutable()
048    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
049    public final class ReplicationSummaryReplicationServer
050           implements Serializable
051    {
052      /**
053       * The serial version UID for this serializable class.
054       */
055      private static final long serialVersionUID = -3021672478708746554L;
056    
057    
058    
059      // The date of the last successful connection to this replication server.
060      private final Date replicationServerLastConnected;
061    
062      // The date of the last failed connection to this replication server.
063      private final Date replicationServerLastFailed;
064    
065      // The number of times connection attempts to this replication server have
066      // failed. The counter is reset after a successful connection.
067      private final Long replicationServerFailedAttempts;
068    
069      // The port number for this replication server.
070      private final Long replicationServerPort;
071    
072      // The generation ID for this replication server.
073      private final String generationID;
074    
075      // The address for this replication server.
076      private final String replicationServerAddress;
077    
078      // The replication server ID for this replication server.
079      private final String replicationServerID;
080    
081      // The status for this replication server.
082      private final String replicationServerStatus;
083    
084      // The value used to create this replication summary replica object.
085      private final String stringRepresentation;
086    
087    
088    
089      /**
090       * Creates a new replication summary replication server object from the
091       * provided string representation.
092       *
093       * @param  value  The value string to be parsed as a replication summary
094       *                replication server object.
095       */
096      public ReplicationSummaryReplicationServer(final String value)
097      {
098        stringRepresentation = value;
099    
100        generationID        = getElementValue(value, "generation-id");
101        replicationServerID = getElementValue(value, "server-id");
102    
103        final String hostPort = getElementValue(value, "server");
104        if (hostPort == null)
105        {
106          replicationServerAddress = null;
107          replicationServerPort    = null;
108        }
109        else
110        {
111          Long p;
112          String a;
113    
114          try
115          {
116            final int colonPos = hostPort.indexOf(':');
117            a = hostPort.substring(0, colonPos);
118            p = Long.parseLong(hostPort.substring(colonPos+1));
119          }
120          catch (Exception e)
121          {
122            debugException(e);
123            a = null;
124            p = null;
125          }
126    
127          replicationServerAddress = a;
128          replicationServerPort    = p;
129        }
130    
131        replicationServerStatus = getElementValue(value, "status");
132        replicationServerLastConnected  =
133             getElementDateValue(value, "last-connected");
134        replicationServerLastFailed = getElementDateValue(value, "last-failed");
135        replicationServerFailedAttempts =
136             getElementLongValue(value, "failed-attempts");
137      }
138    
139    
140    
141      /**
142       * Retrieves the value for the specified element in the replica string.
143       *
144       * @param  s  The string to be parsed.
145       * @param  n  The name of the element for which to retrieve the value.
146       *
147       * @return  The value for the specified element in the replica string, or
148       *          {@code null} if it was not present or could not be determined.
149       */
150      private static String getElementValue(final String s, final String n)
151      {
152        final String nPlusEQ = n + "=\"";
153    
154        int pos = s.indexOf(nPlusEQ);
155        if (pos < 0)
156        {
157          return null;
158        }
159        pos += nPlusEQ.length();
160    
161        final int closePos = s.indexOf('"', pos);
162        if (closePos <= pos)
163        {
164          return null;
165        }
166    
167        return s.substring(pos, closePos);
168      }
169    
170    
171    
172      /**
173       * Retrieves the value for the specified element in the replica string as a
174       * {@code Date} object.
175       *
176       * @param  s  The string to be parsed.
177       * @param  n  The name of the element for which to retrieve the value.
178       *
179       * @return  The value for the specified element in the replica string as a
180       *          {@code Date}, or {@code null} if it was not present or could not
181       *          be determined or parsed as a {@code Date}.
182       */
183      private static Date getElementDateValue(final String s, final String n)
184      {
185        final String stringValue = getElementValue(s, n);
186        if (stringValue == null)
187        {
188          return null;
189        }
190    
191        try
192        {
193          final SimpleDateFormat f =
194               new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
195          return f.parse(stringValue);
196        }
197        catch (final Exception e)
198        {
199          debugException(e);
200          return null;
201        }
202      }
203    
204    
205    
206      /**
207       * Retrieves the value for the specified element in the replica string as a
208       * {@code Long} object.
209       *
210       * @param  s  The string to be parsed.
211       * @param  n  The name of the element for which to retrieve the value.
212       *
213       * @return  The value for the specified element in the replica string as a
214       *          {@code Long}, or {@code null} if it was not present or could not
215       *          be determined or parsed as a {@code Long}.
216       */
217      private static Long getElementLongValue(final String s, final String n)
218      {
219        final String stringValue = getElementValue(s, n);
220        if (stringValue == null)
221        {
222          return null;
223        }
224    
225        try
226        {
227          return Long.valueOf(stringValue);
228        }
229        catch (final Exception e)
230        {
231          debugException(e);
232          return null;
233        }
234      }
235    
236    
237    
238      /**
239       * Retrieves the replication server ID for this replication server.
240       *
241       * @return  The replication server ID for this replication server, or
242       *          {@code null} if that information is not available.
243       */
244      public String getReplicationServerID()
245      {
246        return replicationServerID;
247      }
248    
249    
250    
251      /**
252       * Retrieves the address used to communicate with this replication server.
253       *
254       * @return  The address used to communicate with this replication server, or
255       *          {@code null} if that information is not available.
256       */
257      public String getReplicationServerAddress()
258      {
259        return replicationServerAddress;
260      }
261    
262    
263    
264      /**
265       * Retrieves the port number used to communicate with this replication server.
266       *
267       * @return  The port number used to communicate with this replication server,
268       *          or {@code null} if that information is not available.
269       */
270      public Long getReplicationServerPort()
271      {
272        return replicationServerPort;
273      }
274    
275    
276    
277      /**
278       * Retrieves the generation ID for this replication server.
279       *
280       * @return  The generation ID for this replication server, or {@code null} if
281       *          that information is not available.
282       */
283      public String getGenerationID()
284      {
285        return generationID;
286      }
287    
288    
289    
290      /**
291       * Retrieves the status for this replication server.
292       *
293       * @return  The status for this replication server, or {@code null} if
294       *          that information is not available.
295       */
296      public String getReplicationServerStatus()
297      {
298        return replicationServerStatus;
299      }
300    
301    
302    
303      /**
304       * Retrieves the date of the last successful connection to this replication
305       * server.
306       *
307       * @return  The the date of the last successful connection to this replication
308       *          server, or {@code null} if that information is not available.
309       */
310      public Date getReplicationServerLastConnected()
311      {
312        return replicationServerLastConnected;
313      }
314    
315    
316    
317      /**
318       * Retrieves the date of the last failed connection to this replication
319       * server.
320       *
321       * @return  The the date of the last failed connection to this replication
322       *          server, or {@code null} if that information is not available.
323       */
324      public Date getReplicationServerLastFailed()
325      {
326        return replicationServerLastFailed;
327      }
328    
329    
330    
331      /**
332       * Retrieves the number of failed connection attempts since the last
333       * successful connection to this replication server.
334       *
335       * @return  The number of failed connection attempts since the last successful
336       *          connection to this replication server, or {@code null} if that
337       *          information is not available.
338       */
339      public Long getReplicationServerFailedAttempts()
340      {
341        return replicationServerFailedAttempts;
342      }
343    
344    
345    
346      /**
347       * Retrieves a string representation of this replication summary replica.
348       *
349       * @return  A string representation of this replication summary replica.
350       */
351      @Override()
352      public String toString()
353      {
354        return stringRepresentation;
355      }
356    }