001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.collectable;
025:
026: import org.myoodb.core.*;
027:
028: public class CollectableDbImpl extends org.myoodb.MyOodbObject
029: implements Collectable {
030: protected long m_lastFixUpTime;
031:
032: public static void fixUpReference(java.util.Collection container,
033: long fixUpTime) {
034: java.util.Iterator iter = container.iterator();
035: while (iter.hasNext()) {
036: Object object = iter.next();
037:
038: try {
039: if (object instanceof Collectable) {
040: ((Collectable) object).fixUpReference(fixUpTime);
041: }
042: } catch (org.myoodb.exception.ObjectNotFoundException e) {
043: iter.remove();
044: }
045: }
046: }
047:
048: public static void fixUpReference(java.util.Map container,
049: long fixUpTime) {
050: java.util.Iterator iter = container.values().iterator();
051: while (iter.hasNext()) {
052: Object object = iter.next();
053:
054: try {
055: if (object instanceof Collectable) {
056: ((Collectable) object).fixUpReference(fixUpTime);
057: }
058: } catch (org.myoodb.exception.ObjectNotFoundException e) {
059: iter.remove();
060: }
061: }
062: }
063:
064: public static java.util.Collection getFixUpReference(
065: java.util.Collection container, long fixUpTime) {
066: if (container instanceof java.util.ArrayList) {
067: container = (java.util.Collection) ((java.util.ArrayList) container)
068: .clone();
069: } else if (container instanceof java.util.LinkedList) {
070: container = (java.util.Collection) ((java.util.LinkedList) container)
071: .clone();
072: } else if (container instanceof java.util.HashSet) {
073: container = (java.util.Collection) ((java.util.HashSet) container)
074: .clone();
075: } else if (container instanceof java.util.TreeSet) {
076: container = (java.util.Collection) ((java.util.TreeSet) container)
077: .clone();
078: } else {
079: throw new org.myoodb.exception.PermissionException(
080: "Invalid type for fixUpReference: "
081: + container.getClass());
082: }
083:
084: fixUpReference(container, fixUpTime);
085:
086: return container;
087: }
088:
089: public static java.util.Map getFixUpReference(
090: java.util.Map container, long fixUpTime) {
091: if (container instanceof java.util.HashMap) {
092: container = (java.util.Map) ((java.util.HashMap) container)
093: .clone();
094: } else if (container instanceof java.util.TreeMap) {
095: container = (java.util.Map) ((java.util.TreeMap) container)
096: .clone();
097: } else {
098: throw new org.myoodb.exception.PermissionException(
099: "Invalid type for fixUpReference: "
100: + container.getClass());
101: }
102:
103: fixUpReference(container, fixUpTime);
104:
105: return container;
106: }
107:
108: public CollectableDbImpl() {
109: m_lastFixUpTime = 0;
110: }
111:
112: private AbstractLock getLock() {
113: return getContainer().getLock();
114: }
115:
116: private AbstractTransaction getAssociatedTransaction() {
117: long lockId = getLockIdentifier();
118:
119: return (lockId != -1) ? MyOodbManager.getTheManager()
120: .getTransactionManager().getTransaction(lockId) : null;
121: }
122:
123: protected String invokedBy() {
124: String owner = "";
125: java.lang.Thread thread = java.lang.Thread.currentThread();
126:
127: if (thread instanceof TransactionThread) {
128: owner = ((TransactionThread) thread).getOwner().getName();
129: }
130:
131: return owner;
132: }
133:
134: protected boolean referenceHasBeenFixedUp(long fixUpTime) {
135: return fixUpTime <= m_lastFixUpTime;
136: }
137:
138: public long getLocalTime() {
139: return org.myoodb.core.IdentifierManager.getLocalTime();
140: }
141:
142: public void lock() {
143: // do nothing for now. locking is an implicit behavior for explicit transactions
144: }
145:
146: public boolean isLocked() {
147: return (getAssociatedTransaction() != null)
148: && (getLock().getLockLevel() >= org.myoodb.MyOodbAccess.WRITE);
149: }
150:
151: public boolean isImplicitLock() {
152: AbstractTransaction tx = getAssociatedTransaction();
153: return ((tx != null) && (tx.getTransactionType() == AbstractTransaction.IMPLICIT_TRANSACTION));
154: }
155:
156: public boolean isExplicitLock() {
157: AbstractTransaction tx = getAssociatedTransaction();
158: return ((tx != null) && (tx.getTransactionType() == AbstractTransaction.EXPLICIT_TRANSACTION));
159: }
160:
161: public String isLockedBy() {
162: AbstractTransaction tx = getAssociatedTransaction();
163: return (tx != null) ? tx.getOwner().getName() : null;
164: }
165:
166: public long getLockIdentifier() {
167: return getLock().getLockIdentifier();
168: }
169:
170: public void testReference() {
171: // nothing to do
172: }
173:
174: public void cleanUpReference() {
175: // nothing to do
176: }
177:
178: public void fixUpReference(long fixUpTime) {
179: m_lastFixUpTime = fixUpTime;
180: }
181:
182: public String toString() {
183: return super.toString();
184: }
185: }
|