Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ephemeris] Add option for disabling the cache #4491

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,19 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi
public static final String CONFIG_COUNTRY = "country";
public static final String CONFIG_REGION = "region";
public static final String CONFIG_CITY = "city";
public static final String CONFIG_CACHE_FILES = "cache";

private static final String RESOURCES_ROOT = "jollyday/";
private static final String JOLLYDAY_COUNTRY_DESCRIPTIONS = RESOURCES_ROOT
+ "descriptions/country_descriptions.properties";
private static final String PROPERTY_COUNTRY_DESCRIPTION_PREFIX = "country.description.";
private static final String PROPERTY_COUNTRY_DESCRIPTION_DELIMITER = "\\.";

final List<ParameterOption> countries = new ArrayList<>();
final Map<String, List<ParameterOption>> regions = new HashMap<>();
final Map<String, List<ParameterOption>> cities = new HashMap<>();
private final List<ParameterOption> countries = new ArrayList<>();
private final Map<String, List<ParameterOption>> regions = new HashMap<>();
private final Map<String, List<ParameterOption>> cities = new HashMap<>();

final Map<String, Set<DayOfWeek>> daysets = new HashMap<>();
private final Map<String, Set<DayOfWeek>> daysets = new HashMap<>();
private final Map<Object, HolidayManager> holidayManagers = new HashMap<>();
private final List<String> countryParameters = new ArrayList<>();

Expand All @@ -101,6 +102,7 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi

private @NonNullByDefault({}) String country;
private @Nullable String region;
private boolean cacheFiles = true;

@Activate
public EphemerisManagerImpl(final @Reference LocaleProvider localeProvider, final BundleContext bundleContext) {
Expand Down Expand Up @@ -141,8 +143,7 @@ protected void modified(Map<String, Object> config) {
if (setNameParts.length > 1) {
String setName = setNameParts[1];
Object entry = e.getValue();
if (entry instanceof String) {
String value = entry.toString();
if (entry instanceof String value) {
while (value.startsWith("[")) {
value = value.substring(1);
}
Expand All @@ -166,27 +167,26 @@ protected void modified(Map<String, Object> config) {
}
});

Object configValue = config.get(CONFIG_COUNTRY);
if (configValue != null) {
country = configValue.toString().toLowerCase();
if (config.get(CONFIG_COUNTRY) instanceof String string) {
country = string.toLowerCase();
} else {
country = localeProvider.getLocale().getCountry().toLowerCase();
logger.debug("Using system default country '{}' ", country);
}

configValue = config.get(CONFIG_REGION);
if (configValue != null) {
String region = configValue.toString().toLowerCase();
if (config.get(CONFIG_REGION) instanceof String string) {
String region = string.toLowerCase();
countryParameters.add(region);
this.region = region;
} else {
this.region = null;
}

configValue = config.get(CONFIG_CITY);
if (configValue != null) {
countryParameters.add(configValue.toString());
if (config.get(CONFIG_CITY) instanceof String city) {
countryParameters.add(city);
}

cacheFiles = config.get(CONFIG_CACHE_FILES) instanceof String cache ? Boolean.getBoolean(cache) : true;
}

@Override
Expand Down Expand Up @@ -228,26 +228,31 @@ private URL getUrl(String filename) throws FileNotFoundException {
} catch (MalformedURLException e) {
throw new FileNotFoundException(e.getMessage());
}
} else {
throw new FileNotFoundException(filename);
}
throw new FileNotFoundException(filename);
}

private HolidayManager getHolidayManager(Object managerKey) {
HolidayManager holidayManager = holidayManagers.get(managerKey);
if (holidayManager == null) {
final ManagerParameter parameters;
boolean cacheManager = true;
if (managerKey instanceof String stringKey) {
URL urlOverride = bundle
.getResource(RESOURCES_ROOT + CalendarPartManagerParameter.getConfigurationFileName(stringKey));
parameters = urlOverride != null //
? ManagerParameters.create(urlOverride)
: ManagerParameters.create(stringKey);
} else if (managerKey instanceof URL url) {
parameters = ManagerParameters.create(url);
cacheManager = cacheFiles;
} else {
parameters = ManagerParameters.create((URL) managerKey);
throw new IllegalArgumentException("managerKey has to be either a country either an URL");
}
holidayManager = HolidayManager.getInstance(parameters);
holidayManagers.put(managerKey, holidayManager);
if (cacheManager) {
holidayManagers.put(managerKey, holidayManager);
}
}
return holidayManager;
}
Expand Down Expand Up @@ -279,7 +284,8 @@ private long getDaysUntil(ZonedDateTime from, String searchedHoliday, HolidayMan
List<Holiday> sortedHolidays = getHolidays(from, 366, holidayManager);
Optional<Holiday> result = sortedHolidays.stream()
.filter(holiday -> searchedHoliday.equalsIgnoreCase(holiday.getPropertiesKey())).findFirst();
return result.map(holiday -> from.toLocalDate().until(holiday.getDate(), ChronoUnit.DAYS)).orElse(-1L);
return result.get() instanceof Holiday holiday ? from.toLocalDate().until(holiday.getDate(), ChronoUnit.DAYS)
: -1L;
}

private @Nullable String getFirstBankHolidayKey(ZonedDateTime from, int span, HolidayManager holidayManager) {
Expand Down Expand Up @@ -351,10 +357,11 @@ public boolean isInDayset(String daysetName, ZonedDateTime date) {

private void addDayset(String setName, Iterable<?> values) {
Set<DayOfWeek> dayset = new HashSet<>();
for (Object day : values) {
// fix illegal entries by stripping all non A-Z characters
String dayString = day.toString().toUpperCase().replaceAll("[^A-Z]", "");
dayset.add(DayOfWeek.valueOf(dayString));
for (Object value : values) {
if (value instanceof String day) {
// fix illegal entries by stripping all non A-Z characters
dayset.add(DayOfWeek.valueOf(day.toUpperCase().replaceAll("[^A-Z]", "")));
}
}
daysets.put(setName, dayset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
<description>Get the holidays for a specific city (e.g. New York City = "nyc").</description>
<advanced>true</advanced>
</parameter>
<parameter name="cache" type="boolean">
<label>Cache Files</label>
<description>User defined configuration files are cached once loaded. When set to false, files will be read for each
access.</description>
<advanced>true</advanced>
<default>true</default>
</parameter>
</config-description>

</config-description:config-descriptions>
Loading