/ldaptive/core/trunk/src/main/java/org/ldaptive/pool/CompareValidator.java

http://vt-middleware.googlecode.com/ · Java · 99 lines · 47 code · 14 blank · 38 comment · 2 complexity · 592fc4ba9ce20c8db026ce032d3420d2 MD5 · raw file

  1. /*
  2. $Id: CompareValidator.java 2352 2012-04-11 14:00:33Z dfisher $
  3. Copyright (C) 2003-2012 Virginia Tech.
  4. All rights reserved.
  5. SEE LICENSE FOR MORE INFORMATION
  6. Author: Middleware Services
  7. Email: middleware@vt.edu
  8. Version: $Revision: 2352 $
  9. Updated: $Date: 2012-04-11 16:00:33 +0200 (Wed, 11 Apr 2012) $
  10. */
  11. package org.ldaptive.pool;
  12. import org.ldaptive.CompareOperation;
  13. import org.ldaptive.CompareRequest;
  14. import org.ldaptive.Connection;
  15. import org.ldaptive.LdapAttribute;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. /**
  19. * Validates a connection is healthy by performing a compare operation.
  20. *
  21. * @author Middleware Services
  22. * @version $Revision: 2352 $ $Date: 2012-04-11 16:00:33 +0200 (Wed, 11 Apr 2012) $
  23. */
  24. public class CompareValidator implements Validator<Connection>
  25. {
  26. /** Logger for this class. */
  27. protected final Logger logger = LoggerFactory.getLogger(getClass());
  28. /** Compare request to perform validation with. */
  29. private CompareRequest compareRequest;
  30. /** Creates a new compare validator. */
  31. public CompareValidator()
  32. {
  33. compareRequest = new CompareRequest();
  34. compareRequest.setDn("");
  35. compareRequest.setAttribute(new LdapAttribute("objectClass", "top"));
  36. }
  37. /**
  38. * Creates a new compare validator.
  39. *
  40. * @param cr to use for compares
  41. */
  42. public CompareValidator(final CompareRequest cr)
  43. {
  44. compareRequest = cr;
  45. }
  46. /**
  47. * Returns the compare request.
  48. *
  49. * @return compare request
  50. */
  51. public CompareRequest getCompareRequest()
  52. {
  53. return compareRequest;
  54. }
  55. /**
  56. * Sets the compare request.
  57. *
  58. * @param cr compare request
  59. */
  60. public void setCompareRequest(final CompareRequest cr)
  61. {
  62. compareRequest = cr;
  63. }
  64. /** {@inheritDoc} */
  65. @Override
  66. public boolean validate(final Connection c)
  67. {
  68. boolean success = false;
  69. if (c != null) {
  70. try {
  71. final CompareOperation compare = new CompareOperation(c);
  72. success = compare.execute(compareRequest).getResult();
  73. } catch (Exception e) {
  74. logger.debug(
  75. "validation failed for compare request {}",
  76. compareRequest,
  77. e);
  78. }
  79. }
  80. return success;
  81. }
  82. }