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 0 Mobile:  +49 175 701 2201 E-Mail: 
m.hoffm...@data-in-motion.biz Web: www.datainmotion.de 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> Datum: 22.09.20  15:48  (GMT+01:00) An: 
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
    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

Reply via email to