You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "John E. Conlon" <jc...@verticon.com> on 2006/01/28 01:32:55 UTC

Re: Contribution of the dependency manager to Feli

Good 5 Minute overview. Thanks Peter.

One question it mentions
For each dependency: 
      * Instance fields are set, through reflection, if they exist.


But what if there are multiple dependencies with the same interface -
like the example?

thanks,
John

On Fri, 2006-01-27 at 07:42, Peter Neubauer wrote:
> Hi folks,
> we have made some more adaptations, and documented a bit what can be done with the xenotron service manager. I put the docs and the code up on http://wiki.ops4j.org/dokuwiki/doku.php?id=pax:xenotron until it can be merged with resources in Apapche. Take a look at it and change if you like, or maybe another format is better?
> 
> /peter
> 


Re: Contribution of the dependency manager to Feli

Posted by "John E. Conlon" <jc...@verticon.com>.
Good idea Niclas!

kind regards,
John
On Mon, 2006-01-30 at 19:24, Niclas Hedhman wrote:
> On Tuesday 31 January 2006 07:13, John E. Conlon wrote:
> > Your right, one can always get the dictionary from the Configuration and
> > the Configuration does provide some utility methods for getting typed
> > values, but I don't think I communicated very well my concern.
> 
> Perhaps one can provide the Configuration class as a utility.
> 
> public void update( Dictionary rawConfig )
>     throws Exception
> {
>     Configuration config = new Configuration( rawConfig );
>     :
> }
> 
> or something, and both camps could live happily together.
> 
> 
> Cheers
> Niclas
> 


Re: Contribution of the dependency manager to Feli

Posted by Niclas Hedhman <ni...@hedhman.org>.
On Tuesday 31 January 2006 07:13, John E. Conlon wrote:
> Your right, one can always get the dictionary from the Configuration and
> the Configuration does provide some utility methods for getting typed
> values, but I don't think I communicated very well my concern.

Perhaps one can provide the Configuration class as a utility.

public void update( Dictionary rawConfig )
    throws Exception
{
    Configuration config = new Configuration( rawConfig );
    :
}

or something, and both camps could live happily together.


Cheers
Niclas

Re: Contribution of the dependency manager to Feli

Posted by "John E. Conlon" <jc...@verticon.com>.
Hi Peter,


On Mon, 2006-01-30 at 13:00, Peter Neubauer wrote:
> On Monday 30 January 2006 16:49, John E. Conlon wrote:
> > Adding the configuration dependencies are a big plus. But have you
> > considered making the signature more generic?
> Mmmh the question is what you want to do with this. We were thinking that in 
> 90% of the cases you are using primitives and Strings in the configuration, 
> since if you are configuring with objects (e.g. serialized instances etc) you 
> probably will be better off using some other mechanism like service 
> dependencies etc.
> 
> So, we thought a reasonable help is to avoid the casts you usually have. OTOH 
> you can always retrieve the underlying Dictionary with 
> configuration.toDictionary().
> 
> 
> Not sure that is the best approach, but we have been using the Avalon/DPML 
> Parameters API before and it works very well.


Your right, one can always get the dictionary from the Configuration and
the Configuration does provide some utility methods for getting typed
values, but I don't think I communicated very well my concern. 

The following is one of the goals that Marcel articulates in his paper
about the dependency manager.

<snip>
1. Provide a clean separation between a service implementation
and the ”glue” that binds it to the OSGi framework. The service
implementation should not have to contain any OSGi specific
code.
</snip>

It is the last sentence that I was referring to. The idea of service
implementations being completely separated from OSGi specifics is a
pretty high goal and was achieved with the earlier implementation. 

With the proposed changes to the Dependency manager all services will
now become dependent on (need to import) the OSGi cm and the xentron
packages. IMHO this would conflict with the above goal. 



kind regards,

John 

 






Re: Contribution of the dependency manager to Feli

Posted by Peter Neubauer <pe...@neubauer.se>.
Hi John,

