Hm,

Maybe it is just me who doesn’t like the Optional concept in Java the way it 
is. 😊
In collections it is fine from my point of view, for fields it is totally 
unnecessary as it doesn’t provide additional functionality nor does it provide 
better readable code or lesser bloat.

But ok, that is only my personal opinion and I’m sorry for interrupting.

Mit freundlichen Grüßen / Best regards

Dirk Fauth

Cross Automotive Platforms - Systems, Software and Tools, (CAP-SST/ESM1)
Robert Bosch GmbH | Postfach 30 02 40 | 70442 Stuttgart | GERMANY | 
www.bosch.com
Tel. +49 7153 666-1155 | dirk.fa...@de.bosch.com<mailto:dirk.fa...@de.bosch.com>

Sitz: Stuttgart, Registergericht: Amtsgericht Stuttgart, HRB 14000;
Aufsichtsratsvorsitzender: Franz Fehrenbach; Geschäftsführung: Dr. Volkmar 
Denner,
Prof. Dr. Stefan Asenkerschbaumer, Dr. Michael Bolle, Dr. Christian Fischer, 
Dr. Stefan Hartung,
Dr. Markus Heyn, Harald Kröger, Christoph Kübel, Rolf Najork, Uwe Raschke, 
Peter Tyroller
​
Von: osgi-dev-boun...@mail.osgi.org <osgi-dev-boun...@mail.osgi.org> Im Auftrag 
von Jürgen Albert via osgi-dev
Gesendet: Mittwoch, 23. September 2020 08:56
An: osgi-dev@mail.osgi.org
Betreff: Re: [osgi-dev] SCR: ServiceInjection into Fields that are 
Optional<Service>


Hi,

The Idea of Stefan is not that far off, as CDI already has something similar. 
This would not really a complete new feature, but a more convenient way of 
declaring a reference as a field or constructor injection.

If I'm not mistaken

@Reference
private Optional<FooService> foo;

should be equivilant to

@Reference(cardinality = ReferenceCardinality.OPTIONAL)
private FooService factory;

In CDI this is described here: 
https://docs.osgi.org/specification/osgi.enterprise/7.0.0/service.cdi.html#service.cdi-reference.optional<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.osgi.org%2Fspecification%2Fosgi.enterprise%2F7.0.0%2Fservice.cdi.html%23service.cdi-reference.optional&data=02%7C01%7Cdirk.fauth%40de.bosch.com%7C836e477cc7884b631c5308d85f8dd199%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364409998608001&sdata=OmagQP6UDX0y%2Fn1qvEt7MKwzC0h1%2BPJ%2FOZW%2FJ8hg2AY%3D&reserved=0>

BTW: Marks idea is intriguing as well and would be a comfortable solution as 
well.

Regards,

Jürgen.
Am 23/09/2020 um 07:31 schrieb Fauth Dirk (CAP-SST/ESM1) via osgi-dev:
Hi,

I think the usage of a Java Optional is not correct here. I don’t understand 
why it should be better to have an Optional that needs to be checked for the 
empty state compared to a good old null check. IMHO Optionals in Java are good 
and necessary when thinking of collection processing, but for a field that 
references a single service I don’t see the benefit. And I am not aware of such 
support in OSGi DS.

Even your example with the bind method will not work, because the bind method 
is never invoked if no such service is available and therefore set. So if no 
service is set your Optional will be null and you are in the same situation as 
without the Optional. And in the dynamic case of unbinding an existing service, 
the unbind method is missing to recreate the empty Optional.

Mit freundlichen Grüßen / Best regards

Dirk Fauth

Cross Automotive Platforms - Systems, Software and Tools, (CAP-SST/ESM1)
Robert Bosch GmbH | Postfach 30 02 40 | 70442 Stuttgart | GERMANY | 
www.bosch.com<http://www.bosch.com>
Tel. +49 7153 666-1155 | dirk.fa...@de.bosch.com<mailto:dirk.fa...@de.bosch.com>

Sitz: Stuttgart, Registergericht: Amtsgericht Stuttgart, HRB 14000;
Aufsichtsratsvorsitzender: Franz Fehrenbach; Geschäftsführung: Dr. Volkmar 
Denner,
Prof. Dr. Stefan Asenkerschbaumer, Dr. Michael Bolle, Dr. Christian Fischer, 
Dr. Stefan Hartung,
Dr. Markus Heyn, Harald Kröger, Christoph Kübel, Rolf Najork, Uwe Raschke, 
Peter Tyroller
​
Von: osgi-dev-boun...@mail.osgi.org<mailto:osgi-dev-boun...@mail.osgi.org> 
<osgi-dev-boun...@mail.osgi.org><mailto:osgi-dev-boun...@mail.osgi.org> Im 
Auftrag von Mark Hoffmann via osgi-dev
Gesendet: Mittwoch, 23. September 2020 06:35
An: Stefan Bischof <stbisc...@bipolis.org><mailto:stbisc...@bipolis.org>; OSGi 
Developer Mail List <osgi-dev@mail.osgi.org><mailto:osgi-dev@mail.osgi.org>
Betreff: Re: [osgi-dev] SCR: ServiceInjection into Fields that are 
Optional<Service>

