Hi all,

I've created a simple ZK (zkoss) webapp using Maven 2 and
zk-archetype-webapp from
within Eclipse. I'm trying to convert this project (WAR) into OSGi bundle
simply by
adding more info into maven-war-plugin like this :

...
<plugin>
<artifactId>maven-war-plugin</artifactId>
 <groupId>org.apache.maven.plugins</groupId>
<version>2.1.1</version>
<configuration>
 <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
<archive>
<manifest>
 <addClasspath>false</addClasspath>
</manifest>
<manifestEntries>
 <Bundle-ManifestVersion>2</Bundle-ManifestVersion>
<Bundle-Name>Test :: Core :: ZK</Bundle-Name>
 <Bundle-SymbolicName>test.core.zk</Bundle-SymbolicName>
<Bundle-Version>1.0.0</Bundle-Version>
 <Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
<Bundle-Activator>test.core.ZkWebappActivator</Bundle-Activator>

<Import-Package>org.slf4j,javax.servlet;version="2.5.0",javax.servlet.http;version="2.5.0",javax.swing.filechooser,javax.xml.parsers,javax.xml.transform,javax.xml.transform.dom,javax.xml.transform.stream,org.w3c.dom,org.xml.sax,org.xml.sax.ext,org.xml.sax.helpers,org.osgi.framework;version="[1.5,2)"</Import-Package>
 <Require-Bundle>zk.osgi.bundle;bundle-version="6.5.2"</Require-Bundle>
<Bundle-Description>Core zk bundle</Bundle-Description>

<Bundle-RequiredExecutionEnvironment>JavaSE-1.6</Bundle-RequiredExecutionEnvironment>
<Web-ContextPath>/test</Web-ContextPath>
 <Webapp-Context>/test</Webapp-Context>
</manifestEntries>
</archive>
 </configuration>
</plugin>
...

Activator class looks like this :

package test.core;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZkWebappActivator implements BundleActivator, ServiceListener {
 private static final Logger logger =
LoggerFactory.getLogger(ZkWebappActivator.class);
 // TODO: ok to be static?
private static BundleContext context;
private static ServiceReference dbServiceRef;
 private static DbService dbService;

public void start(BundleContext context) throws Exception {
 logger.info("Starting core zk bundle...");
 ZkWebappActivator.context = context;
 synchronized (this) {
             // Listen for events pertaining to dictionary services.
            context.addServiceListener(this,
                "(&(objectClass=" + DbService.class.getName() + ")" +
                "(Language=*))");

            logger.info("Service listener registered");

            // Query for any service references matching any language.
            ServiceReference[] refs = context.getServiceReferences(
                DbService.class.getName(), "(Language=*)");

            // If we found any dictionary services, then just get
            // a reference to the first one so we can use it.
            if (refs != null)
            {
            logger.info("Found DbService!");
                dbServiceRef = refs[0];
                dbService = (DbService) context.getService(dbServiceRef);
            }
            else {
            logger.info("DbService reference is null!");
            }
        }
 }

public void stop(BundleContext context) throws Exception {
 logger.info("Stopping core zk bundle...");
 context.removeServiceListener(this);

}

public synchronized void serviceChanged(ServiceEvent event) {
 String[] objectClass = (String[])
event.getServiceReference().getProperty("objectClass");
 logger.debug("serviceChanged called!");

        // If a dictionary service was registered, see if we
        // need one. If so, get a reference to it.
        if (event.getType() == ServiceEvent.REGISTERED)
        {
        logger.debug("Detected registration of
"+event.getServiceReference().getClass().getCanonicalName());
            if (dbServiceRef == null)
            {
            logger.debug("Found DbService in serviceChanged!");
                // Get a reference to the service object.
            dbServiceRef = event.getServiceReference();
                dbService = (DbService) context.getService(dbServiceRef);
            }
        }
        // If a dictionary service was unregistered, see if it
        // was the one we were using. If so, unget the service
        // and try to query to get another one.
        else if (event.getType() == ServiceEvent.UNREGISTERING)
        {
        logger.debug("Detected UN-registration of
"+event.getServiceReference().getClass().getCanonicalName());
            if (event.getServiceReference() == dbServiceRef)
            {
                // Unget service object and null references.
                context.ungetService(dbServiceRef);
                dbServiceRef = null;
                dbService = null;


            }
        }
 }

public static DbService getDbService() {
return dbService;
 }

}

Activator class is in the WEB-INF/classes folder.

*The problem : Even if the service is properly registered (I can see it in
the webconsole), this always returns NULL!*
*What is really interesting is that if I create a simple bundle using
karaf-bundle-archetype and copy the same*
*Activator, it will find the service!*
*
*
*I'm using ServiceMix 4.5.2, Java 1.6.45 x64, Windows 7 x64 and Eclipse
Juno SR2.*

*What am I missing here? Thanx in advance.*

-- 
Bratislav Stojanovic, M.Sc.



-- 
Bratislav Stojanovic, M.Sc.

Reply via email to