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