Search in sources :

Example 1 with Duration

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);
    }
}
Also used : Duration(org.apache.wicket.util.time.Duration)

Example 2 with Duration

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);
}
Also used : MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Label(org.apache.wicket.markup.html.basic.Label) Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Example 3 with Duration

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);
}
Also used : Time(org.apache.wicket.util.time.Time) Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Example 4 with Duration

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);
    }
}
Also used : ThreadDumpStrategy(org.apache.wicket.settings.ExceptionSettings.ThreadDumpStrategy) Time(org.apache.wicket.util.time.Time) Duration(org.apache.wicket.util.time.Duration)

Example 5 with Duration

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)));
}
Also used : Duration(org.apache.wicket.util.time.Duration) Time(org.apache.wicket.util.time.Time) Test(org.junit.Test)

Aggregations

Duration (org.apache.wicket.util.time.Duration)17 Test (org.junit.Test)11 MockPageWithLinkAndComponent (org.apache.wicket.MockPageWithLinkAndComponent)4 Time (org.apache.wicket.util.time.Time)4 Label (org.apache.wicket.markup.html.basic.Label)3 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 AbstractResource (org.apache.wicket.request.resource.AbstractResource)2 BaseWicketTester (org.apache.wicket.util.tester.BaseWicketTester)2 JSONArray (com.github.openjson.JSONArray)1 JSONException (com.github.openjson.JSONException)1 JSONObject (com.github.openjson.JSONObject)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Page (org.apache.wicket.Page)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 Method (org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method)1 IAjaxCallListener (org.apache.wicket.ajax.attributes.IAjaxCallListener)1 ThrottlingSettings (org.apache.wicket.ajax.attributes.ThrottlingSettings)1 JSONFunction (org.apache.wicket.ajax.json.JSONFunction)1