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