Search in sources :

Example 1 with StorageConnector

use of org.apache.sis.storage.StorageConnector in project sis by apache.

the class StoreTest method testComponents.

/**
 * Verifies that components in a folder are correctly detected.
 *
 * @throws URISyntaxException if the URL to test data can not be converted to a path of the file system.
 * @throws DataStoreException if an error occurred while reading the resources.
 * @throws IOException if an I/O error occurs.
 */
@Test
public void testComponents() throws URISyntaxException, DataStoreException, IOException {
    final Set<String> identifiers = new HashSet<>(Arrays.asList("EPSG:4326", "Sample 1", "Sample 2", "Sample 3", "data4"));
    final Path path = testDirectory();
    try (Store store = new Store(null, new StorageConnector(path), path, null)) {
        assertEquals("Wrong number of data stores.", 4, store.components().size());
        verifyContent(store, identifiers);
    }
    if (!identifiers.isEmpty()) {
        fail("Missing resources: " + identifiers);
    }
}
Also used : Path(java.nio.file.Path) StorageConnector(org.apache.sis.storage.StorageConnector) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with StorageConnector

use of org.apache.sis.storage.StorageConnector in project sis by apache.

the class FolderStoreProvider method open.

/**
 * Returns a data store implementation associated with this provider for the given parameters.
 *
 * @return a folder data store implementation for the given parameters.
 * @throws DataStoreException if an error occurred while creating the data store instance.
 */
@Override
public DataStore open(final ParameterValueGroup parameters) throws DataStoreException {
    ArgumentChecks.ensureNonNull("parameter", parameters);
    final StorageConnector connector = URIDataStore.Provider.connector(this, parameters);
    final Parameters pg = Parameters.castOrWrap(parameters);
    connector.setOption(OptionKey.LOCALE, pg.getValue(LOCALE));
    connector.setOption(OptionKey.TIMEZONE, pg.getValue(TIMEZONE));
    connector.setOption(OptionKey.ENCODING, pg.getValue(ENCODING));
    final EnumSet<StandardOpenOption> options = EnumSet.of(StandardOpenOption.WRITE);
    if (Boolean.TRUE.equals(pg.getValue(URIDataStore.Provider.CREATE_PARAM))) {
        options.add(StandardOpenOption.CREATE);
    }
    return open(connector, pg.getValue(FORMAT), options);
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) Parameters(org.apache.sis.parameter.Parameters) StandardOpenOption(java.nio.file.StandardOpenOption)

Example 3 with StorageConnector

use of org.apache.sis.storage.StorageConnector in project sis by apache.

the class Store method components.

/**
 * Returns all resources found in the folder given at construction time.
 * Only the resources recognized by a {@link DataStore} will be included.
 * This includes sub-folders. Resources are in no particular order.
 */
