Search in sources :

Example 1 with WritableFeatureSet

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

the class WritableStore method add.

/**
 * Create a new file for the given resource.
 * This implementation uses the provider specified at creation time.
 */
@Override
public synchronized Resource add(final Resource resource) throws DataStoreException {
    ArgumentChecks.ensureNonNull("resource", resource);
    if (!(resource instanceof FeatureSet)) {
        throw new DataStoreException(message(Resources.Keys.CanNotStoreResourceType_2, new Object[] { FolderStoreProvider.NAME, StoreUtilities.getInterface(resource.getClass()) }));
    }
    /*
         * If we determined in a previous method invocation that the given provider can not write feature set,
         * we are better to fail now instead than polluting the directory with files that we can not use.
         */
    if (isReadOnly) {
        throw new ReadOnlyStorageException(messages().getString(Resources.Keys.StoreIsReadOnly));
    }
    /*
         * Infer a filename from the resource identifier, if one can be found.
         * A suffix is added to the filename if available (some formats may have no suffix at all).
         */
    String filename = StoreUtilities.getIdentifier(resource.getMetadata());
    if (filename == null) {
        throw new DataStoreException(message(Resources.Keys.MissingResourceIdentifier_1, StoreUtilities.getLabel(resource)));
    }
    final String[] suffixes = StoreUtilities.getFileSuffixes(componentProvider.getClass());
    if (suffixes.length != 0) {
        filename += '.' + suffixes[0];
    }
    /*
         * Create new store/resource for write access, provided that no store already exist for the path.
         * We use the CREATE_NEW option in order to intentionally fail if the resource already exists.
         */
    final Path path = location.resolve(filename);
    if (!children.containsKey(path)) {
        final StorageConnector connector = new StorageConnector(path);
        connector.setOption(OptionKey.LOCALE, locale);
        connector.setOption(OptionKey.TIMEZONE, timezone);
        connector.setOption(OptionKey.ENCODING, encoding);
        connector.setOption(OptionKey.OPEN_OPTIONS, new StandardOpenOption[] { StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE });
        final DataStore store = componentProvider.open(connector);
        if (children.putIfAbsent(path, store) == null) {
            // TODO: handle transactional case.
            if (store instanceof WritableFeatureSet) {
                StoreUtilities.copy((FeatureSet) resource, (WritableFeatureSet) store);
                // Clear cache. TODO: we should do something more efficient.
                components = null;
                return store;
            }
            /*
                 * If the data store is not a WritableFeatureSet, current implementation can not use it.
                 * Files created by this failed attempt may remain; instead of trying to delete them with
                 * uncertain consequences, we set a flag for avoiding to pollute further the directory.
                 */
            isReadOnly = true;
            children.remove(path, store);
            final String name = store.getDisplayName();
            store.close();
            throw new DataStoreException(message(Resources.Keys.NotAWritableFeatureSet_1, name));
        }
        store.close();
    }
    throw new DataStoreException(message(Resources.Keys.ResourceAlreadyExists_1, path));
}
Also used : Path(java.nio.file.Path) StorageConnector(org.apache.sis.storage.StorageConnector) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) DataStoreException(org.apache.sis.storage.DataStoreException) ReadOnlyStorageException(org.apache.sis.storage.ReadOnlyStorageException) DataStore(org.apache.sis.storage.DataStore) FeatureSet(org.apache.sis.storage.FeatureSet) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet)

Aggregations

Path (java.nio.file.Path)1 DataStore (org.apache.sis.storage.DataStore)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 FeatureSet (org.apache.sis.storage.FeatureSet)1 ReadOnlyStorageException (org.apache.sis.storage.ReadOnlyStorageException)1 StorageConnector (org.apache.sis.storage.StorageConnector)1 WritableFeatureSet (org.apache.sis.storage.WritableFeatureSet)1