001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.io.FilePermission;
025: import java.net.URL;
026: import java.security.CodeSource;
027: import java.security.Permission;
028: import java.security.PermissionCollection;
029: import java.security.Policy;
030:
031: import org.jboss.util.TimedCachePolicy;
032:
033: /** Tests of the TimedCachePolicy class.
034:
035: @see org.jboss.util.TimedCachePolicy
036:
037: @author Scott.Stark@jboss.org
038: @version $Revision: 37390 $
039: */
040: public class TstTimedCache {
041: static class Refreshable implements TimedCachePolicy.TimedEntry {
042: int refreshes;
043: long expirationTime;
044: Object value;
045:
046: Refreshable(long lifetime, Object value, int refreshes) {
047: this .expirationTime = 1000 * lifetime;
048: this .value = value;
049: this .refreshes = refreshes;
050: }
051:
052: public void init(long now) {
053: expirationTime += now;
054: System.out.println(value + ".init(" + now
055: + "), expirationTime=" + expirationTime);
056: }
057:
058: public boolean isCurrent(long now) {
059: System.out.println(value + ".isCurrent(" + now + ") = "
060: + (expirationTime > now));
061: return expirationTime > now;
062: }
063:
064: public boolean refresh() {
065: refreshes--;
066: System.out.println(value + ".refresh() = "
067: + (refreshes > 0));
068: return refreshes > 0;
069: }
070:
071: public void destroy() {
072: System.out.println(value + ".destroy()");
073: }
074:
075: public Object getValue() {
076: return value;
077: }
078: }
079:
080: /**
081: * @param args the command line arguments
082: */
083: public static void main(String args[]) {
084: TimedCachePolicy cache = new TimedCachePolicy(20, false, 1);
085: cache.create();
086: cache.start();
087: cache.insert("1", new Refreshable(5, "value1", 4));
088: cache.insert("2", new Refreshable(3, "value2", 10));
089: cache.insert("3", "value3");
090: long start = System.currentTimeMillis();
091: // Loop until the longest lived value is gone
092: while (cache.peek("2") != null) {
093: long now = System.currentTimeMillis();
094: System.out.println("Elapsed: " + (now - start) / 1000);
095: System.out.println("get(1) -> " + cache.get("1"));
096: System.out.println("get(2) -> " + cache.get("2"));
097: System.out.println("get(3) -> " + cache.get("3"));
098: try {
099: Thread.currentThread().sleep(3 * 1000);
100: } catch (InterruptedException e) {
101: }
102: }
103: long now = System.currentTimeMillis();
104: System.out.println("End, elapsed: " + (now - start) / 1000);
105: System.out.println("get(1) -> " + cache.get("1"));
106: System.out.println("get(2) -> " + cache.get("2"));
107: System.out.println("get(3) -> " + cache.get("3"));
108: }
109:
110: }
|