Line data Source code
1 : // Copyright (C) 2016 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.server; 16 : 17 : import static java.util.stream.Collectors.toList; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.server.config.GerritServerConfig; 21 : import com.google.gerrit.server.index.options.AutoFlush; 22 : import com.google.inject.Injector; 23 : import com.google.inject.Key; 24 : import com.google.inject.Module; 25 : import com.google.inject.ProvisionException; 26 : import java.lang.reflect.Method; 27 : import java.util.Arrays; 28 : import java.util.List; 29 : import java.util.Map; 30 : import org.eclipse.jgit.lib.Config; 31 : 32 : /** Loads configured Guice modules from {@code gerrit.installModule}. */ 33 0 : public class LibModuleLoader { 34 138 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 35 : 36 : public static List<Module> loadModules(Injector parent, LibModuleType moduleType) { 37 138 : Config cfg = getConfig(parent); 38 138 : return Arrays.stream(cfg.getStringList("gerrit", null, "install" + moduleType.getConfigKey())) 39 138 : .map(m -> createModule(parent, m)) 40 138 : .collect(toList()); 41 : } 42 : 43 : public static List<Module> loadReindexModules( 44 : Injector parent, Map<String, Integer> versions, int threads, boolean replica) { 45 15 : Config cfg = getConfig(parent); 46 15 : return Arrays.stream( 47 15 : cfg.getStringList( 48 15 : "gerrit", null, "install" + LibModuleType.INDEX_MODULE_TYPE.getConfigKey())) 49 15 : .map(m -> createReindexModule(m, versions, threads, replica)) 50 15 : .collect(toList()); 51 : } 52 : 53 : private static Module createReindexModule( 54 : String className, Map<String, Integer> versions, int threads, boolean replica) { 55 0 : Class<Module> clazz = loadModule(className); 56 : try { 57 : 58 0 : Method m = 59 0 : clazz.getMethod( 60 : "singleVersionWithExplicitVersions", 61 : Map.class, 62 : int.class, 63 : boolean.class, 64 : AutoFlush.class); 65 : 66 0 : Module module = (Module) m.invoke(null, versions, threads, replica, AutoFlush.DISABLED); 67 0 : logger.atInfo().log("Installed module %s", className); 68 0 : return module; 69 0 : } catch (Exception e) { 70 0 : logger.atSevere().withCause(e).log("Unable to load libModule for %s", className); 71 0 : throw new IllegalStateException(e); 72 : } 73 : } 74 : 75 : private static Config getConfig(Injector i) { 76 138 : return i.getInstance(Key.get(Config.class, GerritServerConfig.class)); 77 : } 78 : 79 : private static Module createModule(Injector injector, String className) { 80 0 : Module m = injector.getInstance(loadModule(className)); 81 0 : logger.atInfo().log("Installed module %s", className); 82 0 : return m; 83 : } 84 : 85 : @SuppressWarnings("unchecked") 86 : private static Class<Module> loadModule(String className) { 87 : try { 88 0 : return (Class<Module>) Class.forName(className); 89 0 : } catch (ClassNotFoundException | LinkageError e) { 90 0 : throw new ProvisionException("Cannot load LibModule " + className, e); 91 : } 92 : } 93 : }