[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/ServiceManager - IServiceManager.py:1.1.2.1 ServiceManager.py:1.1.2.1 __init__.py:1.1.2.1 service-manager.zcml:1.1.2.1

Jim Fulton jim@zope.com
Mon, 4 Mar 2002 11:48:20 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/ServiceManager
In directory cvs.zope.org:/tmp/cvs-serv12773/ServiceManager

Added Files:
      Tag: Zope-3x-branch
	IServiceManager.py ServiceManager.py __init__.py 
	service-manager.zcml 
Log Message:
Moved ServiceManager code to OFS, which is just a logical place for
standard/default TTW object implementations.

Refactored SecurityManager to use container framework.



=== Added File Zope3/lib/python/Zope/App/OFS/ServiceManager/IServiceManager.py ===
##############################################################################
#
# Copyright (c) 2001 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
# 
##############################################################################
"""

$Id: IServiceManager.py,v 1.1.2.1 2002/03/04 16:48:19 jim Exp $
"""
from Interface import Interface
from Zope.ComponentArchitecture.IServiceService import IServiceService
from Zope.App.OFS.IContainer import IContainer

class IServiceManager(IServiceService, IContainer):
    """
    Service Managers act as containers for Services.
    
    If a Service Manager is asked for a service it
    checks for those it contains, before using a context
    based lookup to find another service manager to delegate
    to. if no other service manager is found they defer
    to the ComponentArchitecture ServiceManager which
    contains file based services.
    """

    def bindService(serviceName, serviceComponentName):
        """provide a service implementation"""


    def getBoundService(name):
        """retrieve a bound service implimentation

        Get the component currently bound to the named Service
        in this ServiceService.  Does not search context.
        """
        
    def getServiceDefinitions():
        """returns the dictionary of service definitions"""    


=== Added File Zope3/lib/python/Zope/App/OFS/ServiceManager/ServiceManager.py ===
##############################################################################
#
# Copyright (c) 2001 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
# 
##############################################################################
"""

$Id: ServiceManager.py,v 1.1.2.1 2002/03/04 16:48:19 jim Exp $
"""

from IServiceManager import IServiceManager
from Zope.ComponentArchitecture.IServiceManagerContainer \
     import IServiceManagerContainer
from Zope.ComponentArchitecture import getService, getServiceDefinitions
from Zope.ComponentArchitecture.Service import UndefinedService
from Zope.ComponentArchitecture.Service import InvalidService
from Zope.Exceptions import DuplicationError, NotFoundError
from Zope.App.OFS.Folder.Folder import Folder
from Zope.ContextWrapper import getinnercontext
from Zope.App.OFS.Container.SampleContainer import Container
from Persistence import Persistent

class ServiceManager(Container, Persistent):

    __implements__ = IServiceManager

    def __init__(self):
        self.__defs = {}
        self.__bindings = {}
        Container.__init__(self)
        
    def defineService(self, name, interface):
        """ see ServiceManager Interface """

        if name in self.__defs:
            raise DuplicationError(name)

        # trigger p machinery
        self.__defs == self.__defs

        self.__defs[name] = interface

    def getServiceDefinitions(self):
        serviceDefs = self__defs.items()
        # Get the services defined above us
        parentSM = self._findParentServiceManager()
        if parentSM is not None:
            serviceDefs = serviceDefs + parentSM.getServiceDefinitions()
        else:
            serviceDefs = serviceDefs + getServiceDefinitions()
            
        return serviceDefs

    def _findParentServiceManager(self):
        parent = getinnercontext(self)

        if IServiceManagerContainer.isImplementedBy(parent):
            sm = parent.getServiceManager()
            return sm
    
    def getService(self, object, name):
        """ see IServiceManager Interface"""
        
        service = self.__bindings.get(name)
                
        if service:
            return self.getObject(service)

        object = self._findParentServiceManager()
        while object is not None:
            #            object = getinnercontext(object)
            #            
            #            if IServiceManagerContainer.isImplementedBy(object):
            #                sm = object.getServiceManager()
            #                if sm:
            #                    return sm.getService(object, name)
            return sm.getService(object, name)

        # fallback to the ComponentArchitecture Service Manager
        # XXX when this called the original object might no longer be
        # the context
        return getService(object, name)

    def getBoundService(self, name):
        """ see IServiceManager Interface"""

        return self.__bindings.get(name)

    def bindService(self, serviceName, serviceComponentName):
        """ see IServiceManager Interface"""        

        # This could raise a KeyError if we don't have this component
        serviceComponent = self.getObject(serviceComponentName)

        if serviceName not in self.__defs.keys():
            raise UndefinedService(serviceName)

        if not self.__defs[serviceName].isImplementedBy(serviceComponent):
            raise InvalidService(serviceName, serviceComponentName,
                                 self.__defs[serviceName])

        # Services are added to the Manager through the Folder interface
        # self.setObject(name, component)
        self.__bindings[serviceName] = serviceComponentName

    def getServiceDefinitions(self):
        """ see IServiceManager Interface"""

        return self.__defs.items()



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



=== Added File Zope3/lib/python/Zope/App/OFS/ServiceManager/service-manager.zcml ===
<zopeConfigure
   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'
>

<security:protectClass name="Zope.App.OFS.ServiceManager."
   permission_id="Zope.ManageServices"
   interface="Zope.App.OFS.ServiceManager.IServiceManager." 
   />

<include package="Zope.App.OFS.ServiceManager.Views" file="views.zcml" />

</zopeConfigure>