001/*
002 * Copyright 2009-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2020 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) 2015-2020 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.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049
050
051/**
052 * This class provides a data structure that contains information about a
053 * replication server contained in a replication summary monitor entry.
054 * <BR>
055 * <BLOCKQUOTE>
056 *   <B>NOTE:</B>  This class, and other classes within the
057 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
058 *   supported for use against Ping Identity, UnboundID, and
059 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
060 *   for proprietary functionality or for external specifications that are not
061 *   considered stable or mature enough to be guaranteed to work in an
062 *   interoperable way with other types of LDAP servers.
063 * </BLOCKQUOTE>
064 */
065@NotMutable()
066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
067public final class ReplicationSummaryReplicationServer
068       implements Serializable
069{
070  /**
071   * The serial version UID for this serializable class.
072   */
073  private static final long serialVersionUID = -3021672478708746554L;
074
075
076
077  // The date of the last successful connection to this replication server.
078  private final Date replicationServerLastConnected;
079
080  // The date of the last failed connection to this replication server.
081  private final Date replicationServerLastFailed;
082
083  // The number of times connection attempts to this replication server have
084  // failed. The counter is reset after a successful connection.
085  private final Long replicationServerFailedAttempts;
086
087  // The port number for this replication server.
088  private final Long replicationServerPort;
089
090  // The generation ID for this replication server.
091  private final String generationID;
092
093  // The address for this replication server.
094  private final String replicationServerAddress;
095
096  // The replication server ID for this replication server.
097  private final String replicationServerID;
098
099  // The status for this replication server.
100  private final String replicationServerStatus;
101
102  // The value used to create this replication summary replica object.
103  private final String stringRepresentation;
104
105
106
107  /**
108   * Creates a new replication summary replication server object from the
109   * provided string representation.
110   *
111   * @param  value  The value string to be parsed as a replication summary
112   *                replication server object.
113   */
114  public ReplicationSummaryReplicationServer(final String value)
115  {
116    stringRepresentation = value;
117
118    generationID        = getElementValue(value, "generation-id");
119    replicationServerID = getElementValue(value, "server-id");
120
121    final String hostPort = getElementValue(value, "server");
122    if (hostPort == null)
123    {
124      replicationServerAddress = null;
125      replicationServerPort    = null;
126    }
127    else
128    {
129      Long p;
130      String a;
131
132      try
133      {
134        final int colonPos = hostPort.indexOf(':');
135        a = hostPort.substring(0, colonPos);
136        p = Long.parseLong(hostPort.substring(colonPos+1));
137      }
138      catch (final Exception e)
139      {
140        Debug.debugException(e);
141        a = null;
142        p = null;
143      }
144
145      replicationServerAddress = a;
146      replicationServerPort    = p;
147    }
148
149    replicationServerStatus = getElementValue(value, "status");
150    replicationServerLastConnected  =
151         getElementDateValue(value, "last-connected");
152    replicationServerLastFailed = getElementDateValue(value, "last-failed");
153    replicationServerFailedAttempts =
154         getElementLongValue(value, "failed-attempts");
155  }
156
157
158
159  /**
160   * Retrieves the value for the specified element in the replica string.
161   *
162   * @param  s  The string to be parsed.
163   * @param  n  The name of the element for which to retrieve the value.
164   *
165   * @return  The value for the specified element in the replica string, or
166   *          {@code null} if it was not present or could not be determined.
167   */
168  private static String getElementValue(final String s, final String n)
169  {
170    final String nPlusEQ = n + "=\"";
171
172    int pos = s.indexOf(nPlusEQ);
173    if (pos < 0)
174    {
175      return null;
176    }
177    pos += nPlusEQ.length();
178
179    final int closePos = s.indexOf('"', pos);
180    if (closePos <= pos)
181    {
182      return null;
183    }
184
185    return s.substring(pos, closePos);
186  }
187
188
189
190  /**
191   * Retrieves the value for the specified element in the replica string as a
192   * {@code Date} object.
193   *
194   * @param  s  The string to be parsed.
195   * @param  n  The name of the element for which to retrieve the value.
196   *
197   * @return  The value for the specified element in the replica string as a
198   *          {@code Date}, or {@code null} if it was not present or could not
199   *          be determined or parsed as a {@code Date}.
200   */
201  private static Date getElementDateValue(final String s, final String n)
202  {
203    final String stringValue = getElementValue(s, n);
204    if (stringValue == null)
205    {
206      return null;
207    }
208
209    try
210    {
211      final SimpleDateFormat f =
212           new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
213      return f.parse(stringValue);
214    }
215    catch (final Exception e)
216    {
217      Debug.debugException(e);
218      return null;
219    }
220  }
221
222
223
224  /**
225   * Retrieves the value for the specified element in the replica string as a
226   * {@code Long} object.
227   *
228   * @param  s  The string to be parsed.
229   * @param  n  The name of the element for which to retrieve the value.
230   *
231   * @return  The value for the specified element in the replica string as a
232   *          {@code Long}, or {@code null} if it was not present or could not
233   *          be determined or parsed as a {@code Long}.
234   */
235  private static Long getElementLongValue(final String s, final String n)
236  {
237    final String stringValue = getElementValue(s, n);
238    if (stringValue == null)
239    {
240      return null;
241    }
242
243    try
244    {
245      return Long.valueOf(stringValue);
246    }
247    catch (final Exception e)
248    {
249      Debug.debugException(e);
250      return null;
251    }
252  }
253
254
255
256  /**
257   * Retrieves the replication server ID for this replication server.
258   *
259   * @return  The replication server ID for this replication server, or
260   *          {@code null} if that information is not available.
261   */
262  public String getReplicationServerID()
263  {
264    return replicationServerID;
265  }
266
267
268
269  /**
270   * Retrieves the address used to communicate with this replication server.
271   *
272   * @return  The address used to communicate with this replication server, or
273   *          {@code null} if that information is not available.
274   */
275  public String getReplicationServerAddress()
276  {
277    return replicationServerAddress;
278  }
279
280
281
282  /**
283   * Retrieves the port number used to communicate with this replication server.
284   *
285   * @return  The port number used to communicate with this replication server,
286   *          or {@code null} if that information is not available.
287   */
288  public Long getReplicationServerPort()
289  {
290    return replicationServerPort;
291  }
292
293
294
295  /**
296   * Retrieves the generation ID for this replication server.
297   *
298   * @return  The generation ID for this replication server, or {@code null} if
299   *          that information is not available.
300   */
301  public String getGenerationID()
302  {
303    return generationID;
304  }
305
306
307
308  /**
309   * Retrieves the status for this replication server.
310   *
311   * @return  The status for this replication server, or {@code null} if
312   *          that information is not available.
313   */
314  public String getReplicationServerStatus()
315  {
316    return replicationServerStatus;
317  }
318
319
320
321  /**
322   * Retrieves the date of the last successful connection to this replication
323   * server.
324   *
325   * @return  The the date of the last successful connection to this replication
326   *          server, or {@code null} if that information is not available.
327   */
328  public Date getReplicationServerLastConnected()
329  {
330    return replicationServerLastConnected;
331  }
332
333
334
335  /**
336   * Retrieves the date of the last failed connection to this replication
337   * server.
338   *
339   * @return  The the date of the last failed connection to this replication
340   *          server, or {@code null} if that information is not available.
341   */
342  public Date getReplicationServerLastFailed()
343  {
344    return replicationServerLastFailed;
345  }
346
347
348
349  /**
350   * Retrieves the number of failed connection attempts since the last
351   * successful connection to this replication server.
352   *
353   * @return  The number of failed connection attempts since the last successful
354   *          connection to this replication server, or {@code null} if that
355   *          information is not available.
356   */
357  public Long getReplicationServerFailedAttempts()
358  {
359    return replicationServerFailedAttempts;
360  }
361
362
363
364  /**
365   * Retrieves a string representation of this replication summary replica.
366   *
367   * @return  A string representation of this replication summary replica.
368   */
369  @Override()
370  public String toString()
371  {
372    return stringRepresentation;
373  }
374}