Implement DynamicSet<T>, DynamicMap<T> to provide bindings in Guice
The core server can now declare that it needs a DynamicSet supplied by Guice at runtime: DynamicSet.setOf(binder(), SomeBaseType.class); and then receive this as an injection: DynamicSet<SomeBaseType> theThings Core server code can register static implementations into this set: DynamicSet.bind(binder(), SomeBaseType.class).to(AnImpl.class); Plugins may use the same syntax in their own Guice modules to register their own implementations. When a plugin starts, its registrations will be added to the DynamicSet, and when it stops, the references get cleaned up automatically. During a hot reload of a plugin references from the old plugin and the new plugin are matched up by collection member type and Guice annotation information, and atomically swapped. Plugins can use automatic registration if interfaces are annotated with @ExtensionPoint and the plugin implementation is annoated with @Listen and also implements the interface, directly or indirectly through its base classes or interfaces: (gerrit-extension-api) @ExtensionPoint public interface NewChangeListener { (gerrit-server) DynamicSet.setOf(binder(), NewChangeListener.class); (plugin or extension code) @Listen class OnNewChange implements NewChangeListener { Automatic registration binds the listeners into the system module, which may prevent plugins or extensions from automatically connecting with extension points inside of the HTTP or SSH servers. This shouldn't generally be a problem as the majority of interfaces plugins or extensions care about will be defined in the core server, and thus be in the system module. Change-Id: Ic8f371d97f8f0ddb6cad97fef3b58e1c3d32381f
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
// 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.annotations;
|
||||||
|
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import com.google.gerrit.extensions.registration.DynamicSet;
|
||||||
|
import com.google.inject.BindingAnnotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation for interfaces that accept auto-registered implementations.
|
||||||
|
* <p>
|
||||||
|
* Interfaces that accept automatically registered implementations into their
|
||||||
|
* {@link DynamicSet} must be tagged with this annotation.
|
||||||
|
* <p>
|
||||||
|
* Plugins or extensions that implement an {@code @ExtensionPoint} interface
|
||||||
|
* should use the {@link Listen} annotation to automatically register.
|
||||||
|
*
|
||||||
|
* @see Listen
|
||||||
|
*/
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@BindingAnnotation
|
||||||
|
public @interface ExtensionPoint {
|
||||||
|
}
|
@@ -0,0 +1,39 @@
|
|||||||
|
// 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.annotations;
|
||||||
|
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import com.google.inject.BindingAnnotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation for auto-registered extension point implementations.
|
||||||
|
* <p>
|
||||||
|
* Plugins or extensions using auto-registration should apply this annotation to
|
||||||
|
* any non-abstract class that implements an unnamed extension point, such as a
|
||||||
|
* notification listener. Gerrit will automatically determine which extension
|
||||||
|
* points to apply based on the interfaces the type implements.
|
||||||
|
*
|
||||||
|
* @see Export
|
||||||
|
*/
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@BindingAnnotation
|
||||||
|
public @interface Listen {
|
||||||
|
}
|
@@ -0,0 +1,155 @@
|
|||||||
|
// 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.inject.Binder;
|
||||||
|
import com.google.inject.Key;
|
||||||
|
import com.google.inject.Scopes;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
import com.google.inject.util.Types;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.SortedMap;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A map of members that can be modified as plugins reload.
|
||||||
|
* <p>
|
||||||
|
* Maps index their members by plugin name and export name.
|
||||||
|
* <p>
|
||||||
|
* DynamicMaps are always mapped as singletons in Guice, and only may contain
|
||||||
|
* singletons, as providers are resolved to an instance before the member is
|
||||||
|
* added to the map.
|
||||||
|
*/
|
||||||
|
public abstract class DynamicMap<T> {
|
||||||
|
/**
|
||||||
|
* Declare a singleton {@code DynamicMap<T>} with a binder.
|
||||||
|
* <p>
|
||||||
|
* Maps must be defined in a Guice module before they can be bound:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* DynamicMap.mapOf(binder(), Interface.class);
|
||||||
|
* bind(Interface.class)
|
||||||
|
* .annotatedWith(Exports.named("foo"))
|
||||||
|
* .to(Impl.class);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param member type of value in the map.
|
||||||
|
*/
|
||||||
|
public static <T> void mapOf(Binder binder, Class<T> member) {
|
||||||
|
mapOf(binder, TypeLiteral.get(member));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Declare a singleton {@code DynamicMap<T>} with a binder.
|
||||||
|
* <p>
|
||||||
|
* Maps must be defined in a Guice module before they can be bound:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* DynamicMap.mapOf(binder(), new TypeLiteral<Thing<Bar>>(){});
|
||||||
|
* bind(new TypeLiteral<Thing<Bar>>() {})
|
||||||
|
* .annotatedWith(Exports.named("foo"))
|
||||||
|
* .to(Impl.class);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param member type of value in the map.
|
||||||
|
*/
|
||||||
|
public static <T> void mapOf(Binder binder, TypeLiteral<T> member) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Key<DynamicMap<T>> key = (Key<DynamicMap<T>>) Key.get(
|
||||||
|
Types.newParameterizedType(DynamicMap.class, member.getType()));
|
||||||
|
binder.bind(key)
|
||||||
|
.toProvider(new DynamicMapProvider<T>(member))
|
||||||
|
.in(Scopes.SINGLETON);
|
||||||
|
}
|
||||||
|
|
||||||
|
final ConcurrentMap<NamePair, T> items;
|
||||||
|
|
||||||
|
DynamicMap() {
|
||||||
|
items = new ConcurrentHashMap<NamePair, T>(16, 0.75f, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lookup an implementation by name.
|
||||||
|
*
|
||||||
|
* @param pluginName local name of the plugin providing the item.
|
||||||
|
* @param exportName name the plugin exports the item as.
|
||||||
|
* @return the implementation. Null if the plugin is not running, or if the
|
||||||
|
* plugin does not export this name.
|
||||||
|
*/
|
||||||
|
public T get(String pluginName, String exportName) {
|
||||||
|
return items.get(new NamePair(pluginName, exportName));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the names of all running plugins supplying this type.
|
||||||
|
*
|
||||||
|
* @return sorted set of active plugins that supply at least one item.
|
||||||
|
*/
|
||||||
|
public SortedSet<String> plugins() {
|
||||||
|
SortedSet<String> r = new TreeSet<String>();
|
||||||
|
for (NamePair p : items.keySet()) {
|
||||||
|
r.add(p.pluginName);
|
||||||
|
}
|
||||||
|
return Collections.unmodifiableSortedSet(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the items exported by a single plugin.
|
||||||
|
*
|
||||||
|
* @param pluginName name of the plugin.
|
||||||
|
* @return items exported by a plugin, keyed by the export name.
|
||||||
|
*/
|
||||||
|
public SortedMap<String, T> byPlugin(String pluginName) {
|
||||||
|
SortedMap<String, T> r = new TreeMap<String, T>();
|
||||||
|
for (Map.Entry<NamePair, T> e : items.entrySet()) {
|
||||||
|
if (e.getKey().pluginName.equals(pluginName)) {
|
||||||
|
r.put(e.getKey().exportName, e.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Collections.unmodifiableSortedMap(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class NamePair {
|
||||||
|
private final String pluginName;
|
||||||
|
private final String exportName;
|
||||||
|
|
||||||
|
NamePair(String pn, String en) {
|
||||||
|
this.pluginName = pn;
|
||||||
|
this.exportName = en;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return pluginName.hashCode() * 31 + exportName.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (other instanceof NamePair) {
|
||||||
|
NamePair np = (NamePair) other;
|
||||||
|
return pluginName.equals(np) && exportName.equals(np);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,46 @@
|
|||||||
|
// 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.inject.Binding;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.Injector;
|
||||||
|
import com.google.inject.Provider;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class DynamicMapProvider<T> implements Provider<DynamicMap<T>> {
|
||||||
|
private final TypeLiteral<T> type;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private Injector injector;
|
||||||
|
|
||||||
|
DynamicMapProvider(TypeLiteral<T> type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DynamicMap<T> get() {
|
||||||
|
PrivateInternals_DynamicMapImpl<T> m =
|
||||||
|
new PrivateInternals_DynamicMapImpl<T>();
|
||||||
|
List<Binding<T>> bindings = injector.findBindingsByType(type);
|
||||||
|
if (bindings != null) {
|
||||||
|
for (Binding<T> b : bindings) {
|
||||||
|
m.put("gerrit", b.getKey(), b.getProvider().get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,231 @@
|
|||||||
|
// 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.inject.Binder;
|
||||||
|
import com.google.inject.Key;
|
||||||
|
import com.google.inject.Scopes;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
import com.google.inject.binder.LinkedBindingBuilder;
|
||||||
|
import com.google.inject.internal.UniqueAnnotations;
|
||||||
|
import com.google.inject.name.Named;
|
||||||
|
import com.google.inject.util.Types;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A set of members that can be modified as plugins reload.
|
||||||
|
* <p>
|
||||||
|
* DynamicSets are always mapped as singletons in Guice, and only may contain
|
||||||
|
* singletons, as providers are resolved to an instance before the member is
|
||||||
|
* added to the set.
|
||||||
|
*/
|
||||||
|
public class DynamicSet<T> implements Iterable<T> {
|
||||||
|
/**
|
||||||
|
* Declare a singleton {@code DynamicSet<T>} with a binder.
|
||||||
|
* <p>
|
||||||
|
* Sets must be defined in a Guice module before they can be bound:
|
||||||
|
* <pre>
|
||||||
|
* DynamicSet.setOf(binder(), Interface.class);
|
||||||
|
* DynamicSet.bind(binder(), Interface.class).to(Impl.class);
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param member type of entry in the set.
|
||||||
|
*/
|
||||||
|
public static <T> void setOf(Binder binder, Class<T> member) {
|
||||||
|
setOf(binder, TypeLiteral.get(member));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Declare a singleton {@code DynamicSet<T>} with a binder.
|
||||||
|
* <p>
|
||||||
|
* Sets must be defined in a Guice module before they can be bound:
|
||||||
|
* <pre>
|
||||||
|
* DynamicSet.setOf(binder(), new TypeLiteral<Thing<Foo>>() {});
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param member type of entry in the set.
|
||||||
|
*/
|
||||||
|
public static <T> void setOf(Binder binder, TypeLiteral<T> member) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Key<DynamicSet<T>> key = (Key<DynamicSet<T>>) Key.get(
|
||||||
|
Types.newParameterizedType(DynamicSet.class, member.getType()));
|
||||||
|
binder.bind(key)
|
||||||
|
.toProvider(new DynamicSetProvider<T>(member))
|
||||||
|
.in(Scopes.SINGLETON);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind one implementation into the set using a unique annotation.
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param type type of entries in the set.
|
||||||
|
* @return a binder to continue configuring the new set member.
|
||||||
|
*/
|
||||||
|
public static <T> LinkedBindingBuilder<T> bind(Binder binder, Class<T> type) {
|
||||||
|
return bind(binder, TypeLiteral.get(type));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind one implementation into the set using a unique annotation.
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param type type of entries in the set.
|
||||||
|
* @return a binder to continue configuring the new set member.
|
||||||
|
*/
|
||||||
|
public static <T> LinkedBindingBuilder<T> bind(Binder binder, TypeLiteral<T> type) {
|
||||||
|
return binder.bind(type).annotatedWith(UniqueAnnotations.create());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind a named implementation into the set.
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param type type of entries in the set.
|
||||||
|
* @param name {@code @Named} annotation to apply instead of a unique
|
||||||
|
* annotation.
|
||||||
|
* @return a binder to continue configuring the new set member.
|
||||||
|
*/
|
||||||
|
public static <T> LinkedBindingBuilder<T> bind(Binder binder,
|
||||||
|
Class<T> type,
|
||||||
|
Named name) {
|
||||||
|
return bind(binder, TypeLiteral.get(type));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind a named implementation into the set.
|
||||||
|
*
|
||||||
|
* @param binder a new binder created in the module.
|
||||||
|
* @param type type of entries in the set.
|
||||||
|
* @param name {@code @Named} annotation to apply instead of a unique
|
||||||
|
* annotation.
|
||||||
|
* @return a binder to continue configuring the new set member.
|
||||||
|
*/
|
||||||
|
public static <T> LinkedBindingBuilder<T> bind(Binder binder,
|
||||||
|
TypeLiteral<T> type,
|
||||||
|
Named name) {
|
||||||
|
return binder.bind(type).annotatedWith(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final CopyOnWriteArrayList<AtomicReference<T>> items;
|
||||||
|
|
||||||
|
DynamicSet(Collection<AtomicReference<T>> base) {
|
||||||
|
items = new CopyOnWriteArrayList<AtomicReference<T>>(base);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
final Iterator<AtomicReference<T>> itr = items.iterator();
|
||||||
|
return new Iterator<T>() {
|
||||||
|
private T next;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
while (next == null && itr.hasNext()) {
|
||||||
|
next = itr.next().get();
|
||||||
|
}
|
||||||
|
return next != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T next() {
|
||||||
|
if (hasNext()) {
|
||||||
|
T result = next;
|
||||||
|
next = null;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add one new element to the set.
|
||||||
|
*
|
||||||
|
* @param item the item to add to the collection. Must not be null.
|
||||||
|
* @return handle to remove the item at a later point in time.
|
||||||
|
*/
|
||||||
|
public RegistrationHandle add(final T item) {
|
||||||
|
final AtomicReference<T> ref = new AtomicReference<T>(item);
|
||||||
|
items.add(ref);
|
||||||
|
return new RegistrationHandle() {
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
if (ref.compareAndSet(item, null)) {
|
||||||
|
items.remove(ref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add one new element that may be hot-replaceable in the future.
|
||||||
|
*
|
||||||
|
* @param key unique description from the item's Guice binding. This can be
|
||||||
|
* later obtained from the registration handle to facilitate matching
|
||||||
|
* with the new equivalent instance during a hot reload.
|
||||||
|
* @param item the item to add to the collection right now. Must not be null.
|
||||||
|
* @return a handle that can remove this item later, or hot-swap the item
|
||||||
|
* without it ever leaving the collection.
|
||||||
|
*/
|
||||||
|
public ReloadableRegistrationHandle<T> add(Key<T> key, T item) {
|
||||||
|
AtomicReference<T> ref = new AtomicReference<T>(item);
|
||||||
|
items.add(ref);
|
||||||
|
return new ReloadableHandle(ref, key, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ReloadableHandle implements ReloadableRegistrationHandle<T> {
|
||||||
|
private final AtomicReference<T> ref;
|
||||||
|
private final Key<T> key;
|
||||||
|
private final T item;
|
||||||
|
|
||||||
|
ReloadableHandle(AtomicReference<T> ref, Key<T> key, T item) {
|
||||||
|
this.ref = ref;
|
||||||
|
this.key = key;
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
if (ref.compareAndSet(item, null)) {
|
||||||
|
items.remove(ref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Key<T> getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReloadableHandle replace(Key<T> newKey, T newItem) {
|
||||||
|
if (ref.compareAndSet(item, newItem)) {
|
||||||
|
return new ReloadableHandle(ref, newKey, newItem);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,56 @@
|
|||||||
|
// 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.inject.Binding;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.Injector;
|
||||||
|
import com.google.inject.Provider;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
class DynamicSetProvider<T> implements Provider<DynamicSet<T>> {
|
||||||
|
private final TypeLiteral<T> type;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private Injector injector;
|
||||||
|
|
||||||
|
DynamicSetProvider(TypeLiteral<T> type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DynamicSet<T> get() {
|
||||||
|
return new DynamicSet<T>(find(injector, type));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> List<AtomicReference<T>> find(
|
||||||
|
Injector src,
|
||||||
|
TypeLiteral<T> type) {
|
||||||
|
List<Binding<T>> bindings = src.findBindingsByType(type);
|
||||||
|
int cnt = bindings != null ? bindings.size() : 0;
|
||||||
|
if (cnt == 0) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
List<AtomicReference<T>> r = new ArrayList<AtomicReference<T>>(cnt);
|
||||||
|
for (Binding<T> b : bindings) {
|
||||||
|
r.add(new AtomicReference<T>(b.getProvider().get()));
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,96 @@
|
|||||||
|
// 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.annotations.Export;
|
||||||
|
import com.google.inject.Key;
|
||||||
|
|
||||||
|
/** <b>DO NOT USE</b> */
|
||||||
|
public class PrivateInternals_DynamicMapImpl<T> extends DynamicMap<T> {
|
||||||
|
PrivateInternals_DynamicMapImpl() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store one new element into the map.
|
||||||
|
*
|
||||||
|
* @param pluginName unique name of the plugin providing the export.
|
||||||
|
* @param exportName name the plugin has exported the item as.
|
||||||
|
* @param item the item to add to the collection. Must not be null.
|
||||||
|
* @return handle to remove the item at a later point in time.
|
||||||
|
*/
|
||||||
|
public RegistrationHandle put(
|
||||||
|
String pluginName, String exportName,
|
||||||
|
final T item) {
|
||||||
|
final NamePair key = new NamePair(pluginName, exportName);
|
||||||
|
items.put(key, item);
|
||||||
|
return new RegistrationHandle() {
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
items.remove(key, item);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store one new element that may be hot-replaceable in the future.
|
||||||
|
*
|
||||||
|
* @param pluginName unique name of the plugin providing the export.
|
||||||
|
* @param key unique description from the item's Guice binding. This can be
|
||||||
|
* later obtained from the registration handle to facilitate matching
|
||||||
|
* with the new equivalent instance during a hot reload. The key must
|
||||||
|
* use an {@link @Export} annotation.
|
||||||
|
* @param item the item to add to the collection right now. Must not be null.
|
||||||
|
* @return a handle that can remove this item later, or hot-swap the item
|
||||||
|
* without it ever leaving the collection.
|
||||||
|
*/
|
||||||
|
public ReloadableRegistrationHandle<T> put(
|
||||||
|
String pluginName, Key<T> key,
|
||||||
|
T item) {
|
||||||
|
String exportName = ((Export) key.getAnnotation()).value();
|
||||||
|
NamePair np = new NamePair(pluginName, exportName);
|
||||||
|
items.put(np, item);
|
||||||
|
return new ReloadableHandle(np, key, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ReloadableHandle implements ReloadableRegistrationHandle<T> {
|
||||||
|
private final NamePair np;
|
||||||
|
private final Key<T> key;
|
||||||
|
private final T item;
|
||||||
|
|
||||||
|
ReloadableHandle(NamePair np, Key<T> key, T item) {
|
||||||
|
this.np = np;
|
||||||
|
this.key = key;
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
items.remove(np, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Key<T> getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReloadableHandle replace(Key<T> newKey, T newItem) {
|
||||||
|
if (items.replace(np, item, newItem)) {
|
||||||
|
return new ReloadableHandle(np, newKey, newItem);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,23 @@
|
|||||||
|
// 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.inject.Key;
|
||||||
|
|
||||||
|
public interface ReloadableRegistrationHandle<T> extends RegistrationHandle {
|
||||||
|
public Key<T> getKey();
|
||||||
|
|
||||||
|
public RegistrationHandle replace(Key<T> key, T item);
|
||||||
|
}
|
@@ -16,8 +16,16 @@ package com.google.gerrit.server.plugins;
|
|||||||
|
|
||||||
import static com.google.gerrit.server.plugins.PluginGuiceEnvironment.is;
|
import static com.google.gerrit.server.plugins.PluginGuiceEnvironment.is;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
import com.google.gerrit.extensions.annotations.Export;
|
import com.google.gerrit.extensions.annotations.Export;
|
||||||
|
import com.google.gerrit.extensions.annotations.ExtensionPoint;
|
||||||
|
import com.google.gerrit.extensions.annotations.Listen;
|
||||||
|
import com.google.inject.AbstractModule;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
|
import com.google.inject.Scopes;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
import com.google.inject.internal.UniqueAnnotations;
|
||||||
|
|
||||||
import org.eclipse.jgit.util.IO;
|
import org.eclipse.jgit.util.IO;
|
||||||
import org.objectweb.asm.AnnotationVisitor;
|
import org.objectweb.asm.AnnotationVisitor;
|
||||||
@@ -31,7 +39,11 @@ import org.objectweb.asm.Type;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
@@ -39,11 +51,15 @@ class AutoRegisterModules {
|
|||||||
private static final int SKIP_ALL = ClassReader.SKIP_CODE
|
private static final int SKIP_ALL = ClassReader.SKIP_CODE
|
||||||
| ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
|
| ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
|
||||||
private final String pluginName;
|
private final String pluginName;
|
||||||
|
private final PluginGuiceEnvironment env;
|
||||||
private final JarFile jarFile;
|
private final JarFile jarFile;
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
private final ModuleGenerator sshGen;
|
private final ModuleGenerator sshGen;
|
||||||
private final ModuleGenerator httpGen;
|
private final ModuleGenerator httpGen;
|
||||||
|
|
||||||
|
private Set<Class<?>> sysSingletons;
|
||||||
|
private Map<TypeLiteral<?>, Class<?>> sysListen;
|
||||||
|
|
||||||
Module sysModule;
|
Module sysModule;
|
||||||
Module sshModule;
|
Module sshModule;
|
||||||
Module httpModule;
|
Module httpModule;
|
||||||
@@ -53,6 +69,7 @@ class AutoRegisterModules {
|
|||||||
JarFile jarFile,
|
JarFile jarFile,
|
||||||
ClassLoader classLoader) {
|
ClassLoader classLoader) {
|
||||||
this.pluginName = pluginName;
|
this.pluginName = pluginName;
|
||||||
|
this.env = env;
|
||||||
this.jarFile = jarFile;
|
this.jarFile = jarFile;
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
this.sshGen = env.hasSshModule() ? env.newSshModuleGenerator() : null;
|
this.sshGen = env.hasSshModule() ? env.newSshModuleGenerator() : null;
|
||||||
@@ -60,6 +77,9 @@ class AutoRegisterModules {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AutoRegisterModules discover() throws InvalidPluginException {
|
AutoRegisterModules discover() throws InvalidPluginException {
|
||||||
|
sysSingletons = Sets.newHashSet();
|
||||||
|
sysListen = Maps.newHashMap();
|
||||||
|
|
||||||
if (sshGen != null) {
|
if (sshGen != null) {
|
||||||
sshGen.setPluginName(pluginName);
|
sshGen.setPluginName(pluginName);
|
||||||
}
|
}
|
||||||
@@ -69,6 +89,9 @@ class AutoRegisterModules {
|
|||||||
|
|
||||||
scan();
|
scan();
|
||||||
|
|
||||||
|
if (!sysSingletons.isEmpty() || !sysListen.isEmpty()) {
|
||||||
|
sysModule = makeSystemModule();
|
||||||
|
}
|
||||||
if (sshGen != null) {
|
if (sshGen != null) {
|
||||||
sshModule = sshGen.create();
|
sshModule = sshGen.create();
|
||||||
}
|
}
|
||||||
@@ -78,6 +101,36 @@ class AutoRegisterModules {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Module makeSystemModule() {
|
||||||
|
return new AbstractModule() {
|
||||||
|
@Override
|
||||||
|
protected void configure() {
|
||||||
|
for (Class<?> clazz : sysSingletons) {
|
||||||
|
bind(clazz).in(Scopes.SINGLETON);
|
||||||
|
}
|
||||||
|
for (Map.Entry<TypeLiteral<?>, Class<?>> e : sysListen.entrySet()) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Class<Object> impl = (Class<Object>) e.getValue();
|
||||||
|
|
||||||
|
Annotation n = impl.getAnnotation(Export.class);
|
||||||
|
if (n == null) {
|
||||||
|
n = impl.getAnnotation(javax.inject.Named.class);
|
||||||
|
}
|
||||||
|
if (n == null) {
|
||||||
|
n = impl.getAnnotation(com.google.inject.name.Named.class);
|
||||||
|
}
|
||||||
|
if (n == null) {
|
||||||
|
n = UniqueAnnotations.create();
|
||||||
|
}
|
||||||
|
bind(type).annotatedWith(n).to(impl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private void scan() throws InvalidPluginException {
|
private void scan() throws InvalidPluginException {
|
||||||
Enumeration<JarEntry> e = jarFile.entries();
|
Enumeration<JarEntry> e = jarFile.entries();
|
||||||
while (e.hasMoreElements()) {
|
while (e.hasMoreElements()) {
|
||||||
@@ -103,7 +156,15 @@ class AutoRegisterModules {
|
|||||||
export(def);
|
export(def);
|
||||||
} else {
|
} else {
|
||||||
PluginLoader.log.warn(String.format(
|
PluginLoader.log.warn(String.format(
|
||||||
"Plugin %s tries to export abstract class %s",
|
"Plugin %s tries to @Export(\"%s\") abstract class %s",
|
||||||
|
pluginName, def.exportedAsName, def.className));
|
||||||
|
}
|
||||||
|
} else if (def.listen) {
|
||||||
|
if (def.isConcrete()) {
|
||||||
|
listen(def);
|
||||||
|
} else {
|
||||||
|
PluginLoader.log.warn(String.format(
|
||||||
|
"Plugin %s tries to @Listen abstract class %s",
|
||||||
pluginName, def.className));
|
pluginName, def.className));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,11 +196,81 @@ class AutoRegisterModules {
|
|||||||
} else if (is("javax.servlet.http.HttpServlet", clazz)) {
|
} else if (is("javax.servlet.http.HttpServlet", clazz)) {
|
||||||
if (httpGen != null) {
|
if (httpGen != null) {
|
||||||
httpGen.export(export, clazz);
|
httpGen.export(export, clazz);
|
||||||
|
listen(clazz, clazz);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
int cnt = sysListen.size();
|
||||||
|
listen(clazz, clazz);
|
||||||
|
if (cnt == sysListen.size()) {
|
||||||
|
// If no bindings were recorded, the extension isn't recognized.
|
||||||
|
throw new InvalidPluginException(String.format(
|
||||||
|
"Class %s with @Export(\"%s\") not supported",
|
||||||
|
clazz.getName(), export.value()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void listen(ClassData def) throws InvalidPluginException {
|
||||||
|
Class<?> clazz;
|
||||||
|
try {
|
||||||
|
clazz = Class.forName(def.className, false, classLoader);
|
||||||
|
} catch (ClassNotFoundException err) {
|
||||||
throw new InvalidPluginException(String.format(
|
throw new InvalidPluginException(String.format(
|
||||||
"Class %s with @Export(\"%s\") not supported",
|
"Cannot load %s with @Listen",
|
||||||
clazz.getName(), export.value()));
|
def.className), err);
|
||||||
|
}
|
||||||
|
|
||||||
|
Listen listen = clazz.getAnnotation(Listen.class);
|
||||||
|
if (listen != null) {
|
||||||
|
listen(clazz, clazz);
|
||||||
|
} else {
|
||||||
|
PluginLoader.log.warn(String.format(
|
||||||
|
"In plugin %s asm incorrectly parsed %s with @Listen",
|
||||||
|
pluginName, clazz.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void listen(java.lang.reflect.Type type, Class<?> clazz)
|
||||||
|
throws InvalidPluginException {
|
||||||
|
while (type != null) {
|
||||||
|
Class<?> rawType;
|
||||||
|
if (type instanceof ParameterizedType) {
|
||||||
|
rawType = (Class<?>) ((ParameterizedType) type).getRawType();
|
||||||
|
} else if (type instanceof Class) {
|
||||||
|
rawType = (Class<?>) type;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rawType.getAnnotation(ExtensionPoint.class) != null) {
|
||||||
|
TypeLiteral<?> tl = TypeLiteral.get(type);
|
||||||
|
if (env.hasDynamicSet(tl)) {
|
||||||
|
sysSingletons.add(clazz);
|
||||||
|
sysListen.put(tl, clazz);
|
||||||
|
} else if (env.hasDynamicMap(tl)) {
|
||||||
|
if (clazz.getAnnotation(Export.class) == null) {
|
||||||
|
throw new InvalidPluginException(String.format(
|
||||||
|
"Class %s requires @Export(\"name\") annotation for %s",
|
||||||
|
clazz.getName(), rawType.getName()));
|
||||||
|
}
|
||||||
|
sysSingletons.add(clazz);
|
||||||
|
sysListen.put(tl, clazz);
|
||||||
|
} else {
|
||||||
|
throw new InvalidPluginException(String.format(
|
||||||
|
"Cannot register %s, server does not accept %s",
|
||||||
|
clazz.getName(), rawType.getName()));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
java.lang.reflect.Type[] interfaces = rawType.getGenericInterfaces();
|
||||||
|
if (interfaces != null) {
|
||||||
|
for (java.lang.reflect.Type i : interfaces) {
|
||||||
|
listen(i, clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type = rawType.getGenericSuperclass();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,9 +300,12 @@ class AutoRegisterModules {
|
|||||||
|
|
||||||
private static class ClassData implements ClassVisitor {
|
private static class ClassData implements ClassVisitor {
|
||||||
private static final String EXPORT = Type.getType(Export.class).getDescriptor();
|
private static final String EXPORT = Type.getType(Export.class).getDescriptor();
|
||||||
|
private static final String LISTEN = Type.getType(Listen.class).getDescriptor();
|
||||||
|
|
||||||
String className;
|
String className;
|
||||||
int access;
|
int access;
|
||||||
String exportedAsName;
|
String exportedAsName;
|
||||||
|
boolean listen;
|
||||||
|
|
||||||
boolean isConcrete() {
|
boolean isConcrete() {
|
||||||
return (access & Opcodes.ACC_ABSTRACT) == 0
|
return (access & Opcodes.ACC_ABSTRACT) == 0
|
||||||
@@ -195,6 +329,10 @@ class AutoRegisterModules {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (visible && LISTEN.equals(desc)) {
|
||||||
|
listen = true;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -15,8 +15,10 @@
|
|||||||
package com.google.gerrit.server.plugins;
|
package com.google.gerrit.server.plugins;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import com.google.gerrit.extensions.annotations.PluginName;
|
import com.google.gerrit.extensions.annotations.PluginName;
|
||||||
import com.google.gerrit.extensions.registration.RegistrationHandle;
|
import com.google.gerrit.extensions.registration.RegistrationHandle;
|
||||||
|
import com.google.gerrit.extensions.registration.ReloadableRegistrationHandle;
|
||||||
import com.google.gerrit.lifecycle.LifecycleListener;
|
import com.google.gerrit.lifecycle.LifecycleListener;
|
||||||
import com.google.gerrit.lifecycle.LifecycleManager;
|
import com.google.gerrit.lifecycle.LifecycleManager;
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
@@ -27,6 +29,8 @@ import com.google.inject.Module;
|
|||||||
import org.eclipse.jgit.storage.file.FileSnapshot;
|
import org.eclipse.jgit.storage.file.FileSnapshot;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.jar.Attributes;
|
import java.util.jar.Attributes;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
import java.util.jar.Manifest;
|
import java.util.jar.Manifest;
|
||||||
@@ -55,6 +59,7 @@ public class Plugin {
|
|||||||
private Injector sshInjector;
|
private Injector sshInjector;
|
||||||
private Injector httpInjector;
|
private Injector httpInjector;
|
||||||
private LifecycleManager manager;
|
private LifecycleManager manager;
|
||||||
|
private List<ReloadableRegistrationHandle<?>> reloadableHandles;
|
||||||
|
|
||||||
public Plugin(String name,
|
public Plugin(String name,
|
||||||
File srcJar,
|
File srcJar,
|
||||||
@@ -186,6 +191,10 @@ public class Plugin {
|
|||||||
return jarFile;
|
return jarFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Injector getSysInjector() {
|
||||||
|
return sysInjector;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Injector getSshInjector() {
|
public Injector getSshInjector() {
|
||||||
return sshInjector;
|
return sshInjector;
|
||||||
@@ -197,6 +206,13 @@ public class Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void add(final RegistrationHandle handle) {
|
public void add(final RegistrationHandle handle) {
|
||||||
|
if (handle instanceof ReloadableRegistrationHandle) {
|
||||||
|
if (reloadableHandles == null) {
|
||||||
|
reloadableHandles = Lists.newArrayList();
|
||||||
|
}
|
||||||
|
reloadableHandles.add((ReloadableRegistrationHandle<?>) handle);
|
||||||
|
}
|
||||||
|
|
||||||
add(new LifecycleListener() {
|
add(new LifecycleListener() {
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
@@ -213,6 +229,13 @@ public class Plugin {
|
|||||||
manager.add(listener);
|
manager.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<ReloadableRegistrationHandle<?>> getReloadableHandles() {
|
||||||
|
if (reloadableHandles != null) {
|
||||||
|
return reloadableHandles;
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Plugin [" + name + "]";
|
return "Plugin [" + name + "]";
|
||||||
|
@@ -14,8 +14,16 @@
|
|||||||
|
|
||||||
package com.google.gerrit.server.plugins;
|
package com.google.gerrit.server.plugins;
|
||||||
|
|
||||||
|
import com.google.common.collect.LinkedListMultimap;
|
||||||
|
import com.google.common.collect.ListMultimap;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
import com.google.gerrit.extensions.registration.DynamicMap;
|
||||||
|
import com.google.gerrit.extensions.registration.DynamicSet;
|
||||||
|
import com.google.gerrit.extensions.registration.PrivateInternals_DynamicMapImpl;
|
||||||
|
import com.google.gerrit.extensions.registration.RegistrationHandle;
|
||||||
|
import com.google.gerrit.extensions.registration.ReloadableRegistrationHandle;
|
||||||
import com.google.gerrit.lifecycle.LifecycleListener;
|
import com.google.gerrit.lifecycle.LifecycleListener;
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
import com.google.inject.Binding;
|
import com.google.inject.Binding;
|
||||||
@@ -25,11 +33,17 @@ import com.google.inject.Module;
|
|||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
|
import com.google.inject.internal.UniqueAnnotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,6 +59,7 @@ public class PluginGuiceEnvironment {
|
|||||||
private final CopyConfigModule copyConfigModule;
|
private final CopyConfigModule copyConfigModule;
|
||||||
private final List<StartPluginListener> onStart;
|
private final List<StartPluginListener> onStart;
|
||||||
private final List<ReloadPluginListener> onReload;
|
private final List<ReloadPluginListener> onReload;
|
||||||
|
|
||||||
private Module sysModule;
|
private Module sysModule;
|
||||||
private Module sshModule;
|
private Module sshModule;
|
||||||
private Module httpModule;
|
private Module httpModule;
|
||||||
@@ -52,6 +67,14 @@ public class PluginGuiceEnvironment {
|
|||||||
private Provider<ModuleGenerator> sshGen;
|
private Provider<ModuleGenerator> sshGen;
|
||||||
private Provider<ModuleGenerator> httpGen;
|
private Provider<ModuleGenerator> httpGen;
|
||||||
|
|
||||||
|
private Map<TypeLiteral<?>, DynamicSet<?>> sysSets;
|
||||||
|
private Map<TypeLiteral<?>, DynamicSet<?>> sshSets;
|
||||||
|
private Map<TypeLiteral<?>, DynamicSet<?>> httpSets;
|
||||||
|
|
||||||
|
private Map<TypeLiteral<?>, DynamicMap<?>> sysMaps;
|
||||||
|
private Map<TypeLiteral<?>, DynamicMap<?>> sshMaps;
|
||||||
|
private Map<TypeLiteral<?>, DynamicMap<?>> httpMaps;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PluginGuiceEnvironment(Injector sysInjector, CopyConfigModule ccm) {
|
PluginGuiceEnvironment(Injector sysInjector, CopyConfigModule ccm) {
|
||||||
this.sysInjector = sysInjector;
|
this.sysInjector = sysInjector;
|
||||||
@@ -62,6 +85,21 @@ public class PluginGuiceEnvironment {
|
|||||||
|
|
||||||
onReload = new CopyOnWriteArrayList<ReloadPluginListener>();
|
onReload = new CopyOnWriteArrayList<ReloadPluginListener>();
|
||||||
onReload.addAll(listeners(sysInjector, ReloadPluginListener.class));
|
onReload.addAll(listeners(sysInjector, ReloadPluginListener.class));
|
||||||
|
|
||||||
|
sysSets = dynamicSetsOf(sysInjector);
|
||||||
|
sysMaps = dynamicMapsOf(sysInjector);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean hasDynamicSet(TypeLiteral<?> type) {
|
||||||
|
return sysSets.containsKey(type)
|
||||||
|
|| (sshSets != null && sshSets.containsKey(type))
|
||||||
|
|| (httpSets != null && httpSets.containsKey(type));
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean hasDynamicMap(TypeLiteral<?> type) {
|
||||||
|
return sysMaps.containsKey(type)
|
||||||
|
|| (sshMaps != null && sshMaps.containsKey(type))
|
||||||
|
|| (httpMaps != null && httpMaps.containsKey(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
Module getSysModule() {
|
Module getSysModule() {
|
||||||
@@ -84,6 +122,8 @@ public class PluginGuiceEnvironment {
|
|||||||
public void setSshInjector(Injector injector) {
|
public void setSshInjector(Injector injector) {
|
||||||
sshModule = copy(injector);
|
sshModule = copy(injector);
|
||||||
sshGen = injector.getProvider(ModuleGenerator.class);
|
sshGen = injector.getProvider(ModuleGenerator.class);
|
||||||
|
sshSets = dynamicSetsOf(injector);
|
||||||
|
sshMaps = dynamicMapsOf(injector);
|
||||||
onStart.addAll(listeners(injector, StartPluginListener.class));
|
onStart.addAll(listeners(injector, StartPluginListener.class));
|
||||||
onReload.addAll(listeners(injector, ReloadPluginListener.class));
|
onReload.addAll(listeners(injector, ReloadPluginListener.class));
|
||||||
}
|
}
|
||||||
@@ -103,6 +143,8 @@ public class PluginGuiceEnvironment {
|
|||||||
public void setHttpInjector(Injector injector) {
|
public void setHttpInjector(Injector injector) {
|
||||||
httpModule = copy(injector);
|
httpModule = copy(injector);
|
||||||
httpGen = injector.getProvider(ModuleGenerator.class);
|
httpGen = injector.getProvider(ModuleGenerator.class);
|
||||||
|
httpSets = dynamicSetsOf(injector);
|
||||||
|
httpMaps = dynamicMapsOf(injector);
|
||||||
onStart.addAll(listeners(injector, StartPluginListener.class));
|
onStart.addAll(listeners(injector, StartPluginListener.class));
|
||||||
onReload.addAll(listeners(injector, ReloadPluginListener.class));
|
onReload.addAll(listeners(injector, ReloadPluginListener.class));
|
||||||
}
|
}
|
||||||
@@ -123,27 +165,257 @@ public class PluginGuiceEnvironment {
|
|||||||
for (StartPluginListener l : onStart) {
|
for (StartPluginListener l : onStart) {
|
||||||
l.onStartPlugin(plugin);
|
l.onStartPlugin(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attachSet(sysSets, plugin.getSysInjector(), plugin);
|
||||||
|
attachSet(sshSets, plugin.getSshInjector(), plugin);
|
||||||
|
attachSet(httpSets, plugin.getHttpInjector(), plugin);
|
||||||
|
|
||||||
|
attachMap(sysMaps, plugin.getSysInjector(), plugin);
|
||||||
|
attachMap(sshMaps, plugin.getSshInjector(), plugin);
|
||||||
|
attachMap(httpMaps, plugin.getHttpInjector(), plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void attachSet(Map<TypeLiteral<?>, DynamicSet<?>> sets,
|
||||||
|
@Nullable Injector src,
|
||||||
|
Plugin plugin) {
|
||||||
|
if (src != null && sets != null && !sets.isEmpty()) {
|
||||||
|
for (Map.Entry<TypeLiteral<?>, DynamicSet<?>> e : sets.entrySet()) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
DynamicSet<Object> set = (DynamicSet<Object>) e.getValue();
|
||||||
|
|
||||||
|
for (Binding<Object> b : bindings(src, type)) {
|
||||||
|
plugin.add(set.add(b.getKey(), b.getProvider().get()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void attachMap(Map<TypeLiteral<?>, DynamicMap<?>> maps,
|
||||||
|
@Nullable Injector src,
|
||||||
|
Plugin plugin) {
|
||||||
|
if (src != null && maps != null && !maps.isEmpty()) {
|
||||||
|
for (Map.Entry<TypeLiteral<?>, DynamicMap<?>> e : maps.entrySet()) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
PrivateInternals_DynamicMapImpl<Object> set =
|
||||||
|
(PrivateInternals_DynamicMapImpl<Object>) e.getValue();
|
||||||
|
|
||||||
|
for (Binding<Object> b : bindings(src, type)) {
|
||||||
|
plugin.add(set.put(
|
||||||
|
plugin.getName(),
|
||||||
|
b.getKey(),
|
||||||
|
b.getProvider().get()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onReloadPlugin(Plugin oldPlugin, Plugin newPlugin) {
|
void onReloadPlugin(Plugin oldPlugin, Plugin newPlugin) {
|
||||||
for (ReloadPluginListener l : onReload) {
|
for (ReloadPluginListener l : onReload) {
|
||||||
l.onReloadPlugin(oldPlugin, newPlugin);
|
l.onReloadPlugin(oldPlugin, newPlugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Index all old registrations by the raw type. These may be replaced
|
||||||
|
// during the reattach calls below. Any that are not replaced will be
|
||||||
|
// removed when the old plugin does its stop routine.
|
||||||
|
ListMultimap<TypeLiteral<?>, ReloadableRegistrationHandle<?>> old =
|
||||||
|
LinkedListMultimap.create();
|
||||||
|
for (ReloadableRegistrationHandle<?> h : oldPlugin.getReloadableHandles()) {
|
||||||
|
old.put(h.getKey().getTypeLiteral(), h);
|
||||||
|
}
|
||||||
|
|
||||||
|
reattachMap(old, sysMaps, newPlugin.getSysInjector(), newPlugin);
|
||||||
|
reattachMap(old, sshMaps, newPlugin.getSshInjector(), newPlugin);
|
||||||
|
reattachMap(old, httpMaps, newPlugin.getHttpInjector(), newPlugin);
|
||||||
|
|
||||||
|
reattachSet(old, sysSets, newPlugin.getSysInjector(), newPlugin);
|
||||||
|
reattachSet(old, sshSets, newPlugin.getSshInjector(), newPlugin);
|
||||||
|
reattachSet(old, httpSets, newPlugin.getHttpInjector(), newPlugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> List<T> listeners(Injector src, Class<T> type) {
|
private void reattachMap(
|
||||||
List<Binding<T>> bindings = src.findBindingsByType(TypeLiteral.get(type));
|
ListMultimap<TypeLiteral<?>, ReloadableRegistrationHandle<?>> oldHandles,
|
||||||
List<T> found = Lists.newArrayListWithCapacity(bindings.size());
|
Map<TypeLiteral<?>, DynamicMap<?>> maps,
|
||||||
for (Binding<T> b : bindings) {
|
@Nullable Injector src,
|
||||||
found.add(b.getProvider().get());
|
Plugin newPlugin) {
|
||||||
|
if (src == null || maps == null || maps.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<TypeLiteral<?>, DynamicMap<?>> e : maps.entrySet()) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
PrivateInternals_DynamicMapImpl<Object> map =
|
||||||
|
(PrivateInternals_DynamicMapImpl<Object>) e.getValue();
|
||||||
|
|
||||||
|
Map<Annotation, ReloadableRegistrationHandle<?>> am = Maps.newHashMap();
|
||||||
|
for (ReloadableRegistrationHandle<?> h : oldHandles.get(type)) {
|
||||||
|
Annotation a = h.getKey().getAnnotation();
|
||||||
|
if (a != null && !UNIQUE_ANNOTATION.isInstance(a)) {
|
||||||
|
am.put(a, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Binding<?> binding : bindings(src, e.getKey())) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Binding<Object> b = (Binding<Object>) binding;
|
||||||
|
Key<Object> key = b.getKey();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
ReloadableRegistrationHandle<Object> h =
|
||||||
|
(ReloadableRegistrationHandle<Object>) am.remove(key.getAnnotation());
|
||||||
|
if (h != null) {
|
||||||
|
replace(newPlugin, h, b);
|
||||||
|
oldHandles.remove(type, h);
|
||||||
|
} else {
|
||||||
|
newPlugin.add(map.put(
|
||||||
|
newPlugin.getName(),
|
||||||
|
b.getKey(),
|
||||||
|
b.getProvider().get()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Type used to declare unique annotations. Guice hides this, so extract it. */
|
||||||
|
private static final Class<?> UNIQUE_ANNOTATION =
|
||||||
|
UniqueAnnotations.create().getClass();
|
||||||
|
|
||||||
|
private void reattachSet(
|
||||||
|
ListMultimap<TypeLiteral<?>, ReloadableRegistrationHandle<?>> oldHandles,
|
||||||
|
Map<TypeLiteral<?>, DynamicSet<?>> sets,
|
||||||
|
@Nullable Injector src,
|
||||||
|
Plugin newPlugin) {
|
||||||
|
if (src == null || sets == null || sets.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<TypeLiteral<?>, DynamicSet<?>> e : sets.entrySet()) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
DynamicSet<Object> set = (DynamicSet<Object>) e.getValue();
|
||||||
|
|
||||||
|
// Index all old handles that match this DynamicSet<T> keyed by
|
||||||
|
// annotations. Ignore the unique annotations, thereby favoring
|
||||||
|
// the @Named annotations or some other non-unique naming.
|
||||||
|
Map<Annotation, ReloadableRegistrationHandle<?>> am = Maps.newHashMap();
|
||||||
|
List<ReloadableRegistrationHandle<?>> old = oldHandles.get(type);
|
||||||
|
Iterator<ReloadableRegistrationHandle<?>> oi = old.iterator();
|
||||||
|
while (oi.hasNext()) {
|
||||||
|
ReloadableRegistrationHandle<?> h = oi.next();
|
||||||
|
Annotation a = h.getKey().getAnnotation();
|
||||||
|
if (a != null && !UNIQUE_ANNOTATION.isInstance(a)) {
|
||||||
|
am.put(a, h);
|
||||||
|
oi.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace old handles with new bindings, favoring cases where there
|
||||||
|
// is an exact match on an @Named annotation. If there is no match
|
||||||
|
// pick any handle and replace it. We generally expect only one
|
||||||
|
// handle of each DynamicSet type when using unique annotations, but
|
||||||
|
// possibly multiple ones if @Named was used. Plugin authors that want
|
||||||
|
// atomic replacement across reloads should use @Named annotations with
|
||||||
|
// stable names that do not change across plugin versions to ensure the
|
||||||
|
// handles are swapped correctly.
|
||||||
|
oi = old.iterator();
|
||||||
|
for (Binding<?> binding : bindings(src, type)) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Binding<Object> b = (Binding<Object>) binding;
|
||||||
|
Key<Object> key = b.getKey();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
ReloadableRegistrationHandle<Object> h1 =
|
||||||
|
(ReloadableRegistrationHandle<Object>) am.remove(key.getAnnotation());
|
||||||
|
if (h1 != null) {
|
||||||
|
replace(newPlugin, h1, b);
|
||||||
|
} else if (oi.hasNext()) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
ReloadableRegistrationHandle<Object> h2 =
|
||||||
|
(ReloadableRegistrationHandle<Object>) oi.next();
|
||||||
|
oi.remove();
|
||||||
|
replace(newPlugin, h2, b);
|
||||||
|
} else {
|
||||||
|
newPlugin.add(set.add(b.getKey(), b.getProvider().get()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void replace(Plugin newPlugin,
|
||||||
|
ReloadableRegistrationHandle<T> h, Binding<T> b) {
|
||||||
|
RegistrationHandle n = h.replace(b.getKey(), b.getProvider().get());
|
||||||
|
if (n != null){
|
||||||
|
newPlugin.add(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static <T> List<T> listeners(Injector src, Class<T> type) {
|
||||||
|
List<Binding<T>> bindings = bindings(src, TypeLiteral.get(type));
|
||||||
|
int cnt = bindings != null ? bindings.size() : 0;
|
||||||
|
List<T> found = Lists.newArrayListWithCapacity(cnt);
|
||||||
|
if (bindings != null) {
|
||||||
|
for (Binding<T> b : bindings) {
|
||||||
|
found.add(b.getProvider().get());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static <T> List<Binding<T>> bindings(Injector src, TypeLiteral<T> type) {
|
||||||
|
return src.findBindingsByType(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<TypeLiteral<?>, DynamicSet<?>> dynamicSetsOf(Injector src) {
|
||||||
|
Map<TypeLiteral<?>, DynamicSet<?>> m = Maps.newHashMap();
|
||||||
|
for (Map.Entry<Key<?>, 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<TypeLiteral<?>, DynamicMap<?>> dynamicMapsOf(Injector src) {
|
||||||
|
Map<TypeLiteral<?>, DynamicMap<?>> m = Maps.newHashMap();
|
||||||
|
for (Map.Entry<Key<?>, 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
private static Module copy(Injector src) {
|
private static Module copy(Injector src) {
|
||||||
|
Set<TypeLiteral<?>> dynamicTypes = Sets.newHashSet();
|
||||||
|
for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
|
||||||
|
TypeLiteral<?> type = e.getKey().getTypeLiteral();
|
||||||
|
if (type.getRawType() == DynamicSet.class
|
||||||
|
|| type.getRawType() == DynamicMap.class) {
|
||||||
|
ParameterizedType t = (ParameterizedType) type.getType();
|
||||||
|
dynamicTypes.add(TypeLiteral.get(t.getActualTypeArguments()[0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final Map<Key<?>, Binding<?>> bindings = Maps.newLinkedHashMap();
|
final Map<Key<?>, Binding<?>> bindings = Maps.newLinkedHashMap();
|
||||||
for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
|
for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
|
||||||
if (shouldCopy(e.getKey())) {
|
if (!dynamicTypes.contains(e.getKey().getTypeLiteral())
|
||||||
|
&& shouldCopy(e.getKey())) {
|
||||||
bindings.put(e.getKey(), e.getValue());
|
bindings.put(e.getKey(), e.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user