osgi ds 实例

话不多说  实例呈上
1  服务端bundle
  接口
   package ds.example.service;

public interface NameService {
   public boolean checkName(String name);
}
 
实现

package example.osgi;

import org.osgi.service.component.ComponentContext;

import ds.example.service.NameService;

public class NameImpl implements NameService {
String[] m_name = { "Marry", "John", "David", "Rachel", "Ross" };
  

public void activate(ComponentContext context){
System.out.println("NameService Component Active,within the bundlelifecircle");
}

public void deactivate(ComponentContext context){
System.out.println("NameService Component Deactive,within the bundlelifecircle");
}
@Override
public boolean checkName(String name) {
for (int i = 0; i < m_name.length; i++) {
if (m_name[i].equals(name)) {
return true;
}
}
return false;
}

}

根目录下增加OSGI-INF/component.xml  内容如下
   <?xml version="1.0" encoding="UTF-8"?>
<component name="dsExample">
    <implementation class="example.osgi.NameImpl"/>
    <service>
      <provide interface="ds.example.service.NameService"/>
    </service>
</component>
  
   MF文件内容
  Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ds Plug-in
Bundle-SymbolicName: org.shenhongwei.ds
Bundle-Version: 1.0.0
Service-Component: OSGI-INF/component.xml
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.4.0",
org.osgi.service.component;version="1.0.0"
Export-Package: ds.example.service


2  客户段bundle
   package exampleclient.osgi;

import org.osgi.service.component.ComponentContext;

import ds.example.service.NameService;

public class CheckNameClient {

private NameService nameservice = null;

public void setNameservice(NameService nameservice) {
this.nameservice = nameservice;
String name="ddd";
if(this.nameservice.checkName(name)){
System.out.println("yyyyyyyy");
}
System.out.println("$$$$$$$$$$$$$$$$$$$$$");
}

public void unsetNameservice(NameService nameservice) {
if (this.nameservice != nameservice)
return;
this.nameservice = null;
}
// protected void activate(ComponentContext context) {
// System.out.println("ddddddddddd");
// NameService namecheck = (NameService) context
// .locateService("nameservice");
// String name="dddd";
// if(namecheck.checkName(name)){
// System.out.println("登陆成功");
// }
// System.out.println("登陆失败");
// }

}


根目录下增加OSGI-INF/component.xml  内容如下
  <?xml version="1.0" encoding="UTF-8"?>
<component name="dsExample">
<implementation class="exampleclient.osgi.CheckNameClient" />
<reference name="nameservice" interface="ds.example.service.NameService"
bind="setNameservice" unbind="unsetNameservice" policy="static"
cardinality="1..1" />
</component>

mF文件如下
  Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Client Plug-in
Bundle-SymbolicName: org.shenhongwei.ds.client
Bundle-Version: 1.0.0
Service-Component: OSGI-INF/component.xml
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: ds.example.service,
org.osgi.service.component;version="1.0.0"

ok  运行成功!!!!!!!
          



  

你可能感兴趣的:(xml,osgi)