Home | History | Annotate | Download | only in server2
      1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 from api_data_source import APIDataSource
      6 from api_list_data_source import APIListDataSource
      7 from compiled_file_system import CompiledFileSystem
      8 from content_providers import ContentProviders
      9 from document_renderer import DocumentRenderer
     10 from empty_dir_file_system import EmptyDirFileSystem
     11 from environment import IsDevServer
     12 from gcs_file_system_provider import CloudStorageFileSystemProvider
     13 from github_file_system_provider import GithubFileSystemProvider
     14 from host_file_system_iterator import HostFileSystemIterator
     15 from host_file_system_provider import HostFileSystemProvider
     16 from object_store_creator import ObjectStoreCreator
     17 from platform_bundle import PlatformBundle
     18 from samples_data_source import SamplesDataSource
     19 from table_of_contents_renderer import TableOfContentsRenderer
     20 from template_renderer import TemplateRenderer
     21 from test_branch_utility import TestBranchUtility
     22 from test_object_store import TestObjectStore
     23 
     24 
     25 class ServerInstance(object):
     26 
     27   def __init__(self,
     28                object_store_creator,
     29                compiled_fs_factory,
     30                branch_utility,
     31                host_file_system_provider,
     32                github_file_system_provider,
     33                gcs_file_system_provider,
     34                base_path='/'):
     35     '''
     36     |object_store_creator|
     37         The ObjectStoreCreator used to create almost all caches.
     38     |compiled_fs_factory|
     39         Factory used to create CompiledFileSystems, a higher-level cache type
     40         than ObjectStores. This can usually be derived from just
     41         |object_store_creator| but under special circumstances a different
     42         implementation needs to be passed in.
     43     |branch_utility|
     44         Has knowledge of Chrome branches, channels, and versions.
     45     |host_file_system_provider|
     46         Creates FileSystem instances which host the server at alternative
     47         revisions.
     48     |github_file_system_provider|
     49         Creates FileSystem instances backed by GitHub.
     50     |base_path|
     51         The path which all HTML is generated relative to. Usually this is /
     52         but some servlets need to override this.
     53     '''
     54     self.object_store_creator = object_store_creator
     55 
     56     self.compiled_fs_factory = compiled_fs_factory
     57 
     58     self.host_file_system_provider = host_file_system_provider
     59     host_fs_at_master = host_file_system_provider.GetMaster()
     60 
     61     self.github_file_system_provider = github_file_system_provider
     62     self.gcs_file_system_provider = gcs_file_system_provider
     63 
     64     assert base_path.startswith('/') and base_path.endswith('/')
     65     self.base_path = base_path
     66 
     67     self.host_file_system_iterator = HostFileSystemIterator(
     68         host_file_system_provider,
     69         branch_utility)
     70 
     71     self.platform_bundle = PlatformBundle(
     72         branch_utility,
     73         self.compiled_fs_factory,
     74         host_fs_at_master,
     75         self.host_file_system_iterator,
     76         self.object_store_creator,
     77         self.base_path)
     78 
     79     self.content_providers = ContentProviders(
     80         object_store_creator,
     81         self.compiled_fs_factory,
     82         host_fs_at_master,
     83         self.github_file_system_provider,
     84         self.gcs_file_system_provider)
     85 
     86     # TODO(kalman): Move all the remaining DataSources into DataSourceRegistry,
     87     # then factor out the DataSource creation into a factory method, so that
     88     # the entire ServerInstance doesn't need to be passed in here.
     89     self.template_renderer = TemplateRenderer(self)
     90 
     91     # TODO(kalman): It may be better for |document_renderer| to construct a
     92     # TemplateDataSource itself rather than depending on template_renderer, but
     93     # for that the above todo should be addressed.
     94     self.document_renderer = DocumentRenderer(
     95         TableOfContentsRenderer(host_fs_at_master,
     96                                 compiled_fs_factory,
     97                                 self.template_renderer),
     98         self.platform_bundle)
     99 
    100   @staticmethod
    101   def ForTest(file_system=None, file_system_provider=None, base_path='/'):
    102     object_store_creator = ObjectStoreCreator.ForTest()
    103     if file_system is None and file_system_provider is None:
    104       raise ValueError('Either |file_system| or |file_system_provider| '
    105                        'must be specified')
    106     if file_system and file_system_provider:
    107       raise ValueError('Only one of |file_system| and |file_system_provider| '
    108                        'can be specified')
    109     if file_system_provider is None:
    110       file_system_provider = HostFileSystemProvider.ForTest(
    111           file_system,
    112           object_store_creator)
    113     return ServerInstance(object_store_creator,
    114                           CompiledFileSystem.Factory(object_store_creator),
    115                           TestBranchUtility.CreateWithCannedData(),
    116                           file_system_provider,
    117                           GithubFileSystemProvider.ForEmpty(),
    118                           CloudStorageFileSystemProvider(object_store_creator),
    119                           base_path=base_path)
    120 
    121   @staticmethod
    122   def ForLocal():
    123     object_store_creator = ObjectStoreCreator(start_empty=False,
    124                                               store_type=TestObjectStore)
    125     host_file_system_provider = HostFileSystemProvider.ForLocal(
    126         object_store_creator)
    127     return ServerInstance(
    128         object_store_creator,
    129         CompiledFileSystem.Factory(object_store_creator),
    130         TestBranchUtility.CreateWithCannedData(),
    131         host_file_system_provider,
    132         GithubFileSystemProvider.ForEmpty(),
    133         CloudStorageFileSystemProvider(object_store_creator))
    134