LCOV - code coverage report
Current view: top level - server/config - GerritInstanceNameProvider.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 9 12 75.0 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2018 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 com.google.gerrit.common.Nullable;
      18             : import com.google.inject.Inject;
      19             : import com.google.inject.Provider;
      20             : import com.google.inject.Singleton;
      21             : import java.net.MalformedURLException;
      22             : import java.net.URL;
      23             : import org.eclipse.jgit.lib.Config;
      24             : import org.eclipse.jgit.util.SystemReader;
      25             : 
      26             : /** Provides {@link GerritInstanceName} from {@code gerrit.name}. */
      27             : @Singleton
      28             : public class GerritInstanceNameProvider implements Provider<String> {
      29             :   private final String instanceName;
      30             : 
      31             :   @Inject
      32             :   public GerritInstanceNameProvider(
      33             :       @GerritServerConfig Config config,
      34         146 :       @CanonicalWebUrl @Nullable Provider<String> canonicalUrlProvider) {
      35         146 :     this.instanceName = getInstanceName(config, canonicalUrlProvider);
      36         146 :   }
      37             : 
      38             :   @Override
      39             :   public String get() {
      40         106 :     return instanceName;
      41             :   }
      42             : 
      43             :   private static String getInstanceName(
      44             :       Config config, @Nullable Provider<String> canonicalUrlProvider) {
      45         146 :     String instanceName = config.getString("gerrit", null, "instanceName");
      46         146 :     if (instanceName != null || canonicalUrlProvider == null) {
      47           0 :       return instanceName;
      48             :     }
      49             : 
      50         146 :     return extractInstanceName(canonicalUrlProvider.get());
      51             :   }
      52             : 
      53             :   private static String extractInstanceName(String canonicalUrl) {
      54         146 :     if (canonicalUrl != null) {
      55             :       try {
      56         146 :         return new URL(canonicalUrl).getHost();
      57           0 :       } catch (MalformedURLException e) {
      58             :         // Try something else.
      59             :       }
      60             :     }
      61             : 
      62             :     // Fall back onto whatever the local operating system thinks
      63             :     // this server is called. We hopefully didn't get here as a
      64             :     // good admin would have configured the canonical url.
      65             :     //
      66           0 :     return SystemReader.getInstance().getHostname();
      67             :   }
      68             : }

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