LCOV - code coverage report
Current view: top level - server/plugins - JsPlugin.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 30 30 100.0 %
Date: 2022-11-19 15:00:39 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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.plugins;
      16             : 
      17             : import com.google.gerrit.common.Nullable;
      18             : import com.google.gerrit.extensions.annotations.PluginName;
      19             : import com.google.gerrit.extensions.registration.DynamicSet;
      20             : import com.google.gerrit.extensions.webui.JavaScriptPlugin;
      21             : import com.google.gerrit.extensions.webui.WebUiPlugin;
      22             : import com.google.gerrit.lifecycle.LifecycleManager;
      23             : import com.google.gerrit.server.PluginUser;
      24             : import com.google.inject.AbstractModule;
      25             : import com.google.inject.Guice;
      26             : import com.google.inject.Injector;
      27             : import java.nio.file.Path;
      28             : import org.eclipse.jgit.internal.storage.file.FileSnapshot;
      29             : 
      30             : class JsPlugin extends Plugin {
      31             :   private Injector sysInjector;
      32             : 
      33             :   JsPlugin(String name, Path srcFile, PluginUser pluginUser, FileSnapshot snapshot) {
      34           3 :     super(name, srcFile, pluginUser, snapshot, ApiType.JS);
      35           3 :   }
      36             : 
      37             :   @Override
      38             :   @Nullable
      39             :   public String getVersion() {
      40           3 :     String fileName = getSrcFile().getFileName().toString();
      41           3 :     int firstDash = fileName.indexOf("-");
      42           3 :     if (firstDash > 0) {
      43           3 :       int extension = fileName.lastIndexOf(".js");
      44           3 :       if (extension > 0) {
      45           3 :         return fileName.substring(firstDash + 1, extension);
      46             :       }
      47             :     }
      48           1 :     return "";
      49             :   }
      50             : 
      51             :   @Override
      52             :   public void start(PluginGuiceEnvironment env) throws Exception {
      53           3 :     manager = new LifecycleManager();
      54           3 :     String fileName = getSrcFile().getFileName().toString();
      55           3 :     sysInjector = Guice.createInjector(new StandaloneJsPluginModule(getName(), fileName));
      56           3 :     manager.start();
      57           3 :   }
      58             : 
      59             :   @Override
      60             :   protected void stop(PluginGuiceEnvironment env) {
      61           3 :     if (manager != null) {
      62           3 :       manager.stop();
      63           3 :       sysInjector = null;
      64             :     }
      65           3 :   }
      66             : 
      67             :   @Override
      68             :   public Injector getSysInjector() {
      69           3 :     return sysInjector;
      70             :   }
      71             : 
      72             :   @Override
      73             :   @Nullable
      74             :   public Injector getSshInjector() {
      75           3 :     return null;
      76             :   }
      77             : 
      78             :   @Override
      79             :   @Nullable
      80             :   public Injector getHttpInjector() {
      81           3 :     return null;
      82             :   }
      83             : 
      84             :   @Override
      85             :   protected boolean canReload() {
      86           1 :     return true;
      87             :   }
      88             : 
      89             :   private static final class StandaloneJsPluginModule extends AbstractModule {
      90             :     private final String fileName;
      91             :     private final String pluginName;
      92             : 
      93           3 :     StandaloneJsPluginModule(String pluginName, String fileName) {
      94           3 :       this.pluginName = pluginName;
      95           3 :       this.fileName = fileName;
      96           3 :     }
      97             : 
      98             :     @Override
      99             :     protected void configure() {
     100           3 :       bind(String.class).annotatedWith(PluginName.class).toInstance(pluginName);
     101           3 :       DynamicSet.bind(binder(), WebUiPlugin.class).toInstance(new JavaScriptPlugin(fileName));
     102           3 :     }
     103             :   }
     104             : 
     105             :   @Override
     106             :   public PluginContentScanner getContentScanner() {
     107           1 :     return PluginContentScanner.EMPTY;
     108             :   }
     109             : }

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