summaryrefslogtreecommitdiffstats
path: root/container-jersey2/src/main/java/com/yahoo/container/servlet/jersey/ComponentGraphProvider.java
blob: 39578cd77645a8d63faeab6e1c28996e086425e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.servlet.jersey;

import com.yahoo.container.di.config.ResolveDependencyException;
import com.yahoo.container.di.config.RestApiContext;
import com.yahoo.container.jaxrs.annotation.Component;
import org.glassfish.hk2.api.Injectee;
import org.glassfish.hk2.api.InjectionResolver;
import org.glassfish.hk2.api.ServiceHandle;

import javax.inject.Singleton;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Resolves jdisc container components for jersey 2 components. Similar to
 * Gjoran's ComponentGraphProvider for jersey 1.
 *
 * @author Tony Vaagenes
 * @author ollivir
 */
@Singleton // jersey2 requirement: InjectionResolvers must be in the Singleton scope
public class ComponentGraphProvider implements InjectionResolver<Component> {
    private Collection<RestApiContext.Injectable> injectables;

    public ComponentGraphProvider(Collection<RestApiContext.Injectable> injectables) {
        this.injectables = injectables;
    }

    @Override
    public Object resolve(Injectee injectee, ServiceHandle<?> root) {
        Class<?> wantedClass;
        Type type = injectee.getRequiredType();
        if (type instanceof Class) {
            wantedClass = (Class<?>) type;
        } else {
            throw new UnsupportedOperationException("Only classes are supported, got " + type);
        }

        List<RestApiContext.Injectable> componentsWithMatchingType = new ArrayList<>();
        for (RestApiContext.Injectable injectable : injectables) {
            if (wantedClass.isInstance(injectable.instance)) {
                componentsWithMatchingType.add(injectable);
            }
        }

        if (componentsWithMatchingType.size() == 1) {
            return componentsWithMatchingType.get(0).instance;
        } else {
            String injectionDescription = "class '" + wantedClass + "' to inject into Jersey resource/provider '"
                    + injectee.getInjecteeClass() + "')";
            if (componentsWithMatchingType.size() > 1) {
                String ids = componentsWithMatchingType.stream().map(c -> c.id.toString()).collect(Collectors.joining(","));
                throw new ResolveDependencyException("Multiple components found of " + injectionDescription + ": " + ids);
            } else {
                throw new ResolveDependencyException("Could not find a component of " + injectionDescription + ".");
            }
        }
    }

    @Override
    public boolean isMethodParameterIndicator() {
        return true;
    }

    @Override
    public boolean isConstructorParameterIndicator() {
        return true;
    }
}