[Zope-Checkins] CVS: Zope3/lib/python/Zope/StartUp - RequestFactory.py:1.2 RequestFactoryRegistry.py:1.2 ServerType.py:1.2 ServerTypeRegistry.py:1.2 SiteDefinition.py:1.2 __init__.py:1.2 initZODB.py:1.2 metaConfigure.py:1.2 startup-meta.zcml:1.2 startup-registry.zcml:1.2 startup.py:1.2

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


Update of /cvs-repository/Zope3/lib/python/Zope/StartUp
In directory cvs.zope.org:/tmp/cvs-serv20468/lib/python/Zope/StartUp

Added Files:
	RequestFactory.py RequestFactoryRegistry.py ServerType.py 
	ServerTypeRegistry.py SiteDefinition.py __init__.py 
	initZODB.py metaConfigure.py startup-meta.zcml 
	startup-registry.zcml startup.py 
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.

=== Zope3/lib/python/Zope/StartUp/RequestFactory.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.
+# 
+##############################################################################
+"""ctory.py,v 1.1.2.2 2002/04/02 02:20:40 srichter Exp $
+"""
+
+from Interface import Interface
+import copy
+
+
+class IRequestFactory(Interface):
+    """This is a pure read-only interface, since the values are set through
+       a ZCML directive and we shouldn't be able to change them.
+    """
+
+    def realize(db):
+        """Realize the factory by initalizing the publication.
+
+           The method returns the realized object.
+        """
+        
+
+    def __call__(input_stream, output_steam, env):
+        """Call the Request Factory"""
+
+
+
+
+
+class RequestFactory:
+    """This class will generically create RequestFactories. This way I do
+       not have to create a method for each Server Type there is.
+    """
+
+    __implements__ =  IRequestFactory
+
+    def __init__(self, publication, request):
+        """Initialize Request Factory"""
+        self._pubFactory = publication
+        self._publication = None
+        self._request = request
+
+
+    ############################################################
+    # Implementation methods for interface
+    # Zope.StartUp.RequestFactory.IRequestFactory
+
+    def realize(self, db):
+        'See Zope.StartUp.RequestFactory.IRequestFactory'
+        realized = copy.copy(self)
+        realized._publication = realized._pubFactory(db)
+        return realized
+    
+
+    def __call__(self, input_stream, output_steam, env):
+        'See Zope.StartUp.RequestFactory.IRequestFactory'
+        request = self._request(input_stream, output_steam, env)
+        request.setPublication(self._publication)
+        return request
+
+    #
+    ############################################################


=== Zope3/lib/python/Zope/StartUp/RequestFactoryRegistry.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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+
+from Zope.App.Formulator.SimpleRegistry import SimpleRegistry
+from Zope.App.Formulator.ISimpleRegistry import ISimpleRegistry
+from RequestFactory import IRequestFactory 
+
+class IRequestFactoryRegistry(ISimpleRegistry):
+    """
+    The RequestFactory Registry manages a list of all the fields
+    available in Zope. A registry is useful at this point, since
+    fields can be initialized and registered by many places.
+
+    Note that it does not matter whether we have classes or instances as
+    fields. If the fields are instances, they must implement
+    IInstanceFactory.
+    """
+    pass
+
+
+class RequestFactoryRegistry(SimpleRegistry):
+    """ """
+
+    __implements__ =  (IRequestFactoryRegistry,)
+
+
+
+RequestFactoryRegistry = RequestFactoryRegistry(IRequestFactory)
+registerRequestFactory = RequestFactoryRegistry.register
+getRequestFactory = RequestFactoryRegistry.get


