Search in sources :

Example 1 with StorageKey

use of org.dkpro.lab.storage.StorageService.StorageKey in project dkpro-lab by dkpro.

the class DefaultTaskContext method getFile.

@Override
public File getFile(String aKey, AccessMode aMode) {
    StorageKey key;
    StorageService storage = getStorageService();
    Map<String, String> imports = getMetadata().getImports();
    if (storage.containsKey(getId(), aKey)) {
        // If the context contains the key, we do nothing. Locally available data always
        // supersedes imported data.
        key = new StorageKey(getId(), aKey);
    } else if (imports.containsKey(aKey)) {
        URI uri;
        try {
            uri = new URI(imports.get(aKey));
        } catch (URISyntaxException e) {
            throw new DataAccessResourceFailureException("Imported key [" + aKey + "] resolves to illegal URL [" + imports.get(aKey) + "]", e);
        }
        if ("file".equals(uri.getScheme()) && new File(uri).isFile()) {
            if (aMode == AccessMode.READONLY) {
                return new File(uri);
            } else {
                // Here we should probably just copy the imported file into the context
                throw new DataAccessResourceFailureException("READWRITE access of imported " + "files is not implemented yet.");
            }
        } else {
            key = resolve(aKey, aMode, true);
        }
    } else {
        key = resolve(aKey, aMode, true);
    }
    File file = getStorageService().locateKey(key.contextId, key.key);
    if (file.exists() && !file.isFile()) {
        throw new DataAccessResourceFailureException("Key [" + aKey + "] resolves to [" + file + "] which is not a file.");
    }
    return file;
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) StorageKey(org.dkpro.lab.storage.StorageService.StorageKey) StorageService(org.dkpro.lab.storage.StorageService)

Example 2 with StorageKey

use of org.dkpro.lab.storage.StorageService.StorageKey in project dkpro-lab by dkpro.

the class DefaultTaskContext method getFolder.

@Override
public File getFolder(String aKey, AccessMode aMode) {
    StorageKey key;
    StorageService storage = getStorageService();
    Map<String, String> imports = getMetadata().getImports();
    if (storage.containsKey(getId(), aKey)) {
        // If the context contains the key, we do nothing. Locally available data always
        // supersedes imported data.
        key = new StorageKey(getId(), aKey);
    } else if (imports.containsKey(aKey)) {
        URI uri;
        try {
            uri = new URI(imports.get(aKey));
        } catch (URISyntaxException e) {
            throw new DataAccessResourceFailureException("Imported key [" + aKey + "] resolves to illegal URL [" + imports.get(aKey) + "]", e);
        }
        if ("file".equals(uri.getScheme()) && new File(uri).isDirectory()) {
            if (aMode == AccessMode.READONLY) {
                return new File(uri);
            } else {
                // Here we should probably just copy the imported folder into the context
                throw new DataAccessResourceFailureException("READWRITE access of imported " + "folders is not implemented yet.");
            }
        } else {
            key = resolve(aKey, aMode, true);
        }
    } else {
        key = resolve(aKey, aMode, true);
    }
    File folder = getStorageService().locateKey(key.contextId, key.key);
    if (!folder.exists()) {
        folder.mkdirs();
    }
    if (!folder.isDirectory()) {
        throw new DataAccessResourceFailureException("Key [" + aKey + "] resolves to [" + folder + "] which is not a folder.");
    }
    return folder;
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) StorageKey(org.dkpro.lab.storage.StorageService.StorageKey) StorageService(org.dkpro.lab.storage.StorageService)

Example 3 with StorageKey

use of org.dkpro.lab.storage.StorageService.StorageKey in project dkpro-lab by dkpro.

the class DefaultTaskContext method getStorageLocation.

