Search in sources :

Example 1 with DynamicConfiguration

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

the class Main method main.

public static void main(String[] args) {
    ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
    ServiceLocator locator = factory.create("myLocator");
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration dc = dcs.createDynamicConfiguration();
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(HelloServiceImpl.class).to(GreetingService.class);
        }
    };
    locator.inject(binder);
    binder.bind(dc);
    dc.commit();
    GreetingWrapper wrapper = new GreetingWrapper(locator);
    System.out.println("result: " + wrapper.getInjectedGreeting());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ServiceLocatorFactory(org.glassfish.hk2.api.ServiceLocatorFactory)

Example 2 with DynamicConfiguration

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

the class MyServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
    ServiceLocator locator = factory.create("myLocator");
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration dc = dcs.createDynamicConfiguration();
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(HelloServiceImpl.class).to(GreetingService.class);
        }
    };
    binder.bind(dc);
    dc.commit();
    GreetingWrapper wrapper = new GreetingWrapper(locator);
    response.getWriter().write("Greeting: " + wrapper.getInjectedGreeting());
    locator.shutdown();
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ServiceLocatorFactory(org.glassfish.hk2.api.ServiceLocatorFactory)

Example 3 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration 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 DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration 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 DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project Payara by payara.

the class HK2DomConfigUtilities method addIfNotThere.

private static boolean addIfNotThere(ServiceLocator locator, DynamicConfiguration config, DescriptorImpl desc, HK2Loader loader) {
    IndexedFilter filter = BuilderHelper.createContractFilter(desc.getImplementation());
    if (locator.getBestDescriptor(filter) != null)
        return false;
    if (loader != null) {
        desc.setLoader(loader);
    }
    config.bind(desc);
    return true;
}
Also used : IndexedFilter(org.glassfish.hk2.api.IndexedFilter)

Aggregations

DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)20 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)17 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)6 StartupContext (com.sun.enterprise.module.bootstrap.StartupContext)3 ProcessEnvironment (org.glassfish.api.admin.ProcessEnvironment)2 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)2 IndexedFilter (org.glassfish.hk2.api.IndexedFilter)2 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)2 DescriptorBuilder (org.glassfish.hk2.utilities.DescriptorBuilder)2 AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)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 Version (com.sun.appserv.server.util.Version)1 ProgramOptions (com.sun.enterprise.admin.cli.ProgramOptions)1 EmbeddedInhabitantsParser (com.sun.enterprise.glassfish.bootstrap.EmbeddedInhabitantsParser)1 ModulesRegistry (com.sun.enterprise.module.ModulesRegistry)1 ModuleStartup (com.sun.enterprise.module.bootstrap.ModuleStartup)1 SingleModulesRegistry (com.sun.enterprise.module.single.SingleModulesRegistry)1