So what I did was to create an Activator class that is called when the bundle is deployed and started. The Activator class then gets the instance of org.apache.sling.jcr.api.SlingRepository which we can use to connect to the JCR. Here is the activator code:
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate = true, label = "Commons Activator")
public class Activator implements BundleActivator {
@Reference
private SlingRepository repository;
private static final Logger logger = LoggerFactory.getLogger(Activator.class);
@Activate
@Override
public void start(BundleContext context) throws Exception {
logger.info(context.getBundle().getSymbolicName() + " started");
//My own factory class instance
ResourceResolverDiscoveryService rrf = ResourceResolverDiscoveryService.getInstance();
//Set the 'repository' in your factory class instance
rrf.setSlingRepositoryFactory(repository);
}
@Deactivate
@Override
public void stop(BundleContext context) throws Exception {
logger.info(context.getBundle().getSymbolicName() + " stopped");
}
}
Then in the class where I want to use JCR to store the data I did the following:
public class StoreInJCR {
public void store(Quote quote) throws LoginException, RepositoryException {
SlingRepository slingRepository = ResourceResolverDiscoveryService.getInstance().getSlingRepositoryFactory();
// GOT IT!!! Mission Accomplished
Session session = slingRepository.loginAdministrative(null);
Node root = session.getRootNode();
// Further code
.
.
}
}
Hope someone finds this useful.