@Deprecated
@Override
public File getStorageLocation(String aKey, AccessMode aMode) {
    StorageKey key;
    StorageService storage = getStorageService();
    Map<String, String> imports = getMetadata().getImports();
    if (storage.containsKey(getId(), aKey)) {
        // If the context contains the key, we do nothing. Locally available data always
        // supersedes imported data.
        key = new StorageKey(getId(), aKey);
    } else if (imports.containsKey(aKey)) {
        URI uri;
        try {
            uri = new URI(imports.get(aKey));
        } catch (URISyntaxException e) {
            throw new DataAccessResourceFailureException("Imported key [" + aKey + "] resolves to illegal URL [" + imports.get(aKey) + "]", e);
        }
        if ("file".equals(uri.getScheme()) && new File(uri).isDirectory()) {
            if (aMode == AccessMode.READONLY) {
                return new File(uri);
            } else {
                // Here we should probably just copy the imported folder into the context
                throw new DataAccessResourceFailureException("READWRITE access of imported " + "folders is not implemented yet.");
            }
        } else {
            key = resolve(aKey, aMode, true);
        }
    } else {
        key = resolve(aKey, aMode, true);
    }
    return getStorageService().getStorageFolder(key.contextId, key.key);
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) StorageKey(org.dkpro.lab.storage.StorageService.StorageKey) StorageService(org.dkpro.lab.storage.StorageService)

Example 4 with StorageKey

use of org.dkpro.lab.storage.StorageService.StorageKey in project dkpro-lab by dkpro.

the class DefaultTaskContext method resolve.

public StorageKey resolve(String aKey, AccessMode aMode, boolean aAllowMissing) {
    StorageService storage = getStorageService();
    Map<String, String> imports = getMetadata().getImports();
    if (storage.containsKey(getId(), aKey)) {
        // supersedes imported data.
        return new StorageKey(getId(), aKey);
    } else if (imports.containsKey(aKey)) {
        URI uri;
        try {
            uri = new URI(imports.get(aKey));
        } catch (URISyntaxException e) {
            throw new DataAccessResourceFailureException("Imported key [" + aKey + "] resolves to illegal URL [" + imports.get(aKey) + "]", e);
        }
        // Try resolving by ID or by type/constraints
        StorageKey key = null;
        if (CONTEXT_ID_SCHEME.equals(uri.getScheme()) || LATEST_CONTEXT_SCHEME.equals(uri.getScheme())) {
            TaskContextMetadata meta = resolve(uri);
            key = new StorageKey(meta.getId(), uri.getPath());
        }
        // imported storage folders now but imported stream-access (files) keys later.
        if (key != null) {
            switch(aMode) {
                case ADD_ONLY:
                case READWRITE:
                    storage.copy(getId(), aKey, key, aMode);
                    return new StorageKey(getId(), aKey);
                case READONLY:
                    return key;
            }
        }
        // If this is an external URL, copy it to the current context and then return a location
        // in the current context.
        InputStream is = null;
        try {
            is = uri.toURL().openStream();
            storage.storeBinary(getId(), aKey, is);
            return new StorageKey(getId(), aKey);
        } catch (MalformedURLException e) {
            throw new DataAccessResourceFailureException("Imported external key [" + aKey + "] resolves to illegal URL [" + uri + "]", e);
        } catch (IOException e) {
            throw new DataAccessResourceFailureException("Unable to read data for external key [" + aKey + "] from [" + uri + "]", e);
        } finally {
            close(is);
        }
    } else if (aAllowMissing) {
        return new StorageKey(getId(), aKey);
    }
    throw new DataAccessResourceFailureException("No resource bound to key [" + aKey + "]");
}
Also used : TaskContextMetadata(org.dkpro.lab.task.TaskContextMetadata) MalformedURLException(java.net.MalformedURLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) StorageService(org.dkpro.lab.storage.StorageService) StorageKey(org.dkpro.lab.storage.StorageService.StorageKey)

Aggregations

URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 StorageService (org.dkpro.lab.storage.StorageService)4 StorageKey (org.dkpro.lab.storage.StorageService.StorageKey)4 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)4 File (java.io.File)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 TaskContextMetadata (org.dkpro.lab.task.TaskContextMetadata)1