001: /*
002: * Licensed under the X license (see http://www.x.org/terms.htm)
003: */
004: package org.ofbiz.minerva.pool;
005:
006: import java.util.Date;
007: import java.util.ConcurrentModificationException;
008:
009: /**
010: * Stores the properties of an object in a pool.
011: *
012: * @author Aaron Mulder (ammulder@alumni.princeton.edu)
013: */
014: class ObjectRecord {
015:
016: private long created;
017: private long lastUsed;
018: private Object object;
019: private Object clientObject;
020: private boolean inUse;
021:
022: /**
023: * Created a new record for the specified pooled object. Objects default to
024: * being in use when created, so that they can't be stolen away from the
025: * creator by another thread.
026: */
027: public ObjectRecord(Object ob) {
028: this (ob, true);
029: }
030:
031: /**
032: * Created a new record for the specified pooled object. Sets the initial
033: * state to in use or not.
034: */
035: public ObjectRecord(Object ob, boolean inUse) {
036: created = lastUsed = System.currentTimeMillis();
037: object = ob;
038: this .inUse = inUse;
039: }
040:
041: /**
042: * Gets the date when this connection was originally opened.
043: */
044: public Date getCreationDate() {
045: return new Date(created);
046: }
047:
048: /**
049: * Gets the date when this connection was last used.
050: */
051: public Date getLastUsedDate() {
052: return new Date(lastUsed);
053: }
054:
055: /**
056: * Gets the time (in milliseconds) since this connection was last used.
057: */
058: public long getMillisSinceLastUse() {
059: return System.currentTimeMillis() - lastUsed;
060: }
061:
062: /**
063: * Tells whether this connection is currently in use. This is not
064: * synchronized since you probably want to synchronize at a higher level
065: * (if not in use, do something), etc.
066: */
067: public boolean isInUse() {
068: return inUse;
069: }
070:
071: /**
072: * Sets whether this connection is currently in use.
073: * @throws java.util.ConcurrentModificationException
074: * Occurs when the connection is already in use and it is set to be
075: * in use, or it is not in use and it is set to be not in use.
076: */
077: public synchronized void setInUse(boolean inUse)
078: throws ConcurrentModificationException {
079: if (this .inUse == inUse)
080: throw new ConcurrentModificationException();
081: this .inUse = inUse;
082: lastUsed = System.currentTimeMillis();
083: if (!inUse)
084: clientObject = null;
085: }
086:
087: /**
088: * Sets the last used time to the current time.
089: */
090: public void setLastUsed() {
091: lastUsed = System.currentTimeMillis();
092: }
093:
094: /**
095: * Gets the pooled object associated with this record.
096: */
097: public Object getObject() {
098: return object;
099: }
100:
101: /**
102: * Sets the client object associated with this object. Not always used.
103: */
104: public void setClientObject(Object o) {
105: clientObject = o;
106: }
107:
108: /**
109: * Gets the client object associated with this object. If there is none,
110: * returns the normal object (which is the default).
111: */
112: public Object getClientObject() {
113: return clientObject == null ? object : clientObject;
114: }
115:
116: /**
117: * Shuts down this object - it will be useless thereafter.
118: */
119: public void close() {
120: object = null;
121: clientObject = null;
122: created = lastUsed = Long.MAX_VALUE;
123: inUse = true;
124: }
125: }
|