Line data Source code
1 : // Copyright (C) 2009 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.config; 16 : 17 : import static com.google.inject.Scopes.SINGLETON; 18 : 19 : import com.google.gerrit.server.securestore.DefaultSecureStore; 20 : import com.google.gerrit.server.securestore.SecureStore; 21 : import com.google.gerrit.server.securestore.SecureStoreProvider; 22 : import com.google.inject.AbstractModule; 23 : import com.google.inject.ProvisionException; 24 : import java.io.IOException; 25 : import java.nio.file.Path; 26 : import org.eclipse.jgit.errors.ConfigInvalidException; 27 : import org.eclipse.jgit.lib.Config; 28 : import org.eclipse.jgit.storage.file.FileBasedConfig; 29 : import org.eclipse.jgit.util.FS; 30 : 31 : /** Creates {@link GerritServerConfig}. */ 32 15 : public class GerritServerConfigModule extends AbstractModule { 33 : public static String getSecureStoreClassName(Path sitePath) { 34 15 : if (sitePath != null) { 35 15 : return getSecureStoreFromGerritConfig(sitePath); 36 : } 37 : 38 0 : String secureStoreProperty = System.getProperty("gerrit.secure_store_class"); 39 0 : return nullToDefault(secureStoreProperty); 40 : } 41 : 42 : private static String getSecureStoreFromGerritConfig(Path sitePath) { 43 : try { 44 15 : SitePaths site = new SitePaths(sitePath); 45 15 : FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED); 46 15 : if (!cfg.getFile().exists()) { 47 0 : return DefaultSecureStore.class.getName(); 48 : } 49 : 50 15 : cfg.load(); 51 15 : String className = cfg.getString("gerrit", null, "secureStoreClass"); 52 15 : return nullToDefault(className); 53 0 : } catch (IOException | ConfigInvalidException e) { 54 0 : throw new ProvisionException(e.getMessage(), e); 55 : } 56 : } 57 : 58 : private static String nullToDefault(String className) { 59 15 : return className != null ? className : DefaultSecureStore.class.getName(); 60 : } 61 : 62 : @Override 63 : protected void configure() { 64 15 : bind(SitePaths.class); 65 15 : bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class).in(SINGLETON); 66 15 : bind(Config.class) 67 15 : .annotatedWith(GerritServerConfig.class) 68 15 : .toProvider(GerritServerConfigProvider.class); 69 15 : bind(AllProjectsConfigProvider.class).to(FileBasedAllProjectsConfigProvider.class); 70 15 : bind(GlobalPluginConfigProvider.class).to(FileBasedGlobalPluginConfigProvider.class); 71 15 : bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON); 72 15 : bind(Boolean.class) 73 15 : .annotatedWith(GerritIsReplica.class) 74 15 : .toProvider(GerritIsReplicaProvider.class); 75 15 : } 76 : }