Search in sources :

Example 1 with Time

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;
}
Also used : AbstractResourceStream(org.apache.wicket.util.resource.AbstractResourceStream) IResourceStream(org.apache.wicket.util.resource.IResourceStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Example 2 with Time

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

Example 3 with Time

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

Example 4 with Time

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

Example 5 with Time

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

Aggregations

Time (org.apache.wicket.util.time.Time)21 Test (org.junit.Test)6 IOException (java.io.IOException)5 IResourceStream (org.apache.wicket.util.resource.IResourceStream)5 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)4 Duration (org.apache.wicket.util.time.Duration)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 LocalDateTime (java.time.LocalDateTime)2 File (java.io.File)1 Locale (java.util.Locale)1 Random (java.util.Random)1 TimeZone (java.util.TimeZone)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpHeaderCollection (org.apache.wicket.request.HttpHeaderCollection)1 Response (org.apache.wicket.request.Response)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1