LCOV - code coverage report
Current view: top level - httpd/plugins - HttpAutoRegisterModuleGenerator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 0 30 0.0 %
Date: 2022-11-19 15:00:39 Functions: 0 6 0.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.httpd.plugins;
      16             : 
      17             : import static com.google.gerrit.server.plugins.AutoRegisterUtil.calculateBindAnnotation;
      18             : 
      19             : import com.google.common.collect.LinkedListMultimap;
      20             : import com.google.common.collect.ListMultimap;
      21             : import com.google.gerrit.extensions.annotations.Export;
      22             : import com.google.gerrit.server.plugins.InvalidPluginException;
      23             : import com.google.gerrit.server.plugins.ModuleGenerator;
      24             : import com.google.inject.Module;
      25             : import com.google.inject.Scopes;
      26             : import com.google.inject.TypeLiteral;
      27             : import com.google.inject.servlet.ServletModule;
      28             : import java.lang.annotation.Annotation;
      29             : import java.util.HashMap;
      30             : import java.util.Map;
      31             : import javax.servlet.http.HttpServlet;
      32             : 
      33           0 : class HttpAutoRegisterModuleGenerator extends ServletModule implements ModuleGenerator {
      34           0 :   private final Map<String, Class<HttpServlet>> serve = new HashMap<>();
      35           0 :   private final ListMultimap<TypeLiteral<?>, Class<?>> listeners = LinkedListMultimap.create();
      36             : 
      37             :   @Override
      38             :   protected void configureServlets() {
      39           0 :     for (Map.Entry<String, Class<HttpServlet>> e : serve.entrySet()) {
      40           0 :       bind(e.getValue()).in(Scopes.SINGLETON);
      41           0 :       serve(e.getKey()).with(e.getValue());
      42           0 :     }
      43           0 :     for (Map.Entry<TypeLiteral<?>, Class<?>> e : listeners.entries()) {
      44             :       @SuppressWarnings("unchecked")
      45           0 :       TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
      46             : 
      47             :       @SuppressWarnings("unchecked")
      48           0 :       Class<Object> impl = (Class<Object>) e.getValue();
      49             : 
      50           0 :       Annotation n = calculateBindAnnotation(impl);
      51           0 :       bind(type).annotatedWith(n).to(impl);
      52           0 :     }
      53           0 :   }
      54             : 
      55             :   @Override
      56           0 :   public void setPluginName(String name) {}
      57             : 
      58             :   @SuppressWarnings("unchecked")
      59             :   @Override
      60             :   public void export(Export export, Class<?> type) throws InvalidPluginException {
      61           0 :     if (HttpServlet.class.isAssignableFrom(type)) {
      62           0 :       Class<HttpServlet> old = serve.get(export.value());
      63           0 :       if (old != null) {
      64           0 :         throw new InvalidPluginException(
      65           0 :             String.format(
      66             :                 "@Export(\"%s\") has duplicate bindings:\n  %s\n  %s",
      67           0 :                 export.value(), old.getName(), type.getName()));
      68             :       }
      69           0 :       serve.put(export.value(), (Class<HttpServlet>) type);
      70           0 :     } else {
      71           0 :       throw new InvalidPluginException(
      72           0 :           String.format(
      73             :               "Class %s with @Export(\"%s\") must extend %s",
      74           0 :               type.getName(), export.value(), HttpServlet.class.getName()));
      75             :     }
      76           0 :   }
      77             : 
      78             :   @Override
      79             :   public void listen(TypeLiteral<?> tl, Class<?> clazz) {
      80           0 :     listeners.put(tl, clazz);
      81           0 :   }
      82             : 
      83             :   @Override
      84             :   public Module create() throws InvalidPluginException {
      85           0 :     return this;
      86             :   }
      87             : }

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