LCOV - code coverage report
Current view: top level - pgm/util - BatchProgramModule.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 91 91 100.0 %
Date: 2022-11-19 15:00:39 Functions: 10 10 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2014 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.pgm.util;
      16             : 
      17             : import static com.google.inject.Scopes.SINGLETON;
      18             : 
      19             : import com.google.common.cache.Cache;
      20             : import com.google.common.collect.ImmutableSet;
      21             : import com.google.gerrit.entities.AccountGroup;
      22             : import com.google.gerrit.entities.GroupReference;
      23             : import com.google.gerrit.entities.SubmitRequirement;
      24             : import com.google.gerrit.extensions.api.projects.CommentLinkInfo;
      25             : import com.google.gerrit.extensions.common.AccountVisibility;
      26             : import com.google.gerrit.extensions.config.FactoryModule;
      27             : import com.google.gerrit.extensions.registration.DynamicMap;
      28             : import com.google.gerrit.extensions.registration.DynamicSet;
      29             : import com.google.gerrit.extensions.restapi.RestView;
      30             : import com.google.gerrit.server.CurrentUser;
      31             : import com.google.gerrit.server.IdentifiedUser;
      32             : import com.google.gerrit.server.InternalUser;
      33             : import com.google.gerrit.server.LibModuleLoader;
      34             : import com.google.gerrit.server.LibModuleType;
      35             : import com.google.gerrit.server.ModuleOverloader;
      36             : import com.google.gerrit.server.account.AccountCacheImpl;
      37             : import com.google.gerrit.server.account.AccountVisibilityProvider;
      38             : import com.google.gerrit.server.account.CapabilityCollection;
      39             : import com.google.gerrit.server.account.FakeRealm;
      40             : import com.google.gerrit.server.account.GroupCacheImpl;
      41             : import com.google.gerrit.server.account.GroupIncludeCacheImpl;
      42             : import com.google.gerrit.server.account.Realm;
      43             : import com.google.gerrit.server.account.ServiceUserClassifierImpl;
      44             : import com.google.gerrit.server.account.externalids.ExternalIdCacheModule;
      45             : import com.google.gerrit.server.cache.CacheRemovalListener;
      46             : import com.google.gerrit.server.cache.h2.H2CacheModule;
      47             : import com.google.gerrit.server.cache.mem.DefaultMemoryCacheModule;
      48             : import com.google.gerrit.server.change.ChangeJson;
      49             : import com.google.gerrit.server.change.ChangeKindCacheImpl;
      50             : import com.google.gerrit.server.change.EmailNewPatchSet;
      51             : import com.google.gerrit.server.change.MergeabilityCacheImpl;
      52             : import com.google.gerrit.server.change.PatchSetInserter;
      53             : import com.google.gerrit.server.change.RebaseChangeOp;
      54             : import com.google.gerrit.server.config.AdministrateServerGroups;
      55             : import com.google.gerrit.server.config.CanonicalWebUrl;
      56             : import com.google.gerrit.server.config.CanonicalWebUrlProvider;
      57             : import com.google.gerrit.server.config.DefaultPreferencesCacheImpl;
      58             : import com.google.gerrit.server.config.DefaultUrlFormatter.DefaultUrlFormatterModule;
      59             : import com.google.gerrit.server.config.EnablePeerIPInReflogRecord;
      60             : import com.google.gerrit.server.config.EnablePeerIPInReflogRecordProvider;
      61             : import com.google.gerrit.server.config.GerritServerConfig;
      62             : import com.google.gerrit.server.config.GitReceivePackGroups;
      63             : import com.google.gerrit.server.config.GitUploadPackGroups;
      64             : import com.google.gerrit.server.config.SysExecutorModule;
      65             : import com.google.gerrit.server.extensions.events.AttentionSetObserver;
      66             : import com.google.gerrit.server.extensions.events.EventUtil;
      67             : import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
      68             : import com.google.gerrit.server.extensions.events.RevisionCreated;
      69             : import com.google.gerrit.server.extensions.events.WorkInProgressStateChanged;
      70             : import com.google.gerrit.server.git.PureRevertCache;
      71             : import com.google.gerrit.server.git.SearchingChangeCacheImpl;
      72             : import com.google.gerrit.server.git.TagCache;
      73             : import com.google.gerrit.server.notedb.NoteDbModule;
      74             : import com.google.gerrit.server.patch.DiffExecutorModule;
      75             : import com.google.gerrit.server.patch.DiffOperationsImpl;
      76             : import com.google.gerrit.server.patch.PatchListCacheImpl;
      77             : import com.google.gerrit.server.permissions.DefaultPermissionBackendModule;
      78             : import com.google.gerrit.server.permissions.SectionSortCache;
      79             : import com.google.gerrit.server.plugins.PluginModule;
      80             : import com.google.gerrit.server.project.CommentLinkProvider;
      81             : import com.google.gerrit.server.project.CommitResource;
      82             : import com.google.gerrit.server.project.ProjectCacheImpl;
      83             : import com.google.gerrit.server.project.ProjectState;
      84             : import com.google.gerrit.server.project.SubmitRequirementsEvaluatorImpl;
      85             : import com.google.gerrit.server.project.SubmitRuleEvaluator;
      86             : import com.google.gerrit.server.query.FileEditsPredicate;
      87             : import com.google.gerrit.server.query.approval.ApprovalModule;
      88             : import com.google.gerrit.server.query.change.ChangeData;
      89             : import com.google.gerrit.server.query.change.ChangeIsVisibleToPredicate;
      90             : import com.google.gerrit.server.query.change.ChangeQueryBuilder;
      91             : import com.google.gerrit.server.query.change.ConflictsCacheImpl;
      92             : import com.google.gerrit.server.query.change.DistinctVotersPredicate;
      93             : import com.google.gerrit.server.query.change.HasSubmoduleUpdatePredicate;
      94             : import com.google.gerrit.server.restapi.group.GroupModule;
      95             : import com.google.gerrit.server.rules.DefaultSubmitRule.DefaultSubmitRuleModule;
      96             : import com.google.gerrit.server.rules.IgnoreSelfApprovalRule.IgnoreSelfApprovalRuleModule;
      97             : import com.google.gerrit.server.rules.PrologModule;
      98             : import com.google.gerrit.server.rules.SubmitRule;
      99             : import com.google.gerrit.server.update.BatchUpdate;
     100             : import com.google.inject.Injector;
     101             : import com.google.inject.Key;
     102             : import com.google.inject.Module;
     103             : import com.google.inject.TypeLiteral;
     104             : import com.google.inject.util.Providers;
     105             : import java.util.ArrayList;
     106             : import java.util.Collections;
     107             : import java.util.List;
     108             : import java.util.Set;
     109             : import org.eclipse.jgit.lib.Config;
     110             : 
     111             : /** Module for programs that perform batch operations on a site. */
     112             : public class BatchProgramModule extends FactoryModule {
     113             :   private Injector parentInjector;
     114             : 
     115          15 :   public BatchProgramModule(Injector parentInjector) {
     116          15 :     this.parentInjector = parentInjector;
     117          15 :   }
     118             : 
     119             :   @SuppressWarnings("rawtypes")
     120             :   @Override
     121             :   protected void configure() {
     122          15 :     List<Module> modules = new ArrayList<>();
     123             : 
     124          15 :     modules.add(new DiffExecutorModule());
     125          15 :     modules.add(new SysExecutorModule());
     126          15 :     modules.add(BatchUpdate.module());
     127          15 :     modules.add(PatchListCacheImpl.module());
     128          15 :     modules.add(new DefaultUrlFormatterModule());
     129          15 :     modules.add(DiffOperationsImpl.module());
     130             : 
     131             :     // There is the concept of LifecycleModule, in Gerrit's own extension to Guice, which has these:
     132             :     //  listener().to(SomeClassImplementingLifecycleListener.class);
     133             :     // and the start() methods of each such listener are executed in the order they are declared.
     134             :     // Makes sure that PluginLoader.start() is executed before the LuceneIndexModule.start() so that
     135             :     // plugins get loaded and the respective Guice modules installed so that the on-line reindexing
     136             :     // will happen with the proper classes (e.g. group backends, custom Prolog predicates) and the
     137             :     // associated rules ready to be evaluated.
     138          15 :     modules.add(new PluginModule());
     139             : 
     140             :     // We're just running through each change
     141             :     // once, so don't worry about cache removal.
     142          15 :     bind(new TypeLiteral<DynamicSet<CacheRemovalListener>>() {}).toInstance(DynamicSet.emptySet());
     143          15 :     DynamicMap.mapOf(binder(), new TypeLiteral<Cache<?, ?>>() {});
     144          15 :     bind(new TypeLiteral<List<CommentLinkInfo>>() {})
     145          15 :         .toProvider(CommentLinkProvider.class)
     146          15 :         .in(SINGLETON);
     147          15 :     bind(new TypeLiteral<DynamicMap<RestView<CommitResource>>>() {})
     148          15 :         .toInstance(DynamicMap.emptyMap());
     149          15 :     bind(String.class)
     150          15 :         .annotatedWith(CanonicalWebUrl.class)
     151          15 :         .toProvider(CanonicalWebUrlProvider.class);
     152          15 :     bind(Boolean.class)
     153          15 :         .annotatedWith(EnablePeerIPInReflogRecord.class)
     154          15 :         .toProvider(EnablePeerIPInReflogRecordProvider.class)
     155          15 :         .in(SINGLETON);
     156          15 :     bind(Realm.class).to(FakeRealm.class);
     157          15 :     bind(IdentifiedUser.class).toProvider(Providers.of(null));
     158          15 :     bind(EmailNewPatchSet.Factory.class).toProvider(Providers.of(null));
     159          15 :     bind(CurrentUser.class).to(InternalUser.class);
     160          15 :     factory(PatchSetInserter.Factory.class);
     161          15 :     factory(RebaseChangeOp.Factory.class);
     162             : 
     163             :     // As Reindex is a batch program, don't assume the index is available for
     164             :     // the change cache.
     165          15 :     bind(SearchingChangeCacheImpl.class).toProvider(Providers.of(null));
     166             : 
     167          15 :     bind(new TypeLiteral<ImmutableSet<GroupReference>>() {})
     168          15 :         .annotatedWith(AdministrateServerGroups.class)
     169          15 :         .toInstance(ImmutableSet.of());
     170          15 :     bind(new TypeLiteral<Set<AccountGroup.UUID>>() {})
     171          15 :         .annotatedWith(GitUploadPackGroups.class)
     172          15 :         .toInstance(Collections.emptySet());
     173          15 :     bind(new TypeLiteral<Set<AccountGroup.UUID>>() {})
     174          15 :         .annotatedWith(GitReceivePackGroups.class)
     175          15 :         .toInstance(Collections.emptySet());
     176             : 
     177          15 :     modules.add(new BatchGitModule());
     178          15 :     modules.add(new DefaultPermissionBackendModule());
     179          15 :     modules.add(new DefaultMemoryCacheModule());
     180          15 :     modules.add(new H2CacheModule());
     181          15 :     modules.add(new ExternalIdCacheModule());
     182          15 :     modules.add(new GroupModule());
     183          15 :     modules.add(new NoteDbModule());
     184          15 :     modules.add(AccountCacheImpl.module());
     185          15 :     modules.add(ConflictsCacheImpl.module());
     186          15 :     modules.add(DefaultPreferencesCacheImpl.module());
     187          15 :     modules.add(GroupCacheImpl.module());
     188          15 :     modules.add(GroupIncludeCacheImpl.module());
     189          15 :     modules.add(ProjectCacheImpl.module());
     190          15 :     modules.add(SectionSortCache.module());
     191          15 :     modules.add(ChangeKindCacheImpl.module());
     192          15 :     modules.add(MergeabilityCacheImpl.module());
     193          15 :     modules.add(ServiceUserClassifierImpl.module());
     194          15 :     modules.add(TagCache.module());
     195          15 :     modules.add(PureRevertCache.module());
     196          15 :     modules.add(new ApprovalModule());
     197          15 :     modules.add(SubmitRequirementsEvaluatorImpl.module());
     198          15 :     factory(CapabilityCollection.Factory.class);
     199          15 :     factory(ChangeData.AssistedFactory.class);
     200          15 :     factory(ChangeIsVisibleToPredicate.Factory.class);
     201          15 :     factory(DistinctVotersPredicate.Factory.class);
     202          15 :     factory(HasSubmoduleUpdatePredicate.Factory.class);
     203          15 :     factory(ProjectState.Factory.class);
     204             : 
     205          15 :     DynamicMap.mapOf(binder(), ChangeQueryBuilder.ChangeOperatorFactory.class);
     206          15 :     DynamicMap.mapOf(binder(), ChangeQueryBuilder.ChangeHasOperandFactory.class);
     207          15 :     DynamicMap.mapOf(binder(), ChangeQueryBuilder.ChangeIsOperandFactory.class);
     208             : 
     209             :     // Submit rules
     210          15 :     DynamicSet.setOf(binder(), SubmitRule.class);
     211          15 :     factory(SubmitRuleEvaluator.Factory.class);
     212          15 :     modules.add(new PrologModule(getConfig()));
     213          15 :     modules.add(new DefaultSubmitRuleModule());
     214          15 :     modules.add(new IgnoreSelfApprovalRuleModule());
     215             : 
     216             :     // Global submit requirements
     217          15 :     DynamicSet.setOf(binder(), SubmitRequirement.class);
     218             : 
     219          15 :     factory(FileEditsPredicate.Factory.class);
     220             : 
     221          15 :     bind(ChangeJson.Factory.class).toProvider(Providers.of(null));
     222          15 :     bind(EventUtil.class).toProvider(Providers.of(null));
     223          15 :     bind(GitReferenceUpdated.class).toInstance(GitReferenceUpdated.DISABLED);
     224          15 :     bind(RevisionCreated.class).toInstance(RevisionCreated.DISABLED);
     225          15 :     bind(WorkInProgressStateChanged.class).toInstance(WorkInProgressStateChanged.DISABLED);
     226          15 :     bind(AccountVisibility.class).toProvider(AccountVisibilityProvider.class).in(SINGLETON);
     227          15 :     bind(AttentionSetObserver.class).toInstance(AttentionSetObserver.DISABLED);
     228             : 
     229          15 :     ModuleOverloader.override(
     230             :             modules,
     231          15 :             LibModuleLoader.loadModules(parentInjector, LibModuleType.SYS_BATCH_MODULE_TYPE))
     232          15 :         .stream()
     233          15 :         .forEach(this::install);
     234          15 :   }
     235             : 
     236             :   protected Config getConfig() {
     237          15 :     return parentInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
     238             :   }
     239             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750