On Monday 30 January 2006 16:49, John E. Conlon wrote:
> Adding the configuration dependencies are a big plus. But have you
> considered making the signature more generic?
Mmmh the question is what you want to do with this. We were thinking that in 
90% of the cases you are using primitives and Strings in the configuration, 
since if you are configuring with objects (e.g. serialized instances etc) you 
probably will be better off using some other mechanism like service 
dependencies etc.

So, we thought a reasonable help is to avoid the casts you usually have. OTOH 
you can always retrieve the underlying Dictionary with 
configuration.toDictionary().


Not sure that is the best approach, but we have been using the Avalon/DPML 
Parameters API before and it works very well.

Since we put this out, of course feel free to change this if there are any big 
reasons for getting rid of the dependencies you mention.

WDYT?

/peter

Re: Contribution of the dependency manager to Feli

Posted by "John E. Conlon" <jc...@verticon.com>.
On Sun, 2006-01-29 at 02:58, Peter Neubauer wrote:

> On Saturday 28 January 2006 19:14, John E. Conlon wrote:
> > public void setConfiguration(Configuration config)
> >           throws ConfigurationException{){
> >
> >   }
> > }
> >
> > Is the setConfiguration method signature correct in the above example??
> Yes that's right. we tried to mimic the original 
> update(Configuration config) throws ConfigurationException;
> from the MangedService as much as possible.

Hi Peter,

Adding the configuration dependencies are a big plus. But have you
considered making the signature more generic? 

Like:
public void setConfiguration(Dictionary config) throws Exception){}

While there are some useful utility features in the Configuration class
this would allow the service implementations to be free from
dependencies on:
  com.xenotron.osgi.dependency.Configuration;
  org.osgi.service.cm.ConfigurationException;

kind regards,

John


Re: Contribution of the dependency manager to Feli

Posted by Peter Neubauer <pe...@neubauer.se>.
Hi,
On Saturday 28 January 2006 19:14, John E. Conlon wrote:
> public void setConfiguration(Configuration config)
>           throws ConfigurationException{){
>
>   }
> }
>
> Is the setConfiguration method signature correct in the above example??
Yes that's right. we tried to mimic the original 
update(Configuration config) throws ConfigurationException;
from the MangedService as much as possible.

/peter

Re: Contribution of the dependency manager to Feli

Posted by "John E. Conlon" <jc...@verticon.com>.
On Sat, 2006-01-28 at 05:12, Marcel Offermans wrote:

> So really the bottom line is:
>  - Use the instance field if you want to use the highest ranked service 
> and you don't really care about changing services.
>  - Use callback hooks if you do care about changes, in which case you 
> can decide what to do yourself.
Yes that is very reasonable.

And if one uses an optional log Service and a configurationDependency
that Peter described in his Wiki a service would look something like:


public class SomeServiceImpl implements SomeService{
  import com.xenotron.osgi.dependency.Configuration;
  import org.osgi.service.cm.ConfigurationException;

  private LogService logSvc;
  
  public void init(){
  }

  public void start(){
  }
  
  public void destroy(){
  }

  public void setConfiguration(Configuration config) 
          throws ConfigurationException{){

  }
}

Is the setConfiguration method signature correct in the above example??

kind regards,
John



Re: Contribution of the dependency manager to Feli

Posted by Karl Pauls <ka...@gmail.com>.
> I understand that the
> implementation of the service object will need to implements its own
> thread safety, but that is outside of the scope of dependency manager.

Well, under the constraint that the service has to handle all thread
safety aspects including to its internal fields which can not be
accessed by the client (which might be considered bad style but still
is possible according to the spec due to the possibility to register
classes as services too, not just interfaces) - I agree; In this case
volatile should do it.

> BJ Hargrave
> Senior Technical Staff Member, IBM
> OSGi Fellow and CTO of the OSGi Alliance
> hargrave@us.ibm.com
> Office: +1 407 849 9117 Mobile: +1 386 848 3788

regards,

Karl

--
Karl Pauls
pauls@inf.fu-berlin.de

Re: Contribution of the dependency manager to Feli

