KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > taglibs > cache > CacheEntry


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.web.taglibs.cache;
25
26 /**
27  * CacheEntry holds cached JSP fragments
28  * a) response content b) expiryTime
29  *
30  * XXX: should implement methods to enable serialization of cached response?
31  */

32 public class CacheEntry
33 {
34     public static final int NO_TIMEOUT = -1;
35
36     String JavaDoc content;
37     volatile long expireTime;
38
39     /**
40      * Constructs a CacheEntry using the response string to be
41      * cached and the timeout after which the entry will expire
42      */

43     public CacheEntry(String JavaDoc response, int timeout) {
44         content = response;
45         computeExpireTime(timeout);
46     }
47
48     /**
49      * set the real expire time
50      * @param expireTime in milli seconds
51      */

52     public void setExpireTime(long expireTime) {
53         this.expireTime = expireTime;
54     }
55
56     /**
57      * set the content stored in the entry
58      */

59     public String JavaDoc getContent() {
60         return this.content;
61     }
62
63     /**
64      * compute when this entry to be expired based on timeout relative to
65      * current time.
66      * @param timeout in seconds
67      */

68     public void computeExpireTime(int timeout) {
69         // timeout is relative to current time
70
this.expireTime = (timeout == NO_TIMEOUT) ? timeout :
71                           System.currentTimeMillis() + (timeout * 1000);
72     }
73
74     /**
75      * is this response still valid?
76      */

77     public boolean isValid() {
78         return (expireTime > System.currentTimeMillis() ||
79                 expireTime == NO_TIMEOUT);
80     }
81
82     /**
83      * clear the contents
84      */

85     public void clear() {
86         content = null;
87         expireTime = 0L;
88     }
89 }
90
Popular Tags