Search in sources :

Example 1 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.

the class HK2InjectionManager method createLocator.

/**
     * Creates a new {@link ServiceLocator} instance from static {@link ServiceLocatorFactory} and adds the provided parent
     * locator if the instance is not null.
     *
     * @param name          name of the injection manager.
     * @param parentLocator parent locator, can be {@code null}.
     * @return new instance of injection manager.
     */
private static ServiceLocator createLocator(String name, ServiceLocator parentLocator) {
    ServiceLocator result = factory.create(name, parentLocator, null, ServiceLocatorFactory.CreatePolicy.DESTROY);
    result.setNeutralContextClassLoader(false);
    ServiceLocatorUtilities.enablePerThreadScope(result);
    return result;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator)

Example 2 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.

the class HK2InjectionManager method resolveServiceLocatorParent.

private static ServiceLocator resolveServiceLocatorParent(Object parent) {
    assertParentLocatorType(parent);
    ServiceLocator parentLocator = null;
    if (parent != null) {
        if (parent instanceof ServiceLocator) {
            parentLocator = (ServiceLocator) parent;
        } else if (parent instanceof HK2InjectionManager) {
            parentLocator = ((HK2InjectionManager) parent).getServiceLocator();
        }
    }
    return parentLocator;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator)

Example 3 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.

the class Hk2Helper method bindBinding.

/**
     * Binds the single descriptor using an external {@link DynamicConfiguration}.
     *
     * @param locator HK2 injection manager.
     * @param dc      HK2 Dynamic configuration to bind the object.
     * @param binding Jersey descriptor as a holder of information about an injection point.
     */
private static void bindBinding(ServiceLocator locator, DynamicConfiguration dc, Binding<?, ?> binding) {
    if (ClassBinding.class.isAssignableFrom(binding.getClass())) {
        ActiveDescriptor<?> activeDescriptor = translateToActiveDescriptor((ClassBinding<?>) binding);
        bindBinding(locator, dc, activeDescriptor, binding.getAliases());
    } else if (InstanceBinding.class.isAssignableFrom(binding.getClass())) {
        ActiveDescriptor<?> activeDescriptor = translateToActiveDescriptor((InstanceBinding<?>) binding);
        bindBinding(locator, dc, activeDescriptor, binding.getAliases());
    } else if (InjectionResolverBinding.class.isAssignableFrom(binding.getClass())) {
        InjectionResolverBinding resolverDescriptor = (InjectionResolverBinding) binding;
        bindBinding(locator, dc, wrapInjectionResolver(resolverDescriptor), binding.getAliases());
        bindBinding(locator, dc, translateToActiveDescriptor(resolverDescriptor), binding.getAliases());
    } else if (SupplierClassBinding.class.isAssignableFrom(binding.getClass())) {
        bindSupplierClassBinding(locator, (SupplierClassBinding<?>) binding);
    } else if (SupplierInstanceBinding.class.isAssignableFrom(binding.getClass())) {
        bindSupplierInstanceBinding(locator, (SupplierInstanceBinding<?>) binding);
    } else {
        throw new RuntimeException(LocalizationMessages.UNKNOWN_DESCRIPTOR_TYPE(binding.getClass().getSimpleName()));
    }
}
Also used : InstanceBinding(org.glassfish.jersey.internal.inject.InstanceBinding) SupplierInstanceBinding(org.glassfish.jersey.internal.inject.SupplierInstanceBinding) InjectionResolverBinding(org.glassfish.jersey.internal.inject.InjectionResolverBinding) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) AbstractActiveDescriptor(org.glassfish.hk2.utilities.AbstractActiveDescriptor) SupplierClassBinding(org.glassfish.jersey.internal.inject.SupplierClassBinding)

Example 4 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.

the class Hk2Helper method bindBinding.

/**
     * Binds the single descriptor using a single {@link DynamicConfiguration}.
     *
     * @param locator HK2 injection manager.
     * @param binding Jersey descriptor as a holder of information about an injection point.
     */
private static void bindBinding(ServiceLocator locator, Binding<?, ?> binding) {
    DynamicConfiguration dc = getDynamicConfiguration(locator);
    bindBinding(locator, dc, binding);
    dc.commit();
}
Also used : DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration)

Example 5 with ServiceLocator

use of org.glassfish.hk2.api.ServiceLocator in project jersey by jersey.

the class RequestResponseWrapperProvider method getRequestScopedInitializerProvider.

@Override
public RequestScopedInitializerProvider getRequestScopedInitializerProvider() {
    return new RequestScopedInitializerProvider() {

        @Override
        public RequestScopedInitializer get(final RequestContextProvider context) {
            return new RequestScopedInitializer() {

                @Override
                public void initialize(InjectionManager injectionManager) {
                    ServiceLocator locator = ((HK2InjectionManager) injectionManager).getServiceLocator();
                    locator.<Ref<HttpServletRequest>>getService(REQUEST_TYPE).set(finalWrap(context.getHttpServletRequest()));
                    locator.<Ref<HttpServletResponse>>getService(RESPONSE_TYPE).set(finalWrap(context.getHttpServletResponse()));
                }
            };
        }
    };
}
Also used : RequestScopedInitializer(org.glassfish.jersey.server.spi.RequestScopedInitializer) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestScopedInitializerProvider(org.glassfish.jersey.servlet.internal.spi.RequestScopedInitializerProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) RequestContextProvider(org.glassfish.jersey.servlet.internal.spi.RequestContextProvider) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager)

Aggregations

ServiceLocator (org.glassfish.hk2.api.ServiceLocator)12 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)4 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)4 AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)3 HK2InjectionManager (org.glassfish.jersey.hk2.HK2InjectionManager)3 Test (org.junit.Test)3 Injector (com.google.inject.Injector)2 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)2 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)2 InjectionResolverBinding (org.glassfish.jersey.internal.inject.InjectionResolverBinding)2 InstanceBinding (org.glassfish.jersey.internal.inject.InstanceBinding)2 SupplierClassBinding (org.glassfish.jersey.internal.inject.SupplierClassBinding)2 SupplierInstanceBinding (org.glassfish.jersey.internal.inject.SupplierInstanceBinding)2 ServletModule (com.google.inject.servlet.ServletModule)1 JerseyGuiceModule (com.squarespace.jersey2.guice.JerseyGuiceModule)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Server (org.eclipse.jetty.server.Server)1 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1 ServiceLocatorGenerator (org.glassfish.hk2.extension.ServiceLocatorGenerator)1