Thanks a lot for the code snippet!
It was exactly what I was looking for right now.
Based on your code I have created the following util method which gives a GreenHopper bean instance based on the class.
private <T> T getGreenHopperBean(Class<T> beanClass) {
OsgiContainerManager osgi = ComponentAccessor.getComponentOfType(OsgiContainerManager.class);
if (osgi == null) {
log.warn("OSGI Not Found");
return null;
}
log.debug("Trying to find the {} class from GreenHopper plugin AppContext", beanClass.getName());
Bundle[] bundles = osgi.getBundles();
log.debug("Amount of bundles: " + bundles.length);
try {
for (Bundle bundle : bundles) {
if ("com.pyxis.greenhopper.jira".equals(bundle.getSymbolicName())) {
log.warn("Bundle {}: {}", bundle.getBundleId(), bundle.getSymbolicName());
BundleContext bctx = bundle.getBundleContext();
ServiceReference[] refs = bctx.getAllServiceReferences(null, null);
if (refs != null) {
log.debug("Amount of services: " + refs.length);
for (ServiceReference ref : refs) {
Object prop = ref.getProperty("org.springframework.context.service.name");
if ("com.pyxis.greenhopper.jira".equals(prop)) {
log.debug("Found the service " + bctx.getService(ref));
ApplicationContext ghAppContext = (ApplicationContext) bctx.getService(ref);
String[] beanDefinitionNames = ghAppContext.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
log.trace("> {}", beanDefinitionName);
}
return ghAppContext.getBean(beanClass);
}
}
}
}
}
} catch (InvalidSyntaxException e) {
log.error("Some error during loading external bean", e);
}
return null;
}
Here is the usage example:
GreenHopperCacheManager greenHopperCacheManager = getGreenHopperBean(GreenHopperCacheManager.class);
greenHopperCacheManager.clearCaches(null);
All kudos from me today are going to you