ClasspathCrawler.java

  1. /* Copyright 2002-2020 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.IOException;
  19. import java.io.InputStream;
  20. import java.net.URI;
  21. import java.net.URISyntaxException;
  22. import java.text.ParseException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.regex.Pattern;

  26. import org.hipparchus.exception.DummyLocalizable;
  27. import org.hipparchus.exception.LocalizedCoreFormats;
  28. import org.orekit.annotation.DefaultDataContext;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;


  31. /** Provider for data files stored as resources in the classpath.
  32.  * <p>
  33.  * This class handles a list of data files or zip/jar archives located in the
  34.  * classpath. Since the classpath is not a tree structure the list elements
  35.  * cannot be whole directories recursively browsed as in {@link
  36.  * DirectoryCrawler}, they must be data files or zip/jar archives.
  37.  * </p>
  38.  * <p>
  39.  * A typical use case is to put all data files in a single zip or jar archive
  40.  * and to build an instance of this class with the single name of this zip/jar
  41.  * archive. Two different instances may be used one for user or project specific
  42.  * data and another one for system-wide or general data.
  43.  * </p>
  44.  * <p>
  45.  * All {@link DataProvidersManager#addFilter(DataFilter) registered}
  46.  * {@link DataFilter filters} are applied.
  47.  * </p>
  48.  * <p>
  49.  * Zip archives entries are supported recursively.
  50.  * </p>
  51.  * <p>
  52.  * This is a simple application of the <code>visitor</code> design pattern for
  53.  * list browsing.
  54.  * </p>
  55.  * @see DataProvidersManager
  56.  * @author Luc Maisonobe
  57.  */
  58. public class ClasspathCrawler implements DataProvider {

  59.     /** List elements. */
  60.     private final List<String> listElements;

  61.     /** Class loader to use. */
  62.     private final ClassLoader classLoader;

  63.     /** Build a data classpath crawler.
  64.      * <p>
  65.      * Calling this constructor has the same effect as calling
  66.      * {@link #ClasspathCrawler(ClassLoader, String...)} with
  67.      * {@code ClasspathCrawler.class.getClassLoader()} as first
  68.      * argument.
  69.      * </p>
  70.      * @param list list of data file names within the classpath
  71.      */
  72.     public ClasspathCrawler(final String... list) {
  73.         this(ClasspathCrawler.class.getClassLoader(), list);
  74.     }

  75.     /** Build a data classpath crawler.
  76.      * @param classLoader class loader to use to retrieve the resources
  77.      * @param list list of data file names within the classpath
  78.      */
  79.     public ClasspathCrawler(final ClassLoader classLoader, final String... list) {

  80.         listElements = new ArrayList<>();
  81.         this.classLoader = classLoader;

  82.         // check the resources
  83.         for (final String name : list) {
  84.             if (!"".equals(name)) {

  85.                 final String convertedName = name.replace('\\', '/');
  86.                 try (InputStream stream = classLoader.getResourceAsStream(convertedName)) {
  87.                     if (stream == null) {
  88.                         throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_RESOURCE, name);
  89.                     }
  90.                     listElements.add(convertedName);
  91.                 } catch (IOException exc) {
  92.                     // ignore this error
  93.                 }

  94.             }
  95.         }

  96.     }

  97.     @Override
  98.     @Deprecated
  99.     @DefaultDataContext
  100.     public boolean feed(final Pattern supported, final DataLoader visitor) {
  101.         return feed(supported, visitor, DataContext.getDefault().getDataProvidersManager());
  102.     }

  103.     /** {@inheritDoc} */
  104.     public boolean feed(final Pattern supported,
  105.                         final DataLoader visitor,
  106.                         final DataProvidersManager manager) {

  107.         try {
  108.             OrekitException delayedException = null;
  109.             boolean loaded = false;
  110.             for (final String name : listElements) {
  111.                 try {

  112.                     if (visitor.stillAcceptsData()) {
  113.                         if (ZIP_ARCHIVE_PATTERN.matcher(name).matches()) {

  114.                             // browse inside the zip/jar file
  115.                             final DataProvider zipProvider = new ZipJarCrawler(name);
  116.                             loaded = zipProvider.feed(supported, visitor, manager) || loaded;

  117.                         } else {

  118.                             // apply all registered filters
  119.                             NamedData data = new NamedData(name, () -> classLoader.getResourceAsStream(name));
  120.                             data = manager.applyAllFilters(data);

  121.                             if (supported.matcher(data.getName()).matches()) {
  122.                                 // visit the current file
  123.                                 try (InputStream input = data.getStreamOpener().openStream()) {
  124.                                     final URI uri = classLoader.getResource(name).toURI();
  125.                                     visitor.loadData(input, uri.toString());
  126.                                     loaded = true;
  127.                                 }

  128.                             }

  129.                         }
  130.                     }

  131.                 } catch (OrekitException oe) {
  132.                     // maybe the next path component will be able to provide data
  133.                     // wait until all components have been tried
  134.                     delayedException = oe;
  135.                 } catch (URISyntaxException use) {
  136.                     // this should bever happen
  137.                     throw new OrekitException(use, LocalizedCoreFormats.SIMPLE_MESSAGE, use.getMessage());
  138.                 }
  139.             }

  140.             if (!loaded && delayedException != null) {
  141.                 throw delayedException;
  142.             }

  143.             return loaded;

  144.         } catch (IOException | ParseException ioe) {
  145.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  146.         }

  147.     }

  148. }