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;
}
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;
}
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);
}
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 + "]");
}
Aggregations