Search in sources :

Example 11 with RuntimeIOException

use of org.apache.jena.atlas.RuntimeIOException in project jena by apache.

the class LocationLock method getOwner.

/**
 * Gets the current owner of this locations lock.
 *
 * @return Process ID of owner if locked, zero if the location cannot be
 *         locked or not currently locked
 */
public int getOwner() {
    // Memory locations are never considered locked
    if (location.isMem())
        return NO_OWNER;
    File lockFile = getLockFile();
    if (lockFile.exists()) {
        checkLockFileForRead(lockFile);
        // Can read lock owner from the file
        try {
            String rawLockInfo = IO.readWholeFileAsUTF8(lockFile.getAbsolutePath());
            if (rawLockInfo.endsWith("\n")) {
                // This is most likely down to trying to open a TDB2 database with TDB1
                throw new FileException("Unable to check TDB lock owner, the lock file contents appear to be for a TDB2 database.  " + "Please try loading this location as a TDB2 database. " + TDB.tdbFaqsLink);
            }
            int owner = Integer.parseInt(rawLockInfo);
            return owner;
        } catch (RuntimeIOException e) {
            throw new FileException("Unable to check TDB lock owner due to an IO error reading the lock file. " + TDB.tdbFaqsLink, e);
        } catch (NumberFormatException e) {
            throw new FileException("Unable to check TDB lock owner as the lock file contains invalid data. " + TDB.tdbFaqsLink, e);
        }
    } else {
        // No lock file so nobody owns the lock currently
        return NO_OWNER;
    }
}
Also used : RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) File(java.io.File)

Example 12 with RuntimeIOException

use of org.apache.jena.atlas.RuntimeIOException in project jena by apache.

the class HttpLib method exception.

static HttpException exception(HttpResponse<InputStream> response, int httpStatusCode) {
    URI uri = response.request().uri();
    // long length = HttpLib.getContentLength(response);
    // Not critical path code. Read body regardless.
    InputStream in = response.body();
    String msg;
    try {
        msg = IO.readWholeFileAsUTF8(in);
        if (msg.isBlank())
            msg = null;
    } catch (RuntimeIOException e) {
        msg = null;
    }
    return new HttpException(httpStatusCode, HttpSC.getMessage(httpStatusCode), msg);
}
Also used : RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) HttpException(org.apache.jena.atlas.web.HttpException) URI(java.net.URI)

Example 13 with RuntimeIOException

use of org.apache.jena.atlas.RuntimeIOException in project jena by apache.

the class IOX method copy.

/**
 * Copy a file, not atomic. *
 * Can copy to a directory or over an existing file.
 * @param srcFilename
 * @param dstFilename
 */
public static void copy(String srcFilename, String dstFilename) {
    Path src = Path.of(srcFilename);
    if (!Files.exists(src))
        throw new RuntimeIOException("No such file: " + srcFilename);
    Path dst = Path.of(dstFilename);
    if (Files.isDirectory(dst))
        dst = dst.resolve(src.getFileName());
    try {
        Files.copy(src, dst);
    } catch (IOException ex) {
        FmtLog.error(IOX.class, ex, "IOException copying %s to %s", srcFilename, dstFilename);
        throw IOX.exception(ex);
    }
}
Also used : RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) IOException(java.io.IOException)

Example 14 with RuntimeIOException

use of org.apache.jena.atlas.RuntimeIOException in project jena by apache.

the class Metadata method read.

// Protect all classloader choosing -- sometimes systems mess with even the system
// class loader.
private static void read(Properties properties, String resourceName) {
    // environments.
    try {
        ClassLoader classLoader = null;
        try {
            classLoader = SystemUtils.chooseClassLoader();
        } catch (AtlasException ex) {
        }
        if (classLoader == null) {
            try {
                classLoader = Metadata.class.getClassLoader();
            } catch (AtlasException ex) {
            }
        }
        if (classLoader == null) {
            Log.error(Metadata.class, "No classloader");
            return;
        }
        InputStream in = classLoader.getResourceAsStream(resourceName);
        if (in == null)
            return;
        try {
            properties.loadFromXML(in);
        } catch (InvalidPropertiesFormatException ex) {
            throw new JenaException("Invalid properties file", ex);
        } catch (IOException ex) {
            throw new RuntimeIOException("Metadata ==> IOException", ex);
        }
    } catch (Throwable ex) {
        Log.error(Metadata.class, "Unexpected Throwable", ex);
        return;
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException) InputStream(java.io.InputStream) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) IOException(java.io.IOException) AtlasException(org.apache.jena.atlas.AtlasException)

Example 15 with RuntimeIOException

use of org.apache.jena.atlas.RuntimeIOException in project jena by apache.

the class DatabaseOps method openUniqueFileForWriting.

// --> IOX
private static Pair<OutputStream, Path> openUniqueFileForWriting(Path dirPath, String basename, String ext) {
    if (!Files.isDirectory(dirPath))
        throw new IllegalArgumentException("Not a directory: " + dirPath);
    if (basename.contains("/") || basename.contains("\\"))
        throw new IllegalArgumentException("Basename must not contain a file path separator (\"/\" or \"\\\")");
    String timestamp = DateTimeUtils.nowAsString("yyyy-MM-dd_HHmmss");
    String filename = basename + "_" + timestamp;
    Path p = dirPath.resolve(filename + "." + ext);
    int x = 0;
    for (; ; ) {
        try {
            OutputStream out = Files.newOutputStream(p, StandardOpenOption.CREATE_NEW);
            return Pair.create(out, p);
        } catch (AccessDeniedException ex) {
            throw IOX.exception("Access denied", ex);
        } catch (FileAlreadyExistsException ex) {
        // Drop through and try again.
        } catch (IOException ex) {
            throw IOX.exception(ex);
        }
        // Try again.
        x++;
        if (x >= 5)
            throw new RuntimeIOException("Can't create the unique name: number of attempts exceeded");
        p = dirPath.resolve(filename + "_" + x + "." + ext);
    }
}
Also used : RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException)

Aggregations

RuntimeIOException (org.apache.jena.atlas.RuntimeIOException)17 QueryCancelledException (org.apache.jena.query.QueryCancelledException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 ByteBuffer (java.nio.ByteBuffer)2 HttpException (org.apache.jena.atlas.web.HttpException)2 Context (org.apache.jena.sparql.util.Context)2 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 URI (java.net.URI)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 Path (java.nio.file.Path)1 InvalidPropertiesFormatException (java.util.InvalidPropertiesFormatException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AtlasException (org.apache.jena.atlas.AtlasException)1 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)1 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)1