All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.extensions.registration.PrivateInternals_DynamicTypes Maven / Gradle / Ivy

There is a newer version: 3.10.0-rc4
Show newest version
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.extensions.registration;

import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.inject.Binding;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** DO NOT USE */
public class PrivateInternals_DynamicTypes {
  public static Map, DynamicItem> dynamicItemsOf(Injector src) {
    Map, DynamicItem> m = new HashMap<>();
    for (Map.Entry, Binding> e : src.getBindings().entrySet()) {
      TypeLiteral type = e.getKey().getTypeLiteral();
      if (type.getRawType() == DynamicItem.class) {
        ParameterizedType p = (ParameterizedType) type.getType();
        m.put(
            TypeLiteral.get(p.getActualTypeArguments()[0]),
            (DynamicItem) e.getValue().getProvider().get());
      }
    }
    if (m.isEmpty()) {
      return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(m);
  }

  public static Map, DynamicSet> dynamicSetsOf(Injector src) {
    Map, DynamicSet> m = new HashMap<>();
    for (Map.Entry, Binding> e : src.getBindings().entrySet()) {
      TypeLiteral type = e.getKey().getTypeLiteral();
      if (type.getRawType() == DynamicSet.class) {
        ParameterizedType p = (ParameterizedType) type.getType();
        m.put(
            TypeLiteral.get(p.getActualTypeArguments()[0]),
            (DynamicSet) e.getValue().getProvider().get());
      }
    }
    if (m.isEmpty()) {
      return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(m);
  }

  public static Map, DynamicMap> dynamicMapsOf(Injector src) {
    Map, DynamicMap> m = new HashMap<>();
    for (Map.Entry, Binding> e : src.getBindings().entrySet()) {
      TypeLiteral type = e.getKey().getTypeLiteral();
      if (type.getRawType() == DynamicMap.class) {
        ParameterizedType p = (ParameterizedType) type.getType();
        m.put(
            TypeLiteral.get(p.getActualTypeArguments()[0]),
            (DynamicMap) e.getValue().getProvider().get());
      }
    }
    if (m.isEmpty()) {
      return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(m);
  }

  public static List attachItems(
      Injector src, String pluginName, Map, DynamicItem> items) {
    if (src == null || items == null || items.isEmpty()) {
      return Collections.emptyList();
    }

    List handles = new ArrayList<>(4);
    try {
      for (Map.Entry, DynamicItem> e : items.entrySet()) {
        @SuppressWarnings("unchecked")
        TypeLiteral type = (TypeLiteral) e.getKey();

        @SuppressWarnings("unchecked")
        DynamicItem item = (DynamicItem) e.getValue();

        for (Binding b : bindings(src, type)) {
          handles.add(item.set(b.getKey(), b.getProvider(), pluginName));
        }
      }
    } catch (RuntimeException | Error e) {
      remove(handles);
      throw e;
    }
    return handles;
  }

  public static List attachSets(
      Injector src, String pluginName, Map, DynamicSet> sets) {
    if (src == null || sets == null || sets.isEmpty()) {
      return Collections.emptyList();
    }

    List handles = new ArrayList<>(4);
    try {
      for (Map.Entry, DynamicSet> e : sets.entrySet()) {
        @SuppressWarnings("unchecked")
        TypeLiteral type = (TypeLiteral) e.getKey();

        @SuppressWarnings("unchecked")
        DynamicSet set = (DynamicSet) e.getValue();

        for (Binding b : bindings(src, type)) {
          if (b.getKey().getAnnotation() != null) {
            handles.add(set.add(pluginName, b.getKey(), b.getProvider()));
          }
        }
      }
    } catch (RuntimeException | Error e) {
      remove(handles);
      throw e;
    }
    return handles;
  }

  public static List attachMaps(
      Injector src, String pluginName, Map, DynamicMap> maps) {
    if (src == null || maps == null || maps.isEmpty()) {
      return Collections.emptyList();
    }

    List handles = new ArrayList<>(4);
    try {
      for (Map.Entry, DynamicMap> e : maps.entrySet()) {
        @SuppressWarnings("unchecked")
        TypeLiteral type = (TypeLiteral) e.getKey();

        @SuppressWarnings("unchecked")
        PrivateInternals_DynamicMapImpl map =
            (PrivateInternals_DynamicMapImpl) e.getValue();

        for (Binding b : bindings(src, type)) {
          if (b.getKey().getAnnotation() != null) {
            handles.add(map.put(pluginName, b.getKey(), b.getProvider()));
          }
        }
      }
    } catch (RuntimeException | Error e) {
      remove(handles);
      throw e;
    }
    return handles;
  }

  public static LifecycleListener registerInParentInjectors() {
    return new LifecycleListener() {
      private List handles;

      @Inject private Injector self;

      @Override
      public void start() {
        handles = new ArrayList<>(4);
        Injector parent = self.getParent();
        while (parent != null) {
          handles.addAll(attachSets(self, PluginName.GERRIT, dynamicSetsOf(parent)));
          handles.addAll(attachMaps(self, PluginName.GERRIT, dynamicMapsOf(parent)));
          parent = parent.getParent();
        }
        if (handles.isEmpty()) {
          handles = null;
        }
      }

      @Override
      public void stop() {
        remove(handles);
        handles = null;
      }
    };
  }

  private static void remove(List handles) {
    if (handles != null) {
      for (RegistrationHandle handle : handles) {
        handle.remove();
      }
    }
  }

  private static  List> bindings(Injector src, TypeLiteral type) {
    return src.findBindingsByType(type);
  }
}