Hi Ray,

On Tue, Nov 21, 2017 at 4:34 PM, Raymond Auge <raymond.a...@liferay.com> wrote:
> 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:
>
> @Component
> public 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.

That's a very cool solution, and does exactly what I want.

Thanks!

Robert
_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to