=== Zope3/lib/python/Zope/StartUp/ServerType.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.
+# 
+##############################################################################
+"""e.py,v 1.1.2.2 2002/04/02 02:20:40 srichter Exp $
+"""
+
+from Interface import Interface
+from RequestFactoryRegistry import getRequestFactory
+
+
+class IServerType(Interface):
+    """This is a pure read-only interface, since the values are set through
+       a ZCML directive and we shouldn't be able to change them.
+    """
+
+    def create(task_dispatcher, db, port=None, verbose=None):
+        """Create the server knowing the port, task dispatcher and the ZODB.
+        """
+
+
+class ServerType:
+
+    __implements__ =  IServerType
+
+
+    def __init__(self, name, factory, requestFactory, logFactory,
+                 defaultPort, defaultVerbose):
+        """ """
+        self._name = name
+        self._factory = factory
+        self._requestFactory = requestFactory
+        self._logFactory = logFactory
+        self._defaultPort = defaultPort
+        self._defaultVerbose = defaultVerbose
+
+
+    ############################################################
+    # Implementation methods for interface
+    # Zope.StartUp.ServerType.IServerType
+
+    def create(self, task_dispatcher, db, port=None, verbose=None):
+        'See Zope.StartUp.ServerType.IServerType'
+
+        request_factory = getRequestFactory(self._requestFactory)
+        request_factory = request_factory.realize(db)
+
+        if port is None:
+            port = self._defaultPort
+
+        if verbose is None:
+            verbose = self._defaultVerbose
+
+        apply(self._factory,
+              (request_factory, self._name, '', port),
+              {'task_dispatcher': task_dispatcher,
+               'verbose': verbose,
+               'hit_log': self._logFactory()})
+
+    #
+    ############################################################
+
+        


=== Zope3/lib/python/Zope/StartUp/ServerTypeRegistry.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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+
+from Zope.App.Formulator.SimpleRegistry import SimpleRegistry
+from Zope.App.Formulator.ISimpleRegistry import ISimpleRegistry
+from ServerType import IServerType 
+
+class IServerTypeRegistry(ISimpleRegistry):
+    """
+    The ServerType Registry manages a list of all the fields
+    available in Zope. A registry is useful at this point, since
+    fields can be initialized and registered by many places.
+
+    Note that it does not matter whether we have classes or instances as
+    fields. If the fields are instances, they must implement
+    IInstanceFactory.
+    """
+    pass
+
+
+class ServerTypeRegistry(SimpleRegistry):
+    """ """
+
+    __implements__ =  (IServerTypeRegistry,)
+
+
+
+ServerTypeRegistry = ServerTypeRegistry(IServerType)
+registerServerType = ServerTypeRegistry.register
+getServerType = ServerTypeRegistry.get


