Immediate service activation in OSGi can be tricky but there are some basic rules to consider. Another point to think about is when a service doesn’t contain an activate method. The code base I work in uses Felix’s SCR annotations which makes this search pretty concise. I also assume that the code is in git. If your code isn’t, you should be able to replace
find <dir> -type f -exec <grep-fu here>.
Find activate-immediately services
Using a little git and grep-fu, we can find our services that activate immediately.
git grep -l 'immediate[[:space:]]\?=[[:space:]]\?true'
The command below makes a few assumptions:
- We use SCR annotations that allow
immediate = (true|false)
- We use
immediate = truealmost never except in the
Find services that have an activate method
For our next trick, we’ll find the set of files that have an activate method as noted by the use of an
git grep -l '@Activate'
Find the intersection of activate-immediately services that lack an activate method
Now to the fun. With the information we’ve grepped above, we need the things that are in set 1 but not in set 2. There’s a great little tool in linux called
comm that can help us with the set operations. It needs sorted data to work correctly, so we’ll dress up our previous commands and send them in.
comm -23 <(git grep -l 'immediate[[:space:]]\?=[[:space:]]\?true' | sort) <(git grep -l '@Activate' | sort)
-23 argument tells
comm that we want to suppress unique items from the second set (activate method list) and to suppress items that exist in both sets (immediate + activate). This leaves us with services that don’t have an activate method. If you want to see services that are immediate and have an activate method, change the argument to