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