Search in sources :

Example 26 with Provider

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());
}
Also used : ESMessageList(org.graylog.storage.elasticsearch6.views.searchtypes.ESMessageList) ESSearchTypeHandler(org.graylog.storage.elasticsearch6.views.searchtypes.ESSearchTypeHandler) BeforeClass(org.junit.BeforeClass) Provider(javax.inject.Provider) Query(org.graylog.plugins.views.search.Query) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) SearchType(org.graylog.plugins.views.search.SearchType) Map(java.util.Map) FieldTypesLookup(org.graylog.plugins.views.search.elasticsearch.FieldTypesLookup) Search(org.graylog.plugins.views.search.Search) MessageList(org.graylog.plugins.views.search.searchtypes.MessageList) QueryResult(org.graylog.plugins.views.search.QueryResult) Period(org.joda.time.Period) SearchJob(org.graylog.plugins.views.search.SearchJob) ImmutableSet(com.google.common.collect.ImmutableSet) SearchConfig(org.graylog.plugins.views.search.engine.SearchConfig) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) Test(org.junit.Test) Maps(com.google.common.collect.Maps) QueryStringParser(org.graylog.plugins.views.search.elasticsearch.QueryStringParser) QueryStringDecorators(org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators) IndexLookup(org.graylog.plugins.views.search.elasticsearch.IndexLookup) Mockito.mock(org.mockito.Mockito.mock) ESMessageList(org.graylog.storage.elasticsearch6.views.searchtypes.ESMessageList) QueryStringParser(org.graylog.plugins.views.search.elasticsearch.QueryStringParser) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) FieldTypesLookup(org.graylog.plugins.views.search.elasticsearch.FieldTypesLookup) Provider(javax.inject.Provider) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) BeforeClass(org.junit.BeforeClass)

Example 27 with Provider

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);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Named(javax.inject.Named) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Qualifier(javax.inject.Qualifier) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider)

Example 28 with Provider

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);
        }
    }
}
Also used : Named(javax.inject.Named) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Qualifier(javax.inject.Qualifier)

Example 29 with Provider

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();
}
Also used : HashMap(java.util.HashMap) ModuleRegistry(info.magnolia.module.ModuleRegistry) MockNode(info.magnolia.test.mock.jcr.MockNode) Provider(javax.inject.Provider) Before(org.junit.Before)

Example 30 with Provider

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);
}
Also used : ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Provider(javax.inject.Provider) Field(java.lang.reflect.Field)

Aggregations

Provider (javax.inject.Provider)53 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)23 Test (org.junit.Test)15 ComponentManager (org.xwiki.component.manager.ComponentManager)15 Before (org.junit.Before)11 HashMap (java.util.HashMap)9 File (java.io.File)7 FilesystemExportContext (org.xwiki.url.filesystem.FilesystemExportContext)7 ArrayList (java.util.ArrayList)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 Map (java.util.Map)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 Maps (com.google.common.collect.Maps)4 Type (java.lang.reflect.Type)4 Query (org.graylog.plugins.views.search.Query)4 QueryResult (org.graylog.plugins.views.search.QueryResult)4 SearchJob (org.graylog.plugins.views.search.SearchJob)4 SearchType (org.graylog.plugins.views.search.SearchType)4 IndexLookup (org.graylog.plugins.views.search.elasticsearch.IndexLookup)4 QueryStringDecorators (org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators)4