use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class ResourceStreamRequestHandler method configure.
/**
* Configures the ResourceStreamResource used by this request handler
*
* @param resource
* the resource to configure
*/
protected void configure(ResourceStreamResource resource) {
resource.setFileName(fileName);
if (contentDisposition != null) {
resource.setContentDisposition(contentDisposition);
} else {
resource.setContentDisposition(Strings.isEmpty(fileName) ? ContentDisposition.INLINE : ContentDisposition.ATTACHMENT);
}
final Duration cacheDuration = getCacheDuration();
if (cacheDuration != null) {
resource.setCacheDuration(cacheDuration);
}
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class AjaxTimerBehaviorTest method setVisibleSetsTimeout.
/**
*/
@Test
public void setVisibleSetsTimeout() {
Duration dur = Duration.seconds(20);
final AjaxSelfUpdatingTimerBehavior timer = new AjaxSelfUpdatingTimerBehavior(dur);
final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
final Label label = new Label(MockPageWithLinkAndComponent.COMPONENT_ID, "Hello");
page.add(label);
page.add(new AjaxLink<Void>(MockPageWithLinkAndComponent.LINK_ID) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
}
});
label.setOutputMarkupId(true);
label.setVisible(false);
label.add(timer);
tester.startPage(page);
assertMatches("Wicket.Timer.set", 0);
tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
assertMatches("Wicket.Timer.set", 0);
label.setVisible(true);
tester.startPage(page);
// no visible, so timeout is set
assertMatches("Wicket.Timer.set", 1);
}
use of org.apache.wicket.util.time.Duration 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.Duration 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.Duration 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)));
}
Aggregations