001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.tomcat.util.buf;
018:
019: import java.io.Serializable;
020:
021: // XXX Shouldn't be here - has nothing to do with buffers.
022:
023: /**
024: * Main tool for object expiry.
025: * Marks creation and access time of an "expirable" object,
026: * and extra properties like "id", "valid", etc.
027: *
028: * Used for objects that expire - originally Sessions, but
029: * also Contexts, Servlets, cache - or any other object that
030: * expires.
031: *
032: * @author Costin Manolache
033: */
034: public final class TimeStamp implements Serializable {
035: private long creationTime = 0L;
036: private long lastAccessedTime = creationTime;
037: private long this AccessedTime = creationTime;
038: private boolean isNew = true;
039: private long maxInactiveInterval = -1;
040: private boolean isValid = false;
041: MessageBytes name;
042: int id = -1;
043:
044: Object parent;
045:
046: public TimeStamp() {
047: }
048:
049: // -------------------- Active methods --------------------
050:
051: /**
052: * Access notification. This method takes a time parameter in order
053: * to allow callers to efficiently manage expensive calls to
054: * System.currentTimeMillis()
055: */
056: public void touch(long time) {
057: this .lastAccessedTime = this .this AccessedTime;
058: this .this AccessedTime = time;
059: this .isNew = false;
060: }
061:
062: // -------------------- Property access --------------------
063:
064: /** Return the "name" of the timestamp. This can be used
065: * to associate unique identifier with each timestamped object.
066: * The name is a MessageBytes - i.e. a modifiable byte[] or char[].
067: */
068: public MessageBytes getName() {
069: if (name == null)
070: name = MessageBytes.newInstance();//lazy
071: return name;
072: }
073:
074: /** Each object can have an unique id, similar with name but
075: * providing faster access ( array vs. hashtable lookup )
076: */
077: public int getId() {
078: return id;
079: }
080:
081: public void setId(int id) {
082: this .id = id;
083: }
084:
085: /** Returns the owner of this stamp ( the object that is
086: * time-stamped ).
087: * For a
088: */
089: public void setParent(Object o) {
090: parent = o;
091: }
092:
093: public Object getParent() {
094: return parent;
095: }
096:
097: public void setCreationTime(long time) {
098: this .creationTime = time;
099: this .lastAccessedTime = time;
100: this .this AccessedTime = time;
101: }
102:
103: public long getLastAccessedTime() {
104: return lastAccessedTime;
105: }
106:
107: public long getThisAccessedTime() {
108: return this AccessedTime;
109: }
110:
111: /** Inactive interval in millis - the time is computed
112: * in millis, convert to secs in the upper layer
113: */
114: public long getMaxInactiveInterval() {
115: return maxInactiveInterval;
116: }
117:
118: public void setMaxInactiveInterval(long interval) {
119: maxInactiveInterval = interval;
120: }
121:
122: public boolean isValid() {
123: return isValid;
124: }
125:
126: public void setValid(boolean isValid) {
127: this .isValid = isValid;
128: }
129:
130: public boolean isNew() {
131: return isNew;
132: }
133:
134: public void setNew(boolean isNew) {
135: this .isNew = isNew;
136: }
137:
138: public long getCreationTime() {
139: return creationTime;
140: }
141:
142: // -------------------- Maintainance --------------------
143:
144: public void recycle() {
145: creationTime = 0L;
146: lastAccessedTime = 0L;
147: maxInactiveInterval = -1;
148: isNew = true;
149: isValid = false;
150: id = -1;
151: if (name != null)
152: name.recycle();
153: }
154:
155: }
|