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