Search in sources :

Example 1 with ManagedFile

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

the class FileLockTest method mixedLock.

@Test
public void mixedLock() 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.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(300);
        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 2 with ManagedFile

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

the class FileLockTest method lockAndReadTest.

@Test
public void lockAndReadTest() throws IOException {
    File f = File.createTempFile("common-util-FileLockTest", "tmp");
    try {
        // Now let's try to write the file.
        FileWriter fw = new FileWriter(f);
        fw.append("FileLockTest reading passed !");
        fw.close();
        final ManagedFile managed = new ManagedFile(f, 1000, 1000);
        Lock fl = managed.accessRead();
        FileReader fr = new FileReader(f);
        char[] chars = new char[1024];
        int length = fr.read(chars);
        fr.close();
        fl.unlock();
        // Let's read it back
        System.out.println(new String(chars, 0, length));
    } 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 3 with ManagedFile

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

the class ReadLockTest method readLock.

@Test
public void readLock() throws IOException {
    File f = getFile();
    try {
        // System.out.println("trying the lock on " + f.getAbsolutePath());
        final ManagedFile managed = new ManagedFile(f, 1000, 1000);
        Lock fl = managed.accessRead();
        // System.out.println("Got the lock on " + f.getAbsolutePath());
        List<Future<Boolean>> results = new ArrayList<Future<Boolean>>(5);
        for (int i = 0; i < 5; 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) IOException(java.io.IOException) ManagedFile(org.glassfish.common.util.admin.ManagedFile) Callable(java.util.concurrent.Callable) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock) Future(java.util.concurrent.Future) ManagedFile(org.glassfish.common.util.admin.ManagedFile) File(java.io.File) Test(org.junit.Test)

Example 4 with ManagedFile

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

the class TestClient method main.

public static void main(String[] args) {
    FileLockTest test = new FileLockTest();
    byte[] bytes = new byte[100];
    String mode = "read";
    if (args.length > 0) {
        mode = args[0];
    }
    try {
        File f = test.getFile();
        ManagedFile managed = new ManagedFile(f, -1, -1);
        Lock lock = null;
        try {
            if (mode.equals("read")) {
                lock = managed.accessRead();
            } else if (mode.equals("write")) {
                lock = managed.accessWrite();
            } else {
                // System.out.println("usage : TestClient [ read | write ]. Invalid option : " + mode);
                return;
            }
        } catch (TimeoutException e) {
            e.printStackTrace();
            return;
        }
        // System.out.println("I have the lock in "+ mode +" mode, press enter to release ");
        System.in.read(bytes);
        lock.unlock();
    // System.out.println("released");
    } catch (IOException e) {
        // To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
        return;
    }
}
Also used : IOException(java.io.IOException) ManagedFile(org.glassfish.common.util.admin.ManagedFile) File(java.io.File) ManagedFile(org.glassfish.common.util.admin.ManagedFile) Lock(java.util.concurrent.locks.Lock) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with ManagedFile

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

the class DomainXmlPersistence method getPidFile.

private synchronized ManagedFile getPidFile() throws IOException {
    File location = null;
    try {
        // I am locking indefinitely with a 2 seconds timeOut.
        location = new File(env.getConfigDirPath(), "lockfile");
        if (!location.exists()) {
            if (!location.createNewFile()) {
                if (!location.exists()) {
                    String message = localStrings.getLocalString("cannotCreateLockfile", "Cannot create lock file at {0}, configuration changes will not be persisted", location);
                    logger.log(Level.SEVERE, message);
                    throw new IOException(message);
                }
            }
        }
        return new ManagedFile(location, 2000, -1);
    } catch (IOException e) {
        logger.log(Level.SEVERE, localStrings.getLocalString("InvalidLocation", "Cannot obtain lockfile location {0}, configuration changes will not be persisted", location), e);
        throw e;
    }
}
Also used : ManagedFile(org.glassfish.common.util.admin.ManagedFile) ManagedFile(org.glassfish.common.util.admin.ManagedFile)

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