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.extensions.registration; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.gerrit.extensions.events.LifecycleListener; 19 : import com.google.inject.Binding; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Injector; 22 : import com.google.inject.Key; 23 : import com.google.inject.TypeLiteral; 24 : import java.lang.reflect.ParameterizedType; 25 : import java.util.ArrayList; 26 : import java.util.Collections; 27 : import java.util.HashMap; 28 : import java.util.List; 29 : import java.util.Map; 30 : 31 : /** <b>DO NOT USE</b> */ 32 0 : public class PrivateInternals_DynamicTypes { 33 : public static Map<TypeLiteral<?>, DynamicItem<?>> dynamicItemsOf(Injector src) { 34 149 : Map<TypeLiteral<?>, DynamicItem<?>> m = new HashMap<>(); 35 149 : for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) { 36 149 : TypeLiteral<?> type = e.getKey().getTypeLiteral(); 37 149 : if (type.getRawType() == DynamicItem.class) { 38 149 : ParameterizedType p = (ParameterizedType) type.getType(); 39 149 : m.put( 40 149 : TypeLiteral.get(p.getActualTypeArguments()[0]), 41 149 : (DynamicItem<?>) e.getValue().getProvider().get()); 42 : } 43 149 : } 44 149 : if (m.isEmpty()) { 45 0 : return Collections.emptyMap(); 46 : } 47 149 : return Collections.unmodifiableMap(m); 48 : } 49 : 50 : public static Map<TypeLiteral<?>, DynamicSet<?>> dynamicSetsOf(Injector src) { 51 149 : Map<TypeLiteral<?>, DynamicSet<?>> m = new HashMap<>(); 52 149 : for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) { 53 149 : TypeLiteral<?> type = e.getKey().getTypeLiteral(); 54 149 : if (type.getRawType() == DynamicSet.class) { 55 149 : ParameterizedType p = (ParameterizedType) type.getType(); 56 149 : m.put( 57 149 : TypeLiteral.get(p.getActualTypeArguments()[0]), 58 149 : (DynamicSet<?>) e.getValue().getProvider().get()); 59 : } 60 149 : } 61 149 : if (m.isEmpty()) { 62 103 : return Collections.emptyMap(); 63 : } 64 149 : return Collections.unmodifiableMap(m); 65 : } 66 : 67 : public static Map<TypeLiteral<?>, DynamicMap<?>> dynamicMapsOf(Injector src) { 68 149 : Map<TypeLiteral<?>, DynamicMap<?>> m = new HashMap<>(); 69 149 : for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) { 70 149 : TypeLiteral<?> type = e.getKey().getTypeLiteral(); 71 149 : if (type.getRawType() == DynamicMap.class) { 72 149 : ParameterizedType p = (ParameterizedType) type.getType(); 73 149 : m.put( 74 149 : TypeLiteral.get(p.getActualTypeArguments()[0]), 75 149 : (DynamicMap<?>) e.getValue().getProvider().get()); 76 : } 77 149 : } 78 149 : if (m.isEmpty()) { 79 103 : return Collections.emptyMap(); 80 : } 81 149 : return Collections.unmodifiableMap(m); 82 : } 83 : 84 : public static ImmutableList<RegistrationHandle> attachItems( 85 : Injector src, String pluginName, Map<TypeLiteral<?>, DynamicItem<?>> items) { 86 14 : if (src == null || items == null || items.isEmpty()) { 87 14 : return ImmutableList.of(); 88 : } 89 : 90 14 : List<RegistrationHandle> handles = new ArrayList<>(4); 91 : try { 92 14 : for (Map.Entry<TypeLiteral<?>, DynamicItem<?>> e : items.entrySet()) { 93 : @SuppressWarnings("unchecked") 94 14 : TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey(); 95 : 96 : @SuppressWarnings("unchecked") 97 14 : DynamicItem<Object> item = (DynamicItem<Object>) e.getValue(); 98 : 99 14 : for (Binding<Object> b : bindings(src, type)) { 100 0 : handles.add(item.set(b.getKey(), b.getProvider(), pluginName)); 101 0 : } 102 14 : } 103 0 : } catch (RuntimeException | Error e) { 104 0 : remove(handles); 105 0 : throw e; 106 14 : } 107 14 : return ImmutableList.copyOf(handles); 108 : } 109 : 110 : public static ImmutableList<RegistrationHandle> attachSets( 111 : Injector src, String pluginName, Map<TypeLiteral<?>, DynamicSet<?>> sets) { 112 106 : if (src == null || sets == null || sets.isEmpty()) { 113 106 : return ImmutableList.of(); 114 : } 115 : 116 106 : List<RegistrationHandle> handles = new ArrayList<>(4); 117 : try { 118 106 : for (Map.Entry<TypeLiteral<?>, DynamicSet<?>> e : sets.entrySet()) { 119 : @SuppressWarnings("unchecked") 120 106 : TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey(); 121 : 122 : @SuppressWarnings("unchecked") 123 106 : DynamicSet<Object> set = (DynamicSet<Object>) e.getValue(); 124 : 125 106 : for (Binding<Object> b : bindings(src, type)) { 126 105 : if (b.getKey().getAnnotation() != null) { 127 105 : handles.add(set.add(pluginName, b.getKey(), b.getProvider())); 128 : } 129 105 : } 130 106 : } 131 0 : } catch (RuntimeException | Error e) { 132 0 : remove(handles); 133 0 : throw e; 134 106 : } 135 106 : return ImmutableList.copyOf(handles); 136 : } 137 : 138 : public static ImmutableList<RegistrationHandle> attachMaps( 139 : Injector src, String pluginName, Map<TypeLiteral<?>, DynamicMap<?>> maps) { 140 106 : if (src == null || maps == null || maps.isEmpty()) { 141 106 : return ImmutableList.of(); 142 : } 143 : 144 106 : List<RegistrationHandle> handles = new ArrayList<>(4); 145 : try { 146 106 : for (Map.Entry<TypeLiteral<?>, DynamicMap<?>> e : maps.entrySet()) { 147 : @SuppressWarnings("unchecked") 148 106 : TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey(); 149 : 150 : @SuppressWarnings("unchecked") 151 106 : PrivateInternals_DynamicMapImpl<Object> map = 152 106 : (PrivateInternals_DynamicMapImpl<Object>) e.getValue(); 153 : 154 106 : for (Binding<Object> b : bindings(src, type)) { 155 101 : if (b.getKey().getAnnotation() != null) { 156 101 : handles.add(map.put(pluginName, b.getKey(), b.getProvider())); 157 : } 158 101 : } 159 106 : } 160 0 : } catch (RuntimeException | Error e) { 161 0 : remove(handles); 162 0 : throw e; 163 106 : } 164 106 : return ImmutableList.copyOf(handles); 165 : } 166 : 167 : public static LifecycleListener registerInParentInjectors() { 168 103 : return new LifecycleListener() { 169 : private List<RegistrationHandle> handles; 170 : 171 : @Inject private Injector self; 172 : 173 : @Override 174 : public void start() { 175 103 : handles = new ArrayList<>(4); 176 103 : Injector parent = self.getParent(); 177 103 : while (parent != null) { 178 103 : handles.addAll(attachSets(self, PluginName.GERRIT, dynamicSetsOf(parent))); 179 103 : handles.addAll(attachMaps(self, PluginName.GERRIT, dynamicMapsOf(parent))); 180 103 : parent = parent.getParent(); 181 : } 182 103 : if (handles.isEmpty()) { 183 0 : handles = null; 184 : } 185 103 : } 186 : 187 : @Override 188 : public void stop() { 189 103 : remove(handles); 190 103 : handles = null; 191 103 : } 192 : }; 193 : } 194 : 195 : private static void remove(List<RegistrationHandle> handles) { 196 103 : if (handles != null) { 197 103 : for (RegistrationHandle handle : handles) { 198 103 : handle.remove(); 199 103 : } 200 : } 201 103 : } 202 : 203 : private static <T> List<Binding<T>> bindings(Injector src, TypeLiteral<T> type) { 204 106 : return src.findBindingsByType(type); 205 : } 206 : }