Hi Stefan,

I believe Optionals are not optimal for that.

If a service is removed, you would need a new empty optional instance. 
Optionals doesn't support to hold a state.

In addition to that you don't have callbacks where to get notified about 
adding, modifying or removing a service.

I could imagine, to get an object similar to the promise injected, that 
supports the lifecycle callbacks, as well as resolving, unresolving or 
re-resolving as well as handling cardinality.

A fluent API around the OSGi's ServiveTracker could be a solution. Injecting 
Optional for just a subset of the supported cases in DS would, from my 
perspective "pollute" the spec.

Maybe you can realize a custom solution using ServiceHooks?

Regards,

Mark Hoffmann
M.A. Dipl.-Betriebswirt (FH)
CEO/CTO

Phone: +49 3641 384 910<tel:+49%203641%20384%20910> 0
Mobile: +49 175 701 2201<tel:+49%20175%20701%202201>
E-Mail: m.hoffm...@data-in-motion.biz<mailto:m.hoffm...@data-in-motion.biz>

Web: 
www.datainmotion.de<https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.datainmotion.de%2F&data=02%7C01%7Cdirk.fauth%40de.bosch.com%7C836e477cc7884b631c5308d85f8dd199%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364409998608001&sdata=ALg%2BYzv8NKlcfy93RIe%2FQVpeu88r7f%2FD5%2FNfZUxGwg4%3D&reserved=0>

Data In Motion Consulting GmbH
Kahlaische Strasse 4
07745 Jena
Germany

Geschäftsführer/CEO
Mark Hoffmann
Jürgen Albert

Jena HRB 513025
Steuernummer 162/107/05779
USt-Id DE310002614


-------- Ursprüngliche Nachricht --------
Von: Stefan Bischof via osgi-dev 
<osgi-dev@mail.osgi.org<mailto:osgi-dev@mail.osgi.org>>
Datum: 22.09.20 15:48 (GMT+01:00)
An: osgi-dev@mail.osgi.org<mailto:osgi-dev@mail.osgi.org>
Betreff: [osgi-dev] SCR: ServiceInjection into Fields that are Optional<Service>


Hi,

I like it to use Optionals if it is possible that a field could be null.

In context of OSGi Services with SCR that means I have to handle it like this:

```

@Component
public class MyComponent
{

    Optional<Foo> oFoo = Optional.empty();

    @Reference(cardinality = ReferenceCardinality.OPTIONAL)
    void bindFoo(Foo foo)
    {
        oFoo = Optional.of(foo);
    }
}

```

What I really want to do is this:

```

@Component
public class MyComponent
{

    @Reference
    Optional<Foo> oFoo;

}

```

We have something like this in OSGi - CDI Integration Specification
https://youtu.be/7-UUJ4WkMsg?t=839<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fyoutu.be%2F7-UUJ4WkMsg%3Ft%3D839&data=02%7C01%7Cdirk.fauth%40de.bosch.com%7C836e477cc7884b631c5308d85f8dd199%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364409998617998&sdata=M511MIlXhlhzQUVitzDE6yEeI1IHWoNWLO1E%2BXtlV3M%3D&reserved=0>

It would be nice to have this feature with the new version of the R8 DS Spec.



Regards

Stefan



_______________________________________________

OSGi Developer Mail List

osgi-dev@mail.osgi.org<mailto:osgi-dev@mail.osgi.org>

https://mail.osgi.org/mailman/listinfo/osgi-dev<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.osgi.org%2Fmailman%2Flistinfo%2Fosgi-dev&data=02%7C01%7Cdirk.fauth%40de.bosch.com%7C836e477cc7884b631c5308d85f8dd199%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364409998617998&sdata=nl4WQU4CvQvuOOkhbAqWxyl2XCPGX1cNOR0jIWAehTA%3D&reserved=0>

--

Jürgen Albert

Geschäftsführer



Data In Motion Consulting GmbH



Kahlaische Str. 4

07745 Jena



Mobil:  0157-72521634

E-Mail: j.alb...@datainmotion.de<mailto:j.alb...@datainmotion.de>

Web: 
www.datainmotion.de<https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.datainmotion.de%2F&data=02%7C01%7Cdirk.fauth%40de.bosch.com%7C836e477cc7884b631c5308d85f8dd199%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364409998627993&sdata=vsjHU%2BnJw3%2Bz2aTKwLK%2BOZkqz7yp11ETRT1LzqFB5CQ%3D&reserved=0>



XING:   
https://www.xing.com/profile/Juergen_Albert5<https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.xing.com%2Fprofile%2FJuergen_Albert5&data=02%7C01%7Cdirk.fauth%40de.bosch.com%7C836e477cc7884b631c5308d85f8dd199%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364409998627993&sdata=k%2BMqdlIEy7PJ1e8BEGdHdMIFyBABIVCWPDqQzXQrr7A%3D&reserved=0>



Rechtliches



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

Reply via email to