=== Zope3/lib/python/Zope/StartUp/SiteDefinition.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.
+# 
+##############################################################################
+"""
+This module handles the :startup directives. 
+
+$Id$
+"""
+
+import sys
+
+# Import Configuration-related classes
+from Zope.Configuration.Action import Action
+from Zope.Configuration.ConfigurationDirectiveInterfaces \
+     import INonEmptyDirective
+
+from ServerTypeRegistry import getServerType
+
+# Import Undo-related classes 
+from Zope.ComponentArchitecture import getService
+from Zope.App.Undo.ZODBUndoManager import ZODBUndoManager
+from Zope.App.Undo.IUndoManager import IUndoManager
+
+
+from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
+import asyncore, zLOG
+from Zope.Server import ZLogIntegration
+from Zope.Server.TaskThreads import ThreadedTaskDispatcher
+from Zope.App.ZopePublication.ZopePublication import ZopePublication
+
+import asyncore, zLOG
+
+from ZODB import DB
+
+
+DEFAULT_STORAGE_FILE = 'Data.fs'
+DEFAULT_LOG_FILE = 'STDERR'
+DEFAULT_LOG_CLASS = 'Zope.Server.HTTPServer.CommonHitLogger'
+
+
+class SiteDefinition:
+
+    __class_implements__ = INonEmptyDirective    
+
+    # Some special file names for log files
+    _special_log_files = {'STDERR': sys.stderr,
+                          'STDOUT': sys.stdout}
+
+    
+    def __init__(self, _context, name="default", threads=4):
+        """Initilize is called, when the defineSite directive is invoked.
+        """
+        self._name = name
+        self._threads = int(threads)
+
+        self._zodb = None
+        self.useLog(_context)
+        self._servers = {}
+
+        self._started = 0
+
+
+    def useFileStorage(self, _context, file=DEFAULT_STORAGE_FILE):
+        """Lets you specify the ZODB to use."""
+        from ZODB.FileStorage import FileStorage
+        self._zodb = DB(FileStorage(file))
+        return []
+
+
+    def useMappingStorage(self, _context):
+        """Lets you specify the ZODB to use."""
+        from ZODB.MappingStorage import MappingStorage
+        self._zodb = DB(MappingStorage(file))
+        return []
+
+
+    def useLog(self, _context, file=DEFAULT_LOG_FILE):
+        """Lets you specify the log file to use"""
+
+        if file in self._special_log_files.keys():
+            file = self._special_log_files[file]
+        else:
+            file = open(file, 'w')
+
+        zLOG._set_log_dest(file)
+        return []
+
+
+    def addServer(self, _context, type, port=None, verbose=None):
+        """Add a new server for this site."""
+
+        if port is not None:
+            port = int(port)
+
+        if verbose is not None:
+            if verbose.lower() == 'true': verbose = 1
+            else: verbose = 0
+
+        if type is not None:
+            self._servers[type] = {'port': port,
+                                   'verbose': verbose}
+        else:
+            sys.stderr.out('Warning: Server of Type %s does not exist. ' +
+                           'Directive neglected.') 
+        return []
+
+
+    def start(self):
+        """Now start all the servers"""
+
+        sys.stderr.write('\nStarting Site: %s\n\n' %self._name)
+
+        sys.setcheckinterval(120)
+
+        # setup undo fnctionality
+        getService(None,"Utilities").provideUtility(
+            IUndoManager,
+            ZODBUndoManager(self._zodb)
+            )
+
+        # Setup the task dispatcher
+        td = ThreadedTaskDispatcher()
+        td.setThreadCount(self._threads)
+        
+        # setup the storage, if not already done
+        if self._zodb is None:
+            self.useStorage(_context)
+        
+        # check whether a root was already specified for this ZODB; if
+        # not create one.
+        self._initDB()
+
+        # Start the servers
+        for type, server_info in self._servers.items():
+
+            server = getServerType(type)
+            server.create(td, self._zodb, server_info['port'],
+                          server_info['verbose'])
+
+    def _initDB(self):
+        """Initialize the ZODB"""
+
+        connection = self._zodb.open()
+        root = connection.root()
+        app = root.get(ZopePublication.root_name, None)
+
+        if app is None:
+
+            from Zope.App.OFS.Content.Folder.RootFolder import RootFolder
+            from Transaction import get_transaction
+        
+            app = RootFolder()
+            root[ZopePublication.root_name] = app
+
+            get_transaction().commit()
+
+        connection.close()
+
+
+    def __call__(self):
+        "Handle empty/simple declaration."
+        return [ Action(discriminator = 'Start Servers',
+                        callable = self.start,
+                        args = (),
+                        ) ]    


=== Zope3/lib/python/Zope/StartUp/__init__.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.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+


=== Zope3/lib/python/Zope/StartUp/initZODB.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.
+# 
+##############################################################################


=== Zope3/lib/python/Zope/StartUp/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.
+# 
+##############################################################################
+"""
+This module handles the :startup directives. 
+
+$Id$
+"""
+
+from SiteDefinition import SiteDefinition
+from Zope.Configuration.Action import Action
+from RequestFactory import RequestFactory
+import RequestFactoryRegistry 
+from ServerType import ServerType
+import ServerTypeRegistry 
+
+defineSite = SiteDefinition
+
+
+def registerRequestFactory(_context, name, publication, request):
+    """ """
+    publication = _context.resolve(publication)
+    request = _context.resolve(request)
+    request_factory = RequestFactory(publication, request)
+
+    return [
+        Action(
+            discriminator = name,
+            callable = RequestFactoryRegistry.registerRequestFactory,
+            args = (name, request_factory,),
+            )
+        ]
+
+
+def registerServerType(_context, name, factory, requestFactory, logFactory,
+                       defaultPort, defaultVerbose):
+    """ """
+    factory = _context.resolve(factory)
+    logFactory = _context.resolve(logFactory)
+
+    if defaultVerbose.lower() == 'true':
+        defaultVerbose = 1
+    else:
+        defaultVerbose = 0
+
+    defaultPort = int(defaultPort)
+
+    server_type = ServerType(name, factory, requestFactory, logFactory,
+                             defaultPort, defaultVerbose)
+    
+
+    return [
+        Action(
+            discriminator = name,
+            callable = ServerTypeRegistry.registerServerType,
+            args = (name, server_type),
+            )
+        ]
+


=== Zope3/lib/python/Zope/StartUp/startup-meta.zcml 1.1 => 1.2 ===
+
+  <directives namespace="http://namespaces.zope.org/startup">
+
+    <directive name="defineSite"
+               attributes="name threads"
+               handler="Zope.StartUp.metaConfigure.defineSite">
+
+      <subdirective name="useFileStorage"
+                    attributes="file" />
+
+      <subdirective name="useMappingStorage" />
+
+      <subdirective name="useLog" attributes="file" />
+
+      <subdirective name="addServer"
+                    attributes="type port verbose logClass" />
+
+    </directive>
+
+    <directive name="registerRequestFactory"
+               attributes="name publication request"
+               handler="Zope.StartUp.metaConfigure.registerRequestFactory" />
+
+    <directive name="registerServerType"
+               attributes="name publication request"
+               handler="Zope.StartUp.metaConfigure.registerServerType" />
+
+  </directives>
+
+</zopeConfigure>


=== Zope3/lib/python/Zope/StartUp/startup-registry.zcml 1.1 => 1.2 ===
+   xmlns="http://namespaces.zope.org/zope"
+   xmlns:startup="http://namespaces.zope.org/startup">
+
+
+  <startup:registerRequestFactory name="BrowserRequestFactory"
+    publication = 
+    "Zope.App.ZopePublication.Browser.Publication.BrowserPublication"
+    request = "Zope.Publisher.Browser.BrowserRequest." 
+  />
+
+
+  <startup:registerRequestFactory name="XMLRPCRequestFactory" 
+    publication = 
+    "Zope.App.ZopePublication.XMLRPC.Publication.XMLRPCPublication"
+    request = "Zope.Publisher.XMLRPC.XMLRPCRequest." 
+  />
+
+
+  <startup:registerRequestFactory name="VFSRequestFactory"
+    publication = 
+    "Zope.App.ZopePublication.VFS.Publication.VFSPublication"
+    request = "Zope.Publisher.VFS.VFSRequest." 
+  />
+
+
+  <startup:registerServerType 
+    name = "Browser"
+    factory = "Zope.Server.HTTP.PublisherHTTPServer."
+    requestFactory="BrowserRequestFactory"
+    logFactory = "Zope.Server.HTTP.CommonHitLogger."
+    defaultPort="8080"
+    defaultVerbose="true" />
+
+
+  <startup:registerServerType 
+    name = "XML-RPC"
+    factory = "Zope.Server.HTTP.PublisherHTTPServer."
+    requestFactory="XMLRPCRequestFactory"
+    logFactory = "Zope.Server.HTTP.CommonHitLogger."
+    defaultPort="8081"
+    defaultVerbose="true" />
+
+
+  <startup:registerServerType 
+    name = "FTP"
+    factory = "Zope.Server.FTP.PublisherFTPServer."
+    requestFactory="VFSRequestFactory"
+    logFactory = "Zope.Server.FTP.CommonFTPActivityLogger."
+    defaultPort="8021"
+    defaultVerbose="true" />
+
+
+</zopeConfigure>


=== Zope3/lib/python/Zope/StartUp/startup.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.
+# 
+##############################################################################