Skip to content

Commit

Permalink
resolve calendar's resources via classpath (#1592)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauernfeind authored Nov 19, 2021
1 parent e76af61 commit cca0073
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 37 deletions.
20 changes: 11 additions & 9 deletions DB/src/main/java/io/deephaven/db/util/GroovyDeephavenSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -794,15 +794,17 @@ public int priority() {
}
}

// note: Calendars has an implicit dependency to calendar config paths, which aren't currently
// present
/*
* public static class Calendars implements InitScript {
*
* @Override public String getScriptPath() { return "groovy/2-calendars.groovy"; }
*
* @Override public int priority() { return 2; } }
*/
public static class Calendars implements InitScript {
@Override
public String getScriptPath() {
return "groovy/2-calendars.groovy";
}

@Override
public int priority() {
return 2;
}
}

public static class CountMetrics implements InitScript {
@Override
Expand Down
52 changes: 27 additions & 25 deletions DB/src/main/java/io/deephaven/util/calendar/Calendars.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,30 @@
import io.deephaven.configuration.Configuration;
import io.deephaven.io.logger.Logger;
import io.deephaven.db.tables.utils.NameValidator;
import io.deephaven.util.files.ResourceResolution;
import io.deephaven.internal.log.LoggerFactory;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.io.InputStreamReader;
import java.nio.file.NoSuchFileException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
* A collection of business calendars.
*/
public class Calendars implements Map<String, BusinessCalendar> {

private static final Logger logger = LoggerFactory.getLogger(Calendars.class);
private static final String SUFFIX = ".calendar";
private static final String BUSINESS_CALENDAR_PROP_INTERNAL = "Calendar.internalPath";
private static final String BUSINESS_CALENDAR_PROP_USER = "Calendar.resourcePath";
private static final String BUSINESS_CALENDAR_PROP_INTERNAL = "Calendar.importPath";
private static final String BUSINESS_CALENDAR_PROP_USER = "Calendar.userImportPath";
private static final Calendars instance = new Calendars();
private static final String defaultName = Configuration.getInstance().getProperty("Calendar.default");

Expand Down Expand Up @@ -88,7 +87,7 @@ public static String getDefaultName() {
* @return names of all available calendars
*/
public static String[] calendarNames() {
return instance.keySet().stream().toArray(String[]::new);
return instance.keySet().toArray(String[]::new);
}


Expand All @@ -108,46 +107,49 @@ private Calendars() {
}

private void loadProperty(final Configuration configuration, final String property) {
final String locations = configuration.getProperty(property);
final String location = configuration.getProperty(property);
try {
load(configuration, locations);
load(location);
} catch (NoSuchFileException e) {
logger.warn().append("Problem loading calendars. locations=").append(locations).append(e).endl();
logger.warn().append("Problem loading calendars. importPath=").append(location).append(e).endl();
}
}

private void load(final Configuration configuration, final String businessCalendarLocations)
private void load(final String businessCalendarConfig)
throws NoSuchFileException {
final ResourceResolution resourceResolution =
new io.deephaven.util.files.ResourceResolution(configuration, ";", businessCalendarLocations);
final InputStream configToLoad = this.getClass().getResourceAsStream(businessCalendarConfig);

final BiConsumer<URL, String> consumer = (URL, filePath) -> {
if (configToLoad == null) {
logger.warn("Could not find " + businessCalendarConfig + " on classpath");
throw new RuntimeException("Could not open " + businessCalendarConfig + " from classpath");
}

final Consumer<String> consumer = (filePath) -> {
try {
final InputStream inputStream = URL.openStream();
final InputStream inputStream = this.getClass().getResourceAsStream(filePath);
if (inputStream != null) {
final File calendarFile = inputStreamToFile(inputStream);
final BusinessCalendar businessCalendar = DefaultBusinessCalendar.getInstance(calendarFile);
addCalendar(businessCalendar);
calendarFile.deleteOnExit();
} else {
logger.warn("Could not open " + URL);
throw new RuntimeException("Could not open " + URL);
logger.warn("Could not open " + filePath + " from classpath");
throw new RuntimeException("Could not open " + filePath + " from classpath");
}
} catch (IOException e) {
logger.warn("Problem loading calendar: locations=" + businessCalendarLocations, e);
throw new RuntimeException("Problem loading calendar: locations=" + businessCalendarLocations, e);
logger.warn("Problem loading calendar: location=" + businessCalendarConfig, e);
throw new RuntimeException("Problem loading calendar: location=" + businessCalendarConfig, e);
}
};


try {
resourceResolution.findResources(SUFFIX, consumer);
try (final BufferedReader config = new BufferedReader(new InputStreamReader(configToLoad))) {
config.lines().forEach(consumer);
} catch (NoSuchFileException e) {
logger.warn("Problem loading calendar: locations=" + businessCalendarLocations, e);
logger.warn("Problem loading calendar: location=" + businessCalendarConfig, e);
throw e;
} catch (IOException e) {
logger.warn("Problem loading calendar: locations=" + businessCalendarLocations, e);
throw new RuntimeException("Problem loading calendar: locations=" + businessCalendarLocations, e);
logger.warn("Problem loading calendar: location=" + businessCalendarConfig, e);
throw new RuntimeException("Problem loading calendar: location=" + businessCalendarConfig, e);
}
}

Expand Down
12 changes: 12 additions & 0 deletions configs/default_calendar_imports.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/calendar/BEIJING.calendar
/calendar/DTB.calendar
/calendar/GBLO.calendar
/calendar/GLOBAL.calendar
/calendar/JPOSE.calendar
/calendar/LSE.calendar
/calendar/MOSCOW.calendar
/calendar/MUMBAI.calendar
/calendar/USNY.calendar
/calendar/USNYSE.calendar
/calendar/USNYSEWEEKDAYS.calendar
/calendar/UTC.calendar
2 changes: 1 addition & 1 deletion configs/dh-defaults.prop
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ smtp.mx.domain=io.deephaven
Calendar.default=USNYSE
# The etc path is where things are installed on a server, the configs path is
# for development. This needs to be overridden for a console from a launcher.
Calendar.internalPath=<devroot>/etc/calendar;<devroot>/configs/calendar;/etc/sysconfig/deephaven.d/calendars
Calendar.importPath=/default_calendar_imports.txt

# Plot Themes
Plot.theme.default=LIGHT
Expand Down
2 changes: 1 addition & 1 deletion configs/grpc-api.prop
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ StringUtils.cacheSize=0
TrackedFileHandleFactory.maxOpenFiles=1024

Calendar.default=USNYSE
Calendar.internalPath=<devroot>/configs/calendar
Calendar.importPath=/default_calendar_imports.txt

Plot.theme.default=LIGHT
Plot.theme.internalPath=<devroot>/configs/chartthemes
Expand Down
2 changes: 1 addition & 1 deletion test-configs/dh-tests.prop
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TableDataRefreshService.tableLocationsRefreshMillis=10000
TableDataRefreshService.tableSizeRefreshMillis=1000

Calendar.default=USNYSE
Calendar.internalPath=<devroot>/configs/calendar
Calendar.importPath=/default_calendar_imports.txt

Plot.theme.default=LIGHT
Plot.theme.internalPath=<devroot>/configs/chartthemes
Expand Down

0 comments on commit cca0073

Please sign in to comment.