This is a bug in your code.

In B.activate you have:

     System.out.println("Activation of componentA"); 
     a.printA(); 
So first, the print statement is misleading since this is component B!
Second, since B has an optional reference (0..1) to A, "a" may be null in 
the activator.
So I think what is happening is that B is being activated, it prints out a 
statement that incorrectly says it is A and then takes a null pointer 
exception since "a" is null. Since the activator of B throws an exception, 
B fails to activate. Since B fails to activate, there is no B to satisfy 
the mandatory (1..1) reference that A has on B. Therefore A is unsatisfied 
and cant be activated. So neither is activated.

-- 

BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
[email protected]

office: +1 386 848 1781
mobile: +1 386 848 3788




From:
rajalaxmi chandran <[email protected]>
To:
BJ Hargrave/Austin/i...@ibmus
Cc:
[email protected]
Date:
2009/05/15 04:31
Subject:
Re: [osgi-dev] circular service dependencies in osgi




Hello Hargrave,
 
serviceAimpl has the following code:
 
package serviceAimpl; 
import serviceA.*; 
import serviceB.*; 
import org.osgi.service.component.*; 
 
public class serviceAimpl implements serviceA{ 
serviceB b; 
  public void set(serviceB b) 
  { 
    System.out..println("This is the set"); 
    this.b=b; 
  } 
  public void unset(serviceB b) 
  { 
     System.out.println("This is the unset"); 
     this..b=null; 
  } 
  public void activate(ComponentContext cnt) 
  { 
     System.out.println("Activation of componentA"); 
     b.printB(); 
  } 
  public void deactivate(ComponentContext cnt) 
  { 
     System.out.println("Deactivation of componentA"); 
  } 
  public void printA() 
  { 
    System.out..println("This is the printA function"); 
  } 
} 
 
 
serviceBimpl has the following code:
 
package serviceBimpl; 
import serviceB.*; 
import serviceA.*; 
import org.osgi.service.component.*; 
 
 public class serviceBimpl implements serviceB{ 
 serviceA a; 
   public void printB() 
   { 
     System.out.println("This is the service b print function"); 
   } 
   public void set(serviceA a) 
   { 
     System.out.println("This is the set"); 
     this.a=a; 
   } 
  public void unset(serviceA a) 
  { 
    System.out..println("This is the unset"); 
    this.a=null; 
  } 
  public void activate(ComponentContext cnt) 
  { 
     System.out.println("Activation of componentA"); 
     a.printA(); 
  } 
  public void deactivate(ComponentContext cnt) 
  { 
     System.out.println("Deactivation of componentA"); 
  } 
} 
 
This is what i did in the osgi console
 
972 RESOLVED ComponentA_1.0.0 
984 RESOLVED ComponentB_1.0.0 
 
osgi> start 984 
osgi> start 972 
osgi> Activation of componentA
 
b.printB() is not invoked... why is it?????????????/
Thank you,
 
Raji 
 
B has an optional dynamic reference to A.
A has a mandatory dynamic reference to B.

So B must be activated first without an A service.
A can then be activated with the B service.
B can then have the A service bound to it.


-- 

BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
[email protected]

office: +1 386 848 1781
mobile: +1 386 848 3788




From:
rajalaxmi chandran <[email protected]>
To:
[email protected]
Date:
2009/05/14 12:32
Subject:
[osgi-dev] circular service dependencies in osgi
Sent by:
[email protected]






Hi,
please help....
i am stuck with circular dependecies in osgi. i am using declarative 
services. This is my situation
*Component A registers ServiceA
*Component B registers ServiceB
*Component A needs to use ServiceB
*Component B needs to use ServiceA.

componetA.xml file has:
<?xml version="1.0" encoding="UTF-8"?> 
<component name="componentA" immediate="true"> 
<implementation class="serviceAimpl.serviceAimpl"/> 
      <service> 
          <provide interface="serviceA.serviceA"/> 
      </service> 
<reference name="SERVICEBUSE" interface="serviceB.serviceB" bind="set" 
unbind=unset" cardinality="1..1" policy="dynamic"/> 
</component> 

componentB.xml file has: 
<?xml version="1..0" encoding="UTF-8"?> 
<component name="componentB"> 
<implementation class="serviceBimpl.serviceBimpl"/> 
        <service> 
               <provide interface="serviceB.serviceB"/> 
       </service> 
<reference name="SERVICEAUSE" interface="serviceA.serviceA" bind="set" 
unbind="unset" cardinality="0..1" policy="dynamic"/> 
</component> 

component A gets activated because of the immediate attribute being true, 
but i cannot use the serviceB nor is componentB able to use serviceA.... 
how do i rectify this error???????????? 

how do we implement service contracts in OSGI???? 

please help 
thank you 
Raji

Bring your gang together. Do your thing. Find your favourite Yahoo! Group.
_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to