Wednesday, February 8, 2012

Reflection on not using Reflection

SLF4J has done a good job by allowing users to continue using whatever logging framework they are currently using and then in time move on to LogBack. I like the way they require the adapter jar (e.g. slf4j-jcl.jar or slf4j-log4j12.jar) to have the org.slf4j.impl.StaticLoggerBinder and a few other classes for static binding. And those classes are supposed to implement interfaces from the aptly named org.slf4j.spi package

I thought they would load that class via reflection (=dynamically bind) but looking at the code in org.slf4j.LoggerFactory, it is not the case:
So that might suggest that when they packaged the slf4j-api.jar, they had either slf4j-simple.jar or slf4j-nop.jar in the classpath, however that would result in a circular dependency as ILoggerFactory interface that the above method returns is defined in slf4j-api.jar. So, I think when they packaged slf4j-api.jar, they have a stub implementation of StaicLoggerBinder (and other similar classes) but then remove the .class files from the jar. Sounds a bit unkosher, doesn't it?

I was comparing this approach to how JDBC registers drivers:
java.sql.DriverManager.registerDriver(java.sql.Driver driver);
But this would be called from a static intialization block in the driver implementaion, e.g.
package com.usta.MyDatabase;
public class MyDriver implements Driver {
  static {
    Driver SINGLETON = new MyDriver(...);
And that is why we have to do the following:
Connection con = DriverManager.getConnection("jdbc:mydatabase:dbname","guest","");
And getConnection() will ask the registered drivers if they can handle the url provided.

As we see with JDBC the Driver(the service provider) has to register itself, whereas, with SLF4J ILoggerFactory (the service provider), the registration (actually binding) is done automatically. This makes sense because with JDBC, we might want to use multiple databases at the same time.