I always simply use slf4j and a suitable logger implementation.
Pax logging works quite well. The main advantage is that your logging works
exactly like outside of OSGi.

Internally it is maybe not the cleanest way but as a user this works really
nicely.

Christian

2018-01-22 7:06 GMT+01:00 Tanvir via osgi-dev <osgi-dev@mail.osgi.org>:

>
> In DS, for accessing a service reference, the class has to be a Component.
> That means to access common  services, such as log service, all classes
> becomes Components. In the following example, to access log service all
> classes in Emp bundle became Components.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *@Component public class Emp implements EmpService {   LogService log
> @Reference (cardinality = ReferenceCardinality.MANDATORY)   void
> setLogService(LogService log) { this.log = log; }   } @Component public
> class EmpAddress  {   LogService log   @Reference (cardinality =
> ReferenceCardinality.MANDATORY)   void setLogService(LogService log) {
> this.log = log; } } @Component public class EmpPayroll  {   LogService log
>   @Reference (cardinality = ReferenceCardinality.MANDATORY)   void
> setLogService(LogService log) { this.log = log; } }*
>  ....
>
> *Question: *Is there a way to solve this proliferation of DS Components
> and References?
>
> *Solution A*: We can factor out reference code and move them to a
> RefManager Component. Also maintain a shared static variable to LogService
> for all other classes.
>
> @Component
> public class RefManager {
>   *static *LogService log
>   @Reference (cardinality = ReferenceCardinality.MANDATORY)
>   void setLogService(LogService log) { this.log = log; }
>  *static* LogService  getLogger() { return log;}}
>
> @Component
> public class Emp implements EmpService {
>    ..
>    RefManager. getLogger.log();
> }
>
> public class EmpAddress  {
>      ..
>    RefManager. getLogger.log();
> }
> public class EmpPayroll  {
>    ..
>    RefManager. getLogger.log();
> }
>
>
> *Problem:* Now we can end up with null LogService reference even though
> LogService ref  is MANDATORY . E.g. if EmpService  is activated before
> RefManager  or LogService, it gets null LogService ref.
>
> *Question:* If we set LogService and RefManager as immediate components
> (RefManager  is immediate  by default for  being a Component), is it
> guaranteed that  they will be activated before EmpService?
>
> *Solution B: *
> Another solution for the above problem is to view LogService as OPTIONAL.
> However, an optional reference can be null requiring a null check. That
> means each use of Log Service  api require the following null check.
>
> If (log!=null) log.log(..);
>
> *Problem: * Considering that log apis are heavily used, this seems
> tedious and error-prone. Is there a better way of using OPTIONAL reference?
>
>
>
>
> --
> Best,
> Tanvir
>
>
> _______________________________________________
> OSGi Developer Mail List
> osgi-dev@mail.osgi.org
> https://mail.osgi.org/mailman/listinfo/osgi-dev
>



-- 
-- 
Christian Schneider
http://www.liquid-reality.de

Computer Scientist
http://www.adobe.com
_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to