[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl - ServerControl.py:1.1.2.1 metaConfigure.py:1.1.2.1 server-control-meta.zcml:1.1.2.1 IServerControl.py:1.1.2.2 server-control.zcml:1.1.2.2 StubServerControl.py:NONE

Christian Theune ct@gocept.com
Thu, 18 Apr 2002 16:03:43 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl
In directory cvs.zope.org:/tmp/cvs-serv31265/lib/python/Zope/App/OFS/ApplicationControl/ServerControl

Modified Files:
      Tag: Zope-3x-branch
	IServerControl.py server-control.zcml 
Added Files:
      Tag: Zope-3x-branch
	ServerControl.py metaConfigure.py server-control-meta.zcml 
Removed Files:
      Tag: Zope-3x-branch
	StubServerControl.py 
Log Message:
- Removed the StubServerControl

- Added the ServerControl which implements a full server control (without the actual
    shutdown/restarting capabilities, as those are factored out to hookable methods,
    and we need to discuss how the shutdown will happen exactly - some asyncore rewriting
    is planned AFAIK)

- updated unit tests



=== Added File Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/ServerControl.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__ = """ Server Control Implementation

$Id: ServerControl.py,v 1.1.2.1 2002/04/18 20:03:12 ctheune Exp $"""

from Zope.App.OFS.ApplicationControl.ServerControl.IServerControl import IServerControl, ServerControlError, DoublePriorityError, NotCallableError
import zLOG

import sys


class ServerControl:

    __implements__ = IServerControl

    def __init__(self):
        self._shutdown_reg = {}         # This is the actual shutdown registry.
                                        # It will hold the hooks accessible by their 
                                        # priority. The priority actually needs to be
                                        # a floating point value, to allow most fine
                                        # grained control on the priority. 

    ############################################################
    # Implementation methods for interface
    # Zope.App.OFS.ApplicationControl.ServerControl.IServerControl.

    def shutdown(self):
        'See Zope.App.OFS.ApplicationControl.ServerControl.IServerControl.IServerControl'
        text = ""
        order = self._shutdown_reg.keys()
        order.sort()

        for hook_ in order:
                hook = self._shutdown_reg[hook_]
                hook[0]()

    def restart(self):
        'See Zope.App.OFS.ApplicationControl.ServerControl.IServerControl.IServerControl'

    def registerShutdownHook(self, call, priority, name):
        'See Zope.App.OFS.ApplicationControl.ServerControl.IServerControl.IServerControl'

        priority = float(priority)
        if self._shutdown_reg.has_key(priority):
            raise DoublePriorityError, (call, priority, name)

        if not callable(call):
            raise NotCallableError, (call, priority, name)

        self._shutdown_reg.update({priority: (call, name)})


    #
    ############################################################



## simple log notification for shutdown
def shutdownLogger():
    """simple shutdown logger"""
    zLOG.LOG("ServerControl", zLOG.INFO, "Server is going to be shut down.")

ServerController = ServerController = ServerControl()


=== Added File Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/metaConfigure.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.
# 
##############################################################################
""" Register ServerControl configuration directives.

$Id: metaConfigure.py,v 1.1.2.1 2002/04/18 20:03:12 ctheune Exp $
"""

from Zope.ComponentArchitecture import getUtility
from IServerControl import IServerControl
from Zope.Configuration.Action import Action


def registerShutdownHook(_context, call, name, priority):
    """Register a shutdown hook with the current server control utility"""
    return [
        Action(
            discriminator = ('server-control:registerShutdownHook', name),
            callable = doRegisterShutdownHook,
            args = (_context, call, priority, name),
            )
        ]

def doRegisterShutdownHook(_context, call, priority, name):
    server_control = getUtility(_context, IServerControl)
    server_control.registerShutdownHook(_context.resolve(call), priority, name)



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

  <!-- Zope.App.OFS.ApplicationControl -->
  <directives namespace="http://namespaces.zope.org/server-control">
    <directive name="registerShutdownHook"
               attributes="call, priority, name"
               handler="Zope.App.OFS.ApplicationControl.ServerControl.metaConfigure.registerShutdownHook" />
  </directives>

</zopeConfigure>


=== Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/IServerControl.py 1.1.2.1 => 1.1.2.2 ===
 # 
 ##############################################################################
-__doc__ = """
-Revision information:
-$Id$
+__doc__ = """ Server Control Interface
 
-Server Control Interface
-"""
+$Id$"""
 
 from Interface import Interface
 
+class ServerControlError(Exception):
+    """Represents an error in the ServerControl.
+    """
+
+class DoublePriorityError(ServerControlError):
+    """Raisen when somebody tries to register a second Hook
+       for a priority."""
+
+class NotCallableError(ServerControlError):
+    """Raisen if a given object is not callable."""
+
 class IServerControl(Interface):
     """Server Control Interface defines methods for shutting down and
-       restarting the server."""
+       restarting the server.
+
+       This utility also keeps a registry of things to call when shutting down
+       zope. You can register using this interface or the zcml on the global
+       ServerController instance."""
 
     def shutdown():
         """Shutdown the server gracefully
+
+		Returns: Nothing
         """
 
     def restart():
         """Restart the server gracefully
+
+		Returns: Nothing
         """
-    
+
+    def registerShutdownHook(call, priority, name):
+	"""Register a function that will be callen on server shutdown.
+	   The function needs to takes no argument at all."""


=== Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/server-control.zcml 1.1.2.1 => 1.1.2.2 ===
    xmlns:zmi='http://namespaces.zope.org/zmi'
    xmlns:browser='http://namespaces.zope.org/browser'
+   xmlns:server-control='http://namespaces.zope.org/server-control'
 >
 
-<security:protectClass name=".StubServerControl."
+<security:protectClass name=".ServerControl."
                        permission_id="Zope.ManageApplication"
                        interface=".IServerControl." />
 
-<!-- XXX we need to register a true server controller in a very near future -->
-<utility component=".StubServerControl.StubServerController"
-         provides=".IServerControl." />
+<utility component=".ServerControl.ServerController" provides=".IServerControl." />
 
-<include package=".Views" file="views.zcml" />
+<!-- Hint: Here you see how to register something on Zope shutdown -->
+<server-control:registerShutdownHook name="Shutdown logger" priority="0" call=".ServerControl.shutdownLogger" />
 
+<include package=".Views" file="views.zcml" />
 </zopeConfigure>

=== Removed File Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/StubServerControl.py ===