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