I have this Apache Felix activator which gets data source service:
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;
import org.DX_57.osgi.SH_27.api.SessionHandle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;
public class SessionHandleApp implements BundleActivator {
public static final String DSNAME = "jdbc/Oracle";
public ServiceTracker st;
@Override
public void start(final BundleContext bc) throws Exception {
debug("Activator started");
Filter filter = bc.createFilter("(&" + "(" + Constants.OBJECTCLASS
+ "=" + DataSource.class.getName() + ")" + "(jndi-name="
+ DSNAME + ")" + ")");
st = new ServiceTracker(bc, filter, null) {
@Override
public Object addingService(ServiceReference reference) {
DataSource ds = (DataSource) bc.getService(reference);
try {
debug(ds.getConnection().toString());
SessionHandle sh = new SessionHandleImpl();
sh.setDataSource(ds);
ServiceRegistration registerService = bc.registerService(SessionHandle.class.getName(), sh, new Properties());
} catch (SQLException e) {
}
return super.addingService(reference);
}
@Override
public void removedService(ServiceReference reference,
Object service) {
super.removedService(reference, service);
}
};
st.open();
}
public void stop(BundleContext bc) throws Exception {
boolean ungetService = bc.ungetService(bc.getServiceReference(SessionHandle.class.getName()));
st.close();
}
private void debug(String msg) {
System.out.println("JDBCBundleActivator: " + msg);
}
}
This solution works but legacy aproach with using a database driver classname to configure JDBC does not work well in OSGi.
How I must write a bundle with an activator that initializes the datasource and offers it as a service?