[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/LifeCycle - ILifeCycle.py:1.1.2.1 LifeCycle.py:1.1.2.1 __init__.py:1.1.2.1 configure.zcml:1.1.2.1

Ulrich Eck ueck@net-labs.de
Wed, 11 Dec 2002 05:07:17 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/LifeCycle
In directory cvs.zope.org:/tmp/cvs-serv10280

Added Files:
      Tag: jack-e_scheduler_branch
	ILifeCycle.py LifeCycle.py __init__.py configure.zcml 
Log Message:
initial draft of LifeCycle Events for Z3


=== Added File Zope3/lib/python/Zope/App/LifeCycle/ILifeCycle.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""

Revision information:
$Id: ILifeCycle.py,v 1.1.2.1 2002/12/11 10:07:17 jack-e Exp $
"""
from Interface.Attribute import Attribute

from Zope.Event.IEvent import IEvent

class ILifeCycleEvent(IEvent):
    """The Base interface for LifeCycle Events"""

class IZopeStartupEvent(ILifeCycleEvent):
    """The ZopeStartupEvent"""

class IZopeShutdownEvent(ILifeCycleEvent):
    """The ZopeShutdownEvent"""





=== Added File Zope3/lib/python/Zope/App/LifeCycle/LifeCycle.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
__doc__ = """ LifeCycle Implementation

$Id: LifeCycle.py,v 1.1.2.1 2002/12/11 10:07:17 jack-e Exp $"""
import zLOG

from Transaction import get_transaction
from Zope.App.ZopePublication.ZopePublication import ZopePublication
from Zope.App.Traversing import traverse
from Zope.ComponentArchitecture import getService

from Zope.App.LifeCycle.ILifeCycle import IZopeStartupEvent, IZopeShutdownEvent

class ZopeStartupEvent:

    __implements__ = IZopeStartupEvent


class ZopeShutdownEvent:

    __implements__ = IZopeShutdownEvent

    

class LifeCyclePublication:

    root_name = ZopePublication.root_name

    def __init__(self, db):
        self.db = db
        self._conn = None


    def beforeCall(self):
        get_transaction().begin()


    def getApplication(self):
        self._conn = conn = self.db.open()
        root = conn.root()
        app = root.get(self.root_name, None)
        
        if app is None:
            raise SystemError, "Zope Application Not Found"        

        return app


    def getEventService(self, app, path):
        place = traverse(app, path)
        event_service = getService(place,"Events")
        return event_service


    def publishLifeCycleEvent(self, event_service, event):
        event_service.publish(event)


    def afterCall(self):
        get_transaction().commit()
        self._conn.close()



def publishStartupEvent(_zodb=None):
    zLOG.LOG("LifeCycle", zLOG.INFO, "ZopeStartupEvent.")
    if _zodb is not None:
        pub = LifeCyclePublication(_zodb)
        app = pub.getApplication()
        pub.beforeCall()
        # Eventservice at rootFolder
        evt_svc = pub.getEventService(app, '/')
        pub.publishLifeCycleEvent(evt_svc, ZopeStartupEvent())
        pub.afterCall()
        
    

# There are two ways to shutdown:
# - TTW -> happens within a Transaction/Context
# - Signal -> has no Transaction/Context
# how to handle TTW shutdown ??

def publishShutdownEvent(_zodb=None):
    zLOG.LOG("LifeCycle",zLOG.INFO, "ZopeShutdownEvent.")
    if _zodb is not None:
        pub = LifeCyclePublication(_zodb)
        app = pub.getApplication()
        pub.beforeCall()
        # Eventservice at rootFolder
        evt_svc = pub.getEventService(app, '/')
        pub.publishLifeCycleEvent(evt_svc, ZopeShutdownEvent())
        pub.afterCall()
    else:
        # handle TTW-shutdown (need context ...)
        pass



=== Added File Zope3/lib/python/Zope/App/LifeCycle/__init__.py ===


=== Added File Zope3/lib/python/Zope/App/LifeCycle/configure.zcml ===
<zopeConfigure
   xmlns='http://namespaces.zope.org/zope'
   xmlns:server-control='http://namespaces.zope.org/server-control'
>

  <server-control:registerStartupHook
       name="Publish ZopeStartupEvent"
       priority="1"
       call=".LifeCycle.publishStartupEvent"
       />

  <server-control:registerShutdownHook
       name="Publish ZopeShutdownEvent"
       priority="1"
       call=".LifeCycle.publishShutdownEvent"
       />

</zopeConfigure>