Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 330104

Summary: application context registered as service with objectClass=org.eclipse.gemini.blueprint.context.DelegatedExecutionOsgiBundleApplicationContext but is not an instance of it
Product: [RT] Gemini.Blueprint Reporter: Bjoern <temp>
Component: CoreAssignee: Project Inbox <gemini.blueprint-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: cleau, j.tomas.samuelsson
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Supertype hierarchy of AbstractOsgiBundleApplicationContext
none
Patch against Spring DM subversion revision 2435 none

Description Bjoern CLA 2010-11-12 11:07:09 EST
Build Identifier: 1.0.0.M1

2010-11-12 16:46:33,268 INFO  o.e.g.b.c.s.OsgiBundleXmlApplicationContext[]: Publishing application context as OSGi service with properties {org.springframework.context.service.name=com.bosch.msp.security, Bundle-SymbolicName=com.bosch.msp.security, Bundle-Version=0.0.1.SNAPSHOT}
2010-11-12 16:46:33,268 DEBUG o.e.g.b.c.s.OsgiBundleXmlApplicationContext[]: Publishing service under classes {org.eclipse.gemini.blueprint.context.DelegatedExecutionOsgiBundleApplicationContext, org.eclipse.gemini.blueprint.context.ConfigurableOsgiBundleApplicationContext, org.springframework.context.ConfigurableApplicationContext, org.springframework.context.ApplicationContext, org.springframework.context.Lifecycle, org.springframework.beans.factory.ListableBeanFactory, org.springframework.beans.factory.HierarchicalBeanFactory, org.springframework.context.MessageSource, org.springframework.context.ApplicationEventPublisher, org.springframework.core.io.support.ResourcePatternResolver, org.springframework.beans.factory.BeanFactory, org.springframework.core.io.ResourceLoader, org.springframework.beans.factory.DisposableBean}
2010-11-12 16:46:33,268 DEBUG o.e.g.b.c.s.OsgiBundleXmlApplicationContext[]: Post refresh error
java.lang.IllegalArgumentException: Service object is not an instance of "org.eclipse.gemini.blueprint.context.DelegatedExecutionOsgiBundleApplicationContext".
	at org.apache.felix.framework.Felix.registerService(Felix.java:2835) ~[felix.jar:na]
	at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:251) ~[felix.jar:na]
	at org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext$3.run(AbstractOsgiBundleApplicationContext.java:353) ~[na:na]
	at org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext$3.run(AbstractOsgiBundleApplicationContext.java:351) ~[na:na]
	at java.security.AccessController.doPrivileged(Native Method) ~[na:1.6.0_22]
	at org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext.publishContextAsOsgiServiceIfNecessary(AbstractOsgiBundleApplicationContext.java:351) ~[na:na]
	at org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext.finishRefresh(AbstractOsgiBundleApplicationContext.java:223) ~[na:na]
	at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:328) ~[na:na]
	at org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85) ~[na:na]
	at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:290) ~[na:na]
	at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:137) ~[na:na]
	at java.lang.Thread.run(Thread.java:662) ~[na:1.6.0_22]


Reproducible: Always

Steps to Reproduce:
1. Class
public class Spike
{
    private BundleContext bundleContext;

    public void setBundleContext(final BundleContext bundleContext) {
        this.bundleContext = bundleContext;
        System.out.println("setBundleContext called with BundleContext=" + bundleContext);
    }

    public void init()
    {
        System.out.println("initialized. BundleContext=" + bundleContext);
    }
}

2. Blueprint XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <bean id="spike" class="foo.bar.Spike" init-method="init">
        <property name="bundleContext" ref="blueprintBundleContext"/>
    </bean>
</blueprint>

3. Deploy bundle in Apache Felix 3.0.6 with Gemini Bluprint 1.0.0.M1 and Spring dependencies 3.0.5.RELEASE.
Comment 1 Costin Leau CLA 2011-01-26 10:08:40 EST
Interesting - I recall seeing this problem in the past but don't recall what the issue was. Can you try testing under equinox since the class is an instance of the aforementioned interface and moreover various checks are made when exporting the class.
Comment 2 Bjoern CLA 2011-02-09 07:26:09 EST
Created attachment 188580 [details]
Supertype hierarchy of AbstractOsgiBundleApplicationContext
Comment 3 Bjoern CLA 2011-02-09 07:28:23 EST
Comment on attachment 188580 [details]
Supertype hierarchy of AbstractOsgiBundleApplicationContext

Hi Costin,

with equinox it is working. 

But I investigated the problem a little bit further and extended Felix
implementation at the code lines, the execption is thrown. I Just added to
display the service object's classe name and got the following stack trace:

java.lang.IllegalArgumentException: Service object is not an instance of
"org.eclipse.gemini.blueprint.context.DelegatedExecutionOsgiBundleApplicationContext".
Service object class=class
org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext$3
 at org.apache.felix.framework.Felix.registerService(Felix.java:2847)
~[felix.jar:na]
... 

It seems that Gemini tries to register an anonymous instance of
AbstractOsgiBundleApplicationContext with
objectClass=DelegatedExecutionOsgiBundleApplicationContext

I also digged into Gemini and found that AbstractOsgiBundleApplicationContext
indeed does not implement the interface
DelegatedExecutionOsgiBundleApplicationContext (see attached supertypes
hierachy of AbstractOsgiBundleApplicationContext)

/Bjoern
Comment 4 Tomas Samuelsson CLA 2011-03-11 08:14:44 EST
We've experienced the same problem when using Spring DM 2.0.0.M1 and found the problem to be the fact that the security manager were active. The real problem in our case is the following code in AbstractOsgiBundleApplicationContext.publishContextAsOsgiServiceIfNecessary:


                        // Publish under all the significant interfaces we see
                        boolean hasSecurity = (System.getSecurityManager() != null);

                        if (hasSecurity) {
                                try {
                                        serviceRegistration = AccessController.doPrivileged(new PrivilegedAction<ServiceRegistration>() {
                                                public ServiceRegistration run() {
                                                        return getBundleContext().registerService(serviceNames, AbstractOsgiBundleApplicationContext.this, serviceProperties);
                                                }
                                        }, acc);
                                } catch (AccessControlException ex) {
                                        logger.error("Application context service publication aborted due to security issues "
                                                        + "- does the bundle has the rights to publish the service ? ", ex);
                                }
                        } else {
                                serviceRegistration = getBundleContext().registerService(serviceNames, this, serviceProperties);
                        }

By changing the "this" reference in PrivilegedAction.run to "AbstractOsgiBundleApplicationContext.this" the problem is fixed.

/Tomas
Comment 5 Tomas Samuelsson CLA 2011-03-11 08:15:46 EST
Created attachment 190978 [details]
Patch against Spring DM subversion revision 2435
Comment 6 Costin Leau CLA 2011-06-08 06:00:54 EDT
fixed in rev #14
Thanks for the detailed report Tomas and Bjoern
Comment 7 Costin Leau CLA 2011-06-09 08:46:09 EDT
marked as closed