KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > collectable > CollectableDbImpl


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.collectable;
25
26 public class CollectableDbImpl extends org.myoodb.MyOodbObject implements Collectable
27 {
28     private static long m_lastLocalTime = 0;
29
30     protected long m_lastFixUpTime;
31
32     public static synchronized long getSynchronizedLocalTime()
33     {
34         long localtime = new java.util.Date JavaDoc().getTime();
35
36         if (m_lastLocalTime >= localtime)
37         {
38             localtime = m_lastLocalTime + 1;
39         }
40
41         m_lastLocalTime = localtime;
42
43         return localtime;
44     }
45
46     public static void fixUpReference(java.util.Collection JavaDoc container, long fixUpTime)
47     {
48         java.util.Iterator JavaDoc iter = container.iterator();
49         while (iter.hasNext())
50         {
51             Object JavaDoc object = iter.next();
52
53             try
54             {
55                 if (object instanceof Collectable)
56                 {
57                     ((Collectable) object).fixUpReference(fixUpTime);
58                 }
59             }
60             catch (org.myoodb.exception.ObjectNotFoundException e)
61             {
62                 iter.remove();
63             }
64         }
65     }
66
67     public static void fixUpReference(java.util.Map JavaDoc container, long fixUpTime)
68     {
69         java.util.Iterator JavaDoc iter = container.values().iterator();
70         while (iter.hasNext())
71         {
72             Object JavaDoc object = iter.next();
73
74             try
75             {
76                 if (object instanceof Collectable)
77                 {
78                     ((Collectable) object).fixUpReference(fixUpTime);
79                 }
80             }
81             catch (org.myoodb.exception.ObjectNotFoundException e)
82             {
83                 iter.remove();
84             }
85         }
86     }
87
88     public CollectableDbImpl()
89     {
90         m_lastFixUpTime = 0;
91     }
92
93     private org.myoodb.core.AbstractTransaction getCurrentlyAssociatedTransaction()
94     {
95         return org.myoodb.core.MyOodbManager.getTheManager().getTransactionManager().getTransaction(getLockIdentifier());
96     }
97
98     protected String JavaDoc invokedBy()
99     {
100         String JavaDoc owner = "";
101
102         java.lang.Thread JavaDoc thread = java.lang.Thread.currentThread();
103
104         if (thread instanceof org.myoodb.core.command.CommandThread)
105         {
106             owner = ((org.myoodb.core.command.CommandThread) thread).getOwner().getName();
107         }
108
109         return owner;
110     }
111
112     protected boolean referenceHasBeenFixedUp(long fixUpTime)
113     {
114         return fixUpTime <= m_lastFixUpTime;
115     }
116
117     public long getLocalTime()
118     {
119         return getSynchronizedLocalTime();
120     }
121
122     public void lock()
123     {
124         // do nothing for now. locking is an implicit behavior for explicit transactions
125
}
126
127     public boolean isLocked()
128     {
129         return (getContainer().getLock().getLockLevel() >= org.myoodb.core.AbstractLock.ACCESS_WRITE);
130     }
131
132     public boolean isImplicitLock()
133     {
134         org.myoodb.core.AbstractTransaction tx = getCurrentlyAssociatedTransaction();
135         return ((tx != null) && (tx.getTransactionType() == org.myoodb.core.AbstractTransaction.IMPLICIT_TRANSACTION));
136     }
137
138     public boolean isExplicitLock()
139     {
140         org.myoodb.core.AbstractTransaction tx = getCurrentlyAssociatedTransaction();
141         return ((tx != null) && (tx.getTransactionType() == org.myoodb.core.AbstractTransaction.EXPLICIT_TRANSACTION));
142     }
143
144     public String JavaDoc isLockedBy()
145     {
146         org.myoodb.core.AbstractTransaction tx = getCurrentlyAssociatedTransaction();
147         return (tx != null) ? tx.getOwner().getName() : null;
148     }
149
150     public long getLockIdentifier()
151     {
152         return getContainer().getLock().getLockIdentifier();
153     }
154
155     public void testReference()
156     {
157         // nothing to do
158
}
159
160     public void cleanUpReference()
161     {
162         // nothing to do
163
}
164
165     public void fixUpReference(long fixUpTime)
166     {
167         m_lastFixUpTime = fixUpTime;
168     }
169
170     public String JavaDoc toString()
171     {
172         return super.toString();
173     }
174 }
175
Popular Tags