How do I get both Service Object and ServiceReference, when using declarative services












1















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;
}
}
}









share|improve this question



























    1















    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;
    }
    }
    }









    share|improve this question

























      1












      1








      1








      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;
      }
      }
      }









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 23 '13 at 13:39









      DonDon

      6601926




      6601926
























          2 Answers
          2






          active

          oldest

          votes


















          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.






          share|improve this answer

































            0














            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.






            share|improve this answer
























            • 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











            • 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











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            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.






            share|improve this answer






























              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.






              share|improve this answer




























                2












                2








                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                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

























                    0














                    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.






                    share|improve this answer
























                    • 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











                    • 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
















                    0














                    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.






                    share|improve this answer
























                    • 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











                    • 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














                    0












                    0








                    0







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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 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











                    • 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













                    • 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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$