You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Auto Sending Stop 1 <hu...@hotmail.com> on 2013/10/04 11:38:15 UTC

Question about getService method

Hi,

    I am new to the Apache Felix platform, and I am using the examples provided
in the Apache Felix OSGi Tutorial page to learn how to write OSGi bundle.

    Unfortunately, I run into a problem when trying to use the service provided by
another bundle, and followings are the codes of the Activator.java.

-------------------------------------------------------------
/*
 * Apache Felix OSGi tutorial.
**/

package tutorial.example3;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import tutorial.example2.service.DictionaryService;

public class Activator implements BundleActivator
{
    public void start(BundleContext context) throws Exception
    {
        // Query for all service references matching any language.
        ServiceReference[] refs = context.getServiceReferences(
            DictionaryService.class.getName(), "(Language=*)");

        if (refs != null)
        {
            try
            {
                System.out.println("Enter a blank line to exit.");
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                String word = "";

                // Loop endlessly.
                while (true)
                {
                    // Ask the user to enter a word.
                    System.out.print("Enter word: ");
                    word = in.readLine();

                    // If the user entered a blank line, then
                    // exit the loop.
                    if (word.length() == 0)
                    {
                        break;
                    }

                    // First, get a dictionary service and then check
                    // if the word is correct.
                    DictionaryService dictionary =
                        (DictionaryService) context.getService(refs[0]);
                    if (dictionary.checkWord(word))
                    {
                        System.out.println("Correct.");
                    }
                    else
                    {
                        System.out.println("Incorrect.");
                    }

                    // Unget the dictionary service.
                    context.ungetService(refs[0]);
                }
            } catch (IOException ex) { }
        }
        else
        {
            System.out.println("Couldn't find any dictionary service...");
        }
    }

    public void stop(BundleContext context)
    {
        // NOTE: The service is automatically released.
    }
}
-------------------------------------------------------------------------------------------

    Two unchecked warnings were found when I used the javac command to
compile the code.

    javac -Xlint:unchecked -d classes/   *.java

===================================================
Activator.java: warning: [unchecked] unchecked conversion
                        (DictionaryService) context.getService(refs[0]);
                                                                   ^
  required: ServiceReference<S>
  found:    ServiceReference
  where S is a type-variable:
    S extends Object declared in method <S>getService(ServiceReference<S>)

Activator.java: warning: [unchecked] unchecked method invocation: method getService in interface BundleContext is applied to given types
                        (DictionaryService) context.getService(refs[0]);
                                                              ^
  required: ServiceReference<S>
  found: ServiceReference
  where S is a type-variable:
    S extends Object declared in method <S>getService(ServiceReference<S>)
2 warnings
===================================================

    By checking the OSGi Service Platform Release 4 Core Specification Version 4.3,
the parameter fed to the getService method should be ServiceReference<S>.
However, the type of refs in the example is declared to be ServiceReference.

    Could I cast the type of refs before feeding into the getService method?
Or there is a better solution to solve this problem?

    Thanks.

            Todd

 		 	   		  

Re: Question about getService method

Posted by Tommy Svensson <to...@natusoft.se>.
Hello Tod,

>  required: ServiceReference<S>
>  found: ServiceReference


It is telling you that it wants a "ServiceReference<DictionaryService>", rather than just a "ServiceReference" of unknown type. 

You should also not be doing what you are doing in the Activator start method. The activator is basically for registering services. You should avoid to call services from the Activator. This because each bundle activator are called synchronously. That is the next bundle will not be started until your start method has finished. So if the DictionaryService is provided by a bundle that happens to be started after your bundle it will never be available in your start() method! Both the start() and stop() methods should execute rather quickly. With your while(true) in the start() method you are hanging the entire OSGi server! What haven't been started before your bundle will never be started. 

It is possible to start a thread from the start() method and then access other services from that thread since then the start() method will finish in parallel and the OSGi server will continue to start bundles until all are started, and first at that point all published services will be available. 

Regards,
Tommy Svensson
 

4 okt 2013 kl. 11:38 skrev Auto Sending Stop 1 <hu...@hotmail.com>:

> Hi,
> 
>    I am new to the Apache Felix platform, and I am using the examples provided
> in the Apache Felix OSGi Tutorial page to learn how to write OSGi bundle.
> 
>    Unfortunately, I run into a problem when trying to use the service provided by
> another bundle, and followings are the codes of the Activator.java.
> 
> -------------------------------------------------------------
> /*
> * Apache Felix OSGi tutorial.
> **/
> 
> package tutorial.example3;
> 
> import java.io.BufferedReader;
> import java.io.InputStreamReader;
> import java.io.IOException;
> 
> import org.osgi.framework.BundleActivator;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.ServiceReference;
> 
> import tutorial.example2.service.DictionaryService;
> 
> public class Activator implements BundleActivator
> {
>    public void start(BundleContext context) throws Exception
>    {
>        // Query for all service references matching any language.
>        ServiceReference[] refs = context.getServiceReferences(
>            DictionaryService.class.getName(), "(Language=*)");
> 
>        if (refs != null)
>        {
>            try
>            {
>                System.out.println("Enter a blank line to exit.");
>                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
>                String word = "";
> 
>                // Loop endlessly.
>                while (true)
>                {
>                    // Ask the user to enter a word.
>                    System.out.print("Enter word: ");
>                    word = in.readLine();
> 
>                    // If the user entered a blank line, then
>                    // exit the loop.
>                    if (word.length() == 0)
>                    {
>                        break;
>                    }
> 
>                    // First, get a dictionary service and then check
>                    // if the word is correct.
>                    DictionaryService dictionary =
>                        (DictionaryService) context.getService(refs[0]);
>                    if (dictionary.checkWord(word))
>                    {
>                        System.out.println("Correct.");
>                    }
>                    else
>                    {
>                        System.out.println("Incorrect.");
>                    }
> 
>                    // Unget the dictionary service.
>                    context.ungetService(refs[0]);
>                }
>            } catch (IOException ex) { }
>        }
>        else
>        {
>            System.out.println("Couldn't find any dictionary service...");
>        }
>    }
> 
>    public void stop(BundleContext context)
>    {
>        // NOTE: The service is automatically released.
>    }
> }
> -------------------------------------------------------------------------------------------
> 
>    Two unchecked warnings were found when I used the javac command to
> compile the code.
> 
>    javac -Xlint:unchecked -d classes/   *.java
> 
> ===================================================
> Activator.java: warning: [unchecked] unchecked conversion
>                        (DictionaryService) context.getService(refs[0]);
>                                                                   ^
>  required: ServiceReference<S>
>  found:    ServiceReference
>  where S is a type-variable:
>    S extends Object declared in method <S>getService(ServiceReference<S>)
> 
> Activator.java: warning: [unchecked] unchecked method invocation: method getService in interface BundleContext is applied to given types
>                        (DictionaryService) context.getService(refs[0]);
>                                                              ^
>  required: ServiceReference<S>
>  found: ServiceReference
>  where S is a type-variable:
>    S extends Object declared in method <S>getService(ServiceReference<S>)
> 2 warnings
> ===================================================
> 
>    By checking the OSGi Service Platform Release 4 Core Specification Version 4.3,
> the parameter fed to the getService method should be ServiceReference<S>.
> However, the type of refs in the example is declared to be ServiceReference.
> 
>    Could I cast the type of refs before feeding into the getService method?
> Or there is a better solution to solve this problem?
> 
>    Thanks.
> 
>            Todd
> 
> 		 	   		  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org