DataProvidersManager.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.data;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.text.ParseException;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.Iterator;
  25. import java.util.LinkedHashSet;
  26. import java.util.List;
  27. import java.util.Set;
  28. import java.util.regex.Pattern;

  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.files.rinex.HatanakaCompressFilter;

  32. /** This class manages supported {@link DataProvider data providers}.
  33.  * <p>
  34.  * This class is the primary point of access for all data loading features. It
  35.  * is used for example to load Earth Orientation Parameters used by IERS frames,
  36.  * to load UTC leap seconds used by time scales, to load planetary ephemerides...
  37.  *
  38.  * <p>
  39.  * It is user-customizable: users can add their own data providers at will. This
  40.  * allows them for example to use a database or an existing data loading library
  41.  * in order to embed an Orekit enabled application in a global system with its
  42.  * own data handling mechanisms. There is no upper limitation on the number of
  43.  * providers, but often each application will use only a few.
  44.  * </p>
  45.  *
  46.  * <p>
  47.  * If the list of providers is empty when attempting to {@link #feed(String, DataLoader)
  48.  * feed} a file loader, the {@link #addDefaultProviders()} method is called
  49.  * automatically to set up a default configuration. This default configuration
  50.  * contains one {@link DataProvider data provider} for each component of the
  51.  * path-like list specified by the java property <code>orekit.data.path</code>.
  52.  * See the {@link #feed(String, DataLoader) feed} method documentation for further
  53.  * details. The default providers configuration is <em>not</em> set up if the list
  54.  * is not empty. If users want to have both the default providers and additional
  55.  * providers, they must call explicitly the {@link #addDefaultProviders()} method.
  56.  * </p>
  57.  *
  58.  * <p>
  59.  * The default configuration uses a predefined set of {@link DataFilter data filters}
  60.  * that already handled gzip-compressed files (recognized by the {@code .gz} suffix),
  61.  * Unix-compressed files (recognized by the {@code .Z} suffix) and Hatanaka compressed
  62.  * RINEX files. Users can access the {@link #getFiltersManager() filters manager} to
  63.  * set up custom filters for handling specific types of filters (decompression,
  64.  * deciphering...).
  65.  * </p>
  66.  *
  67.  * @author Luc Maisonobe
  68.  * @see DirectoryCrawler
  69.  * @see ClasspathCrawler
  70.  */
  71. public class DataProvidersManager {

  72.     /** Name of the property defining the root directories or zip/jar files path for default configuration. */
  73.     public static final String OREKIT_DATA_PATH = "orekit.data.path";

  74.     /** Supported data providers. */
  75.     private final List<DataProvider> providers;

  76.     /** Manager for filters.
  77.      * @since 11.0
  78.      */
  79.     private final FiltersManager filtersManager;

  80.     /** Loaded data. */
  81.     private final Set<String> loaded;

  82.     /** Build an instance with default configuration. */
  83.     public DataProvidersManager() {
  84.         providers      = new ArrayList<>();
  85.         filtersManager = new FiltersManager();
  86.         loaded         = new LinkedHashSet<>();
  87.         resetFiltersToDefault();
  88.     }

  89.     /** Get the manager for filters.
  90.      * @return filters manager
  91.      * @since 11.0
  92.      */
  93.     public FiltersManager getFiltersManager() {
  94.         return filtersManager;
  95.     }

  96.     /** Reset all filters to default.
  97.      * <p>
  98.      * This method {@link FiltersManager#clearFilters() clears} the
  99.      * {@link #getFiltersManager() filter manager} and then
  100.      * {@link FiltersManager#addFilter(DataFilter) adds} back the
  101.      * default filters
  102.      * </p>
  103.      * @since 11.0
  104.      */
  105.     public void resetFiltersToDefault() {

  106.         // clear the existing filters
  107.         filtersManager.clearFilters();

  108.         // set up predefined filters
  109.         filtersManager.addFilter(new GzipFilter());
  110.         filtersManager.addFilter(new UnixCompressFilter());
  111.         filtersManager.addFilter(new HatanakaCompressFilter());

  112.     }

  113.     /** Add the default providers configuration.
  114.      * <p>
  115.      * The default configuration contains one {@link DataProvider data provider}
  116.      * for each component of the path-like list specified by the java property
  117.      * <code>orekit.data.path</code>.
  118.      * </p>
  119.      * <p>
  120.      * If the property is not set or is null, no data will be available to the library
  121.      * (for example no pole corrections will be applied and only predefined UTC steps
  122.      * will be taken into account). No errors will be triggered in this case.
  123.      * </p>
  124.      * <p>
  125.      * If the property is set, it must contains a list of existing directories or zip/jar
  126.      * archives. One {@link DirectoryCrawler} instance will be set up for each
  127.      * directory and one {@link ZipJarCrawler} instance (configured to look for the
  128.      * archive in the filesystem) will be set up for each zip/jar archive. The list
  129.      * elements in the java property are separated using the standard path separator for
  130.      * the operating system as returned by {@link System#getProperty(String)
  131.      * System.getProperty("path.separator")}. This standard path separator is ":" on
  132.      * Linux and Unix type systems and ";" on Windows types systems.
  133.      * </p>
  134.      */
  135.     public void addDefaultProviders() {

  136.         // get the path containing all components
  137.         final String path = System.getProperty(OREKIT_DATA_PATH);
  138.         if (path != null && !"".equals(path)) {

  139.             // extract the various components
  140.             for (final String name : path.split(System.getProperty("path.separator"))) {
  141.                 if (!"".equals(name)) {

  142.                     final File file = new File(name);

  143.                     // check component
  144.                     if (!file.exists()) {
  145.                         if (DataProvider.ZIP_ARCHIVE_PATTERN.matcher(name).matches()) {
  146.                             throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_FILE, name);
  147.                         } else {
  148.                             throw new OrekitException(OrekitMessages.DATA_ROOT_DIRECTORY_DOES_NOT_EXIST, name);
  149.                         }
  150.                     }

  151.                     if (file.isDirectory()) {
  152.                         addProvider(new DirectoryCrawler(file));
  153.                     } else if (DataProvider.ZIP_ARCHIVE_PATTERN.matcher(name).matches()) {
  154.                         addProvider(new ZipJarCrawler(file));
  155.                     } else {
  156.                         throw new OrekitException(OrekitMessages.NEITHER_DIRECTORY_NOR_ZIP_OR_JAR, name);
  157.                     }

  158.                 }
  159.             }
  160.         }

  161.     }

  162.     /** Add a data provider to the supported list.
  163.      * @param provider data provider to add
  164.      * @see #removeProvider(DataProvider)
  165.      * @see #clearProviders()
  166.      * @see #isSupported(DataProvider)
  167.      * @see #getProviders()
  168.      */
  169.     public void addProvider(final DataProvider provider) {
  170.         providers.add(provider);
  171.     }

  172.     /** Remove one provider.
  173.      * @param provider provider instance to remove
  174.      * @return instance removed (null if the provider was not already present)
  175.      * @see #addProvider(DataProvider)
  176.      * @see #clearProviders()
  177.      * @see #isSupported(DataProvider)
  178.      * @see #getProviders()
  179.      * @since 5.1
  180.      */
  181.     public DataProvider removeProvider(final DataProvider provider) {
  182.         for (final Iterator<DataProvider> iterator = providers.iterator(); iterator.hasNext();) {
  183.             final DataProvider current = iterator.next();
  184.             if (current == provider) {
  185.                 iterator.remove();
  186.                 return provider;
  187.             }
  188.         }
  189.         return null;
  190.     }

  191.     /** Remove all data providers.
  192.      * @see #addProvider(DataProvider)
  193.      * @see #removeProvider(DataProvider)
  194.      * @see #isSupported(DataProvider)
  195.      * @see #getProviders()
  196.      */
  197.     public void clearProviders() {
  198.         providers.clear();
  199.     }

  200.     /** Check if some provider is supported.
  201.      * @param provider provider to check
  202.      * @return true if the specified provider instance is already in the supported list
  203.      * @see #addProvider(DataProvider)
  204.      * @see #removeProvider(DataProvider)
  205.      * @see #clearProviders()
  206.      * @see #getProviders()
  207.      * @since 5.1
  208.      */
  209.     public boolean isSupported(final DataProvider provider) {
  210.         for (final DataProvider current : providers) {
  211.             if (current == provider) {
  212.                 return true;
  213.             }
  214.         }
  215.         return false;
  216.     }

  217.     /** Get an unmodifiable view of the list of supported providers.
  218.      * @return unmodifiable view of the list of supported providers
  219.      * @see #addProvider(DataProvider)
  220.      * @see #removeProvider(DataProvider)
  221.      * @see #clearProviders()
  222.      * @see #isSupported(DataProvider)
  223.      */
  224.     public List<DataProvider> getProviders() {
  225.         return Collections.unmodifiableList(providers);
  226.     }

  227.     /** Get an unmodifiable view of the set of data file names that have been loaded.
  228.      * <p>
  229.      * The names returned are exactly the ones that were given to the {@link
  230.      * DataLoader#loadData(InputStream, String) DataLoader.loadData} method.
  231.      * </p>
  232.      * @return unmodifiable view of the set of data file names that have been loaded
  233.      * @see #feed(String, DataLoader)
  234.      * @see #clearLoadedDataNames()
  235.      */
  236.     public Set<String> getLoadedDataNames() {
  237.         return Collections.unmodifiableSet(loaded);
  238.     }

  239.     /** Clear the set of data file names that have been loaded.
  240.      * @see #getLoadedDataNames()
  241.      */
  242.     public void clearLoadedDataNames() {
  243.         loaded.clear();
  244.     }

  245.     /** Feed a data file loader by browsing all data providers.
  246.      * <p>
  247.      * If this method is called with an empty list of providers, a default
  248.      * providers configuration is set up. This default configuration contains
  249.      * only one {@link DataProvider data provider}: a {@link DirectoryCrawler}
  250.      * instance that loads data from files located somewhere in a directory hierarchy.
  251.      * This default provider is <em>not</em> added if the list is not empty. If users
  252.      * want to have both the default provider and other providers, they must add it
  253.      * explicitly.
  254.      * </p>
  255.      * <p>
  256.      * The providers are used in the order in which they were {@link #addProvider(DataProvider)
  257.      * added}. As soon as one provider is able to feed the data loader, the loop is
  258.      * stopped. If no provider is able to feed the data loader, then the last error
  259.      * triggered is thrown.
  260.      * </p>
  261.      * @param supportedNames regular expression for file names supported by the visitor
  262.      * @param loader data loader to use
  263.      * @return true if some data has been loaded
  264.      */
  265.     public boolean feed(final String supportedNames, final DataLoader loader) {

  266.         final Pattern supported = Pattern.compile(supportedNames);

  267.         // set up a default configuration if no providers have been set
  268.         if (providers.isEmpty()) {
  269.             addDefaultProviders();
  270.         }

  271.         // monitor the data that the loader will load
  272.         final DataLoader monitoredLoader = new MonitoringWrapper(loader);

  273.         // crawl the data collection
  274.         OrekitException delayedException = null;
  275.         for (final DataProvider provider : providers) {
  276.             try {

  277.                 // try to feed the visitor using the current provider
  278.                 if (provider.feed(supported, monitoredLoader, this)) {
  279.                     return true;
  280.                 }

  281.             } catch (OrekitException oe) {
  282.                 // remember the last error encountered
  283.                 delayedException = oe;
  284.             }
  285.         }

  286.         if (delayedException != null) {
  287.             throw delayedException;
  288.         }

  289.         return false;

  290.     }

  291.     /** Data loading monitoring wrapper class. */
  292.     private class MonitoringWrapper implements DataLoader {

  293.         /** Wrapped loader. */
  294.         private final DataLoader loader;

  295.         /** Simple constructor.
  296.          * @param loader loader to monitor
  297.          */
  298.         MonitoringWrapper(final DataLoader loader) {
  299.             this.loader = loader;
  300.         }

  301.         /** {@inheritDoc} */
  302.         public boolean stillAcceptsData() {
  303.             // delegate to monitored loader
  304.             return loader.stillAcceptsData();
  305.         }

  306.         /** {@inheritDoc} */
  307.         public void loadData(final InputStream input, final String name)
  308.             throws IOException, ParseException, OrekitException {

  309.             // delegate to monitored loader
  310.             loader.loadData(input, name);

  311.             // monitor the fact new data has been loaded
  312.             loaded.add(name);

  313.         }

  314.     }

  315. }