Esempio n. 1
0
  private void bindProvidersAndResources(
      final Set<ComponentProvider> componentProviders,
      final ComponentBag componentBag,
      final Set<Class<?>> resourceClasses,
      final Set<Object> resourceInstances) {

    final JerseyResourceContext resourceContext = locator.getService(JerseyResourceContext.class);
    final DynamicConfiguration dc = Injections.getConfiguration(locator);
    final Set<Class<?>> registeredClasses = runtimeConfig.getRegisteredClasses();

    // Merge programmatic resource classes with component classes.
    Set<Class<?>> classes = Sets.newIdentityHashSet();
    classes.addAll(
        Sets.filter(
            componentBag.getClasses(ComponentBag.EXCLUDE_META_PROVIDERS),
            new Predicate<Class<?>>() {
              @Override
              public boolean apply(Class<?> componentClass) {
                return Providers.checkProviderRuntime(
                    componentClass,
                    componentBag.getModel(componentClass),
                    RuntimeType.SERVER,
                    !registeredClasses.contains(componentClass),
                    resourceClasses.contains(componentClass));
              }
            }));
    classes.addAll(resourceClasses);

    // Bind classes.
    for (Class<?> componentClass : classes) {
      ContractProvider model = componentBag.getModel(componentClass);
      if (resourceClasses.contains(componentClass)) {
        if (bindWithComponentProvider(componentClass, model, componentProviders)) {
          continue;
        }

        if (!Resource.isAcceptable(componentClass)) {
          LOGGER.warning(LocalizationMessages.NON_INSTANTIABLE_COMPONENT(componentClass));
          continue;
        }

        if (model != null
            && !Providers.checkProviderRuntime(
                componentClass,
                model,
                RuntimeType.SERVER,
                !registeredClasses.contains(componentClass),
                true)) {
          model = null;
        }
        resourceContext.unsafeBindResource(componentClass, model, dc);
      } else {
        ProviderBinder.bindProvider(componentClass, model, dc);
      }
    }

    // Merge programmatic resource instances with other component instances.
    Set<Object> instances = Sets.newHashSet();
    instances.addAll(
        Sets.filter(
            componentBag.getInstances(ComponentBag.EXCLUDE_META_PROVIDERS),
            new Predicate<Object>() {
              @Override
              public boolean apply(Object component) {
                final Class<?> componentClass = component.getClass();
                return Providers.checkProviderRuntime(
                    componentClass,
                    componentBag.getModel(componentClass),
                    RuntimeType.SERVER,
                    !registeredClasses.contains(componentClass),
                    resourceInstances.contains(component));
              }
            }));
    instances.addAll(resourceInstances);

    // Bind instances.
    for (Object component : instances) {
      ContractProvider model = componentBag.getModel(component.getClass());
      if (resourceInstances.contains(component)) {
        if (model != null
            && !Providers.checkProviderRuntime(
                component.getClass(),
                model,
                RuntimeType.SERVER,
                !registeredClasses.contains(component.getClass()),
                true)) {
          model = null;
        }
        resourceContext.unsafeBindResource(component, model, dc);
      } else {
        ProviderBinder.bindProvider(component, model, dc);
      }
    }

    dc.commit();
  }