Forums de discussion

Cannot cast into itself

thumbnail
Chris Jurado, modifié il y a 6 années.

Cannot cast into itself

Junior Member Publications: 66 Date d'inscription: 16/07/10 Publications récentes
So here's a bizarre error I'm getting while trying to build a sample OSGi service to run from a JSF portlet:

Caused by: java.lang.ClassCastException: com.dmi.sample.SingleClassSample cannot be cast to com.dmi.sample.SingleClassSample
at com.dmi.nasa.focus.demo.control.OSGiServiceClient.postConstruct(OSGiServiceClient.java:39)


This is from my managed bean:

	@PostConstruct
	public void postConstruct() {
		sampleServiceTracker = getServiceTracker();
	    sampleServiceTracker.open();
	    singleClassSample  = (SingleClassSample) sampleServiceTracker.getService();  //This is line 39
	}
	
	@PreDestroy
	public void preDestroy(){
		sampleServiceTracker.close();
	}


And this is my ServiceTracker:

package com.dmi.nasa.focus.demo.control;

import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import com.dmi.sample.SingleClassSample;

public class SampleServiceTracker extends ServiceTracker<singleclasssample, singleclasssample>{

    public SampleServiceTracker(BundleContext bundleContext) {
        super(bundleContext, SingleClassSample.class, null);
    }

}
</singleclasssample,>


My component class:

package com.dmi.sample;

import org.osgi.service.component.annotations.Component;

@Component(
		immediate = true,
		service = SingleClassSample.class)

public class SingleClassSample {

	public String makeItLive() {
		String message = "Success!";
		System.out.println(message);
		return message;
	}
	
}


And the Activator:

package com.dmi.sample;

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

public class EclipseServiceActivator implements BundleActivator{

    private ServiceRegistration registration;
    
    @Override
    public void start(BundleContext context) throws Exception {
 
        registration = context.registerService(SingleClassSample.class.getName(), new SingleClassSample(), null);
        System.out.println("###########Service Registered Successfully##############");
    }
 
    @Override
    public void stop(BundleContext context) throws Exception {
        registration.unregister();
        System.out.println("###########Service Unregistered##############");
         
    }

}


The service does register successfully.

I had tried this a different way earlier using an Interface and an implementation class, and got the same error. I did it again using just the one class and it didn't make a difference. Have you seen this before? I'm assuming I've just missed something elemental here, but I need another set of eyes.
thumbnail
Juan Gonzalez, modifié il y a 6 années.

RE: Cannot cast into itself

Liferay Legend Publications: 3089 Date d'inscription: 28/10/08 Publications récentes
thumbnail
Chris Jurado, modifié il y a 6 années.

RE: Cannot cast into itself

Junior Member Publications: 66 Date d'inscription: 16/07/10 Publications récentes
I had, but since the problem didn't seem to be quite the same I didn't look into it and I should have. I'll do that and let you know.
thumbnail
Chris Jurado, modifié il y a 6 années.

RE: Cannot cast into itself

Junior Member Publications: 66 Date d'inscription: 16/07/10 Publications récentes
Ok after modifying that suggested solution in my Maven pom.xml, it worked.

By adding the "provided" scope tag to the dependency it solved the issue.

		<dependency>
			<groupid>com.dmi.sample</groupid>
			<artifactid>eclipse.module.sample</artifactid>
			<scope>provided</scope>
			<version>1.0.0</version>
		</dependency>


So this solves the issue whether it's phrased as the other user had it, or as I had it.

Thanks very much.
thumbnail
Juan Gonzalez, modifié il y a 6 années.

RE: Cannot cast into itself

Liferay Legend Publications: 3089 Date d'inscription: 28/10/08 Publications récentes
Great! The issue here is not having your service-api jar in your WEB-INF/lib, as it uses the OSGI module one.

Thanks.