Search in sources :

Example 51 with DataAccessResourceFailureException

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

Example 52 with DataAccessResourceFailureException

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);
}
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 53 with DataAccessResourceFailureException

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 + "]");
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IOException(java.io.IOException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException)

Aggregations

DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)53 SQLException (java.sql.SQLException)13 IOException (java.io.IOException)10 File (java.io.File)9 Connection (java.sql.Connection)9 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)6 HibernateException (org.hibernate.HibernateException)5 Session (org.hibernate.Session)5 InputStream (java.io.InputStream)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 StorageService (org.dkpro.lab.storage.StorageService)4 StorageKey (org.dkpro.lab.storage.StorageService.StorageKey)4 QueryRuntimeException (org.hisp.dhis.common.QueryRuntimeException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 Statement (java.sql.Statement)3 InOrder (org.mockito.InOrder)3