@Override
@SuppressWarnings("ReturnOfCollectionOrArrayField")
public synchronized Collection<Resource> components() throws DataStoreException {
    if (components == null) {
        final List<DataStore> resources = new ArrayList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(location, this)) {
            for (final Path candidate : stream) {
                /*
                     * The candidate path may be a symbolic link to a file that we have previously read.
                     * In such case, use the existing data store.   A use case is a directory containing
                     * hundred of GeoTIFF files all accompanied by ".prj" files having identical content.
                     * (Note: those ".prj" files should be invisible since they should be identified as
                     * GeoTIFF auxiliary files, but current Store implementation does not know that).
                     */
                final Path real = candidate.toRealPath();
                DataStore next = children.get(real);
                if (next instanceof Store) {
                    // Warn about directories only.
                    ((Store) next).sharedRepository(real);
                }
                if (next == null) {
                    /*
                         * The candidate file has never been read before. Try to read it now.
                         * If the file format is unknown (UnsupportedStorageException), we will
                         * check if we can open it as a child folder store before to skip it.
                         */
                    final StorageConnector connector = new StorageConnector(candidate);
                    connector.setOption(OptionKey.LOCALE, locale);
                    connector.setOption(OptionKey.TIMEZONE, timezone);
                    connector.setOption(OptionKey.ENCODING, encoding);
                    try {
                        if (componentProvider == null) {
                            // May throw UnsupportedStorageException.
                            next = DataStores.open(connector);
                        } else if (componentProvider.probeContent(connector).isSupported()) {
                            // Open a file of specified format.
                            next = componentProvider.open(connector);
                        } else if (Files.isDirectory(candidate)) {
                            // Open a sub-directory.
                            next = new Store(this, connector);
                        } else {
                            // Not the format specified at construction time.
                            connector.closeAllExcept(null);
                            continue;
                        }
                    } catch (UnsupportedStorageException ex) {
                        if (!Files.isDirectory(candidate)) {
                            connector.closeAllExcept(null);
                            listeners.warning(Level.FINE, null, ex);
                            continue;
                        }
                        next = new Store(this, connector);
                    } catch (DataStoreException ex) {
                        try {
                            connector.closeAllExcept(null);
                        } catch (DataStoreException s) {
                            ex.addSuppressed(s);
                        }
                        throw ex;
                    }
                    /*
                         * At this point we got the data store. It could happen that a store for
                         * the same file has been added concurrently, so we need to check again.
                         */
                    final DataStore existing = children.putIfAbsent(real, next);
                    if (existing != null) {
                        next.close();
                        next = existing;
                        if (next instanceof Store) {
                            // Warn about directories only.
                            ((Store) next).sharedRepository(real);
                        }
                    }
                }
                resources.add(next);
            }
        } catch (DirectoryIteratorException | UncheckedIOException ex) {
            // The cause is an IOException (no other type allowed).
            throw new DataStoreException(canNotRead(), ex.getCause());
        } catch (IOException ex) {
            throw new DataStoreException(canNotRead(), ex);
        } catch (BackingStoreException ex) {
            throw ex.unwrapOrRethrow(DataStoreException.class);
        }
        components = UnmodifiableArrayList.wrap(resources.toArray(new Resource[resources.size()]));
    }
    // Safe because unmodifiable list.
    return components;
}
Also used : Path(java.nio.file.Path) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) StorageConnector(org.apache.sis.storage.StorageConnector) DataStoreException(org.apache.sis.storage.DataStoreException) ArrayList(java.util.ArrayList) UnmodifiableArrayList(org.apache.sis.internal.util.UnmodifiableArrayList) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) DataStore(org.apache.sis.storage.DataStore) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DataStore(org.apache.sis.storage.DataStore) UnsupportedStorageException(org.apache.sis.storage.UnsupportedStorageException)

Example 4 with StorageConnector

use of org.apache.sis.storage.StorageConnector in project sis by apache.

the class StoreProviderTest method testProbeContentFromReader.

/**
 * Implementation of {@link #testProbeContentFromReader()}.
 */
private static void testProbeContentFromReader(final boolean isSupported, final int version, final StoreProvider p, final String wkt) throws DataStoreException {
    final StorageConnector c = new StorageConnector(new StringReader(wkt));
    final ProbeResult r = p.probeContent(c);
    c.closeAllExcept(null);
    assertEquals("isSupported", isSupported, r.isSupported());
    if (isSupported) {
        assertEquals("mimeType", "application/wkt", r.getMimeType());
        assertEquals("version", version, r.getVersion().getMajor());
    }
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) StringReader(java.io.StringReader) ProbeResult(org.apache.sis.storage.ProbeResult)

Example 5 with StorageConnector

use of org.apache.sis.storage.StorageConnector in project sis by apache.

the class StoreTest method testFromReader.

/**
 * Tests {@link Store#getMetadata()} reading from a {@link java.io.Reader}.
 *
 * @throws DataStoreException if an error occurred while reading the metadata.
 */
@Test
public void testFromReader() throws DataStoreException {
    final Metadata metadata;
    try (Store store = new Store(null, new StorageConnector(new StringReader(WKT)))) {
        metadata = store.getMetadata();
        assertSame("Expected cached value.", metadata, store.getMetadata());
    }
    validate((GeographicCRS) TestUtilities.getSingleton(metadata.getReferenceSystemInfo()));
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) Metadata(org.opengis.metadata.Metadata) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

StorageConnector (org.apache.sis.storage.StorageConnector)18 Test (org.junit.Test)11 StringReader (java.io.StringReader)4 ProbeResult (org.apache.sis.storage.ProbeResult)4 Path (java.nio.file.Path)3 DataStore (org.apache.sis.storage.DataStore)3 Metadata (org.opengis.metadata.Metadata)3 ChannelDecoderTest (org.apache.sis.internal.netcdf.impl.ChannelDecoderTest)2 Parameters (org.apache.sis.parameter.Parameters)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 DirectoryIteratorException (java.nio.file.DirectoryIteratorException)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AbstractFeature (org.apache.sis.feature.AbstractFeature)1 URIDataStore (org.apache.sis.internal.storage.URIDataStore)1 UnmodifiableArrayList (org.apache.sis.internal.util.UnmodifiableArrayList)1