The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

sankallada (11) [Avatar] Offline
#1
Hi,

I'm a newbie in this technology and following the book "OSGi in Depth" to get a hold on it. I'm facing an issue while running the code provided for Chapter 2.

Please find my Development environment details below.

Java : Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)
Felix : felix-framework-4.0.2
Os : windows 7 (64bit)

Please find the code below (from OSGiinDepthch02modules)

Service
==========
package manning.osgi.helloworld;

public interface Printer {

public void print(String message);

}

impl
===
package manning.osgi.helloworld.impl;

import java.text.DateFormat;
import java.util.Date;

import manning.osgi.helloworld.Printer;


public class PrinterImpl implements Printer {

private static final String SEPARATOR = ": ";

public void print(String message) {
System.out.println(getCurrentTime() + SEPARATOR + message);
}

private String getCurrentTime() {
return DateFormat.getTimeInstance(DateFormat.LONG).format(new Date());
}
}

Activator
======

package manning.osgi.helloworld.impl;

import manning.osgi.helloworld.Printer;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;


public class PrinterActivator implements BundleActivator {

private ServiceRegistration serviceRegistration;

public void start(BundleContext bundleContext) throws Exception {
serviceRegistration =
bundleContext.registerService(
Printer.class.getName(),
new PrinterImpl(),
null);
}

public void stop(BundleContext bundleContext) throws Exception {
serviceRegistration.unregister();
}

}


Manifest
======
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: helloworld.impl
Bundle-Version: 1.4
Export-Package: manning.osgi.helloworld;version=1.4.0
Import-Package: org.osgi.framework
Bundle-Activator: manning.osgi.helloworld.impl.PrinterActivator



Code for Client Bundle is given as below
=============================

Printer Client
------------------
package manning.osgi.helloworld.client;

import manning.osgi.helloworld.Printer;

public class RunnablePrinterClient implements Runnable {

private static final int TWO_SECS = 2000;

private boolean stop;

private Printer printer;

void start() {
stop = false;
new Thread(this).start();
}

void stop() {
stop = true;
}

public void run() {
while (!stop) {

printer.print("Hello...");

try {
Thread.sleep(TWO_SECS);
} catch (InterruptedException e) {
// Someone else interrupted the thread, let's just stop.
stop = true;
}
}
}

void setPrinterService(Printer printer) {
this.printer = printer;
}

}

Activator
------------

package manning.osgi.helloworld.client;

import manning.osgi.helloworld.Printer;

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;


public class PrinterClientActivator implements BundleActivator, ServiceListener {

private RunnablePrinterClient runnablePrinterClient = new RunnablePrinterClient();
private BundleContext bundleContext;
private ServiceReference serviceReference;

/* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
this.bundleContext = bundleContext;

serviceReference =
bundleContext.getServiceReference(Printer.class.getName());

if (serviceReference != null) {
Printer printer = (Printer)
bundleContext.getService(serviceReference);

if (printer != null) {
runnablePrinterClient.setPrinterService(printer);
runnablePrinterClient.start();
}
}

bundleContext.addServiceListener(this,
"(objectClass=" + Printer.class.getName() + ")");
}

/* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
runnablePrinterClient.stop();
if (serviceReference != null) {
bundleContext.ungetService(serviceReference);
}

bundleContext.removeServiceListener(this);
}

/* (non-Javadoc)
* @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
*/
public void serviceChanged(ServiceEvent serviceEvent) {
switch (serviceEvent.getType()) {
case ServiceEvent.UNREGISTERING: {
runnablePrinterClient.stop();
bundleContext.ungetService(serviceEvent.getServiceReference());
break;
}
case ServiceEvent.REGISTERED: {
Printer printer = (Printer)
bundleContext.getService(serviceEvent.getServiceReference());

if (printer != null) {
runnablePrinterClient.setPrinterService(printer);
runnablePrinterClient.start();
}
break;
}
}
}
}

Manifest
------------
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: helloworld.client
Bundle-Version: 1.1.0
Import-Package: manning.osgi.helloworld;version=1.4.0,
org.osgi.framework
Bundle-Activator: manning.osgi.helloworld.client.PrinterClientActivator


The bundles are built successfully, and I'm able to install the provider bundle in "Felix". But while trying to start the client bundle i'm getting the following error.


Exception in thread "Thread-1" java.lang.IncompatibleClassChangeError: Found interface manning.osgi.helloworld.Printer, but class was expected
at manning.osgi.helloworld.client.RunnablePrinterClient.run(RunnablePrinterClient.java:25)
at java.lang.Thread.run(Thread.java:662)

I'm unable to find the root cause for this issue. Please help

Note: I tried to run this sample with equinox (org.eclipse.osgi_3.7.0.v20110613.jar) too, but managed to get the same error only.



San
holly.cummins (16) [Avatar] Offline
#2
Re: Issue with a sample provided along with the Book
Hi,

This is actually the forum for Enterprise OSGi in Action (which is what OSGi in Depth was called before it was called OSGi in Depth, so it's an understandable confusion). I see you've also posted on the OSGi in Depth forum, so hopefully Alex will be able to help you out over there.

Of course, as you're here, you may also find Enterprise OSGi in Action an interesting book! I think it's a good complement to the material covered in OSGi in Depth.

Holly