use of javax.inject.Provider in project graylog2-server by Graylog2.
the class ElasticsearchBackendTest method setup.
@BeforeClass
public static void setup() {
Map<String, Provider<ESSearchTypeHandler<? extends SearchType>>> handlers = Maps.newHashMap();
handlers.put(MessageList.NAME, () -> new ESMessageList(new QueryStringDecorators.Fake()));
final FieldTypesLookup fieldTypesLookup = mock(FieldTypesLookup.class);
final QueryStringParser queryStringParser = new QueryStringParser();
backend = new ElasticsearchBackend(handlers, null, mock(IndexLookup.class), new QueryStringDecorators.Fake(), (elasticsearchBackend, ssb, job, query) -> new ESGeneratedQueryContext(elasticsearchBackend, ssb, job, query, fieldTypesLookup), false, new ObjectMapperProvider().get());
}
use of javax.inject.Provider in project kernel by exoplatform.
the class ConcurrentContainer method resolveType.
/**
* Resolves the given type and generic type
*/
private Object resolveType(Class<?> type, Type genericType, Annotation[] annotations, String logMessagePrefix) {
if (type.isPrimitive()) {
if (LOG.isDebugEnabled()) {
LOG.debug(logMessagePrefix + ": Primitive types are not supported");
}
return 1;
}
Named named = null;
Class<?> qualifier = null;
for (int i = 0, length = annotations.length; i < length; i++) {
Annotation a = annotations[i];
if (a instanceof Named) {
named = (Named) a;
break;
} else if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
qualifier = a.annotationType();
break;
}
}
if (type.isInterface() && type.equals(Provider.class)) {
if (!(genericType instanceof ParameterizedType)) {
if (LOG.isDebugEnabled()) {
LOG.debug(logMessagePrefix + ": The generic type is not of type ParameterizedType");
}
return 2;
}
ParameterizedType aType = (ParameterizedType) genericType;
Type[] typeVars = aType.getActualTypeArguments();
Class<?> expectedType = (Class<?>) typeVars[0];
final ComponentAdapter<?> adapter;
if (named != null) {
adapter = holder.getComponentAdapter(named.value(), expectedType);
} else if (qualifier != null) {
adapter = holder.getComponentAdapter(qualifier, expectedType);
} else {
adapter = holder.getComponentAdapterOfType(expectedType);
}
if (adapter == null) {
if (LOG.isDebugEnabled()) {
LOG.debug(logMessagePrefix + ": We have no value to set so we skip it");
}
return 3;
}
return new Provider<Object>() {
public Object get() {
return adapter.getComponentInstance();
}
};
} else {
if (named != null) {
return holder.getComponentInstance(named.value(), type);
} else if (qualifier != null) {
return holder.getComponentInstance(qualifier, type);
} else {
return holder.getComponentInstanceOfType(type);
}
}
}
use of javax.inject.Provider in project kernel by exoplatform.
the class ConcurrentContainerMT method resolveType.
/**
* Resolves the given type and generic type
*/
private Object resolveType(final Class<?> type, Type genericType, Annotation[] annotations, String logMessagePrefix, List<Dependency> dependencies) {
if (type.isPrimitive()) {
if (LOG.isDebugEnabled()) {
LOG.debug(logMessagePrefix + ": Primitive types are not supported");
}
return 1;
}
Named named = null;
Class<?> qualifier = null;
for (int i = 0, length = annotations.length; i < length; i++) {
Annotation a = annotations[i];
if (a instanceof Named) {
named = (Named) a;
break;
} else if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
qualifier = a.annotationType();
break;
}
}
if (type.isInterface() && type.equals(Provider.class)) {
if (!(genericType instanceof ParameterizedType)) {
if (LOG.isDebugEnabled()) {
LOG.debug(logMessagePrefix + ": The generic type is not of type ParameterizedType");
}
return 2;
}
ParameterizedType aType = (ParameterizedType) genericType;
Type[] typeVars = aType.getActualTypeArguments();
Class<?> expectedType = (Class<?>) typeVars[0];
final ComponentAdapter<?> adapter;
final Object key;
if (named != null) {
adapter = holder.getComponentAdapter(key = named.value(), expectedType);
} else if (qualifier != null) {
adapter = holder.getComponentAdapter(key = qualifier, expectedType);
} else {
key = expectedType;
adapter = holder.getComponentAdapterOfType(expectedType);
}
if (adapter == null) {
if (LOG.isDebugEnabled()) {
LOG.debug(logMessagePrefix + ": We have no value to set so we skip it");
}
return 3;
}
final Provider<Object> result = new Provider<Object>() {
public Object get() {
return adapter.getComponentInstance();
}
};
dependencies.add(new DependencyByProvider(key, expectedType, result, adapter));
return result;
} else {
if (named != null) {
final String name = named.value();
dependencies.add(new DependencyByName(name, type));
return holder.getComponentAdapter(name, type);
} else if (qualifier != null) {
dependencies.add(new DependencyByQualifier(qualifier, type));
return holder.getComponentAdapter(qualifier, type);
} else {
dependencies.add(new DependencyByType(type));
return holder.getComponentAdapterOfType(type);
}
}
}
use of javax.inject.Provider in project magnolia-vanity-url by aperto.
the class VirtualVanityUriMappingTest method setUp.
@Before
public void setUp() throws Exception {
_uriMapping = new VirtualVanityUriMapping();
Provider moduleProvider = mock(Provider.class);
VanityUrlModule module = new VanityUrlModule();
Map<String, String> excludes = new HashMap<>();
excludes.put("pages", ".*\\..*");
module.setExcludes(excludes);
when(moduleProvider.get()).thenReturn(module);
_uriMapping.setVanityUrlModule(moduleProvider);
Provider serviceProvider = mock(Provider.class);
VanityUrlService vanityUrlService = mock(VanityUrlService.class);
when(vanityUrlService.queryForVanityUrlNode("/home", "default")).thenReturn(null);
MockNode mockNode = new MockNode("xmas");
when(vanityUrlService.queryForVanityUrlNode("/xmas", "default")).thenReturn(mockNode);
when(vanityUrlService.createRedirectUrl(mockNode)).thenReturn("redirect:/internal/page.html");
when(serviceProvider.get()).thenReturn(vanityUrlService);
_uriMapping.setVanityUrlService(serviceProvider);
Provider registryProvider = mock(Provider.class);
ModuleRegistry moduleRegistry = mock(ModuleRegistry.class);
when(registryProvider.get()).thenReturn(moduleRegistry);
_uriMapping.setModuleRegistry(registryProvider);
initWebContext();
initComponentProvider();
}
use of javax.inject.Provider in project guice by google.
the class MiniGuice method addJitBinding.
private void addJitBinding(Key key, Object requiredBy) {
Class<?> type = (Class<?>) key.type;
/*
* Lookup the injectable fields and their corresponding keys.
*/
final List<Field> injectedFields = new ArrayList<Field>();
List<Object> fieldKeysList = new ArrayList<Object>();
for (Class<?> c = type; c != Object.class; c = c.getSuperclass()) {
for (Field field : c.getDeclaredFields()) {
if (!field.isAnnotationPresent(javax.inject.Inject.class)) {
continue;
}
field.setAccessible(true);
injectedFields.add(field);
Key fieldKey = key(field, field.getGenericType(), field.getAnnotations());
fieldKeysList.add(fieldKey);
requireKey(fieldKey, field);
}
}
final Key[] fieldKeys = fieldKeysList.toArray(new Key[fieldKeysList.size()]);
/*
* Lookup @Inject-annotated constructors. If there's no @Inject-annotated
* constructor, use a default constructor if the class has other injections.
*/
Constructor<?> injectedConstructor = null;
for (Constructor<?> constructor : type.getDeclaredConstructors()) {
if (!constructor.isAnnotationPresent(javax.inject.Inject.class)) {
continue;
}
if (injectedConstructor != null) {
throw new IllegalArgumentException("Too many injectable constructors on " + type);
}
constructor.setAccessible(true);
injectedConstructor = constructor;
}
if (injectedConstructor == null) {
if (fieldKeys.length == 0) {
throw new IllegalArgumentException("No injectable constructor on " + type + " required by " + requiredBy);
}
try {
injectedConstructor = type.getConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("No injectable constructor on " + type + " required by " + requiredBy);
}
}
/*
* Create a provider that invokes the constructor and sets its fields.
*/
final Constructor<?> constructor = injectedConstructor;
final Key[] parameterKeys = parametersToKeys(constructor, constructor.getGenericParameterTypes(), constructor.getParameterAnnotations());
final Provider<Object> unscoped = new Provider<Object>() {
@Override
public Object get() {
Object[] constructorParameters = keysToValues(parameterKeys);
try {
Object result = constructor.newInstance(constructorParameters);
Object[] fieldValues = keysToValues(fieldKeys);
for (int i = 0; i < fieldValues.length; i++) {
injectedFields.get(i).set(result, fieldValues[i]);
}
return result;
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getCause());
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
};
boolean singleton = type.isAnnotationPresent(javax.inject.Singleton.class);
putBinding(new Key(type, null), unscoped, singleton);
}
Aggregations