Posted by BJ Hargrave <ha...@us.ibm.com>.
I agree in general. But for this specific problem being covered by 
dependency manager here, setting a field to an object reference in a 
thread safe manner, volatile seems enough (even with pre 1.5 semantics). I 
am familiar with the referenced material you site. Can you explain why you 
think volatile is not sufficient here? I understand that the 
implementation of the service object will need to implements its own 
thread safety, but that is outside of the scope of dependency manager.

BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
hargrave@us.ibm.com
Office: +1 407 849 9117 Mobile: +1 386 848 3788



Karl Pauls <ka...@gmail.com> 
2006-01-30 04:35 PM
Please respond to
felix-dev@incubator.apache.org


To
felix-dev@incubator.apache.org
cc

Subject
Re: Contribution of the dependency manager to Feli






> But that is a problem for the service implementation to deal with: that 
is
> the service implementation must be thread safe. All that the dependency
> manager needs to be responsible for is to provide a reference to the
> service object in a thread safe way. The work the service implementation
> needs to do to be thread safe is independent of whether dependency 
manager
> is used to provide the service object to a client. The service object
> should be properly initialized before dependency manager has a reference
> to it and a well designed service should not have field accessible to
> clients. If the data of the service is only accessible via method calls,
> the service implementation can take the proper action to ensure thread
> safety.

Still, the issue remains: volatile will not be strong enough in a pre
1.5 environment [1](and iirc that was one of the things desired)
hence, the only solution is to advice the client to set and get in a
synchronized block (and the dependency manager to use the
callback-hook).

> BJ Hargrave
> Senior Technical Staff Member, IBM
> OSGi Fellow and CTO of the OSGi Alliance
> hargrave@us.ibm.com
> Office: +1 407 849 9117 Mobile: +1 386 848 3788

regards,

Karl

[1] 
http://www-128.ibm.com/developerworks/java/library/j-jtp03304/#resources
--
Karl Pauls
pauls@inf.fu-berlin.de



Re: Contribution of the dependency manager to Feli

Posted by Karl Pauls <ka...@gmail.com>.
> But that is a problem for the service implementation to deal with: that is
> the service implementation must be thread safe. All that the dependency
> manager needs to be responsible for is to provide a reference to the
> service object in a thread safe way. The work the service implementation
> needs to do to be thread safe is independent of whether dependency manager
> is used to provide the service object to a client. The service object
> should be properly initialized before dependency manager has a reference
> to it and a well designed service should not have field accessible to
> clients. If the data of the service is only accessible via method calls,
> the service implementation can take the proper action to ensure thread
> safety.

Still, the issue remains: volatile will not be strong enough in a pre
1.5 environment [1](and iirc that was one of the things desired)
hence, the only solution is to advice the client to set and get in a
synchronized block (and the dependency manager to use the
callback-hook).

> BJ Hargrave
> Senior Technical Staff Member, IBM
> OSGi Fellow and CTO of the OSGi Alliance
> hargrave@us.ibm.com
> Office: +1 407 849 9117 Mobile: +1 386 848 3788

regards,

Karl

[1] http://www-128.ibm.com/developerworks/java/library/j-jtp03304/#resources
--
Karl Pauls
pauls@inf.fu-berlin.de

Re: Contribution of the dependency manager to Feli

Posted by BJ Hargrave <ha...@us.ibm.com>.
Karl Pauls <ka...@gmail.com> wrote on 2006-01-30 03:41:42 PM:

> > I think that by implementing the change you suggest above
> > (requiring volatile) the problem is solved.
> 
> Unfortunately, this doesn't solve your problem (at least not in regard
> to the old java memory model). This has changed in 1.5 and yes, the
> new one would solve your issues in regard to the reference - still it
> doesn't solve the initialization problem (i.e., while the reference is
> guaranteed to be volatile - get and set is atomic - one could still
> hit the situation where the fields of the Service are not
> initialized).

But that is a problem for the service implementation to deal with: that is 
the service implementation must be thread safe. All that the dependency 
manager needs to be responsible for is to provide a reference to the 
service object in a thread safe way. The work the service implementation 
needs to do to be thread safe is independent of whether dependency manager 
is used to provide the service object to a client. The service object 
should be properly initialized before dependency manager has a reference 
to it and a well designed service should not have field accessible to 
clients. If the data of the service is only accessible via method calls, 
the service implementation can take the proper action to ensure thread 
safety.


BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
hargrave@us.ibm.com
Office: +1 407 849 9117 Mobile: +1 386 848 3788



Re: Contribution of the dependency manager to Feli

Posted by Karl Pauls <ka...@gmail.com>.
> I think that by implementing the change you suggest above
> (requiring volatile) the problem is solved.

Unfortunately, this doesn't solve your problem (at least not in regard
to the old java memory model). This has changed in 1.5 and yes, the
new one would solve your issues in regard to the reference - still it
doesn't solve the initialization problem (i.e., while the reference is
guaranteed to be volatile - get and set is atomic - one could still
hit the situation where the fields of the Service are not
initialized).

This whole thing boils down to the problem of double-checked locking
and this is long since declared unsafe (check it out by doing a google
or the pointers at the end).

In other words, there is nothing you can do to prevent this problem
except to access the field inside of a synchronized block (and this
means _all_ access has to happen through a synchronized block _on the
same object_) e.g.,

class Foo
{
      private Object sema = new Object();

     private Service field; // never accessed without holding the lock on sema!

     public Object get()
     {
          synchronized (sema)
          {
                return field;
          }
     }

     public void set(Service newService)
     {
          synchronized (sema)
          {
                field = newService;
          }
     }
}

In essence, all you can provide as a workaround is to advice that in
case of multithreaded access one needs to use the callback-hook (that
needs to be synchronized) and only access the service through another
method (that is synchronized _on the same object_). In the long-run
you will need to use the suggested byte-code engineering method to
really resolve the issues of this approach.

> Furthermore it might be good to educate people about this issue.

Yes, especially that volatile is no solution to it either :-)

> Greetings, Marcel

That said, note that problems with the volatile approach are not
likely to appear in normal (i.e., every day - Intel, Sparc etc.)
execution environments due to their somewhat stronger memory model
(compared to the Java Memory Model). Unfortunately, OSGi does target
other platforms as well right ...

Best regards,

Karl

p.s.: other then this I think the work on the dependency manager is
great - keep up the good work!

p.p.s.: Anyone interested in how to get the benefits of double-checked
locking but remain thread-save? Do the initialization in the static
block of a member class:

public class Singleton
{
	public static Object getInstance(){
		return Member.instance;
	}

	static class Member
	{
		static Object instance;
		
		static {
			// do
			instance = new Object();
			// initialization
		}
	}
}

[1] http://www.javaworld.com/jw-02-2001/jw-0209-double.html

[2] http://www-128.ibm.com/developerworks/java/library/j-jtp03304/#resources

[3] http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-double-p2.html

--
Karl Pauls
pauls@inf.fu-berlin.de

Re: Contribution of the dependency manager to Feli

Posted by Marcel Offermans <ma...@luminis.nl>.
Hello BJ,

Thanks again for your feedback!

BJ Hargrave wrote:

>>    synchronized (new Object()) {
>>    
>>
>Isn't new Object() a wasted memory allocation? Simply use some object you 
>allocate only once :-)
>  
>
Yes it is. I will change this (or change it altogether, see below).

About the whole volatile/synchronized issue:

>Well you could force that all such fields be declared volatile. You can 
>make this part of your specification and enforce via reflection. That is, 
>do not set the field if it is not declared volatile. If you don't do 
>something like this, then your model is dangerous to use since it is 
>inherently not thread-safe and may lead to unpredictable bugs.
>  
>
Agreed, I think that is probably the best way to deal with the situation.

>A more complex solution to this was proposed by Peter Kriens. It involves 
>bytecode weaving to replace the field with a method call. This method can 
>then always perform the proper actions to enforce the java memory model.
>  
>
Yes I read about that when he was working on that solution. To be 
honest, I think that by implementing the change you suggest above 
(requiring volatile) the problem is solved. Furthermore it might be good 
to educate people about this issue.

Greetings, Marcel


Re: Contribution of the dependency manager to Feli

Posted by BJ Hargrave <ha...@us.ibm.com>.
Marcel Offermans <ma...@luminis.nl> wrote on 2006-01-30 
11:53:59 AM:

> BJ Hargrave wrote:
> 
> >Interesting. With the possible asynchronous changes to the field being 
> >made by the dependency manager, the field MUST be declared volatile or 
all 
> >access to it must be protected by a suitable synchronization for proper 

> >java memory model semantics. Otherwise strange and hard to debug 
problems 
> >may arise since different threads can end up using different values for 

> >the field.
> > 
> >
> Yes, this is an issue that I am aware of. First of all, you can solve 
> this problem by using volatile as you suggest. If you need more control, 

> it is probably better to use the callback hooks.
> 
> >How do you deal with this? While for the log example here, no serious 
> >problems will arise if the wrong log is used. But if the field 
references 
> >some other service, serious bugs could occur.
> > 
> >
> Even if you do not use volatile, I tried to make sure that changes to 
> the field are written to memory. I read about the Java Memory Model on 
> various sites and finally got the following information from Doug Lea:
> 
> http://gee.cs.oswego.edu/dl/cpj/jmm.html
> 
> Here he writes (about visibility):
> 
> "In essence, releasing a lock forces a flush of all writes from working 
> memory employed by the thread, [...] While lock actions provide 
> exclusion only for the operations performed within a synchronized method 

> or block, these memory effects are defined to cover all fields used by 
> the thread performing the action.

As this quote points out, it only affects the thread's working memory 
which includes the field. So when the synchronized region is exiting, the 
thread (and it processor) will flush caches to main memory. However, the 
client code may be running on another thread/processor and its processor 
cache will not be synchronized with main memory. This is why both threads 
will need to either treat the field as volatile or both protect access to 
the field with a synchronized region.

>  Note the double meaning of synchronized: it deals with locks that 
> permit higher-level synchronization protocols, while at the same time 
> dealing with the memory system (sometimes via low-level memory barrier 
> machine instructions) to keep value representations in synch across 
> threads. This reflects one way in which concurrent programming bears 
> more similarity to distributed programming than to sequential 
> programming. The latter sense of synchronized may be viewed as a 
> mechanism by which a method running in one thread indicates that it is 
> willing to send and/or receive changes to variables to and from methods 
> running in other threads. From this point of view, using locks and 
> passing messages might be seen merely as syntactic variants of each 
other."
> 
> So in the dependency manager (ServiceImpl class) I have the following 
> code fragment to update a field through reflection:
> 
>     synchronized (new Object()) {

Isn't new Object() a wasted memory allocation? Simply use some object you 
allocate only once :-)

>         fields[j].set(m_serviceInstance, instance);
>     }
> 
> As far as I can tell, as soon as the code leaves this synchronized 
> block, the field is written to memory.

Yes. It should be forced to main memory. This does not mean that other 
threads which read the field will synchronize their processor's cache with 
main memory.

> 
> Note that I don't think this will guarantee that all threads that read 
> this variable will automatically re-fetch their cached copy but as far 
> as I can see there's not much I can do about that (apart from having 
> people define such fields as volatile).

Well you could force that all such fields be declared volatile. You can 
make this part of your specification and enforce via reflection. That is, 
do not set the field if it is not declared volatile. If you don't do 
something like this, then your model is dangerous to use since it is 
inherently not thread-safe and may lead to unpredictable bugs.

> 
> But any input on this is very welcome. It's a tricky subject but very 
> important to get right.

A more complex solution to this was proposed by Peter Kriens. It involves 
bytecode weaving to replace the field with a method call. This method can 
then always perform the proper actions to enforce the java memory model.

> 
> Greetings, Marcel
> 


BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
hargrave@us.ibm.com
Office: +1 407 849 9117 Mobile: +1 386 848 3788


Re: Contribution of the dependency manager to Feli

Posted by Marcel Offermans <ma...@luminis.nl>.
BJ Hargrave wrote:

>Interesting. With the possible asynchronous changes to the field being 
>made by the dependency manager, the field MUST be declared volatile or all 
>access to it must be protected by a suitable synchronization for proper 
>java memory model semantics. Otherwise strange and hard to debug problems 
>may arise since different threads can end up using different values for 
>the field.
>  
>
Yes, this is an issue that I am aware of. First of all, you can solve 
this problem by using volatile as you suggest. If you need more control, 
it is probably better to use the callback hooks.

