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

                                          

Reply via email to