此例子和 Example6 很类似,不一样的是我们在这里使用了 SpellChecker 服务检测用户输入句子中的错误单词,并且使用 ServiceTracker 获取 SpellChecker 服务,具体代码如下:
/*
* Apache Felix OSGi tutorial.
**/
package tutorial.example7;
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.util.tracker.ServiceTracker;
import tutorial.example6.service.SpellChecker;
/**
* 使用了 ServiceTracker 动态监测 SpellChecker 服务是否可用,
* 并使用 SpellChecker 检测用户输入的句子中的错误单词。
**/
public class Activator implements BundleActivator
{
// Bundle's context.
private BundleContext m_context = null;
// The service tacker object.
private ServiceTracker m_tracker = null;
/**
* Implements BundleActivator.start(). Creates a Service
* Tracker object to monitor spell checker services. Enters
* a spell check loop where it reads passages from standard
* input and checks their spelling using the spell checker service.
* (NOTE: It is very bad practice to use the calling thread
* to perform a lengthy process like this; this is only done
* for the purpose of the tutorial.)
* @param context the framework context for the bundle.
**/
public void start(BundleContext context) throws Exception
{
m_context = context;
// Create a service tracker to monitor dictionary services.
m_tracker = new ServiceTracker(
m_context,
m_context.createFilter(
"(objectClass=" + SpellChecker.class.getName() + ")"),
null);
m_tracker.open();
try
{
System.out.println("Enter a blank line to exit.");
String passage = "";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Loop endlessly.
while (true)
{
// Ask the user to enter a passage.
System.out.print("Enter passage: ");
passage = in.readLine();
// Get the selected dictionary service, if available.
SpellChecker checker = (SpellChecker) m_tracker.getService();
// If the user entered a blank line, then
// exit the loop.
if (passage.length() == 0)
{
break;
}
// If there is no spell checker, then say so.
else if (checker == null)
{
System.out.println("No spell checker available.");
}
// Otherwise check passage and print misspelled words.
else
{
String[] errors = checker.check(passage);
if (errors == null)
{
System.out.println("Passage is correct.");
}
else
{
System.out.println("Incorrect word(s):");
for (int i = 0; i < errors.length; i++)
{
System.out.println(" " + errors[i]);
}
}
}
}
} catch (Exception ex) { }
}
/**
* Implements BundleActivator.stop(). Does nothing since
* the framework will automatically unget any used services.
* @param context the framework context for the bundle.
**/
public void stop(BundleContext context)
{
}
}
构建 manifest.mf:
Bundle-Name: Spell checker client
Bundle-Description: A bundle that uses the spell checker service
Bundle-Vendor: Richard Hall
Bundle-Version: 1.0.0
Bundle-Activator: tutorial.example7.Activator
Import-Package: org.osgi.framework,
org.osgi.util.tracker,
tutorial.example6.service
编译、打包:
D:\devInstall\apache\felix-framework-6.0.0\examples\demo07\src> javac -cp ../../example6/target/example6.jar;../../../bin/felix.jar -encoding UTF-8 -d ../target ./tutorial/example7/Activator.java
D:\devInstall\apache\felix-framework-6.0.0\examples\demo07\src> cd ../target
D:\devInstall\apache\felix-framework-6.0.0\examples\demo07\src> cp ../src/manifest.mf ./
D:\devInstall\apache\felix-framework-6.0.0\examples\demo07\src> jar cvfm example7.jar ./manifest.mf -C . .
已添加清单
正在添加: manifest.mf(输入 = 291) (输出 = 181)(压缩了 37%)
正在添加: tutorial/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: tutorial/example7/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: tutorial/example7/Activator.class(输入 = 2218) (输出 = 1233)(压缩了 44%)
安装、运行:
g! install file:./examples/demo07/target/example7.jar
Bundle ID: 23
g! start 23
Enter a blank line to exit.
Enter passage: ~
g! lb
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
20|Active | 1|Spell checker service (1.0.0)|1.0.0
21|Active | 1|English dictionary (1.0.0)|1.0.0
23|Active | 1|Spell checker client (1.0.0)|1.0.0
当名称为 Spell checker client 的 bundle 启动的时候,如果系统中存在 spell checker 服务,则使用该服务循环检测用户每次输入的句子中的错误单词。