Add osgi support for ThingHandlerService implementations. #1941
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It was mentioned in openhab/openhab-addons#9044 that this would be a useful feature so I went ahead and added support for it here.
Because osgi doesn't support annotation processing for manually created instances, I came up with a workaround that should be acceptable. To take advantage of the osgi
@Reference
annotations you can make the ThingHandlerService implementation a prototype component and then list the implementation class as the only service.For example:
There are a couple of things goin on here.
setThingHandler
after activation. I tried to find a way to be able to callsetThingHandler
before activation but I did not find any way to do so.There is a small change that I had to make in the ThingHandlerService interface though.
Since this new method of service initialization can only call
ThingHandlerService.setThingHandler
after the component has been activated, it would cause confusion and possibly conflicts for users that expect to implementThingHandlerService.activate()
andThingHandlerService.deactivate()
. Sinceactivate
anddeactivate
would happen as part of osgi component creation, users would find thatsetThingHandler
would not yet have been called by the framework. They would only have a null thing handler instance duringactivate
.To get around this problem I created two new methods
initialize()
anddispose()
which would be called aftersetThingHandler
is called so that users could do proper initialization of their ThingHandlerService.I hope that all ThingHandlerService implementations will eventually move over to this new naming convention and we can try removing the
ThingHandlerService.activate
andThingHandlerService.deactivate
methods at some point. I've deprecated those methods in the meantime.Signed-off-by: Connor Petty mistercpp2000+gitsignoff@gmail.com