>From OSGi point of view, the service is active and the component is marked
as "satisfied". An instance of the component is being obtained from OSGi
but it only fails when the ModelAdapterFactory tries to inject this back
into the model class.

Here is the method from the ModelAdapterFactory where the Exception is
thrown from:


>    1. private boolean setField(Field field, Object createdObject, Object
>    value) {
>    2. if (value != null) {
>    3. if (!isAcceptableType(field.getClass(), value) && value instanceof
>    Adaptable) {
>    4. value = ((Adaptable) value).adaptTo(field.getClass());
>    5. if (value == null) {
>    6. return false;
>    7. }
>    8. }
>    9. boolean accessible = field.isAccessible();
>    10. try {
>    11. if (!accessible) {
>    12. field.setAccessible(true);
>    13. }
>    14. field.set(createdObject, value);
>    15. return true;
>    16. } catch (Exception e) {
>    17. log.error("unable to inject field", e);
>    18. return false;
>    19. } finally {
>    20. if (!accessible) {
>    21. field.setAccessible(false);
>    22. }
>    23. }
>    24. } else {
>    25. return false;
>    26. }
>    27. }
>
>



On Wed, Nov 19, 2014 at 4:26 PM, Jason Bailey <[email protected]> wrote:

> That's an usual problem you're having. You should be able to see the
> service you've defined in the OSGi panel before executing the request that
> creates the model.
>
> -Jason
> ________________________________________
> From: Hasanein Khafaji <[email protected]>
> Sent: Wednesday, November 19, 2014 6:54 AM
> To: [email protected]
> Subject: Re: Injecting OSGI services using sling models
>
> Sorry, I forgot to add that. It implements ManagedService.
>
> Do you think it has anything to do with the fact that the interface
> and the implementation are in different bundles, thus get loaded by a
> different class loader? and end up being incompatible when reflection
> is used to set one type to the other?
>
> I've tried o keep the interface and the implementation in a single
> bundle and it seems to be working now. But I am deviating from the
> project convention we have in place to keep interfaces in separate
> bundle than it's implementation.
>
> On Wed, Nov 19, 2014 at 11:43 AM, Felix Meschberger <[email protected]>
> wrote:
> > Hi
> >
> > Could it be that you announce the component to be „ManagedService“ but
> the class does not implement it and thus the component cannot be
> instantiated at all ?
> >
> > Regards
> > Felix
> >
> >> Am 19.11.2014 um 12:32 schrieb Hasanein Khafaji <[email protected]
> >:
> >>
> >> Let's assume that Bundle A defines a service interface and export the
> >> package that contains it to OSGi:
> >>
> >> Public interface ServiceInterface
> >> {
> >>      .....
> >> }
> >>
> >> Bundle B, provides an OSGi component/service that implement the
> >> service interface
> >>
> >> @Component(immediate = true, metatype = true)
> >> @Service(value = {ServiceInterface.class, ManagedService.class})
> >> @org.apache.felix.scr.annotations.Properties({
> >>        @Property(name = Constants.SERVICE_PID, value =
> >> "com.example.MyService"),
> >>        @Property(name = Constants.SERVICE_VENDOR, value = "Contoso"),
> >>        @Property(name = Constants.SERVICE_DESCRIPTION, value = "My
> Service"),
> >> })
> >> public class MyService implements ServiceInterface
> >> {
> >>     ...
> >> }
> >>
> >> Bundle C, contains Sling model classes that needs to use the above
> >> service, we inject it using @Inject or @Inject @Source("osgi-service")
> >> as per Sling Model documentation
> >>
> >> @Model(adaptables = {Resource.class})
> >> public class MyModel
> >> {
> >>    @Inject
> >>    private ServiceInterface serviceInterface;
> >>    ...
> >> }
> >>
> >> The problem is that the service never get injected into the model as
> >> it should be, the Exception we get is thrown by the
> >> ModelAdapterFactory saying that it's unable to inject MyService into
> >> ServiceInterface as if they were an incompatible types but they are
> >> not.
> >>
> >> On Wed, Nov 19, 2014 at 9:24 AM, Sarwar Bhuiyan
> >> <[email protected]> wrote:
> >>> Could you paste that bit of code please?
> >>>
> >>> On Wednesday, November 19, 2014, Hass Joseph Khafaji <
> [email protected]>
> >>> wrote:
> >>>
> >>>> The model is simply a sling model class, not an osgi service at all.
> >>>>
> >>>> The model is being looked up via slightly, thus it has to be fully
> >>>> populated when that happen which require that the model invoke a
> service to
> >>>> obtain data from.
> >>>>
> >>>> Sling model documentation says that you can inject osgi services into
> >>>> sling models but that is not seem to be working.
> >>>>
> >>>> Sent from my iPhone
> >>>>
> >>>>> On 19 Nov 2014, at 09:09, Sarwar Bhuiyan <[email protected]
> >>>> <javascript:;>> wrote:
> >>>>>
> >>>>> The model is not an OSGi service/component right?
> >>>>>
> >>>>> I'm not sure of why you need a reference to an osgi service in the
> model
> >>>>> but you can probably use a setter by yourself but I still think you
> can
> >>>>> just have another service which does the action on the model instead
> of
> >>>> the
> >>>>> model calling the service.
> >>>>>
> >>>>> On Wednesday, November 19, 2014, Hass Joseph Khafaji <
> >>>> [email protected] <javascript:;>>
> >>>>> wrote:
> >>>>>
> >>>>>> Hello,
> >>>>>>
> >>>>>> I am trying to inject OSGI service defined using scr annotation
> into a
> >>>>>> sling model class using the @inject annotation.
> >>>>>>
> >>>>>> What we seem to be getting is that the ModelAdapterFactory is
> unable to
> >>>>>> inject the service into the corresponding field as if it was an
> >>>>>> incompatible type. We not doing anything fancy here, it's just a
> simple
> >>>>>> service injection.
> >>>>>>
> >>>>>> Did anyone faced a similar issue in the past? Do we have to use
> custom
> >>>>>> injectors for this?
> >>>>>>
> >>>>>> Note: tried to use the @inject @source with an osgi-service but
> getting
> >>>>>> the same result.
> >>>>>>
> >>>>>> Cheers.
> >>>>
> >
>

Reply via email to