You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2018/04/28 20:29:30 UTC
[juneau] branch master updated: Fix issue when using config files
in read-only locations.
This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 50befe6 Fix issue when using config files in read-only locations.
50befe6 is described below
commit 50befe67095a8c137ecc1e9aedb3350b6a10e787
Author: JamesBognar <ja...@apache.org>
AuthorDate: Sat Apr 28 16:27:09 2018 -0400
Fix issue when using config files in read-only locations.
---
.../juneau/config/store/ConfigFileStore.java | 60 ++++++++++++++--------
1 file changed, 39 insertions(+), 21 deletions(-)
diff --git a/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/store/ConfigFileStore.java b/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/store/ConfigFileStore.java
index 8d89336..e87e57c 100644
--- a/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/store/ConfigFileStore.java
+++ b/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/store/ConfigFileStore.java
@@ -223,8 +223,11 @@ public class ConfigFileStore extends ConfigStore {
if (! Files.exists(p))
return "";
- try (FileChannel fc = FileChannel.open(p, READ, WRITE, CREATE)) {
- try (FileLock lock = fc.lock()) {
+ boolean isWritable = isWritable(p);
+ OpenOption[] oo = isWritable ? new OpenOption[]{READ,WRITE,CREATE} : new OpenOption[]{READ};
+
+ try (FileChannel fc = FileChannel.open(p, oo)) {
+ try (FileLock lock = isWritable ? fc.lock() : null) {
ByteBuffer buf = ByteBuffer.allocate(1024);
StringBuilder sb = new StringBuilder();
while (fc.read(buf) != -1) {
@@ -255,27 +258,29 @@ public class ConfigFileStore extends ConfigStore {
if ((!exists) && (!isEmpty(expectedContents)))
return "";
- try (FileChannel fc = FileChannel.open(p, READ, WRITE, CREATE)) {
- try (FileLock lock = fc.lock()) {
- String currentContents = "";
- if (exists) {
- ByteBuffer buf = ByteBuffer.allocate(1024);
- StringBuilder sb = new StringBuilder();
- while (fc.read(buf) != -1) {
- sb.append(charset.decode((ByteBuffer)(buf.flip())));
- buf.clear();
+ if (isWritable(p)) {
+ try (FileChannel fc = FileChannel.open(p, READ, WRITE, CREATE)) {
+ try (FileLock lock = fc.lock()) {
+ String currentContents = "";
+ if (exists) {
+ ByteBuffer buf = ByteBuffer.allocate(1024);
+ StringBuilder sb = new StringBuilder();
+ while (fc.read(buf) != -1) {
+ sb.append(charset.decode((ByteBuffer)(buf.flip())));
+ buf.clear();
+ }
+ currentContents = sb.toString();
}
- currentContents = sb.toString();
- }
- if (expectedContents != null && ! isEquals(currentContents, expectedContents)) {
- if (currentContents == null)
- cache.remove(name);
- else
- cache.put(name, currentContents);
- return currentContents;
+ if (expectedContents != null && ! isEquals(currentContents, expectedContents)) {
+ if (currentContents == null)
+ cache.remove(name);
+ else
+ cache.put(name, currentContents);
+ return currentContents;
+ }
+ fc.position(0);
+ fc.write(charset.encode(newContents));
}
- fc.position(0);
- fc.write(charset.encode(newContents));
}
}
@@ -286,6 +291,19 @@ public class ConfigFileStore extends ConfigStore {
return null;
}
+
+ private synchronized boolean isWritable(Path p) {
+ try {
+ if (! Files.exists(p)) {
+ Files.createDirectories(p.getParent());
+ if (! Files.exists(p))
+ p.toFile().createNewFile();
+ }
+ } catch (IOException e) {
+ return false;
+ }
+ return Files.isWritable(p);
+ }
@Override /* ConfigStore */
public synchronized ConfigFileStore update(String name, String newContents) {
--
To stop receiving notification emails like this one, please contact
jamesbognar@apache.org.