diff --git a/openide.filesystems/apichanges.xml b/openide.filesystems/apichanges.xml --- a/openide.filesystems/apichanges.xml +++ b/openide.filesystems/apichanges.xml @@ -49,6 +49,22 @@ Filesystems API + + + FileLock implements AutoCloseable + + + + + +

+ FileLock + implements AutoCloseable to work well in try-with-resources constructs. +

+
+ + +
FileSystem.Status API removed diff --git a/openide.filesystems/manifest.mf b/openide.filesystems/manifest.mf --- a/openide.filesystems/manifest.mf +++ b/openide.filesystems/manifest.mf @@ -2,6 +2,6 @@ OpenIDE-Module: org.openide.filesystems OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml -OpenIDE-Module-Specification-Version: 9.1 +OpenIDE-Module-Specification-Version: 9.2 diff --git a/openide.filesystems/nbproject/project.properties b/openide.filesystems/nbproject/project.properties --- a/openide.filesystems/nbproject/project.properties +++ b/openide.filesystems/nbproject/project.properties @@ -41,7 +41,7 @@ # made subject to such option by the copyright holder. javac.compilerargs=-Xlint -Xlint:-serial -javac.source=1.6 +javac.source=1.7 module.jar.dir=core javadoc.main.page=org/openide/filesystems/doc-files/api.html javadoc.arch=${basedir}/arch.xml diff --git a/openide.filesystems/src/org/openide/filesystems/FileLock.java b/openide.filesystems/src/org/openide/filesystems/FileLock.java --- a/openide.filesystems/src/org/openide/filesystems/FileLock.java +++ b/openide.filesystems/src/org/openide/filesystems/FileLock.java @@ -55,14 +55,17 @@ * Normally this is sufficient protection. If you really need an atomic read, you may * simply lock the file, perform the read, and unlock it when done. The file will still * be protected against writes, although the read operation did not request a lock. -* +*

+* The {@code FileLock} implements {@link AutoCloseable}, so it can be created within +* try-with-resources resource clause and the lock will be released at the end of the try block. +* * @see FileObject -* +* @since 9.2 implements {@code AutoCloseable} interface. * @author Petr Hamernik, Jaroslav Tulach, Ian Formanek * @version 0.16, Jun 5, 1997 * */ -public class FileLock { // XXX JDK 7: implements AutoCloseable +public class FileLock implements AutoCloseable { // ========================= NONE file lock ===================================== /** Constant that can be used in filesystems that do not support locking. @@ -109,6 +112,14 @@ locked = false; } + /** + * Releases the lock. Equivalent to {@link #releaseLock} call. + */ + @Override + public void close() { + releaseLock(); + } + // End of the original part // ============================================================================ diff --git a/openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectTestHid.java b/openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectTestHid.java --- a/openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectTestHid.java +++ b/openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectTestHid.java @@ -283,23 +283,20 @@ FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); - FileLock lock; - try { - lock = fo1.lock(); + // use the new AutoCloseable feature + try (FileLock lock = fo1.lock()) { + fo1.rename(lock, "Aaa", "java"); + assertEquals("Name is Aaa", "Aaa", fo1.getName()); + fo1.rename(lock, "bbb", "java"); + assertEquals("Name is bbb", "bbb", fo1.getName()); + fo1.rename(lock, "aaa", "java"); + assertEquals("Name is lowercase", "aaa", fo1.getName()); } catch (IOException iex) { fsAssert( "expected copy will success on writable FS", fs.isReadOnly() || fo1.isReadOnly() ); - return; } - fo1.rename(lock, "Aaa", "java"); - assertEquals("Name is Aaa", "Aaa", fo1.getName()); - fo1.rename(lock, "bbb", "java"); - assertEquals("Name is bbb", "bbb", fo1.getName()); - fo1.rename(lock, "aaa", "java"); - assertEquals("Name is lowercase", "aaa", fo1.getName()); - lock.releaseLock(); } /** Test of copy method, of class org.openide.filesystems.FileObject. */ @@ -540,16 +537,12 @@ FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); FileObject fo2 = getTestFile2(fold); - FileLock lock = null; - try { - lock = fo1.lock(); + try (FileLock lock = fo1.lock()) { fo1.move(lock, fold,fo2.getName(),fo2.getExt()); } catch (IOException iex) { /** Test passed*/ return; - } finally { - if (lock != null) lock.releaseLock(); } fsFail ("move should fire exception if file already exists"); }