osgi

OSGI
OSGI(Open Service Gateway Interface)是一个开放服务协议。Equinox是OSGI的一个实现。
服务以模块(Bundle)的形式组织。
模块之间通过服务接口或package引用。

模块的复用和扩展

在基于OSGI的方式下,模块的复用就非常简单了,只需要将bundle部署即可为系统增加相应的模块。

服务可以在运行期间被动态的加载,卸载,启动,停止。

OSGI带来了什么

从需求实现方面,OSGI为动态扩充,修改系统功能和改变系统行为提供了支撑。

从技术角度方面,OSGI带来了规范化的模块组织及统一的开发方式,这为传统的模块的组织,模块开发以及模块积累提供了一种全新的指导以及支撑。

可插拔的系统

可动态改变行为的系统

稳定、高效的系统 -- 微内核

规范的,可积累的模块

面向服务的组件模型设计思想

Bundle中的概念

导入以及输出的包

接口,服务

bundle的依赖

Rquire-bundle - 直接引用其他的bundle

服务的注册和取得

context.registerService(服务标识名,服务实例,服务实例属性)

ServiceReference serviceRef = context.getServiceReference(服务标识名);

Object service = context.getService(serviceRef);

监听服务的状态,监听系统的状态

发布系统

把bundle打包成jar

OSGI控制台

Equinox基于OSGI的扩展

Extension Registery机制

Eclipse Adapter机制

深入OSGI

Module Layer (Module:bundle)

Module的共享机制

module的classloader机制


查找bundle中的资源文件

bundle.getResource,bundle.getEntry,bundle.findEntries

require-Bundle也可以视为实现资源文件共享的一种方式

三种特殊形式的bundle

Require-bundle

Fragment Bundle - 附属于其他的bundle

Extension Bundle - 用于扩展system bundle -> 附属于system bundle的fragment bundle

Lifecycle Layer

INSTALLED,RESOLVED,STARTING,ACTIVE,STOPPING,UNINSTALLED

Service Layer

定义了bundle动态协作的服务发布、查找和绑定模型

服务注册,服务的获取,服务的监听

Sercurity Layer

java本身的security策略和数字签名策略

StartLevel Service - 启动顺序控制

使用场景

安全模式,Splash Screen,处理不稳定的bundle,高优先级bundles

Declarative Services 服务的声明

Component的概念

对外提供Service

使用其他Component提供的Service

交由OSGI框架管理生命周期



OSGI-INF/components.xml:

<?xml version="1.0" encoding="UTF-8"?>

<component name="osgi.DemoComponent">

<implementation class="org.riawork.opendoc.osgi.DemoComponent"/>

<service>

<provide interface="org.riawork.opendoc.osgi.ValidatorService" /> <!-- 提供服务 -->

<reference name="HTTPService" interface="org.osgi.service.http.HttpService" bind="setHttpService"

unbind="unsetHttpService" /> <!-- 引用服务 -->

</service>

</component>

MANIFEST.MF:

Service-Component:OSGI-INF/components.xml

Service的应用策略

Cardinality,应用策略(static,dynamic),属性过滤

Configuration Admin Service (动态配置信息管理)


跟踪属性变化

ManagedService接口,updated方法

private ServiceRegistration serviceReg=null;
private ServiceRegistration manSrvReg=null;

public void start(BundleContext context) throws Exception {
DemoCM cm=new DemoCMImpl();
Dictionary props=new Hashtable();
props.put("cm.port", "1220");

//注册服务
serviceReg=context.registerService(DemoCM.class.getName(), cm, props);
Dictionary manProps=new Hashtable();

manProps.put(Constants.SERVICE.PID, DemoCM.class.getName()); //要跟踪的服务

//注册监控服务
manSrvReg=context.registerService(ManagedService.class.getName(), this, manProps);
}

public void stop(BundleContext context) throws Exception {
serviceReg.unregister(); manSrvReg.unregister();
}

public void updated(Dictionary props) throws ConfigurationException {
serviceReg.setProperties(props);
}

管理属性

ServiceReference serviceRef=context.getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin admin=(ConfigurationAdmin)context.getService(serviceRef);
Configuration config=admin.getConfiguration(DemoCM.class.getName());

config.getProperties...;
config.update();



Event Admin Service


其它的服务

HttpService,Log service, Preference Service, Metatype Service, XML Parser Service...



OSGI 关键部分讲解

.....



应用OSGI

模块化设计



面向服务的组件模型设计

动态设计

面向接口的开发



www.osgi.org, www.eclipse.org/equinox








Eclipse 中的应用
Eclipse许多按照OSGI框架搭建起来的子系统组成的。plugins。
这些子系统又有各自的extension points,使得其它的plugins可以在它们上面扩展功能。
子系统:
Platform runtime
Platform management(workspace)
workbench UI
Help system
Team support
Debug support
other utilities

When you want to provide code that extends the platform, you do this by defining system extensions in your plug-in. The platform has a well-defined set of extension points - places where you can hook into the platform and contribute system behavior. From the platform's perspective, your plug-in is no different than basic plug-ins like the resource management system or the workbench itself.

创建一个plugin工程
其中的MANIFEST.MF是OSGI的元数据描述文件。描述了这个plugin(OSGI中叫bundle)提供哪些服务,依赖哪些服务等。
plugin.xml是eclipse特定的描述文件。它描述了这个plugin的扩展方式。



GEF (Graphical Editing Framework)
MVC架构,GEF只负责model的表现和编辑,它提供了Viewers(graphical and tree-based)。

EMF(Eclipse Modeling Framework)
EMF is a Java framework and code generation facility for building tools and other applications based on a structured model

从一个模型产生Java代码(?).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值