|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2019 Slava Semushin <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.feature.country; |
| 19 | + |
| 20 | +import lombok.RequiredArgsConstructor; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import ru.mystamps.web.common.LinkEntityDto; |
| 24 | +import ru.mystamps.web.support.togglz.Features; |
| 25 | + |
| 26 | +import java.util.Date; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.Callable; |
| 30 | + |
| 31 | +/** |
| 32 | + * Implementation that delegates calls to a country service when the feature is enabled. |
| 33 | + * |
| 34 | + * When the {@code USE_COUNTRY_MICROSERVICE} feature is disabled or when a call has failed, |
| 35 | + * it falls back to the default implementation. |
| 36 | + * |
| 37 | + * @see ApiCountryService |
| 38 | + * @see CountryServiceImpl |
| 39 | + */ |
| 40 | +@RequiredArgsConstructor |
| 41 | +@SuppressWarnings("PMD.TooManyMethods") |
| 42 | +public class TogglzWithFallbackCountryService implements CountryService { |
| 43 | + |
| 44 | + private static final Logger LOG = |
| 45 | + LoggerFactory.getLogger(TogglzWithFallbackCountryService.class); |
| 46 | + |
| 47 | + private final ApiCountryService apiService; |
| 48 | + private final CountryServiceImpl fallbackService; |
| 49 | + |
| 50 | + @Override |
| 51 | + public String add(AddCountryDto dto, Integer userId) { |
| 52 | + return executeOneOf( |
| 53 | + () -> apiService.add(dto, userId), |
| 54 | + () -> fallbackService.add(dto, userId) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public List<Integer> findIdsByNames(List<String> names) { |
| 60 | + return executeOneOf( |
| 61 | + () -> apiService.findIdsByNames(names), |
| 62 | + () -> fallbackService.findIdsByNames(names) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public List<Integer> findIdsWhenNameStartsWith(String name) { |
| 68 | + return executeOneOf( |
| 69 | + () -> apiService.findIdsWhenNameStartsWith(name), |
| 70 | + () -> fallbackService.findIdsWhenNameStartsWith(name) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public List<LinkEntityDto> findAllAsLinkEntities(String lang) { |
| 76 | + return executeOneOf( |
| 77 | + () -> apiService.findAllAsLinkEntities(lang), |
| 78 | + () -> fallbackService.findAllAsLinkEntities(lang) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public LinkEntityDto findOneAsLinkEntity(String slug, String lang) { |
| 84 | + return executeOneOf( |
| 85 | + () -> apiService.findOneAsLinkEntity(slug, lang), |
| 86 | + () -> fallbackService.findOneAsLinkEntity(slug, lang) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public long countAll() { |
| 92 | + return executeOneOf( |
| 93 | + apiService::countAll, |
| 94 | + fallbackService::countAll |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public long countCountriesOf(Integer collectionId) { |
| 100 | + return executeOneOf( |
| 101 | + () -> apiService.countCountriesOf(collectionId), |
| 102 | + () -> fallbackService.countCountriesOf(collectionId) |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public long countBySlug(String slug) { |
| 108 | + return executeOneOf( |
| 109 | + () -> apiService.countBySlug(slug), |
| 110 | + () -> fallbackService.countBySlug(slug) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public long countByName(String name) { |
| 116 | + return executeOneOf( |
| 117 | + () -> apiService.countByName(name), |
| 118 | + () -> fallbackService.countByName(name) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public long countByNameRu(String name) { |
| 124 | + return executeOneOf( |
| 125 | + () -> apiService.countByNameRu(name), |
| 126 | + () -> fallbackService.countByNameRu(name) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public long countAddedSince(Date date) { |
| 132 | + return executeOneOf( |
| 133 | + () -> apiService.countAddedSince(date), |
| 134 | + () -> fallbackService.countAddedSince(date) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public long countUntranslatedNamesSince(Date date) { |
| 140 | + return executeOneOf( |
| 141 | + () -> apiService.countUntranslatedNamesSince(date), |
| 142 | + () -> fallbackService.countUntranslatedNamesSince(date) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Map<String, Integer> getStatisticsOf(Integer collectionId, String lang) { |
| 148 | + return executeOneOf( |
| 149 | + () -> apiService.getStatisticsOf(collectionId, lang), |
| 150 | + () -> fallbackService.getStatisticsOf(collectionId, lang) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String suggestCountryForUser(Integer userId) { |
| 156 | + return executeOneOf( |
| 157 | + () -> apiService.suggestCountryForUser(userId), |
| 158 | + () -> fallbackService.suggestCountryForUser(userId) |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + private static <T> T executeOneOf(Callable<T> firstImpl, Callable<T> defaultImpl) { |
| 163 | + try { |
| 164 | + if (Features.USE_COUNTRY_MICROSERVICE.isActive()) { |
| 165 | + try { |
| 166 | + return firstImpl.call(); |
| 167 | + } catch (UnsupportedOperationException ignored) { |
| 168 | + // the method isn't yet implemented, fallback to the default implementation |
| 169 | + |
| 170 | + } catch (RuntimeException e) { // NOPMD: AvoidCatchingGenericException; catch-all |
| 171 | + LOG.warn( |
| 172 | + "Failed to call a country service. Fallback to default implementation", |
| 173 | + e |
| 174 | + ); |
| 175 | + } |
| 176 | + } |
| 177 | + return defaultImpl.call(); |
| 178 | + |
| 179 | + } catch (Exception ex) { // NOPMD: AvoidCatchingGenericException; try to catch-all |
| 180 | + throw new RuntimeException(ex); // NOPMD: AvoidThrowingRawExceptionTypes |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments