[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl - IServerControl.py:1.2 ServerControl.py:1.2 __init__.py:1.2 metaConfigure.py:1.2 server-control-meta.zcml:1.2 server-control.zcml:1.2

Jim Fulton jim@zope.com
Mon, 10 Jun 2002 19:28:23 -0400


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

Added Files:
	IServerControl.py ServerControl.py __init__.py 
	metaConfigure.py server-control-meta.zcml server-control.zcml 
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.


=== Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/IServerControl.py 1.1 => 1.2 ===
+#
+# 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 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.
+
+       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/ServerControl.py 1.1 => 1.2 ===
+#
+# 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$"""
+
+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 priority in self._shutdown_reg:
+            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.")


=== Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/__init__.py 1.1 => 1.2 ===


=== Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/metaConfigure.py 1.1 => 1.2 ===
+#
+# 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$
+"""
+
+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)
+


=== Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/server-control-meta.zcml 1.1 => 1.2 ===
+
+  <directives namespace="http://namespaces.zope.org/server-control">
+    <directive name="registerShutdownHook"
+               attributes="call priority name"
+               handler=".metaConfigure.registerShutdownHook" />
+  </directives>
+
+</zopeConfigure>


=== Zope3/lib/python/Zope/App/OFS/ApplicationControl/ServerControl/server-control.zcml 1.1 => 1.2 ===
+   xmlns='http://namespaces.zope.org/zope'
+   xmlns:security='http://namespaces.zope.org/security'
+   xmlns:zmi='http://namespaces.zope.org/zmi'
+   xmlns:browser='http://namespaces.zope.org/browser'
+   xmlns:server-control='http://namespaces.zope.org/server-control'
+>
+
+  <utility factory=".ServerControl.ServerControl"
+           permission="Zope.ManageApplication"
+           provides=".IServerControl." />
+
+  <!-- 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>