Thanks for the explanation Simone, just learned one more feature of Guice today! Also, sorry for my silly reply. :)
Philippe On Fri, Nov 5, 2010 at 12:41 PM, Simone Tripodi <[email protected]> wrote: > @Philippe > Guice uses an extensible way to convert Strings to > primitives/primitives wrappers/Enums and user defined types. If you > want to know more take a look at sisu guice-converter[1] (wrote by > Stuart that replied at this mail) or to mine Rocoto converters[2]. > > BTW my sample was just a dummy-silly way to expose the problem, I > wrote a piece of code that loads automatically tons of configurations > and I don't know which kind of data my team mates will plug :) > > @Stuart > thanks for explanation, very appreciated. So now I have a question for > you, being more expert than me, and given the nature of the problem > I'm sure you can suggest me a valid suggestion :P > In Rocoto I've been maintaining a module[3] that extends the > com.google.inject.name.Names behavior and that's able to resolve > properties where users can define placeholders, like you do in > Ant/Maven, so users can define properties in the form: > > a = ${b} ${c} > b = ${d|e} {f} > c = ${g} ${h|i} > ... > > where the pipe |, where present, separates the key name and the > default value. Now, the strategy I adopted is letting Guice resolve > the variables for me, so > I wrote a Provider<String>[4] that parses the properties, but that in > some cases forces me binding the properties to the provider... and > according the behavior you described, it obviously fails. > > Do you have any suggestion how to realize a kind of workaround? > Many thanks in advance!!! > Simo > > [1] > https://github.com/sonatype/sisu/tree/master/sisu-inject/guice-bean/guice-bean-converters/src/main/java/org/sonatype/guice/bean/converters/ > [2] > http://code.google.com/p/rocoto/source/browse/#svn/trunk/src/main/java/com/googlecode/rocoto/converters > [3] > http://code.google.com/p/rocoto/source/browse/trunk/src/main/java/com/googlecode/rocoto/configuration/ConfigurationModule.java > [4] > http://code.google.com/p/rocoto/source/browse/trunk/src/main/java/com/googlecode/rocoto/configuration/resolver/PropertiesResolver.java > > http://people.apache.org/~simonetripodi/ > http://www.99soft.org/ > > > > On Fri, Nov 5, 2010 at 6:25 PM, Stuart McCulloch <[email protected]> wrote: >> On 5 November 2010 10:59, Simone Tripodi <[email protected]> wrote: >>> >>> Hi all guys, >>> I'm in trouble with a silly question but I hope you don't mind and >>> provide me a little help :P >>> >>> Given the simple POJO created by Guice: >>> >>> {{{ >>> @Singleton >>> public class MyConfig { >>> >>> @Inject @Named("myconfig.autoclose") boolean autoClose(); // please >>> note the type >>> >>> // setter here >>> >>> } >>> }}} >>> >>> immagine the proeprty come from a text file, if in the module I bind >>> the property via: >>> >>> {{{ >>> >>> binder.bindConstant().annotatedWith(Names.named("myconfig.autoclose")).to(myAlchemyObject.getStringValue()); >>> }}} >>> >>> or, like in the com.google.inject.name.Names class: >>> >>> {{{ >>> binder.bind(Key.get(String.class, >>> >>> Names.Named("myconfig.autoclose"))).toInstance(myAlchemyObject.getStringValue()); >>> }}} >>> >>> things work well, the String will be successfully (if the value >>> matches) to boolean and the startup works well. >>> >>> Problems come when binding the String to a Provider<String>: >>> >>> {{{ >>> binder.bind(Key.get(String.class, >>> Names.Named("myconfig.autoclose"))).toProvider(new Provider<String>() >>> { >>> public String get() { >>> return myAlchemyObject.getStringValue(); >>> } >>> }); >>> }}} >>> >>> the Injector raises an error: >>> >>> {{{ >>> 1) No implementation for java.lang.Boolean annotated with >>> @com.google.inject.name.Named(value=myconfig.autoclose) was bound. >>> while locating java.lang.Boolean annotated with >>> @com.google.inject.name.Named(value=myconfig.autoclose) >>> for field at MyConfig.autoClose(MyConfig.java:XX) >>> }}} >>> >>> Any idea why? >> >> well looking at the convertConstantStringBinding method, it only considers >> String bindings which are constant - ie. those bound using bindConstant or >> toInstance - so your Provider binding is never considered for conversion >> I think this is a conscious design decision rather than an implementation >> detail - iirc this wasn't meant to be a full-featured type conversion system >> but something to help with basic configuration (hence constant values) >>> >>> Any suggestion will be more than appreciated, thanks in advance! >>> Simo >>> >>> http://people.apache.org/~simonetripodi/ >>> http://www.99soft.org/ >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "google-guice" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/google-guice?hl=en. >>> >> >> >> >> -- >> Cheers, Stuart >> >> -- >> You received this message because you are subscribed to the Google Groups >> "google-guice" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/google-guice?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
