Search in sources :

Example 1 with AutoDiscoverable

use of org.glassfish.jersey.internal.spi.AutoDiscoverable in project jersey by jersey.

the class CommonConfig method configureAutoDiscoverableProviders.

/**
     * Configure {@link AutoDiscoverable auto-discoverables} in the injection manager.
     *
     * @param injectionManager locator in which the auto-discoverables should be configured.
     * @param forcedOnly defines whether all or only forced auto-discoverables should be configured.
     */
public void configureAutoDiscoverableProviders(final InjectionManager injectionManager, final boolean forcedOnly) {
    // Check whether meta providers have been initialized for a config this config has been loaded from.
    if (!disableMetaProviderConfiguration) {
        final Set<AutoDiscoverable> providers = new TreeSet<>((o1, o2) -> {
            final int p1 = o1.getClass().isAnnotationPresent(Priority.class) ? o1.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
            final int p2 = o2.getClass().isAnnotationPresent(Priority.class) ? o2.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
            return (p1 < p2 || p1 == p2) ? -1 : 1;
        });
        // Forced (always invoked).
        final List<ForcedAutoDiscoverable> forcedAutoDiscroverables = new LinkedList<>();
        for (Class<ForcedAutoDiscoverable> forcedADType : ServiceFinder.find(ForcedAutoDiscoverable.class, true).toClassArray()) {
            forcedAutoDiscroverables.add(injectionManager.createAndInitialize(forcedADType));
        }
        providers.addAll(forcedAutoDiscroverables);
        // Regular.
        if (!forcedOnly) {
            providers.addAll(Providers.getProviders(injectionManager, AutoDiscoverable.class));
        }
        for (final AutoDiscoverable autoDiscoverable : providers) {
            final ConstrainedTo constrainedTo = autoDiscoverable.getClass().getAnnotation(ConstrainedTo.class);
            if (constrainedTo == null || type.equals(constrainedTo.value())) {
                try {
                    autoDiscoverable.configure(this);
                } catch (final Exception e) {
                    LOGGER.log(Level.FINE, LocalizationMessages.AUTODISCOVERABLE_CONFIGURATION_FAILED(autoDiscoverable.getClass()), e);
                }
            }
        }
    }
}
Also used : ConstrainedTo(javax.ws.rs.ConstrainedTo) AutoDiscoverable(org.glassfish.jersey.internal.spi.AutoDiscoverable) ForcedAutoDiscoverable(org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable) TreeSet(java.util.TreeSet) Priority(javax.annotation.Priority) ForcedAutoDiscoverable(org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 Priority (javax.annotation.Priority)1 ConstrainedTo (javax.ws.rs.ConstrainedTo)1 AutoDiscoverable (org.glassfish.jersey.internal.spi.AutoDiscoverable)1 ForcedAutoDiscoverable (org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable)1