Search in sources :

Example 1 with DataStoreProvider

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

the class FolderStoreProvider 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);
    }
    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

IOException (java.io.IOException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1 URIDataStore (org.apache.sis.internal.storage.URIDataStore)1 DataStore (org.apache.sis.storage.DataStore)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 DataStoreProvider (org.apache.sis.storage.DataStoreProvider)1