Search in sources :

Example 1 with ServiceBean

use of io.fabric8.cdi.bean.ServiceBean in project fabric8 by fabric8io.

the class Fabric8Extension method afterDiscovery.

public void afterDiscovery(@Observes final AfterBeanDiscovery event, BeanManager beanManager) {
    KubernetesHolder.useBeanManager(beanManager);
    // Only add the bean if no other bean is found.
    if (beanManager.getBeans(KubernetesClient.class).isEmpty()) {
        event.addBean(new KubernetesClientBean());
    }
    // We need to process factories in reverse order so that we make feasible forwarding for service id etc.
    List<FactoryMethodContext> reverseFactories = new ArrayList<>(FactoryMethodContext.sort(factories));
    Collections.reverse(reverseFactories);
    for (final FactoryMethodContext factoryMethodContext : reverseFactories) {
        ServiceBean.doWith(factoryMethodContext.getReturnType(), new ServiceBean.Callback() {

            @Override
            public ServiceBean apply(ServiceBean bean) {
                String serviceId = bean.getServiceName();
                String serviceProtocol = or(bean.getServiceProtocol(), getFactoryMethodProtocol(factoryMethodContext.getFactoryMethod().getJavaMember()));
                String servicePort = or(bean.getServicePort(), getFactoryMethodPort(factoryMethodContext.getFactoryMethod().getJavaMember()));
                String servicePath = or(bean.getServicePath(), getFactoryMethodPath(factoryMethodContext.getFactoryMethod().getJavaMember()));
                Boolean serviceExternal = bean.getServiceExternal();
                Boolean serviceEndpoint = bean.getServiceEndpoint();
                // Ensure that there is a factory String -> sourceType before adding producer.
                if (!String.class.equals(factoryMethodContext.getSourceType())) {
                    ServiceBean.getBean(serviceId, serviceProtocol, servicePort, servicePath, null, serviceEndpoint, serviceExternal, factoryMethodContext.getSourceType());
                }
                return bean.withProducer(new FactoryMethodProducer(factoryMethodContext.getBean(), factoryMethodContext.getFactoryMethod(), serviceId, serviceProtocol, servicePort, servicePath));
            }
        });
    }
    for (ServiceUrlBean bean : ServiceUrlBean.getBeans()) {
        event.addBean(bean);
    }
    for (ServiceUrlCollectionBean bean : ServiceUrlCollectionBean.getBeans()) {
        event.addBean(bean);
    }
    for (ServiceBean bean : ServiceBean.getBeans()) {
        if (bean.getProducer() != null) {
            event.addBean(bean);
        }
    }
    for (ConfigurationBean b : ConfigurationBean.getBeans()) {
        event.addBean(b);
    }
}
Also used : FactoryMethodProducer(io.fabric8.cdi.producers.FactoryMethodProducer) ConfigurationBean(io.fabric8.cdi.bean.ConfigurationBean) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ArrayList(java.util.ArrayList) ServiceUrlCollectionBean(io.fabric8.cdi.bean.ServiceUrlCollectionBean) KubernetesClientBean(io.fabric8.cdi.bean.KubernetesClientBean) ServiceUrlBean(io.fabric8.cdi.bean.ServiceUrlBean) ServiceBean(io.fabric8.cdi.bean.ServiceBean)

Example 2 with ServiceBean

use of io.fabric8.cdi.bean.ServiceBean in project fabric8 by fabric8io.

the class FactoryMethodProducer method produce.

@Override
public T produce(CreationalContext<T> ctx) {
    List<Object> arguments = new ArrayList<>();
    for (AnnotatedParameter<X> parameter : factoryMethod.getParameters()) {
        Type type = parameter.getBaseType();
        ServiceName parameterServiceName = parameter.getAnnotation(ServiceName.class);
        Protocol parameterProtocol = parameter.getAnnotation(Protocol.class);
        PortName parameterPortName = parameter.getAnnotation(PortName.class);
        Path parameterPath = parameter.getAnnotation(Path.class);
        Endpoint paramEndpoint = parameter.getAnnotation(Endpoint.class);
        External paramExternal = parameter.getAnnotation(External.class);
        Configuration configuration = parameter.getAnnotation(Configuration.class);
        // A point without @ServiceName is invalid.
        // Even if method defines @ServiceName, the annotation on the injection point takes precedence
        String serviceName = pointName;
        String serviceProtocol = or(pointProtocol, parameterProtocol != null ? parameterProtocol.value() : null);
        String servicePort = or(pointPort, parameterPortName != null ? parameterPortName.value() : null);
        String servicePath = or(pointPath, parameterPath != null ? parameterPath.value() : null);
        Boolean serviceEndpoint = paramEndpoint != null ? paramEndpoint.value() : false;
        Boolean serviceExternal = paramExternal != null ? paramExternal.value() : false;
        // If the @ServiceName exists on the current String property
        if (parameterServiceName != null && String.class.equals(type)) {
            try {
                String serviceUrl = getServiceUrl(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, ctx);
                arguments.add(serviceUrl);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && List.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(endpointList);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && Set.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(new HashSet<>(endpointList));
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current property which is a non-String
        if (parameterServiceName != null && !String.class.equals(type)) {
            try {
                Object serviceBean = getServiceBean(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, type, ctx);
                arguments.add(serviceBean);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(BEAN_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), type, serviceName), t);
            }
        } else // If the current parameter is annotated with @Configuration
        if (configuration != null) {
            try {
                Object config = getConfiguration(serviceName, (Class<Object>) type, ctx);
                arguments.add(config);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(CONF_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else {
            try {
                Object other = BeanProvider.getContextualReference(Types.asClass(type), true);
                arguments.add(other);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(PARAMETER_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), parameter.getPosition()), t);
            }
        }
    }
    try {
        return (T) factoryMethod.getJavaMember().invoke(bean.create(ctx), arguments.toArray());
    } catch (Throwable t) {
        throw new RuntimeException(String.format(INVOCATION_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), arguments), t);
    }
}
Also used : Path(io.fabric8.annotations.Path) HashSet(java.util.HashSet) Set(java.util.Set) Configuration(io.fabric8.annotations.Configuration) ArrayList(java.util.ArrayList) PortName(io.fabric8.annotations.PortName) Type(java.lang.reflect.Type) Endpoint(io.fabric8.annotations.Endpoint) ServiceName(io.fabric8.annotations.ServiceName) External(io.fabric8.annotations.External) ArrayList(java.util.ArrayList) List(java.util.List) Protocol(io.fabric8.annotations.Protocol)

Aggregations

ArrayList (java.util.ArrayList)2 Configuration (io.fabric8.annotations.Configuration)1 Endpoint (io.fabric8.annotations.Endpoint)1 External (io.fabric8.annotations.External)1 Path (io.fabric8.annotations.Path)1 PortName (io.fabric8.annotations.PortName)1 Protocol (io.fabric8.annotations.Protocol)1 ServiceName (io.fabric8.annotations.ServiceName)1 ConfigurationBean (io.fabric8.cdi.bean.ConfigurationBean)1 KubernetesClientBean (io.fabric8.cdi.bean.KubernetesClientBean)1 ServiceBean (io.fabric8.cdi.bean.ServiceBean)1 ServiceUrlBean (io.fabric8.cdi.bean.ServiceUrlBean)1 ServiceUrlCollectionBean (io.fabric8.cdi.bean.ServiceUrlCollectionBean)1 FactoryMethodProducer (io.fabric8.cdi.producers.FactoryMethodProducer)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1