Problem with initialization of preferences [message #735789] |
Wed, 12 October 2011 17:32 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
Hi,
I need to access preferences in my validator (language compliance
settings), and I have configured as described below.
The main problem is that an implementation of
IPreferenceStoreInitializer does not get a call to initialize until too
late.
How can I ensure that the preference store is initialized before trying
to access it?
This is how I do it now:
In the UI module, bind the preference helper (that answers things like
"what is the compliance level".
public void configureDefaultPreferences(Binder binder) {
binder.bind(IPreferenceStoreInitializer.class).annotatedWith(Names.named("PPPreferencesHelper"))
.to(PPPreferencesHelper.class);
}
In the UI module, I then do this:
public com.google.inject.Provider<IValidationAdvisor>
provideValidationAdvisor() {
return PreferenceBasedValidationAdvisorProvider.create();
}
Which in turn does:
@Inject
private PPPreferencesHelper preferences;
public static <T extends IValidationAdvisor>
PreferenceBasedValidationAdvisorProvider<T> create() {
return new PreferenceBasedValidationAdvisorProvider<T>();
}
@Override
public IValidationAdvisor get() {
return
ValidationAdvisor.create(preferences.getValidationComplianceLevel());
}
The ValidationAdvisor.create just looks at the enum returned by
getValidationComplianceLevel and returns a new instance of an
implementation of IValidationAdvisor.
By the time, the provider gets a call to get(), the PPPreferenceHelper
has not yet received a call to intialize.
In my standalone implementation, the compliance level is set via a
parameter to the module, but in the UI, I need the compliance level to
be set from preferences.
Regards
- henrik
|
|
|
Re: Problem with initialization of preferences [message #735815 is a reply to message #735789] |
Wed, 12 October 2011 19:26 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
I can answer this myself.
The JavaValidator seems to be bound eagerly by something that is
Singleton and thus, anything injected into the validator gets created at
this point.
It did not help to inject a provider and then get an instance from the
provider in the JavaValidator's constructor (for the same reason).
What worked was to get the instance at the next stage when the validator
is used.
- henrik
On 10/12/11 7:32 PM, Henrik Lindberg wrote:
> Hi,
> I need to access preferences in my validator (language compliance
> settings), and I have configured as described below.
>
> The main problem is that an implementation of
> IPreferenceStoreInitializer does not get a call to initialize until too
> late.
>
> How can I ensure that the preference store is initialized before trying
> to access it?
>
> This is how I do it now:
>
> In the UI module, bind the preference helper (that answers things like
> "what is the compliance level".
>
> public void configureDefaultPreferences(Binder binder) {
> binder.bind(IPreferenceStoreInitializer.class).annotatedWith(Names.named("PPPreferencesHelper"))
> .to(PPPreferencesHelper.class);
> }
>
> In the UI module, I then do this:
> public com.google.inject.Provider<IValidationAdvisor>
> provideValidationAdvisor() {
> return PreferenceBasedValidationAdvisorProvider.create();
> }
>
> Which in turn does:
>
> @Inject
> private PPPreferencesHelper preferences;
> public static <T extends IValidationAdvisor>
> PreferenceBasedValidationAdvisorProvider<T> create() {
> return new PreferenceBasedValidationAdvisorProvider<T>();
> }
>
> @Override
> public IValidationAdvisor get() {
> return
> ValidationAdvisor.create(preferences.getValidationComplianceLevel());
> }
>
> The ValidationAdvisor.create just looks at the enum returned by
> getValidationComplianceLevel and returns a new instance of an
> implementation of IValidationAdvisor.
>
> By the time, the provider gets a call to get(), the PPPreferenceHelper
> has not yet received a call to intialize.
>
> In my standalone implementation, the compliance level is set via a
> parameter to the module, but in the UI, I need the compliance level to
> be set from preferences.
>
> Regards
> - henrik
|
|
|
Powered by
FUDForum. Page generated in 0.03631 seconds