Search in sources :

Example 16 with DataStoreException

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

the class Store method write.

/**
 * Replaces the content of this GPX file by the given metadata and features.
 *
 * @param  metadata  the metadata to write, or {@code null} if none.
 * @param  features  the features to write, or {@code null} if none.
 * @throws ConcurrentReadException if the {@code features} stream was provided by this data store.
 * @throws DataStoreException if an error occurred while writing the data.
 */
public synchronized void write(final Metadata metadata, final Stream<? extends AbstractFeature> features) throws DataStoreException {
    try {
        /*
             * If we created a reader for reading metadata, we need to close that reader now otherwise the call
             * to 'new Writer(…)' will fail.  Note that if that reader was in use by someone else, the 'reader'
             * field would be null and the 'new Writer(…)' call should detect that a reader is in use somewhere.
             */
        final Reader r = reader;
        if (r != null) {
            reader = null;
            r.close();
        }
        /*
             * Get the writer if no read or other write operation is in progress, then write the data.
             */
        try (Writer writer = new Writer(this, org.apache.sis.internal.storage.gpx.Metadata.castOrCopy(metadata, locale))) {
            writer.writeStartDocument();
            if (features != null) {
                features.forEachOrdered(writer);
            }
            writer.writeEndDocument();
        }
    } catch (BackingStoreException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof DataStoreException) {
            throw (DataStoreException) cause;
        }
        throw new DataStoreException(e.getLocalizedMessage(), cause);
    } catch (Exception e) {
        if (e instanceof UncheckedIOException) {
            e = ((UncheckedIOException) e).getCause();
        }
        throw new DataStoreException(e);
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) UncheckedIOException(java.io.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) IllegalNameException(org.apache.sis.storage.IllegalNameException) DataStoreContentException(org.apache.sis.storage.DataStoreContentException) DataStoreException(org.apache.sis.storage.DataStoreException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) UncheckedIOException(java.io.UncheckedIOException) ConcurrentReadException(org.apache.sis.storage.ConcurrentReadException) FactoryException(org.opengis.util.FactoryException)

Example 17 with DataStoreException

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

the class LandsatStore method loadMetadata.

/**
 * Parses the main Landsat text file.
 * Also creates the array of components, but without loading GeoTIFF data yet.
 */
private void loadMetadata() throws DataStoreException {
    if (source == null) {
        throw new DataStoreClosedException(getLocale(), LandsatStoreProvider.NAME, StandardOpenOption.READ);
    }
    final String name = getDisplayName();
    final NameFactory factory = DefaultFactories.forBuildin(NameFactory.class);
    final NameSpace scope = (name != null) ? factory.createNameSpace(factory.createLocalName(null, name), null) : null;
    final Band[] resources;
    int count = 0;
    try (BufferedReader reader = (source instanceof BufferedReader) ? (BufferedReader) source : new LineNumberReader(source)) {
        // Will be closed at the end of this try-finally block.
        source = null;
        final MetadataReader parser = new MetadataReader(this, getDisplayName(), listeners);
        parser.read(reader);
        metadata = parser.getMetadata();
        /*
             * Create the array of components. The resource identifier is the band name.
             * The namespace of each identifier is the name of the data set directory.
             */
        resources = new Band[parser.bands.size()];
        for (final Map.Entry<BandName, Band> entry : parser.bands.entrySet()) {
            final Band component = entry.getValue();
            if (component.filename != null) {
                component.identifier = factory.createLocalName(scope, entry.getKey().name());
                resources[count++] = component;
            }
        }
    } catch (IOException e) {
        throw new DataStoreException(e);
    } catch (FactoryException e) {
        throw new DataStoreReferencingException(e);
    }
    components = BandGroup.group(listeners, resources, count);
    for (final BandGroup c : components) {
        c.identifier = factory.createLocalName(scope, c.group.name());
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) DataStoreClosedException(org.apache.sis.storage.DataStoreClosedException) FactoryException(org.opengis.util.FactoryException) NameSpace(org.opengis.util.NameSpace) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader) BufferedReader(java.io.BufferedReader) DataStoreReferencingException(org.apache.sis.storage.DataStoreReferencingException) Map(java.util.Map) NameFactory(org.opengis.util.NameFactory)

Example 18 with DataStoreException

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

the class LandsatStore method close.

/**
 * Closes this Landsat store and releases any underlying resources.
 *
 * @throws DataStoreException if an error occurred while closing the Landsat file.
 */
@Override
public synchronized void close() throws DataStoreException {
    metadata = null;
    DataStoreException error = null;
    for (final Band band : BandGroup.bands(components)) {
        try {
            band.closeDataStore();
        } catch (DataStoreException e) {
            if (error == null) {
                error = e;
            } else {
                error.addSuppressed(e);
            }
        }
    }
    components = null;
    if (error != null)
        throw error;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException)

Example 19 with DataStoreException

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

the class NetcdfStore method close.

/**
 * Closes this netCDF store and releases any underlying resources.
 *
 * @throws DataStoreException if an error occurred while closing the netCDF file.
 */
