📄 clientactivator.java
字号:
package exampleclient.osgi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import example.service.NameService;
/**
* This class implements a bundle that uses a name
* service to check for name existence in the name array. This bundle
* uses the first service that it finds and does not monitor
* the dynamic availability of the service
**/
public class ClientActivator implements BundleActivator {
/**
* Implements BundleActivator.start(). Queries for
* all available name services. If none are found it
* simply prints a message and returns, otherwise it reads
* name from standard input and checks for their existence
* from the name array that it finds.
* @param context the framework context for the bundle.
**/
public void start(BundleContext context) throws Exception {
ServiceReference[] refs = context.getServiceReferences(
NameService.class.getName(), "(ClassRoom=*)");
if (refs != null)
{
try
{
System.out.println("Enter a blank line to exit.");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name = "";
// Loop endlessly.
while (true)
{
// Ask the user to enter a name.
System.out.print("Enter a Name: ");
name = in.readLine();
// If the user entered a blank line, then
// exit the loop.
if (name.length() == 0)
{
break;
}
// First, get a name service and then check
// if the name is correct.
NameService nameservice =
(NameService) context.getService(refs[0]);
if (nameservice.checkName(name))
{
System.out.println("The Name is Correct.");
}
else
{
System.out.println("The Name is Incorrect.");
}
// Unget the name service.
context.ungetService(refs[0]);
}
} catch (IOException ex) { }
}
else
{
System.out.println("Couldn't find any name service...");
}
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -