在HttpService寄存器servlet中指定时,resourceprovider不起作用

时间:2015-05-29 10:55:16

标签: osgi vaadin

我正在开发基于vaadin的osgi应用程序。在简要提到问题之前,我会说出我想要做什么。 我有一个主要的vaadin模块,它是一个Web应用程序,并具有相应的vaadin ui。然后我有子模块是vaadin自定义组件,它们被添加到主模块。

好的,我遇到的问题是当我尝试从子模块访问资源到我的主模块时。发生了添加,删除捆绑但是从不触发getResource()。我希望HttpService registerServlet应该尝试在URL例如

时检索资源
  

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 { }
}

资源提供者

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);
    }
}

1 个答案:

答案 0 :(得分:0)

传递给registerServlet的路径应为&#34; / main&#34;。您指示&#34; / main / *&#34;,但是wild-carding是web.xml语法。见概要说明书第102.4节。