use of org.apache.wicket.util.time.Time in project wicket by apache.
the class ConcatBundleResource method getResourceStream.
@Override
public IResourceStream getResourceStream() {
List<IResourceStream> resources = collectResourceStreams();
if (resources == null) {
return null;
}
byte[] bytes;
try {
bytes = readAllResources(resources);
} catch (IOException e) {
return null;
} catch (ResourceStreamNotFoundException e) {
return null;
}
final String contentType = findContentType(resources);
final Time lastModified = findLastModified(resources);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
final long length = bytes.length;
AbstractResourceStream ret = new AbstractResourceStream() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
return inputStream;
}
@Override
public Bytes length() {
return Bytes.bytes(length);
}
@Override
public String getContentType() {
return contentType;
}
@Override
public Time lastModifiedTime() {
return lastModified;
}
@Override
public void close() throws IOException {
inputStream.close();
}
};
return ret;
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class StoredResponsesMapTest method getExpiredValue.
/**
* Verifies that getting a value which is expired will return <code>null</code>.
*
* @throws Exception
*/
@Test
public void getExpiredValue() throws Exception {
Time start = Time.now();
Duration timeout = Duration.milliseconds(50);
StoredResponsesMap map = new StoredResponsesMap(1000, timeout);
assertEquals(0, map.size());
map.put("1", new BufferedWebResponse(null));
assertEquals(1, map.size());
// sleep for twice longer than the timeout
TimeUnit.MILLISECONDS.sleep(timeout.getMilliseconds() * 2);
assertTrue("The timeout has passed.", Time.now().subtract(start).compareTo(timeout) == 1);
Object value = map.get("1");
assertNull(value);
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class PageAccessSynchronizer method lockPage.
/**
* Acquire a lock to a page
*
* @param pageId
* page id
* @throws CouldNotLockPageException
* if lock could not be acquired
*/
public void lockPage(int pageId) throws CouldNotLockPageException {
final Thread thread = Thread.currentThread();
final PageLock lock = new PageLock(pageId, thread);
final Time start = Time.now();
boolean locked = false;
final boolean isDebugEnabled = logger.isDebugEnabled();
PageLock previous = null;
Duration timeout = getTimeout(pageId);
while (!locked && start.elapsedSince().lessThan(timeout)) {
if (isDebugEnabled) {
logger.debug("'{}' attempting to acquire lock to page with id '{}'", thread.getName(), pageId);
}
previous = locks.get().putIfAbsent(pageId, lock);
if (previous == null || previous.thread == thread) {
// first thread to acquire lock or lock is already owned by this thread
locked = true;
} else {
// wait for a lock to become available
long remaining = remaining(start, timeout);
if (remaining > 0) {
previous.waitForRelease(remaining, isDebugEnabled);
}
}
}
if (locked) {
if (isDebugEnabled) {
logger.debug("{} acquired lock to page {}", thread.getName(), pageId);
}
} else {
if (logger.isWarnEnabled()) {
logger.warn("Thread '{}' failed to acquire lock to page with id '{}', attempted for {} out of allowed {}." + " The thread that holds the lock has name '{}'.", thread.getName(), pageId, start.elapsedSince(), timeout, previous.thread.getName());
if (Application.exists()) {
ThreadDumpStrategy strategy = Application.get().getExceptionSettings().getThreadDumpStrategy();
switch(strategy) {
case ALL_THREADS:
Threads.dumpAllThreads(logger);
break;
case THREAD_HOLDING_LOCK:
Threads.dumpSingleThread(logger, previous.thread);
break;
case NO_THREADS:
default:
}
}
}
throw new CouldNotLockPageException(pageId, thread.getName(), timeout);
}
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class PageAccessSynchronizerTest method testBlocking.
/**
* @throws Exception
*/
@Test
public void testBlocking() throws Exception {
final PageAccessSynchronizer sync = new PageAccessSynchronizer(Duration.seconds(5));
final Duration hold = Duration.seconds(1);
final Time[] t1locks = new Time[1];
final Time[] t2locks = new Time[1];
class T1 extends Thread {
@Override
public void run() {
sync.lockPage(1);
t1locks[0] = Time.now();
hold.sleep();
sync.unlockAllPages();
}
}
class T2 extends Thread {
@Override
public void run() {
sync.lockPage(1);
t2locks[0] = Time.now();
sync.unlockAllPages();
}
}
T1 t1 = new T1();
t1.setName("t1");
T2 t2 = new T2();
t2.setName("t2");
t1.start();
Duration.milliseconds(100).sleep();
t2.start();
t1.join();
t2.join();
assertTrue(!t2locks[0].before(t1locks[0].add(hold)));
}
use of org.apache.wicket.util.time.Time in project wicket by apache.
the class ValueMapTest method getAs.
/**
* test other getAs methods
*/
@Test
public void getAs() {
ValueMap vm = new ValueMap();
Boolean booleanValue = true;
Integer integerValue = 42;
Long longValue = integerValue * 1L;
Double doubleValue = integerValue * 1.0D;
Time timeValue = Time.now();
Duration durationValue = Duration.hours(1);
boolean defBoolean = !booleanValue;
int defInteger = 10101;
long defLong = defInteger * 1L;
double defDouble = defInteger * 1.0D;
Time defTime = Time.now();
Duration defDuration = Duration.hours(42);
vm.put("num", integerValue.toString());
vm.put("num.bad", "xxx");
vm.put("time", timeValue.toString());
vm.put("time.bad", "xxx");
vm.put("duration", durationValue.toString());
vm.put("duration.bad", "xxx");
vm.put("boolean", booleanValue.toString());
vm.put("boolean.bad", "xxx");
// boolean
assertEquals(booleanValue, vm.getAsBoolean("boolean"));
assertNull(vm.getAsBoolean("boolean.bad"));
assertEquals(defBoolean, vm.getAsBoolean("boolean.bad", defBoolean));
assertNull(vm.getAsBoolean("boolean.missing"));
assertEquals(defBoolean, vm.getAsBoolean("boolean.missing", defBoolean));
assertEquals(!defBoolean, vm.getAsBoolean("boolean.missing", !defBoolean));
// integer
assertEquals(integerValue, vm.getAsInteger("num"));
assertNull(vm.getAsInteger("num.bad"));
assertEquals(defInteger, vm.getAsInteger("num.bad", defInteger));
assertNull(vm.getAsInteger("num.missing"));
assertEquals(defInteger, vm.getAsInteger("num.missing", defInteger));
// long
assertEquals(longValue, vm.getAsLong("num"));
assertNull(vm.getAsLong("num.bad"));
assertEquals(defLong, vm.getAsLong("num.bad", defLong));
assertNull(vm.getAsLong("num.missing"));
assertEquals(defLong, vm.getAsLong("num.missing", defLong));
// double
assertEquals(doubleValue, vm.getAsDouble("num"));
assertNull(vm.getAsDouble("num.bad"));
assertEquals(defDouble, vm.getAsDouble("num.bad", defDouble), 0.001);
assertNull(vm.getAsDouble("num.missing"));
assertEquals(defDouble, vm.getAsDouble("num.missing", defDouble), 0.001);
// time
// use toSTring since
assertEquals(timeValue.toString(), vm.getAsTime("time").toString());
// equals seems
// broken
assertNull(vm.getAsTime("time.bad"));
assertEquals(defTime, vm.getAsTime("time.bad", defTime));
assertNull(vm.getAsTime("time.missing"));
assertEquals(defTime, vm.getAsTime("time.missing", defTime));
// duration
assertEquals(durationValue, vm.getAsDuration("duration"));
assertNull(vm.getAsDuration("duration.bad"));
assertEquals(defDuration, vm.getAsDuration("duration.bad", defDuration));
assertNull(vm.getAsDuration("duration.missing"));
assertEquals(defDuration, vm.getAsDuration("duration.missing", defDuration));
}
Aggregations