>How do you deal with this? While for the log example here, no serious 
>problems will arise if the wrong log is used. But if the field references 
>some other service, serious bugs could occur.
>  
>
Even if you do not use volatile, I tried to make sure that changes to 
the field are written to memory. I read about the Java Memory Model on 
various sites and finally got the following information from Doug Lea:

http://gee.cs.oswego.edu/dl/cpj/jmm.html

Here he writes (about visibility):

"In essence, releasing a lock forces a flush of all writes from working 
memory employed by the thread, [...] While lock actions provide 
exclusion only for the operations performed within a synchronized method 
or block, these memory effects are defined to cover all fields used by 
the thread performing the action.
 Note the double meaning of synchronized: it deals with locks that 
permit higher-level synchronization protocols, while at the same time 
dealing with the memory system (sometimes via low-level memory barrier 
machine instructions) to keep value representations in synch across 
threads. This reflects one way in which concurrent programming bears 
more similarity to distributed programming than to sequential 
programming. The latter sense of synchronized may be viewed as a 
mechanism by which a method running in one thread indicates that it is 
willing to send and/or receive changes to variables to and from methods 
running in other threads. From this point of view, using locks and 
passing messages might be seen merely as syntactic variants of each other."

So in the dependency manager (ServiceImpl class) I have the following 
code fragment to update a field through reflection:

    synchronized (new Object()) {
        fields[j].set(m_serviceInstance, instance);
    }

As far as I can tell, as soon as the code leaves this synchronized 
block, the field is written to memory.

Note that I don't think this will guarantee that all threads that read 
this variable will automatically re-fetch their cached copy but as far 
as I can see there's not much I can do about that (apart from having 
people define such fields as volatile).

But any input on this is very welcome. It's a tricky subject but very 
important to get right.

Greetings, Marcel


Re: Contribution of the dependency manager to Feli

Posted by BJ Hargrave <ha...@us.ibm.com>.
Marcel Offermans <ma...@luminis.nl> wrote on 2006-01-28 
06:12:21 AM:

> John E. Conlon wrote:
> 
> >If only one field is specified and if there are multiple services; and
> >the services are dynamically coming and going how will the service
> >referenced in the one field react? 
> > 
> >
> Let's use an example scenario here. Say we have an optional dependency 
> with LogService (quite common) and that at first, there is no log 
> service available. The field will then be initialized with a "null 
> object" so you can safely invoke methods on this log service (but these 
> methods will do nothing).
> 
> Now a log service is registered. At this moment, the instance field will 

> be changed (using reflection). So from this point on, all methods you 
> invoke on log service will actually invoke the freshly added log 
service.
> 
> Now another log service is registered with a higher ranking. Again, the 
> instance field will be modified, rerouting all new calls to the higher 
> ranked log service.
> 
> Now if that higher ranked log service is removed again, the instance 
> will revert to the other, still registered log service. If that one is 
> removed too, you will get a null object again.

Interesting. With the possible asynchronous changes to the field being 
made by the dependency manager, the field MUST be declared volatile or all 
access to it must be protected by a suitable synchronization for proper 
java memory model semantics. Otherwise strange and hard to debug problems 
may arise since different threads can end up using different values for 
the field.

How do you deal with this? While for the log example here, no serious 
problems will arise if the wrong log is used. But if the field references 
some other service, serious bugs could occur.


> 
> For the example of a log service, this instance changing all the time 
> will be just fine. Some log messages will be ignored, or written to the 
> log service, depending on what's available at that time. The log service 

> itself has no state or anything so bundles using it won't care as much 
> about the implementation changing. In these cases, just using the 
> instance field.
> 
> If you do care about changes, you can still use the instance field, but 
> you should at least use the callback hooks too so you are notified and 
> can act on changes to this field (of course, it might be a little more 
> complicated then that, because you will also be notified when a lower 
> ranked service is added and that will not change our instance field).
> 
> So really the bottom line is:
>  - Use the instance field if you want to use the highest ranked service 
> and you don't really care about changing services.
>  - Use callback hooks if you do care about changes, in which case you 
> can decide what to do yourself.
> 
> >(Thanks Marcel - your article is very well written.)
> > 
> >
> Thanks!
> 
> Greetings, Marcel
> 


