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