Hi,
2010/8/2 Hynek Mlnařík <[email protected]> > Thanks, now I see what's happening there. > > If the LoggerComponent had @Required fields and its instance was created > via an explicit constructor, it wouldn't get an InstanceManager instance in > __IM initialized: it is only initialized from iPOJO own constructor that > wouldn't be called. Hence it also wouldn't evaluate any of the @Required > fields. So if I understand it correctly, to make the @Required fields work: > > - Upon calling LoggerComponentStrategy.getServiceFactory(InstanceManager > im), the strategy creates a new ServiceObjectFactory that is parametrized by > im and returns it as its result. > - This returned ServiceObjectFactory would implement > getService(ComponentInstance) so that it invokes _setInstanceManager (via > reflection as it is private) on the resulting iPOJO component instance, > passing it the im instance manager as an argument. Then the @Required fields > of the result would be able to provide correct values. > What about,in the LoggerComponentStrategy, calling InstanceManager.createPojoObject() which call the correct constructor (instead of new LoggerComponent). Then invoking a setName method to pass the logger name ? Calling InstanceManager.createPojoObject avoids to deal with the instance manager injection as it injects it correctly. Regards, Clement > > --Hynek > > On 02.8.2010 13:51:27, clement escoffier wrote: > > 2010/8/2 Hynek Mlnařík <[email protected]> > > > However the problem with calling a parameterless constructor remains. > I've > > > made a short research into the issue and created a test case which > could be > > > found at http://www.sendspace.com/file/imhlcx. It is a simple > > > two-component sample (one component requires the other). The components > are > > > created programmatically (via IPOJOHelper class fragment taken from > > > junit4osgi project). It correctly does not invoke a parameterless > > > constructor of a.b.LoggerComponent when a.b.LoggerComponent class does > not > > > contain a @Validate annotated method. However, if such a method exists > in > > > the class, a parameterless constructor is invoked. This seems a bug to > me. > > > Could you please confirm it or let me know what I'm doing wrong? > > > > The @Validate callback will create the object using a parameterless > > constructor. Indeed, to call the validate method, iPOJO needs to create > an > > object first. This object is created by using a parameterless argument or > a > > static factory method (@Component.factory_method). This method is also > > called without parameters. If you want to set 'fields', just declare > > properties and give the values in the instance configuration. > > > > But in your case, it will not help :-). You're using service object > creation > > strategy, so you can't use the @Validate annotation because both have > > the responsibility to create the service objects. @Validate is called > first > > and immediately after the instance validation. The object is created > using > > the parameterless constructor. However, to get your code working > correctly, > > just call the method annotated with @Validate in the constructor and > remove > > the @Validate. > > LoggerComponent(final String name) { > > this.name = name; > > System.out.println("constructor with parameter " + name); > > validateLoggerComponent(); // <-- Added > > } > > > > Only the service providing strategy has the responsibility to create your > > object. And so, it will call the correct constructor. > > > > Regards, > > > > Clement > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > >

