[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Service - ServiceManagerBindingsView.py:1.1.2.1 IServiceManager.py:1.1.2.2 ServiceManager.py:1.1.2.2

Michael McLay mclay@nist.gov
Sat, 9 Feb 2002 16:13:44 -0500


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

Modified Files:
      Tag: Zope-3x-branch
	IServiceManager.py ServiceManager.py 
Added Files:
      Tag: Zope-3x-branch
	ServiceManagerBindingsView.py 
Log Message:
Added ServiceManagerBindingView and Service/pt/services_binding.pt.
Moved funtionality from IServiceService to IServiceManager and added
getServiceDefinitions() to Service.py. Changed the name of provideService
in ServiceManager to bindService and modified the functionality to allow
multiple service implementations to be associated with a service definition.



=== Added File Zope3/lib/python/Zope/App/Service/ServiceManagerBindingsView.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
# 
##############################################################################
"""

Revision information: $Id: ServiceManagerBindingsView.py,v 1.1.2.1 2002/02/09 21:13:13 mclay Exp $"""

from Zope.PageTemplate.PageTemplateFile import PageTemplateFile
from Zope.Publisher.Browser.AttributePublisher import AttributePublisher


class ServiceManagerBindingsView(AttributePublisher):

    index = PageTemplateFile('pt/services_bindings.pt')

    def getServicesTable(self):
	"""
	"""
	allServices = self.getServiceDefinitions()
	localServices = self.objectItems()
	services = []
	for serviceName, service in allServices:
	    serviceMap={}
	    availableServices = []

	    acquiredOrNone = 'None'
	    bound = self.getBoundService(serviceName)
	    if bound is None:
		# Because ServiceManager impliments the same interface as
		# the global service manager, it needs to take an "object"
		# which is the location to start the search for ServiceManagers
		# if it's own lookup fails.  By default, this is "self"
		acquired = self.getService(self, serviceName)
		if acquired:
		    acquiredOrNone = 'Acquired'
		bound = acquiredOrNone
		
		
	    availableServices.append(acquiredOrNone)

	    
	    
	    for localServiceName, localService in localServices:
		if service.isImplementedBy(localService):
		    availableServices.append(localService)

	    serviceMap['name'] = serviceName
	    serviceMap['services'] = availableServices
	    serviceMap['bound'] = bound
	    services.append(serviceMap)
	return services
    
    def action(self):
	pass

    


=== Zope3/lib/python/Zope/App/Service/IServiceManager.py 1.1.2.1 => 1.1.2.2 ===
     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"""    


=== Zope3/lib/python/Zope/App/Service/ServiceManager.py 1.1.2.1 => 1.1.2.2 ===
      import IServiceManagerContainer
 
-from Zope.ComponentArchitecture import getService
+from Zope.ComponentArchitecture import getService, getServiceDefinitions
 
 
 
@@ -42,6 +42,7 @@
 
     def __init__(self):
         self.__defs = {}
+	self.__bindings = {}
         Folder.__init__(self)
         
     def defineService(self, name, interface):
@@ -54,41 +55,71 @@
         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.getObject(name, None)
-        
+        service = self.__bindings.get(name)
+                
         if service:
-            return 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)            
+	    #            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 provideService(self, name, component):
+    def getBoundService(self, name):
+	""" see IServiceManager Interface"""
+
+	return self.__bindings.get(name)
+
+    def bindService(self, serviceName, serviceComponentName):
         """ see IServiceManager Interface"""        
 
-        if name in self.objectIds():
-            raise DuplicationError(name)
+	# This could raise a KeyError if we don't have this component
+	serviceComponent = self.getObject(serviceComponentName)
 
-        if name not in self.__defs.keys():
+	if serviceName not in self.__defs.keys():
             raise UndefinedService(name)
 
-        if not self.__defs[name].isImplementedBy(component):
+        if not self.__defs[serviceName].isImplementedBy(serviceComponent):
             raise InvalidService(name, component, self.__defs[name])
-        
-        self.setObject(name, component)
 
+        # 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