BJ Hargrave
Senior Technical Staff Member, IBM
OSGi Fellow and CTO of the OSGi Alliance
hargrave@us.ibm.com
Office: +1 407 849 9117 Mobile: +1 386 848 3788


Re: Contribution of the dependency manager to Feli

Posted by Marcel Offermans <ma...@luminis.nl>.
John E. Conlon wrote:

>If only one field is specified and if there are multiple services; and
>the services are dynamically coming and going how will the service
>referenced in the one field react?  
>  
>
Let's use an example scenario here. Say we have an optional dependency 
with LogService (quite common) and that at first, there is no log 
service available. The field will then be initialized with a "null 
object" so you can safely invoke methods on this log service (but these 
methods will do nothing).

Now a log service is registered. At this moment, the instance field will 
be changed (using reflection). So from this point on, all methods you 
invoke on log service will actually invoke the freshly added log service.

Now another log service is registered with a higher ranking. Again, the 
instance field will be modified, rerouting all new calls to the higher 
ranked log service.

Now if that higher ranked log service is removed again, the instance 
will revert to the other, still registered log service. If that one is 
removed too, you will get a null object again.

For the example of a log service, this instance changing all the time 
will be just fine. Some log messages will be ignored, or written to the 
log service, depending on what's available at that time. The log service 
itself has no state or anything so bundles using it won't care as much 
about the implementation changing. In these cases, just using the 
instance field.

If you do care about changes, you can still use the instance field, but 
you should at least use the callback hooks too so you are notified and 
can act on changes to this field (of course, it might be a little more 
complicated then that, because you will also be notified when a lower 
ranked service is added and that will not change our instance field).

So really the bottom line is:
 - Use the instance field if you want to use the highest ranked service 
and you don't really care about changing services.
 - Use callback hooks if you do care about changes, in which case you 
can decide what to do yourself.

>(Thanks Marcel - your article is very well written.)
>  
>
Thanks!

Greetings, Marcel


Re: Contribution of the dependency manager to Feli

Posted by "John E. Conlon" <jc...@verticon.com>.
On Fri, 2006-01-27 at 18:53, Marcel Offermans wrote:
> >
> >But what if there are multiple dependencies with the same interface -
> >like the example?
> >  
> >
> Good question. Since there obviously only is one instance field, it will 
> be set to the highest ranking service implementation. So the field will 
> only give you access to the "preferred" service.

If only one field is specified and if there are multiple services; and
the services are dynamically coming and going how will the service
referenced in the one field react?  

> If you want to keep track of multiple services (for example if you add a 
> dependency to track all Device services or something like that) then 
> don't use an instance field but use the callback hooks instead and make 
> a list of every service that is added.
>  
Cool. The callback hooks can be used to add to or remove instances from
the list of a common type of service. 

> The article I wrote (which can be 
> found on the OSGi website) contains some examples of this, btw.

(Thanks Marcel - your article is very well written.)

kind regards,
John


Re: Contribution of the dependency manager to Feli

Posted by Marcel Offermans <ma...@luminis.nl>.
John E. Conlon wrote:

>Good 5 Minute overview. Thanks Peter.
>  
>
Agreed, thanks!

>One question it mentions
>For each dependency: 
>      * Instance fields are set, through reflection, if they exist.
>
>
>But what if there are multiple dependencies with the same interface -
>like the example?
>  
>
Good question. Since there obviously only is one instance field, it will 
be set to the highest ranking service implementation. So the field will 
only give you access to the "preferred" service.

If you want to keep track of multiple services (for example if you add a 
dependency to track all Device services or something like that) then 
don't use an instance field but use the callback hooks instead and make 
a list of every service that is added. The article I wrote (which can be 
found on the OSGi website) contains some examples of this, btw.

Greetings, Marcel