KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > ObjectRecord


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool;
5
6 import java.util.Date JavaDoc;
7 import java.util.ConcurrentModificationException JavaDoc;
8
9 /**
10  * Stores the properties of an object in a pool.
11  *
12  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
13  */

14 class ObjectRecord {
15
16     private long created;
17     private long lastUsed;
18     private Object JavaDoc object;
19     private Object JavaDoc clientObject;
20     private boolean inUse;
21
22     /**
23      * Created a new record for the specified pooled object. Objects default to
24      * being in use when created, so that they can't be stolen away from the
25      * creator by another thread.
26      */

27     public ObjectRecord(Object JavaDoc ob) {
28         this(ob, true);
29     }
30
31     /**
32      * Created a new record for the specified pooled object. Sets the initial
33      * state to in use or not.
34      */

35     public ObjectRecord(Object JavaDoc ob, boolean inUse) {
36         created = lastUsed = System.currentTimeMillis();
37         object = ob;
38         this.inUse = inUse;
39     }
40
41     /**
42      * Gets the date when this connection was originally opened.
43      */

44     public Date JavaDoc getCreationDate() {
45         return new Date JavaDoc(created);
46     }
47
48     /**
49      * Gets the date when this connection was last used.
50      */

51     public Date JavaDoc getLastUsedDate() {
52         return new Date JavaDoc(lastUsed);
53     }
54
55     /**
56      * Gets the time (in milliseconds) since this connection was last used.
57      */

58     public long getMillisSinceLastUse() {
59         return System.currentTimeMillis() - lastUsed;
60     }
61
62     /**
63      * Tells whether this connection is currently in use. This is not
64      * synchronized since you probably want to synchronize at a higher level
65      * (if not in use, do something), etc.
66      */

67     public boolean isInUse() {
68         return inUse;
69     }
70
71     /**
72      * Sets whether this connection is currently in use.
73      * @throws java.util.ConcurrentModificationException
74      * Occurs when the connection is already in use and it is set to be
75      * in use, or it is not in use and it is set to be not in use.
76      */

77     public synchronized void setInUse(boolean inUse) throws ConcurrentModificationException JavaDoc {
78         if (this.inUse == inUse)
79             throw new ConcurrentModificationException JavaDoc();
80         this.inUse = inUse;
81         lastUsed = System.currentTimeMillis();
82         if (!inUse) clientObject = null;
83     }
84
85     /**
86      * Sets the last used time to the current time.
87      */

88     public void setLastUsed() {
89         lastUsed = System.currentTimeMillis();
90     }
91
92     /**
93      * Gets the pooled object associated with this record.
94      */

95     public Object JavaDoc getObject() {
96         return object;
97     }
98
99     /**
100      * Sets the client object associated with this object. Not always used.
101      */

102     public void setClientObject(Object JavaDoc o) {
103         clientObject = o;
104     }
105
106     /**
107      * Gets the client object associated with this object. If there is none,
108      * returns the normal object (which is the default).
109      */

110     public Object JavaDoc getClientObject() {
111         return clientObject == null ? object : clientObject;
112     }
113
114     /**
115      * Shuts down this object - it will be useless thereafter.
116      */

117     public void close() {
118         object = null;
119         clientObject = null;
120         created = lastUsed = Long.MAX_VALUE;
121         inUse = true;
122     }
123 }
124
Popular Tags