`
wdhdd889
  • 浏览: 150106 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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  运行成功!!!!!!!
          



  
分享到:
评论
3 楼 zengsir2008 2012-08-20  
怎么运行?
2 楼 liuyinhou 2011-07-06  
用eclipse带的org.eclipse.osgi  org.eclipse.osgi.util org.eclipse.osgi.services跑起来,然后怎么看运行效果啊?命令行里没有输出内容啊?
1 楼 liuyinhou 2011-07-06  
怎么运行?

相关推荐

Global site tag (gtag.js) - Google Analytics