I am working on vaadin based osgi application. Before mentioning the problem briefly, i would tell what i am trying to do.
I have a main vaadin module which is a web app and has a corresponding vaadin ui. then I have sub modules which are vaadin custom components and they get added to the main module.
Ok, the problem I am facing is when I am trying to access resources from the submodule into my main module. The add, remove of the bundle happens but the getResource() is never triggered. I expect that HttpService registerServlet should try to retreive the resource when the URL is for example like
http://localhost:8181/main/APP/connector/0/6/source/image2.png
public class Activator implements BundleActivator, BundleListener {
@Override
public void start(final BundleContext context) throws Exception {
Activator.context = context;
customModuleResourceProvider = new CustomModuleResourceProvider();
context.addBundleListener(this);
registerCustomModuleBundles(context);
httpServiceTracker = new ServiceTracker(context, HttpService.class.getName(), new HttpServiceTrackerCustomizer());
httpServiceTracker.open();
}
private void registerCustomModuleBundles(final BundleContext ctx) {
for (final Bundle bundle : ctx.getBundles()) {
if (bundle.getSymbolicName() != null && bundle.getSymbolicName().startsWith("com.org.app.submodule")) {
resourceProvider.add(bundle);
}
}
}
public class HttpServiceTrackerCustomizer implements ServiceTrackerCustomizer {
HttpService httpService;
@Override
public HttpService addingService(final ServiceReference reference) {
httpService = (HttpService) context.getService(reference);
try {
final Dictionary<String, String> initParams = new Hashtable<String, String>();
initParams.put("UI", "com.org.app.mainui");
httpService.registerServlet("/main/*", new SimpleVaadinServletForUI(), initParams, customModuleResourceProvider);
} catch (final ServletException e) {
e.printStackTrace();
} catch (final NamespaceException e) {
e.printStackTrace();
}
return httpService;
}
}
...
public class SimpleVaadinServletForUI extends VaadinServlet { }
}
Resource Provider
public class CustomModuleResourceProvider implements HttpContext {
private static final Logger LOG = LoggerFactory.getLogger(CustomModuleResourceProvider.class);
private final List<Bundle> resources = new ArrayList();
@Override
public boolean handleSecurity(final HttpServletRequest request, final HttpServletResponse response)
throws IOException {
return true;
}
@Override
public URL getResource(final String uri) {
URL resource = null;
LOG.debug("URl CMRP {} ", uri);
for (final Bundle bundle : resources) {
resource = bundle.getResource(uri);
if (resource != null) {
break;
}
}
return resource;
}
@Override
public String getMimeType(final String name) {
LOG.debug("Mime type {} ", name);
return "image/png";
}
public void add(final Bundle bundle) {
resources.add(bundle);
}
public void remove(final Bundle bundle) {
resources.remove(bundle);
}
}