001: package org.enhydra.jdbc.util;
002:
003: import java.util.regex.Pattern;
004:
005: import org.enhydra.jdbc.util.Logger;
006: import org.apache.commons.logging.impl.Log4JLogger;
007: import org.apache.commons.logging.Log;
008:
009: /**
010: * one RequestCacheObject for one sql request found in the
011: * configuration file
012: */
013: public class RequestCacheObject {
014:
015: // the sql request (sql or pattern)
016: private String request_ = null;
017:
018: // time is the beginning of the first ResulSet computing
019: private long time_ = 0;
020:
021: // the Object to cache
022: private Object result_ = null;
023:
024: // pattern, compiled from the configuration source file
025: private Pattern pattern_ = null;
026:
027: // the current object must be live timeToLive_
028: private long timeToLive_ = 0;
029:
030: private static Logger logger;
031:
032: public RequestCacheObject(long ttl) {
033: logger = new Logger((Log) (new Log4JLogger(
034: "org.enhydra.jdbc.util")));
035: timeToLive_ = ttl;
036: }
037:
038: public RequestCacheObject(String req, Pattern pattern, long ttl) {
039: logger = new Logger((Log) (new Log4JLogger(
040: "org.enhydra.jdbc.util")));
041: logger.debug("RequestCacheObject:RequestCacheObject req=<"
042: + req + "> ttl=<" + ttl + ">");
043: request_ = req;
044: pattern_ = pattern;
045: timeToLive_ = ttl;
046: }
047:
048: public void setResult(Object rset) {
049: logger.debug("RequestCacheObject:setResult for=<" + request_
050: + ">");
051: result_ = rset;
052: time_ = System.currentTimeMillis();
053: }
054:
055: public boolean isAlive() {
056: long now = System.currentTimeMillis();
057: if (now - time_ > timeToLive_) {
058: logger.debug("RequestCacheObject:isAlive out of live");
059: return false;
060: } else {
061: if (result_ == null) {
062: logger
063: .debug("RequestCacheObject:isAlive Object is null ");
064: return false;
065: } else {
066: logger
067: .debug("RequestCacheObject:isAlive Object is not null");
068: return true;
069: }
070: }
071: }
072:
073: public Object getResult() {
074: if (result_ == null)
075: logger
076: .debug("RequestCacheObject:getResult result_ is null");
077:
078: return result_;
079: }
080:
081: public Pattern getPattern() {
082: return pattern_;
083: }
084:
085: public String getRequest() {
086: return request_;
087: }
088:
089: public String toString() {
090: StringBuffer sbuf = new StringBuffer();
091: sbuf.append("request=<" + request_ + "> time=<" + time_
092: + "> time to live=<" + timeToLive_ + "> pattern=<"
093: + pattern_.pattern() + ">");
094: return sbuf.toString();
095: }
096:
097: public void close() {
098: request_ = null;
099: /* TBD
100: try {
101: result_.close();
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: */
106: result_ = null;
107: pattern_ = null;
108: }
109: }
|