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
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
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 *Mark Hoffmann via osgi-dev
*Gesendet:* Mittwoch, 23. September 2020 06:35
*An:* Stefan Bischof <stbisc...@bipolis.org>; OSGi Developer Mail List
<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%7C51498f5471384847812d08d85f7a1363%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364325203048757&sdata=fOCfVktsB9WtHbAiSKSRVQl3aMz%2FaQGWkXpUJ5xfkEI%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%7C51498f5471384847812d08d85f7a1363%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C637364325203058712&sdata=NgPro2qM5YlFjMxcAuqwhyVgQW0HWQRMUcjUNF1hDjA%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
https://mail.osgi.org/mailman/listinfo/osgi-dev
--
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
Web: www.datainmotion.de
XING: https://www.xing.com/profile/Juergen_Albert5
Rechtliches
Jena HBR 513025
_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev