users@hk2.java.net

Re: Dynamically bind a Factory to a ServiceLocator?

From: john wells <john.wells_at_oracle.com>
Date: Tue, 28 Oct 2014 20:57:14 -0400

You are correct, this is more difficult than it should be. You can do
it, but you'll have to become an expert on how to construct
descriptors. I also think it is worth adding a helper function to
ServiceLocatorUtilities, like "addFactoryConstant" or something like that.

In the meantime, the best thing is probably to:

1. Use
BuilderHelper.activeLink(WhatTheFactoryIsProviding.class).buildProvideMethod()
to get the ActiveDescriptor for the provide method of the factory
2. Use BuilderHelper.createConstantDescriptor to get the
ActiveDescriptor for the factory
3. Create your own implementation of FactoryDescriptors to add the
previous two descriptors into
4. Use ServiceLocatorUtilities.addFactoryDescriptors to add your factory

This is kind of roundabout, and I think there are at least two things
this shows a need for:

1. A utility implementation of FactoryDescriptors so that you don't
have to create one yourself
2. Methods in BuilderHelper and ServiceLocatorUtilities that allow you
to add constant factories

So... it is possible today, just tricky, and it shouldn't be so tricky
IMO...

On 10/28/2014 8:15 PM, buko wrote:
> For various reasons I'd like to be able to dynamically add a Factory
> to a ServiceLocator after the ServiceLocator has been created. Try as
> I might can't figure out how this is done.
>
> Let's say I have an instance of GenericFactory<T> and a type T. Given
> a ServiceLocator I'd like to add the factory instance such that other
> users (before I proceed to the next runlevel) can @Inject T and it'll
> call the factory instance.
>
> I've been poking at this for a while using
> BuildHelper.createConstantDescriptor and the FactoryDescriptors
> interface but nothing works. Any insights on how this might look?