use of org.springframework.dao.DataAccessResourceFailureException 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 + "]");
}
use of org.springframework.dao.DataAccessResourceFailureException 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.springframework.dao.DataAccessResourceFailureException in project dkpro-lab by dkpro.
the class FileSystemStorageService method storeBinary.
@Override
public void storeBinary(String aContextId, String aKey, StreamWriter aProducer) {
File context = getContextFolder(aContextId, false);
File tmpFile = new File(context, aKey + ".tmp");
File finalFile = new File(context, aKey);
OutputStream os = null;
try {
// Necessary if the key addresses a sub-directory
tmpFile.getParentFile().mkdirs();
log.debug("Storing to: " + finalFile);
os = new FileOutputStream(tmpFile);
if (aKey.endsWith(".gz")) {
os = new GZIPOutputStream(os);
}
aProducer.write(os);
} catch (Exception e) {
tmpFile.delete();
throw new DataAccessResourceFailureException(e.getMessage(), e);
} finally {
Util.close(os);
}
// exists. So try to delete the target file before renaming.
if (finalFile.exists()) {
boolean deleteSuccess = finalFile.delete();
if (!deleteSuccess) {
throw new DataAccessResourceFailureException("Unable to delete [" + finalFile + "] in order to replace it with an updated version.");
}
}
// Make sure the file is only visible under the final name after all data has been
// written into it.
boolean renameSuccess = tmpFile.renameTo(finalFile);
if (!renameSuccess) {
throw new DataAccessResourceFailureException("Unable to rename [" + tmpFile + "] to [" + finalFile + "]");
}
}
Aggregations