2
# This file is part of GNU Enterprise.
4
# GNU Enterprise is free software; you can redistribute it
5
# and/or modify it under the terms of the GNU General Public
6
# License as published by the Free Software Foundation; either
7
# version 2, or (at your option) any later version.
9
# GNU Enterprise is distributed in the hope that it will be
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public
15
# License along with program; see the file COPYING. If not,
16
# write to the Free Software Foundation, Inc., 59 Temple Place
17
# - Suite 330, Boston, MA 02111-1307, USA.
19
# Copyright 2001-2005 Free Software Foundation
25
# Helper module that keeps track of object references for use
26
# with non-Object interfaces
31
import sha, time, thread
33
class InvalidObjectReference(StandardError):
40
def getObjectReference(object):
42
return object._ObjectLibrarian__refid
43
except AttributeError:
45
# This is to introduce some obscurity to the id handles...
47
_index += time.clock()
48
object._ObjectLibrarian__refid = \
49
sha.new("%s%s%s"%(id(object), object, _index)).hexdigest()
53
object._ObjectLibrarian__creationtime = now
54
object._ObjectLibrarian__lastusage = now
56
return object._ObjectLibrarian__refid
59
def archiveObject(object):
60
_objects[getObjectReference(object)] = object
63
def retrieveObject(handle):
65
object = _objects[handle]
67
if hasattr(object,"_ObjectLibrarian__myGC"):
68
# call private Garbage Collector (f.e. for sessions)
69
object._ObjectLibrarian__myGC.beingRetrieved(object)
71
# just store last used time
72
object._ObjectLibrarian__lastusage = time.time()
76
raise InvalidObjectReference
79
def deferenceObject(object):
81
del _objects[object._ObjectLibrarian__refid]
84
except AttributeError:
87
def getObjectLibrarianStatus():
89
status["objcount"]=len(_objects)
92
class GarbageCollector:
94
self.defaultTimeout=3600 # in seconds
95
self.loopLength=20 # in seconds
98
def setDefaultTimeout(self,timespan):
99
self.defaultTimeout=timespan
101
def getDefaultTimeout(self):
102
return self.defaultTimeout
104
def setLoopLength(self,length):
105
self.loopLength=length
107
def getLoopLength(self):
108
return self.loopLength
111
# is this threadsafe ?
117
object=_objects[handle]
118
if not hasattr(object,"_ObjectLibrarian__myGC"):
119
if object._ObjectLibrarian__lastusage + self.defaultTimeout < now:
120
deferenceObject(object)
121
GDebug.printMesg(1,"Timeout reached for object %s" & object)
133
time.sleep(self.loopLength)
137
def start_in_new_thread(self):
141
thread.start_new_thread(self.loop,())
144
_global_GarbageCollector = GarbageCollector()