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);
}
}
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);
}
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;
}
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());
}
}
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()));
}
Aggregations