ClasspathCrawler.java

  1. /* Copyright 2002-2013 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.Matcher;
  26. import java.util.regex.Pattern;
  27. import java.util.zip.GZIPInputStream;

  28. import org.apache.commons.math3.exception.util.DummyLocalizable;
  29. import org.apache.commons.math3.exception.util.LocalizedFormats;
  30. import org.orekit.errors.OrekitException;
  31. import org.orekit.errors.OrekitMessages;


  32. /** Provider for data files stored as resources in the classpath.

  33.  * <p>
  34.  * This class handles a list of data files or zip/jar archives located in the
  35.  * classpath. Since the classpath is not a tree structure the list elements
  36.  * cannot be whole directories recursively browsed as in {@link
  37.  * DirectoryCrawler}, they must be data files or zip/jar archives.
  38.  * </p>
  39.  * <p>
  40.  * A typical use case is to put all data files in a single zip or jar archive
  41.  * and to build an instance of this class with the single name of this zip/jar
  42.  * archive. Two different instances may be used one for user or project specific
  43.  * data and another one for system-wide or general data.
  44.  * </p>
  45.  * <p>
  46.  * Gzip-compressed files are supported.
  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.      * @exception OrekitException if a list elements is not an existing resource
  72.      */
  73.     public ClasspathCrawler(final String... list) throws OrekitException {
  74.         this(ClasspathCrawler.class.getClassLoader(), list);
  75.     }

  76.     /** Build a data classpath crawler.
  77.      * @param classLoader class loader to use to retrieve the resources
  78.      * @param list list of data file names within the classpath
  79.      * @exception OrekitException if a list elements is not an existing resource
  80.      */
  81.     public ClasspathCrawler(final ClassLoader classLoader, final String... list)
  82.         throws OrekitException {

  83.         listElements = new ArrayList<String>();
  84.         this.classLoader = classLoader;

  85.         // check the resources
  86.         for (final String name : list) {
  87.             if (!"".equals(name)) {

  88.                 final String convertedName = name.replace('\\', '/');
  89.                 final InputStream stream = classLoader.getResourceAsStream(convertedName);
  90.                 if (stream == null) {
  91.                     throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_RESOURCE, name);
  92.                 }

  93.                 listElements.add(convertedName);
  94.                 try {
  95.                     stream.close();
  96.                 } catch (IOException exc) {
  97.                     // ignore this error
  98.                 }
  99.             }
  100.         }

  101.     }

  102.     /** {@inheritDoc} */
  103.     public boolean feed(final Pattern supported, final DataLoader visitor)
  104.         throws OrekitException {

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

  110.                     if (visitor.stillAcceptsData()) {
  111.                         if (ZIP_ARCHIVE_PATTERN.matcher(name).matches()) {

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

  115.                         } else {

  116.                             // remove suffix from gzip files
  117.                             final Matcher gzipMatcher = GZIP_FILE_PATTERN.matcher(name);
  118.                             final String baseName = gzipMatcher.matches() ? gzipMatcher.group(1) : name;

  119.                             if (supported.matcher(baseName).matches()) {

  120.                                 final InputStream stream      = classLoader.getResourceAsStream(name);
  121.                                 final URI uri                 = classLoader.getResource(name).toURI();

  122.                                 // visit the current file
  123.                                 if (gzipMatcher.matches()) {
  124.                                     visitor.loadData(new GZIPInputStream(stream), uri.toString());
  125.                                 } else {
  126.                                     visitor.loadData(stream, uri.toString());
  127.                                 }

  128.                                 stream.close();
  129.                                 loaded = true;

  130.                             }

  131.                         }
  132.                     }

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

  142.             if (!loaded && delayedException != null) {
  143.                 throw delayedException;
  144.             }

  145.             return loaded;

  146.         } catch (IOException ioe) {
  147.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  148.         } catch (ParseException pe) {
  149.             throw new OrekitException(pe, new DummyLocalizable(pe.getMessage()));
  150.         }

  151.     }

  152. }