How to use OSGi services in Tapestry

I have my Tapestry web application that offers full CRUD functionality for some data set. So far, I've been using mocks to develop and test it.

However, I need to now replace that mock by an OSGi service (ran with Felix).

My main questions/issues are:

  • Do I need to inject the OSGi service into my tapestry app and if so, how to do it?
  • Assuming it's injected, are there any practices on how to use it? Any special ways I should call the methods or anything like that?
  • Do I need to do something with it after I've called the methods I need, or is that done automatically?
  • Do I need to worry about HttpService (I glanced some complaints about it, but didn't quite understand it)
  • To provide you with some context, this is how I'm doing it now

    public class MyPage {
        ...
        @Inject
        private MyDao dao;
        ...
        @Property
        private List<Entry> entries;
        ...
        void onPrepareForRender() {
            ...
            this.entries = this.dao.getAll()
            ...
        }
        ...
    }
    

    What I would like is to replace MyDao dao with an OSGi service that provides the same functionality so I can use it in onPrepareForRender method, for example.

    I can safely assume that the OSGi bundle associated with the service will already be runnning.

    The rest of the application is made as an OSGi application. I've added/changed stuff in my pom.xml (maven-bundle-plugin, added some manifest entries as instructed in http://www.javabeat.net/2011/11/writing-an-osgi-web-application/).

    However, it still doesn't work.

    I'm trying to convert my WAR to WAB, btw.

    Thank you in advance :)

    EDIT: Problem while converting and deploying from WAR to WAB

    When I package my web-app as war stuff works fine. However, when I package it as bundle , and try to deploy it in GlassFish, I get the following error message:

    Error occurred during deployment: There is no installed container capable of handling this application admin-war. Please see server.log for more details.

    When I go to server.log, here's what it says:

    [#|2013-08-27T17:11:57.600+0200|INFO|glassfish3.1.2|org.glassfish.admingui|_ThreadID=28;_ThreadName=Thread-2;|uploadFileName=admin-war-2.01-SNAPSHOT.jar|#]
    
    [#|2013-08-27T17:11:57.920+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:57.939+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=33;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:57.972+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:57.973+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:57.974+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:57.990+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:57.991+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:58.006+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:58.008+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:58.008+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:58.024+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:58.025+0200|WARNING|glassfish3.1.2|javax.enterprise.system.tools.deployment.com.sun.enterprise.deploy.shared|_ThreadID=29;_ThreadName=Thread-2;|DPL8031: Ignoring WEB-INF/ because the containing archive C:glassfish3glassfishdomainsdomain1applicationsadmin-war recorded it as a pre-existing stale file|#]
    
    [#|2013-08-27T17:11:58.026+0200|SEVERE|glassfish3.1.2|javax.enterprise.system.core.com.sun.enterprise.v3.server|_ThreadID=29;_ThreadName=Thread-2;|Module type not recognized for module C:glassfish3glassfishdomainsdomain1applicationsadmin-war|#]
    
    [#|2013-08-27T17:11:58.026+0200|SEVERE|glassfish3.1.2|javax.enterprise.system.core.com.sun.enterprise.v3.server|_ThreadID=29;_ThreadName=Thread-2;|There is no installed container capable of handling this application admin-war|#]
    
    [#|2013-08-27T17:11:58.087+0200|INFO|glassfish3.1.2|org.glassfish.admingui|_ThreadID=28;_ThreadName=Thread-2;|Exception Occurred :Error occurred during deployment: There is no installed container capable of handling this application admin-war. Please see server.log for more details.|#]
    

    I'm not that familiar with OSGI but you will need to use build* methods in your AppModule to provide the DAO's. In my example below, I have assumed that OsgiBundle is a way of accessing OSGI services, I have no idea what the actual OSGI class is called.

    eg:

    public class MyAppModule {
        public void contributeApplicationDefaults(MappedConfiguration<String, String> config) {
            config.add("osgi.path", "path/to/osgi.xml");
        }
    
        public OsgiBundle buildOsgiBundle(@Symbol("osgi.path") String osgiPath) {
            // lookup the OSGI bundle somehow
        }
    
        public MyDao buildMyDao(OsgiBundle osgiBundle) {
            return osgiBundle.getService(MyDao.class);
        }   
    
        public SomeOtherDao buildSomeOtherDao(OsgiBundle osgiBundle) {
            return osgiBundle.getService(SomeOtherDao.class);
        }   
    }
    

    If you want deeper integration (ie automatically publish all OSGI services as Tapestry IOC services) then take a look at the TapestrySpringFilter which publishes all spring beans as Tapestry IOC services.

    链接地址: http://www.djcxy.com/p/74726.html

    上一篇: 运行一个包含在OSGi包中的jar

    下一篇: 如何在Tapestry中使用OSGi服务