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