当前位置: 首页 > 工具软件 > Apache Celix > 使用案例 >

(04) Apache Felix 入门 - 03 - 01

邢焕
2023-12-01

1、Dictionary Service 的另外一个实现

下面的例子中我们编写了 DictionaryService 的另外一个实现,它可以检测输入的法语是否正确,但是如果仅仅是处于这个目的,则这个例子并没有什么实际的简直,我们真正的目的是向读者说明,OSGI 是允许一个服务的多个实现同时存在的,这个例子会在我们后面的 example5 中被使用到,具体实现和 example2 很相似,如下:

/*
 * Apache Felix OSGi tutorial.
**/

package tutorial.example2b;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceEvent;

import tutorial.example2.service.DictionaryService;

/**
 * 在下面的 bundle 中我们使用 context 注册了一个检测法语单词是否正确的服务。
**/
public class Activator implements BundleActivator
{
    /**
     * Implements BundleActivator.start(). Registers an
     * instance of a dictionary service using the bundle context;
     * attaches properties to the service that can be queried
     * when performing a service look-up.
     * @param context the framework context for the bundle.
    **/
    public void start(BundleContext context)
    {
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put("Language", "French");
        context.registerService(
            DictionaryService.class.getName(), new DictionaryImpl(), props);
    }

    /**
     * Implements BundleActivator.stop(). Does nothing since
     * the framework will automatically unregister any registered services.
     * @param context the framework context for the bundle.
    **/
    public void stop(BundleContext context)
    {
        // NOTE: The service is automatically unregistered.
    }

    /**
     * A private inner class that implements a dictionary service;
     * see DictionaryService for details of the service.
    **/
    private static class DictionaryImpl implements DictionaryService
    {
        // The set of words contained in the dictionary.
        String[] m_dictionary =
            { "bienvenue", "au", "tutoriel", "osgi" };

        /**
         * Implements DictionaryService.checkWord(). Determines
         * if the passed in word is contained in the dictionary.
         * @param word the word to be checked.
         * @return true if the word is in the dictionary,
         *         false otherwise.
        **/
        public boolean checkWord(String word)
        {
            word = word.toLowerCase();

            // This is very inefficient
            for (int i = 0; i < m_dictionary.length; i++)
            {
                if (m_dictionary[i].equals(word))
                {
                    return true;
                }
            }
            return false;
        }
    }
}

构建 manifest.mf:

Bundle-Name: French dictionary
Bundle-Description: A bundle that registers a French dictionary service
Bundle-Vendor: Apache Felix
Bundle-Version: 1.0.0
Bundle-Activator: tutorial.example2b.Activator
Import-Package: org.osgi.framework,
 tutorial.example2.service

编译、打包:

D:\devInstall\apache\felix-framework-6.0.0\examples\demo2b\src> javac -cp ..\..\..\bin\felix.jar;..\..\demo02\target\example2.jar -encoding UTF-8 -d ../target .\tutorial\example2b\*.java
D:\devInstall\apache\felix-framework-6.0.0\examples\demo2b\src> cd ../target
D:\devInstall\apache\felix-framework-6.0.0\examples\demo2b\src> cp ../src/manifest.mf ./
D:\devInstall\apache\felix-framework-6.0.0\examples\demo2b\src>  jar cvfm example2b.jar ./manifest.mf -C . .
已添加清单
正在添加: manifest.mf(输入 = 270) (输出 = 167)(压缩了 38%)
正在添加: tutorial/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: tutorial/example2b/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: tutorial/example2b/Activator$1.class(输入 = 213) (输出 = 164)(压缩了 23%)
正在添加: tutorial/example2b/Activator$DictionaryImpl.class(输入 = 900) (输出 = 548)(压缩了 39%)
正在添加: tutorial/example2b/Activator.class(输入 = 1046) (输出 = 553)(压缩了 47%)

安装、运行,结果如下:

START LEVEL 1                                                                                   
   ID|State      |Level|Name                                                                    
    0|Active     |    0|System Bundle (6.0.0)|6.0.0                                             
    1|Active     |    1|jansi (1.17.1)|1.17.1                                                   
    2|Active     |    1|JLine Bundle (3.7.0)|3.7.0                                              
    3|Active     |    1|Apache Felix Bundle Repository (2.0.10)|2.0.10                          
    4|Active     |    1|Apache Felix Gogo Command (1.0.2)|1.0.2                                 
    5|Active     |    1|Apache Felix Gogo JLine Shell (1.1.0)|1.1.0                             
    6|Active     |    1|Apache Felix Gogo Runtime (1.1.0)|1.1.0                                 
    7|Active     |    1|Service listener example (1.0.0)|1.0.0                                  
g! install file:./examples/demo02/target/example2.jar                                           
Bundle ID: 8                                                                                    
g! start 8                                                                                      
Ex1: Service of type tutorial.example2.service.DictionaryService registered.                              
g! install file:./examples/demo2b/target/example2b.jar                                          
Bundle ID: 10                                                                                   
g! start 10                                                                                     
Ex1: Service of type tutorial.example2.service.DictionaryService registered.                    
g!                                                                                              

 

 类似资料: