Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSGi: make it possible to use Vaadin components as OSGi services #5125

Open
denis-anisimov opened this issue Feb 25, 2019 · 11 comments
Open

Comments

@denis-anisimov
Copy link
Contributor

The main idea is to provide a way to do something like this:

@Route("")
@Component(scope=ServiceScope.PROTOTYPE)
public class MainView extends VerticalLayout {

    @Reference
    GreeterService greeter;

    public MainView() {
        Button button = new Button("Click me",
                event -> Notification.show(greeter.greet()));
        add(button);
    }
}

See here: https://github.com/Sandared/flow-osgi/blob/master/README.md

The original ticket is:
#5017

So it should be possible to use DS features (like injecting via @Reference in the example) in Vaadin components (at least in Route targets).

For the first iteration we require from the component being declared as a service via the annotation
@Component(scope=ServiceScope.PROTOTYPE).

It's a boilerplate code which you should use every time but let's require this at least for the first iteration (see discussion #5017 (comment) and following comments). But let's go with it for now.

If you inject a component via @Id then this component should be able also use DS features.

So it's primarily about a custom Instantiator.
It's getOrCreate method (most likely only this method) should instantiate classes via OSGi mechanism.
See https://github.com/Sandared/flow-osgi/blob/master/flow.osgi.integration/src/main/java/io/jatoms/flow/osgi/integration/v2/OSGiInstantiatorV2.java as an implementation example.

Let's avoid using ServiceLoader as a way to register an Instantiator instance.
So to be able to use a custom Instantiator we should subclass VaadinServletService and override its method createInstantiator.
This is very much like Spring add-on: https://github.com/vaadin/spring/blob/master/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringInstantiator.java.

So as a part of this ticket the following things should be done:

  • make a custom OSGi aware Instantiator class .
  • Make a subclass of VaadinServletService which uses this instantator
  • Make a subclass of VaadinServlet which uses a custom VaadinServletService
  • Make sure that requested features works : unit / IT tests are needed. I think we should not make OSGi specific tests as a part of test-root-context module. The best way would be to add UI components and test as a part of the felix-jetty module (which is used now as a module for testing OSGi). It already has IT test (which is not really an IT test). But it has no UI components. Note that the project config will need to be changed if you add UI components right to the module (the module is not deployed to the OSGi container): module should become an OSGi bundle and it should be deployed .
@denis-anisimov
Copy link
Contributor Author

All OSGi related classes should go into flow-osgi module.
Let's add flow-server as a dependency for flow-osgi (may be with provided scope).

Separate ticket for servlet auto-registration : #4796

@Braus
Copy link
Contributor

Braus commented Apr 3, 2019

Hi, I'm working on OSGI with Vaadin, are there any updates in making this work as planned? And are there workarounds regarding this issue so far?

Kind regards,

@Sandared
Copy link

Sandared commented Apr 7, 2019

Depending on the progress so far: Maybe another possible way is an integration between Flow and OSGi via CDI? There is a Flow CDI integration and there is a new OSGi CDI integration.
Maybe those can work together?

@pleku
Copy link
Contributor

pleku commented Apr 9, 2019

Hello, just as an update: we are not currently actively working on this due to other priorities.

Contributions are welcome and we will give any help needed and answer questions in case someone is interested in taking a look at this.

Currently my best guess is that we will be able to take a look at further OSGi improvements in the early summer. That is too late for V14, but hopefully we can make a minor release for Flow with the improvements and keep backwards compatibility to 14.

@pleku pleku modified the milestones: V14 Candidates, Candidates Apr 15, 2019
This was referenced May 28, 2019
@JulianFeinauer
Copy link
Contributor

This topic is very interesting for me also as I'm currently evaluating the use of Karaf and OSGi, see #6525. So I'm willing to help out here, if possible.

@JulianFeinauer
Copy link
Contributor

@Sandared did you make any progress on the Vaadin Flow CDI OSGi Integration? Thanks!

@Sandared
Copy link

Hi Julian,

unfortunately I didn't dig further into this issue after my initial tests, as we decided to use a complete different technology stack.

@JulianFeinauer
Copy link
Contributor

Hey @Sandared what a pitty : )
But nonetheless, for all the previous work and the response, 👍

@stbischof
Copy link
Contributor

stbischof commented Oct 17, 2020

Use the same way like Http-Whiteboard or JaxRs-Whiteboard
@Route/@RouteAlias , @Theme, @PWA could be a @ComponentPropertyType

Then an @Component annotation would be enough to create the Component properties. This property could be the marker (maybe together with the @Component(service=com.vaadin.flow.component.Component.class).
Then we can catch all this Services with declaritive Services/a Servicetracker.
The route could then be found in the Service-Properties. ref #9107
Also changing this Properties with ConfigAdmin would be easy.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@Documented
@ComponentPropertyType
public @interface Route {

	String PREFIX_ = "com.vaadin.flow.router.";

mvn dependency for @ComponentPropertyType (OSGi R7)

<dependency>
   <groupId>org.osgi</groupId>
   <artifactId>org.osgi.service.component.annotations</artifactId>
   <version>1.4.0</version>
   <scope>provided</scope>
</dependency>

generated Service Component Registry file: OSGI-INF/com.example.starter.flow.better.MainView.xml. SCR reads this file ob bundle activation and instanciates <implementation class="com.example.starter.flow.better.MainView"/> an registers the service with the shown properties.

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="com.example.starter.flow.better.MainView">
  <property name="com.vaadin.flow.router.route" type="String" value=""/>
  <property name="com.vaadin.flow.router.layout" type="String" value="com.vaadin.flow.component.UI"/>
  <property name="com.vaadin.flow.router.absolute" type="Boolean" value="false"/>
  <property name="com.vaadin.flow.router.registerAtStartup" type="Boolean" value="true"/>
  <implementation class="com.example.starter.flow.better.MainView"/>
</scr:component>

@stbischof
Copy link
Contributor

Here is a Vaadin-OSGi-Implementation and Examples to that can handle this Topic.

@Sandared
Copy link

Sandared commented Jun 25, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants