You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContextHelper.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth.ldap;
  21. import javax.annotation.Nullable;
  22. import javax.naming.Context;
  23. import javax.naming.NamingException;
  24. import org.sonar.api.utils.log.Logger;
  25. import org.sonar.api.utils.log.Loggers;
  26. /**
  27. * @author Evgeny Mandrikov
  28. */
  29. public final class ContextHelper {
  30. private static final Logger LOG = Loggers.get(ContextHelper.class);
  31. private ContextHelper() {
  32. }
  33. /**
  34. * <pre>
  35. * public void useContextNicely() throws NamingException {
  36. * InitialDirContext context = null;
  37. * boolean threw = true;
  38. * try {
  39. * context = new InitialDirContext();
  40. * // Some code which does something with the Context and may throw a NamingException
  41. * threw = false; // No throwable thrown
  42. * } finally {
  43. * // Close context
  44. * // If an exception occurs, only rethrow it if (threw==false)
  45. * close(context, threw);
  46. * }
  47. * }
  48. * </pre>
  49. *
  50. * @param context the {@code Context} object to be closed, or null, in which case this method does nothing
  51. * @param swallowIOException if true, don't propagate {@code NamingException} thrown by the {@code close} method
  52. * @throws NamingException if {@code swallowIOException} is false and {@code close} throws a {@code NamingException}.
  53. */
  54. public static void close(@Nullable Context context, boolean swallowIOException) throws NamingException {
  55. if (context == null) {
  56. return;
  57. }
  58. try {
  59. context.close();
  60. } catch (NamingException e) {
  61. if (swallowIOException) {
  62. LOG.warn("NamingException thrown while closing context.", e);
  63. } else {
  64. throw e;
  65. }
  66. }
  67. }
  68. public static void closeQuietly(@Nullable Context context) {
  69. try {
  70. close(context, true);
  71. } catch (NamingException e) {
  72. LOG.error("Unexpected NamingException", e);
  73. }
  74. }
  75. }