@Override
public synchronized void close() throws DataStoreException {
    final Decoder reader = decoder;
    decoder = null;
    metadata = null;
    components = null;
    if (reader != null)
        try {
            reader.close();
        } catch (IOException e) {
            throw new DataStoreException(e);
        }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) Decoder(org.apache.sis.internal.netcdf.Decoder)

Example 20 with DataStoreException

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

the class StoreProvider method open.

/**
 * Shared implementation of public {@code open(…)} methods.
 * Note that this method may modify the given {@code options} set for its own purpose.
 *
 * @param  connector  information about the storage (URL, path, <i>etc</i>).
 * @param  format     format name for directory content, or {@code null} if unspecified.
 * @param  options    whether to create a new directory, overwrite existing content, <i>etc</i>.
 */
private DataStore open(final StorageConnector connector, final String format, final EnumSet<StandardOpenOption> options) throws DataStoreException {
    /*
         * Determine now the provider to use for directory content. We do that for determining if the component
         * has write capability. If not, then the WRITE, CREATE and related options will be ignored.  If we can
         * not determine whether the component store has write capabilities (i.e. if canWrite(…) returns null),
         * assume that the answer is "yes".
         */
    final DataStoreProvider componentProvider;
    if (format != null) {
        componentProvider = StoreUtilities.providerByFormatName(format.trim());
        if (Boolean.FALSE.equals(StoreUtilities.canWrite(componentProvider.getClass()))) {
            // No write capability.
            options.clear();
        }
    } else {
        componentProvider = null;
        // Can not write if we don't know the components format.
        options.clear();
    }
    final Path path = connector.getStorageAs(Path.class);
    final Store store;
    try {
        /*
             * If the user asked to create a new directory, we need to perform this task before
             * to create the Store (otherwise constructor will fail with NoSuchFileException).
             * In the particular case of CREATE_NEW, we unconditionally attempt to create the
             * directory in order to rely on the atomic check performed by Files.createDirectory(…).
             */
        boolean isNew = false;
        if (options.contains(StandardOpenOption.CREATE)) {
            if (options.contains(StandardOpenOption.CREATE_NEW) || Files.notExists(path)) {
                // IOException if the directory already exists.
                Files.createDirectory(path);
                isNew = true;
            }
        }
        if (isNew || (options.contains(StandardOpenOption.WRITE) && Files.isWritable(path))) {
            // May throw NoSuchFileException.
            store = new WritableStore(this, connector, path, componentProvider);
        } else {
            // May throw NoSuchFileException.
            store = new Store(this, connector, path, componentProvider);
        }
        /*
             * If there is a destructive operation to perform (TRUNCATE_EXISTING), do it last only
             * after we have successfully created the data store. The check for directory existence
             * is also done after creation to be sure to check the path used by the store.
             */
        if (!Files.isDirectory(path)) {
            throw new DataStoreException(Resources.format(Resources.Keys.FileIsNotAResourceDirectory_1, path));
        }
        if (options.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
            WritableStore.deleteRecursively(path, false);
        }
    } catch (IOException e) {
        /*
             * In case of error, Java FileSystem implementation tries to throw a specific exception
             * (NoSuchFileException or FileAlreadyExistsException), but this is not guaranteed.
             */
        int isDirectory = 0;
        final short errorKey;
        if (e instanceof FileAlreadyExistsException) {
            if (path != null && Files.isDirectory(path)) {
                isDirectory = 1;
            }
            errorKey = Resources.Keys.FileAlreadyExists_2;
        } else if (e instanceof NoSuchFileException) {
            errorKey = Resources.Keys.NoSuchResourceDirectory_1;
        } else {
            errorKey = Resources.Keys.CanNotCreateFolderStore_1;
        }
        throw new DataStoreException(Resources.format(errorKey, (path != null) ? path : connector.getStorageName(), isDirectory), e);
    }
    connector.closeAllExcept(path);
    return store;
}
Also used : Path(java.nio.file.Path) DataStoreException(org.apache.sis.storage.DataStoreException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DataStoreProvider(org.apache.sis.storage.DataStoreProvider) NoSuchFileException(java.nio.file.NoSuchFileException) URIDataStore(org.apache.sis.internal.storage.URIDataStore) DataStore(org.apache.sis.storage.DataStore) IOException(java.io.IOException)

Aggregations

DataStoreException (org.apache.sis.storage.DataStoreException)39 IOException (java.io.IOException)16 DataStore (org.apache.sis.storage.DataStore)8 Path (java.nio.file.Path)6 DataStoreContentException (org.apache.sis.storage.DataStoreContentException)5 FeatureSet (org.apache.sis.storage.FeatureSet)5 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)4 IllegalNameException (org.apache.sis.storage.IllegalNameException)4 Resource (org.apache.sis.storage.Resource)4 BackingStoreException (org.apache.sis.util.collection.BackingStoreException)4 FactoryException (org.opengis.util.FactoryException)4 Reader (java.io.Reader)3 UncheckedIOException (java.io.UncheckedIOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 URISyntaxException (java.net.URISyntaxException)3 ByteBuffer (java.nio.ByteBuffer)3 ProbeResult (org.apache.sis.storage.ProbeResult)3 BufferedReader (java.io.BufferedReader)2 Closeable (java.io.Closeable)2 File (java.io.File)2