Search in sources :

Example 6 with ManagedFile

use of org.glassfish.common.util.admin.ManagedFile in project Payara by payara.

the class FileLockTest method lockForReadAndWriteTest.

@Test
public void lockForReadAndWriteTest() throws IOException {
    // the last one to close always win.
    if (!System.getProperty("os.name").toUpperCase().contains("WINDOWS")) {
        return;
    }
    File f = File.createTempFile("common-util-FileLockTest", "tmp");
    try {
        // Now let's try to write the file.
        FileWriter fw = new FileWriter(f);
        fw.append("lockForReadAndWriteTest passed !");
        fw.flush();
        fw.close();
        try {
            System.out.println("file length " + f.length());
            FileReader fr = new FileReader(f);
            char[] chars = new char[1024];
            int length = fr.read(chars);
            fr.close();
            System.out.println(new String(chars, 0, length));
        } catch (IOException unexpected) {
            System.out.println("Failed, got an exception reading : " + unexpected.getMessage());
            throw unexpected;
        }
        final ManagedFile managed = new ManagedFile(f, 1000, 1000);
        Lock fl = managed.accessRead();
        FileWriter fwr = null;
        try {
            fwr = new FileWriter(f);
            fwr.append("lockForReadAndWriteTest failed !");
            fwr.close();
        } catch (IOException expected) {
            System.out.println("Got an expected exception : " + expected.getMessage());
        }
        fl.unlock();
        if (f.length() == 0) {
            System.out.println("The write lock was an advisory lock, file content was deleted !");
            return;
        }
        FileReader fr = new FileReader(f);
        char[] chars = new char[1024];
        int length = fr.read(chars);
        fr.close();
        // Let's read it back
        if (length > 0) {
            System.out.println(new String(chars, 0, length));
        } else {
            System.out.println("lockForReadAndWriteTest failed, file content is empty");
        }
    // Assert.assertTrue(new String(chars).contains("passed"));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        f.delete();
    }
}
Also used : ManagedFile(org.glassfish.common.util.admin.ManagedFile) ManagedFile(org.glassfish.common.util.admin.ManagedFile) URISyntaxException(java.net.URISyntaxException) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 7 with ManagedFile

use of org.glassfish.common.util.admin.ManagedFile in project Payara by payara.

the class FileLockTest method readLock.

@Test
public void readLock() throws IOException {
    File f = getFile();
    try {
        final ManagedFile managed = new ManagedFile(f, 1000, 1000);
        Lock fl = managed.accessRead();
        List<Future<Boolean>> results = new ArrayList<Future<Boolean>>();
        for (int i = 0; i < 5; i++) {
            final int number = i;
            results.add(Executors.newFixedThreadPool(2).submit(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    try {
                        Lock second = managed.accessRead();
                        second.unlock();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    return Boolean.TRUE;
                }
            }));
        }
        Thread.sleep(100);
        fl.unlock();
        for (Future<Boolean> result : results) {
            Boolean exitCode = result.get();
            Assert.assertEquals(exitCode.booleanValue(), true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) ManagedFile(org.glassfish.common.util.admin.ManagedFile) URISyntaxException(java.net.URISyntaxException) Lock(java.util.concurrent.locks.Lock) ManagedFile(org.glassfish.common.util.admin.ManagedFile) Test(org.junit.Test)

Example 8 with ManagedFile

use of org.glassfish.common.util.admin.ManagedFile in project Payara by payara.

the class FileLockTest method lockAndRenameTest.

// @Test
public void lockAndRenameTest() throws IOException {
    File f = File.createTempFile("common-util-FileLockTest", "tmp");
    try {
        final ManagedFile managed = new ManagedFile(f, 1000, 1000);
        Lock fl = managed.accessWrite();
        File dest = new File("filelock");
        if (f.renameTo(new File("filelock"))) {
            System.out.println("File renaming successful");
        } else {
            System.out.println("File renaming failed");
        }
        if (dest.exists()) {
            System.out.println("File is there...");
        }
        dest.delete();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ManagedFile(org.glassfish.common.util.admin.ManagedFile) ManagedFile(org.glassfish.common.util.admin.ManagedFile) URISyntaxException(java.net.URISyntaxException) Lock(java.util.concurrent.locks.Lock)

Example 9 with ManagedFile

use of org.glassfish.common.util.admin.ManagedFile in project Payara by payara.

the class FileLockTest method writeLock.

@Test
public void writeLock() throws IOException {
    final Random random = new Random();
    File f = getFile();
    try {
        final ManagedFile managed = new ManagedFile(f, 1000, 1000);
        Lock fl = managed.accessWrite();
        mainWriteState = States.LOCKED;
        List<Future<Boolean>> results = new ArrayList<Future<Boolean>>();
        final ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 3; i++) {
            final int number = i;
            results.add(executor.submit(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    try {
                        final Lock second = managed.accessWrite();
                        writeStates[number] = States.LOCKED;
                        assertWriteStates();
                        writeStates[number] = States.RELEASED;
                        second.unlock();
                        assertWriteStates();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    return Boolean.TRUE;
                }
            }));
            results.add(executor.submit(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    try {
                        final Lock second = managed.accessRead();
                        readStates[number] = States.LOCKED;
                        assertWriteStates();
                        Thread.sleep(random.nextInt(300));
                        readStates[number] = States.RELEASED;
                        second.unlock();
                        assertWriteStates();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    return Boolean.TRUE;
                }
            }));
        }
        Thread.sleep(100);
        mainWriteState = States.RELEASED;
        fl.unlock();
        for (Future<Boolean> result : results) {
            Boolean exitCode = result.get();
            Assert.assertEquals(exitCode.booleanValue(), true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) ManagedFile(org.glassfish.common.util.admin.ManagedFile) URISyntaxException(java.net.URISyntaxException) Lock(java.util.concurrent.locks.Lock) Random(java.util.Random) ManagedFile(org.glassfish.common.util.admin.ManagedFile) Test(org.junit.Test)

Example 10 with ManagedFile

use of org.glassfish.common.util.admin.ManagedFile in project Payara by payara.

the class FileLockTest method lockAndWriteTest.

@Test
public void lockAndWriteTest() throws IOException {
    File f = File.createTempFile("common-util-FileLockTest", "tmp");
    try {
        final ManagedFile managed = new ManagedFile(f, 1000, 1000);
        ManagedFile.ManagedLock fl = managed.accessWrite();
        // Now let's try to write the file.
        RandomAccessFile raf = fl.getLockedFile();
        raf.writeUTF("lockAndWriteTest Passed !");
        fl.unlock();
        // Let's read it back
        FileReader fr = new FileReader(f);
        char[] chars = new char[1024];
        int length = fr.read(chars);
        fr.close();
        f.delete();
        System.out.println(new String(chars, 0, length));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ManagedFile(org.glassfish.common.util.admin.ManagedFile) ManagedFile(org.glassfish.common.util.admin.ManagedFile) URISyntaxException(java.net.URISyntaxException) Test(org.junit.Test)

Aggregations

ManagedFile (org.glassfish.common.util.admin.ManagedFile)11 URISyntaxException (java.net.URISyntaxException)9 Lock (java.util.concurrent.locks.Lock)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)4 File (java.io.File)2 IOException (java.io.IOException)2 Random (java.util.Random)2 Callable (java.util.concurrent.Callable)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1