Search in sources :

Example 1 with NameBinding

use of javax.ws.rs.NameBinding in project jersey by jersey.

the class ComponentBag method modelFor.

/**
     * Create a contract provider for a given component class.
     *
     * @param componentClass  component class to create contract provider model for.
     * @param defaultPriority default component priority. If {@value ContractProvider#NO_PRIORITY},
     *                        the value from the component class {@link javax.annotation.Priority} annotation will be used
     *                        (if any).
     * @param contractMap     map of contracts and their binding priorities. If {@code null}, the contracts will
     *                        gathered by introspecting the component class. Content of the contract map
     *                        may be modified during the registration processing.
     * @param modelEnhancer   custom contract provider model enhancer.
     * @return contract provider model for the class.
     */
private static ContractProvider modelFor(final Class<?> componentClass, final int defaultPriority, final Map<Class<?>, Integer> contractMap, final Inflector<ContractProvider.Builder, ContractProvider> modelEnhancer) {
    Map<Class<?>, Integer> contracts = contractMap;
    if (contracts == null) {
        // introspect
        contracts = asMap(Providers.getProviderContracts(componentClass));
    } else {
        // filter custom contracts
        final Iterator<Class<?>> it = contracts.keySet().iterator();
        while (it.hasNext()) {
            final Class<?> contract = it.next();
            if (contract == null) {
                it.remove();
                continue;
            }
            boolean failed = false;
            if (!Providers.isSupportedContract(contract)) {
                Errors.error(LocalizationMessages.CONTRACT_NOT_SUPPORTED(contract, componentClass), Severity.WARNING);
                failed = true;
            }
            if (!contract.isAssignableFrom(componentClass)) {
                Errors.error(LocalizationMessages.CONTRACT_NOT_ASSIGNABLE(contract, componentClass), Severity.WARNING);
                failed = true;
            }
            if (failed) {
                it.remove();
            }
        }
    }
    final ContractProvider.Builder builder = ContractProvider.builder(componentClass).addContracts(contracts).defaultPriority(defaultPriority);
    // Process annotations (priority, name bindings, scope)
    final boolean useAnnotationPriority = defaultPriority == ContractProvider.NO_PRIORITY;
    for (Annotation annotation : componentClass.getAnnotations()) {
        if (annotation instanceof Priority) {
            if (useAnnotationPriority) {
                builder.defaultPriority(((Priority) annotation).value());
            }
        } else {
            for (Annotation metaAnnotation : annotation.annotationType().getAnnotations()) {
                if (metaAnnotation instanceof NameBinding) {
                    builder.addNameBinding(annotation.annotationType());
                }
                if (metaAnnotation instanceof Scope) {
                    builder.scope(annotation.annotationType());
                }
            }
        }
    }
    return modelEnhancer.apply(builder);
}
Also used : Scope(javax.inject.Scope) ContractProvider(org.glassfish.jersey.model.ContractProvider) Priority(javax.annotation.Priority) NameBinding(javax.ws.rs.NameBinding) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Priority (javax.annotation.Priority)1 Scope (javax.inject.Scope)1 NameBinding (javax.ws.rs.NameBinding)1 ContractProvider (org.glassfish.jersey.model.ContractProvider)1