PageRenderTime 3287ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/java/com/google/gerrit/server/config/GerritServerConfigModule.java

https://gitlab.com/chenfengxu/gerrit
Java | 82 lines | 59 code | 9 blank | 14 comment | 4 complexity | d66308cfa5d1a9b3fe89d341bb91a0c7 MD5 | raw file
  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. package com.google.gerrit.server.config;
  15. import static com.google.inject.Scopes.SINGLETON;
  16. import com.google.gerrit.server.securestore.DefaultSecureStore;
  17. import com.google.gerrit.server.securestore.SecureStore;
  18. import com.google.gerrit.server.securestore.SecureStoreProvider;
  19. import com.google.inject.AbstractModule;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Injector;
  22. import com.google.inject.ProvisionException;
  23. import java.io.IOException;
  24. import java.nio.file.Path;
  25. import org.eclipse.jgit.errors.ConfigInvalidException;
  26. import org.eclipse.jgit.lib.Config;
  27. import org.eclipse.jgit.storage.file.FileBasedConfig;
  28. import org.eclipse.jgit.util.FS;
  29. /** Creates {@link GerritServerConfig}. */
  30. public class GerritServerConfigModule extends AbstractModule {
  31. public static String getSecureStoreClassName(Path sitePath) {
  32. if (sitePath != null) {
  33. return getSecureStoreFromGerritConfig(sitePath);
  34. }
  35. String secureStoreProperty = System.getProperty("gerrit.secure_store_class");
  36. return nullToDefault(secureStoreProperty);
  37. }
  38. private static String getSecureStoreFromGerritConfig(Path sitePath) {
  39. AbstractModule m =
  40. new AbstractModule() {
  41. @Override
  42. protected void configure() {
  43. bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
  44. bind(SitePaths.class);
  45. }
  46. };
  47. Injector injector = Guice.createInjector(m);
  48. SitePaths site = injector.getInstance(SitePaths.class);
  49. FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
  50. if (!cfg.getFile().exists()) {
  51. return DefaultSecureStore.class.getName();
  52. }
  53. try {
  54. cfg.load();
  55. String className = cfg.getString("gerrit", null, "secureStoreClass");
  56. return nullToDefault(className);
  57. } catch (IOException | ConfigInvalidException e) {
  58. throw new ProvisionException(e.getMessage(), e);
  59. }
  60. }
  61. private static String nullToDefault(String className) {
  62. return className != null ? className : DefaultSecureStore.class.getName();
  63. }
  64. @Override
  65. protected void configure() {
  66. bind(SitePaths.class);
  67. bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class).in(SINGLETON);
  68. bind(Config.class)
  69. .annotatedWith(GerritServerConfig.class)
  70. .toProvider(GerritServerConfigProvider.class);
  71. bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON);
  72. }
  73. }