PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/java/com/google/gerrit/server/plugins/JsPlugin.java

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