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.

TimeoutConfigurationImpl.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.alm.client;
  21. import java.util.OptionalLong;
  22. import org.sonar.api.config.Configuration;
  23. import org.sonar.api.utils.log.Loggers;
  24. /**
  25. * Implementation of {@link TimeoutConfiguration} reading values from configuration properties.
  26. */
  27. public class TimeoutConfigurationImpl implements TimeoutConfiguration {
  28. private static final String CONNECT_TIMEOUT_PROPERTY = "sonar.alm.timeout.connect";
  29. private static final String READ_TIMEOUT_PROPERTY = "sonar.alm.timeout.read";
  30. private static final long DEFAULT_TIMEOUT = 30_000;
  31. private final Configuration configuration;
  32. public TimeoutConfigurationImpl(Configuration configuration) {
  33. this.configuration = configuration;
  34. }
  35. @Override
  36. public long getConnectTimeout() {
  37. return safelyParseLongValue(CONNECT_TIMEOUT_PROPERTY).orElse(DEFAULT_TIMEOUT);
  38. }
  39. private OptionalLong safelyParseLongValue(String property) {
  40. return configuration.get(property)
  41. .map(value -> {
  42. try {
  43. return OptionalLong.of(Long.parseLong(value));
  44. } catch (NumberFormatException e) {
  45. Loggers.get(TimeoutConfigurationImpl.class)
  46. .warn("Value of property {} can not be parsed to a long: {}", property, value);
  47. return OptionalLong.empty();
  48. }
  49. })
  50. .orElse(OptionalLong.empty());
  51. }
  52. @Override
  53. public long getReadTimeout() {
  54. return safelyParseLongValue(READ_TIMEOUT_PROPERTY).orElse(DEFAULT_TIMEOUT);
  55. }
  56. }