felix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Schneider (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (FELIX-5749) Allow to use components that depend on optional imports
Date Tue, 21 Nov 2017 17:49:01 GMT

    [ https://issues.apache.org/jira/browse/FELIX-5749?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16261147#comment-16261147

Christian Schneider commented on FELIX-5749:

Sorry .. missed your initial message. I will create an example and report back if it works
out of the box. If it works I will create a small blog about it as I think people should use
this as a best practice for handling optional imports. 

> Allow to use components that depend on optional imports
> -------------------------------------------------------
>                 Key: FELIX-5749
>                 URL: https://issues.apache.org/jira/browse/FELIX-5749
>             Project: Felix
>          Issue Type: New Feature
>    Affects Versions: scr-2.0.12
>            Reporter: Christian Schneider
> When desigining the scope of a bundle you sometimes have an optional part that could
be externalized into its own bundle but you decide to keep it in your bundle to limit the
number of bundles. In this case you have to use an optional import and make sure the code
that depends on this import only runs when this import is wired. This code is often quite
awkward and often also buggy.
> We discussed on osgi-dev that you can make such code a lot simpler to write by using
> This is how the code would look like:
> You externalize the code that depends on the optional import into one or more components.
These components offer a service interface that is not dependent on the optional import. Inside
the component you can work freely with the optional packages. You have to make sure this component
is disabled by default. Then you write a "starter" component that enables the component if
the package is available.
> I think scr could support such "optional" components without the disabled trick. We could
load the component class and if it fails disable the component. If it works we enable it.

> So if the package is wired later and we get a refresh this approach would activate the
component without any additional effort from the developer side.
> ----
> Below I am copying a snippet from Ray that details what they did.
> 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
> @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());
>         }
>     }
> }

This message was sent by Atlassian JIRA

View raw message