How do I get both Service Object and ServiceReference, when using declarative services
I need both ServiceReference and Service Object when I'm using my Bundle in osgi. However, when I do it as showed by the code below, my bundle can no longer find the bind method. what am I doing wrong?
public class AServiceImpl implements AService{
TestService t;
JournalService journalService;
BundleContext context;
ServiceReference testServiceRef;
public void bindTestService(TestService testService, ServiceReference sv) {
t = testService;
testServiceRef = sv;
//this.testServiceRef = testServiceRef;
test.api.TestStaticVariable.getUniqueInstance().add("TEST A");
System.out.println(test.api.TestStaticVariable.getUniqueInstance().toString());
}
public void unbindTestService(TestService testService){
System.out.println("");
if(t.equals(testService)){
t = null;
}
}
public void bindJournalService(JournalService journalService){
this.journalService = journalService;
}
public void unbindJournalService(JournalService journalService){
if(this.journalService.equals(journalService)){
this.journalService = null;
}
}
}
java osgi
add a comment |
I need both ServiceReference and Service Object when I'm using my Bundle in osgi. However, when I do it as showed by the code below, my bundle can no longer find the bind method. what am I doing wrong?
public class AServiceImpl implements AService{
TestService t;
JournalService journalService;
BundleContext context;
ServiceReference testServiceRef;
public void bindTestService(TestService testService, ServiceReference sv) {
t = testService;
testServiceRef = sv;
//this.testServiceRef = testServiceRef;
test.api.TestStaticVariable.getUniqueInstance().add("TEST A");
System.out.println(test.api.TestStaticVariable.getUniqueInstance().toString());
}
public void unbindTestService(TestService testService){
System.out.println("");
if(t.equals(testService)){
t = null;
}
}
public void bindJournalService(JournalService journalService){
this.journalService = journalService;
}
public void unbindJournalService(JournalService journalService){
if(this.journalService.equals(journalService)){
this.journalService = null;
}
}
}
java osgi
add a comment |
I need both ServiceReference and Service Object when I'm using my Bundle in osgi. However, when I do it as showed by the code below, my bundle can no longer find the bind method. what am I doing wrong?
public class AServiceImpl implements AService{
TestService t;
JournalService journalService;
BundleContext context;
ServiceReference testServiceRef;
public void bindTestService(TestService testService, ServiceReference sv) {
t = testService;
testServiceRef = sv;
//this.testServiceRef = testServiceRef;
test.api.TestStaticVariable.getUniqueInstance().add("TEST A");
System.out.println(test.api.TestStaticVariable.getUniqueInstance().toString());
}
public void unbindTestService(TestService testService){
System.out.println("");
if(t.equals(testService)){
t = null;
}
}
public void bindJournalService(JournalService journalService){
this.journalService = journalService;
}
public void unbindJournalService(JournalService journalService){
if(this.journalService.equals(journalService)){
this.journalService = null;
}
}
}
java osgi
I need both ServiceReference and Service Object when I'm using my Bundle in osgi. However, when I do it as showed by the code below, my bundle can no longer find the bind method. what am I doing wrong?
public class AServiceImpl implements AService{
TestService t;
JournalService journalService;
BundleContext context;
ServiceReference testServiceRef;
public void bindTestService(TestService testService, ServiceReference sv) {
t = testService;
testServiceRef = sv;
//this.testServiceRef = testServiceRef;
test.api.TestStaticVariable.getUniqueInstance().add("TEST A");
System.out.println(test.api.TestStaticVariable.getUniqueInstance().toString());
}
public void unbindTestService(TestService testService){
System.out.println("");
if(t.equals(testService)){
t = null;
}
}
public void bindJournalService(JournalService journalService){
this.journalService = journalService;
}
public void unbindJournalService(JournalService journalService){
if(this.journalService.equals(journalService)){
this.journalService = null;
}
}
}
java osgi
java osgi
asked Jul 23 '13 at 13:39
DonDon
6601926
6601926
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
See section 112.3.2 of OSGi compendium 4.3 at https://osgi.org/download/r4v43/osgi.cmpn-4.3.0.pdf.
So just use the ServiceReference
and get the object through the component context as described in 112.3.2.
add a comment |
I found a proper working solution to the problem:
I just use the BundleContext, retrieved from the activate method. There, I use the method:
testServiceRef = context.getServiceReference(TestService.class);
where the TestService is retrieved from the bind method.
Actually, it is better to have the ServiceReference injected in the bind method and then calltestService = context.getService(testServiceRef)
Make sure to also unget the service in the unbind method.
– BJ Hargrave
Jul 23 '13 at 20:42
Can you clarify why it is better?
– Don
Jul 24 '13 at 13:11
Because DS injects the BundleContext and the ServiceReference. You just need to get and unget the service. Your proposal of getting the ServiceReference ignores DS's binding of the service to your component.
– BJ Hargrave
Jul 25 '13 at 12:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17811873%2fhow-do-i-get-both-service-object-and-servicereference-when-using-declarative-se%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
See section 112.3.2 of OSGi compendium 4.3 at https://osgi.org/download/r4v43/osgi.cmpn-4.3.0.pdf.
So just use the ServiceReference
and get the object through the component context as described in 112.3.2.
add a comment |
See section 112.3.2 of OSGi compendium 4.3 at https://osgi.org/download/r4v43/osgi.cmpn-4.3.0.pdf.
So just use the ServiceReference
and get the object through the component context as described in 112.3.2.
add a comment |
See section 112.3.2 of OSGi compendium 4.3 at https://osgi.org/download/r4v43/osgi.cmpn-4.3.0.pdf.
So just use the ServiceReference
and get the object through the component context as described in 112.3.2.
See section 112.3.2 of OSGi compendium 4.3 at https://osgi.org/download/r4v43/osgi.cmpn-4.3.0.pdf.
So just use the ServiceReference
and get the object through the component context as described in 112.3.2.
edited Nov 20 '18 at 17:41
Kirby
9,37466077
9,37466077
answered Jul 23 '13 at 14:54
Peter KriensPeter Kriens
11.7k12648
11.7k12648
add a comment |
add a comment |
I found a proper working solution to the problem:
I just use the BundleContext, retrieved from the activate method. There, I use the method:
testServiceRef = context.getServiceReference(TestService.class);
where the TestService is retrieved from the bind method.
Actually, it is better to have the ServiceReference injected in the bind method and then calltestService = context.getService(testServiceRef)
Make sure to also unget the service in the unbind method.
– BJ Hargrave
Jul 23 '13 at 20:42
Can you clarify why it is better?
– Don
Jul 24 '13 at 13:11
Because DS injects the BundleContext and the ServiceReference. You just need to get and unget the service. Your proposal of getting the ServiceReference ignores DS's binding of the service to your component.
– BJ Hargrave
Jul 25 '13 at 12:30
add a comment |
I found a proper working solution to the problem:
I just use the BundleContext, retrieved from the activate method. There, I use the method:
testServiceRef = context.getServiceReference(TestService.class);
where the TestService is retrieved from the bind method.
Actually, it is better to have the ServiceReference injected in the bind method and then calltestService = context.getService(testServiceRef)
Make sure to also unget the service in the unbind method.
– BJ Hargrave
Jul 23 '13 at 20:42
Can you clarify why it is better?
– Don
Jul 24 '13 at 13:11
Because DS injects the BundleContext and the ServiceReference. You just need to get and unget the service. Your proposal of getting the ServiceReference ignores DS's binding of the service to your component.
– BJ Hargrave
Jul 25 '13 at 12:30
add a comment |
I found a proper working solution to the problem:
I just use the BundleContext, retrieved from the activate method. There, I use the method:
testServiceRef = context.getServiceReference(TestService.class);
where the TestService is retrieved from the bind method.
I found a proper working solution to the problem:
I just use the BundleContext, retrieved from the activate method. There, I use the method:
testServiceRef = context.getServiceReference(TestService.class);
where the TestService is retrieved from the bind method.
answered Jul 23 '13 at 15:14
DonDon
6601926
6601926
Actually, it is better to have the ServiceReference injected in the bind method and then calltestService = context.getService(testServiceRef)
Make sure to also unget the service in the unbind method.
– BJ Hargrave
Jul 23 '13 at 20:42
Can you clarify why it is better?
– Don
Jul 24 '13 at 13:11
Because DS injects the BundleContext and the ServiceReference. You just need to get and unget the service. Your proposal of getting the ServiceReference ignores DS's binding of the service to your component.
– BJ Hargrave
Jul 25 '13 at 12:30
add a comment |
Actually, it is better to have the ServiceReference injected in the bind method and then calltestService = context.getService(testServiceRef)
Make sure to also unget the service in the unbind method.
– BJ Hargrave
Jul 23 '13 at 20:42
Can you clarify why it is better?
– Don
Jul 24 '13 at 13:11
Because DS injects the BundleContext and the ServiceReference. You just need to get and unget the service. Your proposal of getting the ServiceReference ignores DS's binding of the service to your component.
– BJ Hargrave
Jul 25 '13 at 12:30
Actually, it is better to have the ServiceReference injected in the bind method and then call
testService = context.getService(testServiceRef)
Make sure to also unget the service in the unbind method.– BJ Hargrave
Jul 23 '13 at 20:42
Actually, it is better to have the ServiceReference injected in the bind method and then call
testService = context.getService(testServiceRef)
Make sure to also unget the service in the unbind method.– BJ Hargrave
Jul 23 '13 at 20:42
Can you clarify why it is better?
– Don
Jul 24 '13 at 13:11
Can you clarify why it is better?
– Don
Jul 24 '13 at 13:11
Because DS injects the BundleContext and the ServiceReference. You just need to get and unget the service. Your proposal of getting the ServiceReference ignores DS's binding of the service to your component.
– BJ Hargrave
Jul 25 '13 at 12:30
Because DS injects the BundleContext and the ServiceReference. You just need to get and unget the service. Your proposal of getting the ServiceReference ignores DS's binding of the service to your component.
– BJ Hargrave
Jul 25 '13 at 12:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17811873%2fhow-do-i-get-both-service-object-and-servicereference-when-using-declarative-se%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown