2015-05-29 52 views
0

我正在致力於基於vaadin的osgi應用程序。在簡要提及問題之前,我會告訴我要做什麼。 我有一個主要vaadin模塊,這是一個web應用程序,並具有相應的vaadin用戶界面。然後我有子模塊,它是vaadin自定義組件,並將它們添加到主模塊中。當在HttpService註冊servlet中指定時,resourceprovider不起作用

好吧,我面臨的問題是當我試圖從子模塊訪問資源到我的主要模塊。捆綁的添加,刪除發生,但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); 
    } 
} 

回答

0

傳遞給registerServlet路徑應是「/主」。您指示「/ main/*」,但通配符是web.xml語法。參見綱要規範的第102.4節。