Hey Robert,

We had the same use case and solved it the following way:

Given your component which has the optional import package (doesn't matter
how it's used):

import com.liferay.demo.foo.Foo; // The optional package
@Component(
    enabled = false // disable by default so DS ignores it)public
class OptionalPackageConsumer implements Foo {...}

Make sure the component is *disabled* by default. This will prevent
SCR from classloading the component class.

Second, you construct a "starter" component who's job it is to check
for the available package:

@Componentpublic class OptionalPackageConsumerStarter {
   @Activate
    void activate(ComponentContext componentContext) {
        try {
            Class.forName(com.liferay.demo.foo.Foo.class.getName());

            
componentContext.enableComponent(OptionalPackageConsumer.class.getName());
        }
        catch (Throwable t) {
            _log.warn("Could not find {}", t.getMessage());
        }
    }
}

Sure, it's more work and the logic above is not perfect because you
may not want the "starter" component to start when the package is
unavailable. But this basic idea has worked for us.

- Ray


On Tue, Nov 21, 2017 at 9:16 AM, Robert Munteanu via osgi-dev <
[email protected]> wrote:

> On Tue, Nov 21, 2017 at 4:00 PM, Christian Schneider
> <[email protected]> wrote:
> > I think it can not work with a simple optional import. What could work
> is a
> > separate component that has
> > the optional class as a mandatory reference.
> >
> > This component could then be injected into your component with an
> optional
> > reference and with an interface that is independent of the optional
> import.
>
> That's a good point. Sadly I can't use this approach since it's going
> to generate a warning in the logs and we have a strict policy against
> that.
>
> > I think declarative services do not yet support this but it would be a
> very
> > valuable feature as it allows to handle optional imports without any
> class
> > loading magic on the user side.
>
> +1
>
> Robert
> >
> > Christian
> >
> > 2017-11-21 14:53 GMT+01:00 Robert Munteanu via osgi-dev
> > <[email protected]>:
> >>
> >> Hi Carsten,
> >>
> >> On Tue, Nov 21, 2017 at 3:50 PM, Carsten Ziegeler <[email protected]
> >
> >> wrote:
> >> > Hi,
> >> >
> >> > if I understand you correctly you have an optional package import to
> the
> >> > package providing BarService?
> >>
> >> Yes, that is correct.
> >>
> >> > In that case your class SomeComponent can't be loaded if that package
> is
> >> > not available and there is no magic to get around this.
> >>
> >> I was afraid of that. I'll try using a ServiceTracker, guess that's
> >> the next best thing.
> >>
> >> Thanks,
> >>
> >> Robert
> >> _______________________________________________
> >> OSGi Developer Mail List
> >> [email protected]
> >> https://mail.osgi.org/mailman/listinfo/osgi-dev
> >
> >
> >
> >
> > --
> > --
> > Christian Schneider
> > http://www.liquid-reality.de
> >
> > Computer Scientist
> > http://www.adobe.com
> >
>
>
>
> --
> http://robert.muntea.nu/
> _______________________________________________
> OSGi Developer Mail List
> [email protected]
> https://mail.osgi.org/mailman/listinfo/osgi-dev
>



-- 
*Raymond Augé* <http://www.liferay.com/web/raymond.auge/profile>
 (@rotty3000)
Senior Software Architect *Liferay, Inc.* <http://www.liferay.com>
 (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance <http://osgi.org> (@OSGiAlliance)
_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to