OSGI Specification

来源:互联网 发布:linux 查看软件版本 编辑:程序博客网 时间:2024/04/28 21:52

OSGI Specification

OSGiOpen Service Gateway Initiative的简称,该组织建立于1999年,是一个非赢利机构,旨在建立一个开放的服务规范,为通过网络向设备提供服务建立开放的标准。

OSGI 规范包括了构建开放的可交付网络服务的各方面,OSGI规范又包括了以下子规范。

Framework规范(OSGI核心,提供一个安全的可管理的Java Framework来部署可扩展的Java服务。)

Package Admin Service规范(来管理不同的Bundle之间的引用关系。当Bundle更新或者反安装时判断是否有其他的服务正在使用当前的Bundle

Start Level规范(定义了启动和停止一个OSGi Service Platform时,不同的Bundles的启动或者停止的先后顺序)

Permission Admin Service规范(Bundle是否许可执行另外的Bundle的代码)

URL Handlers Service规范(怎样注册URL Schema,如何将java.io.InputStream对象转换为特定的Java对象)

Log Service规范 (以下略。。。)

Configuration Admin Service规范

Device Access Specification

User Admin Service Specification

IO Connector Service Specification

Http Service Specification

Preference Service Specification

Wire Admin Service Specification

XML Parser Service Specification

Metatype Specification

Service Tracker Specification

Measurment and State Specification

Position Specification

Execution Environment Specfication

OSGI Framework

       FrameworkOSGI Service Platform规范的核心组成部分。它提供了一个通用的、安全可管理的Java framework。通过这个Framework可以支持一种叫做bundlesService application的部署和扩展。

       OSGI兼容设备可以下载并且安装OSGI bundles,也可一当他们不再需要的时候删除。bundles安装后会注册一定数量的Services,并被由同一个Framework下的其他bundles使用。

       在一个动态扩展的的 OSGI环境中,Framework管理bundles的安装和更新。同时也管理bundlesServices之间的依赖关系。

       Framework提供给bundle开发者必须的资源来在Java平台上开发,为开发的bundles提供了代码动态加载的功能, 也使得开发者开发、部署一个大规模的Services变的很容易。

       其次,FrameworkJava bundle开发者提供了简明一致的编程模型。简化了开发部署的复杂性。这个编程模型允许开发者将自己的接口规范绑定到OSGI环境中的ServiceThe selection of a specific implementation, optimized for a specific need or from a specific vendor, can thus be deferred to run-time.

       一个一致的编程模型帮助开发者可以应付一些可估计的危急错误。Framework将会运行在不同的硬件环境上,但一致的接口确保软件组建可以运行在一致的服务接口上。

The Bundle Object

对于每一个安装在OSGI Service Platformbundle都有一个与之关联的bundle object。一个bundle对象用来管理bundle的生命周期。这项工作通常由Management Agent来做。

Bundle State

bundle有以下状态;

INSTALLED – The bundle has been successfully installed. Native code clauses must have been validated.

RESOLVED – All Java classes that the bundle needs are available. This state indicates that the bundle is either ready to be started or has stopped.

STARTING – The bundle is being started, and the BundleActivato r. start method has been called and has not yet returned.

STOPPING – The bundle is being stopped, and the BundleActivato r. stop method has been called and has not yet returned.

ACTIVE – The bundle has successfully started and is running.

UNINSTALLED – The bundle has been uninstalled. It cannot move into another state.

Bundle接口定义了getState()方法来返回bundle的状态。

Installing Bundles

BundleContext定义了以下两各方法来安装bundle

l         instal lBundle(String) – Installs a bundle from the specified location string (which should be a URL).

l         instal lBundle(Stri ng,InputStream) – Installs a bundle from the specified InputStream object.

每一个bundle都由一个String来唯一标识。如果一个已经安装的bundle正在使用这个specified locationinstallBundle()方法必须返回原来的bundle对象,而不是安装一个新的bundle。将要安装的bundle必须是持久的并且安装是全自动的。

当安装一个bundle时,Framework会去找出bundle依赖的Native code。如果不存在,bundle不能被安装。

一旦一个bundle安装完成,会创建一个Bundle对象。对bundle的生命周期的操作都通过该对象完成。可以用来startstopupdateuninstallbundle

Resolving Bundles

Framework成功查找完bundle的代码依赖关系,bundle的状态就变为RESOLVED。依赖关系包括:

l         Classpath dependencies from the bundle’s Bundle-Classpath manifest header.

l         Package dependencies from the bundle’s Export-Package and Import-Package manifest headers.

Plugin.xmlManifest对照关系表

plugin.xml tag/attribute

manifest.mf header

Bundle-SymbolicName

Bundle-Version

Bundle-Name

Bundle-Vendor

Bundle-Activator

Fragment-Host

Fragment-Host: ; bundle-version=

,

Require-Bundle

,

Bundle-ClassPath

 

Starting Bundles

       Bundle接口定义了start()方法来启动一个bundle。如果启动成功,bundle的状态变为ACTIVE并保持这个状态知道被停止。

       Bundle为了被启动,首先必须先分析它的依赖关系。如果分析依赖关系失败,而去启动这个bundlestart()方法会抛出一个BundleException例外。

       如果bundle依赖关系分析成功,通过调用Bundle Activator对象来激活bundleBundleActivator 接口定义了方法可供Framework调用,来启动或者停止bundle

       开发者可以在bundlemanifest文件中定义一个Bundle-Activator manifest header, 来告诉OSGI environment作为Bundle Activator的类的文件全名。该类实现了BundleActivator接口。

       对一个bundle来说,提供一个Bundle Activator并不是必须的。但是只有通过这种方法才能得到bundleBundleContext对象。

BundleActivato 提供以下方法启动或停止一个bundle

? start(BundleContext) – This method can allocate resources that a bundle needs and start threads, and also usually registers the bundle’s services. If this method does not register any services, the bundle can register the services it needs at a later time, for example in a callback, as long as it is in the ACTIVE state.

? stop(BundleContext) – This method must undo all the actions of the BundleActivator.star t(BundleContext) method. However, it is unnecessary to unregister services or Framework listeners because they must be cleaned up by the Framework anyway.

Stopping Bundles

Bundle interface定义了stop()方法停止一个bundle。该方法停止一个bundle并将bundle的状态改为RESOLVED

BundleActivator接口定义了stop(BundleContext)方法,由Framework调用来停止一个bundle。该方法必须释放补激活以来的所有资源。所有和正在停止的bundle相关的线程必须马上停止。线程代码不会再使用同Framework相关的对象。

Updating Bundles

Bundle interface定义了两个方法来update一个bundle

l         update() – This method updates a bundle.

l         update(InputStream) – This method updates a bundle from the specified InputStream object.

Update过程支持从一个版本的bundle迁移到一个新版本的bundle。一个新的bundle和另外一个bundle是向后版本兼容的。

       Framework必须确保在任何时刻只有一个版本的bundleclass是可用的。如果被更新的bundle导出的任何的package被其他的bundle使用,那么那些package将不能被更新。他们的老版本将会一直保留,直到org.osgi.service.admin.PackageAdmin.refreshPackages方法被调用或者Framework重启。

Uninstalling Bundles

Bundle interface定义了uninstall()方法来从Frameworkuninstall一个bundle。这个方法会让Framework通知其他的bundles说一个bundle已经被uninstalled,并且将这个bundles的状态设置为UNINSTALLED

OSGI & eclipse

自从eclipse RC3版本中集成并实现了OSGI规范。eclipse构建在OSGI层之上。Plugins成为了OSGI bundles可以被启动,停止。当eclipse启动时,它首先将原来的Plugins转换为OSGI bundles。这样就使在运行时刻安装,卸载,启动和更新Plugin成为可能。尽管还有一些功能上的不完善,但现在我们还是可以在eclipse M6或更新的版本上运行一个通常意义下的OSGI bundles。目前eclipse还不能提供一个很好的环境来Debug OSGI bundles。手机制造业也已经在下一代的smart phone中采用OSGI规范,所以将来在eclipse中调试手机程序也将会变的很容易。

eclipse沿用了一种分层结构。OSGI层作为底层的基础来管理OSGI bundlesOSGI层提供了创建和部署OSGI bundles的能力。在OSGI层之上是eclipse registryPlugin加入了扩展和扩展点的概念。当一个OSGI bundleOSGI层解析后会写入plugin registry中。plugin registry用来管理扩展和扩展点。在registry层之上就是Workbench Workspace

org.eclipse.osgi.internal.resolver.BundleInstaller

       public void installBundle(BundleDescription toInstall) throws BundleException;

       public void uninstallBundle(BundleDescription toUninstall) throws BundleException;

       public void updateBundle(BundleDescription toRefresh) throws BundleException;

A bundle installer allows the platform admin implementation to delegate the behavior of installing/uninstalling bundles to another object

org.eclipse.core.runtime.adaptor. EclipseBundleInstaller

EclipseBundleInstaller实现了BundleInstaller接口

org.osgi.framework. BundleActivator

public abstract void start(BundleContext context) throws Exception;

public abstract void stop(BundleContext context) throws Exception;

org.eclipse.core.runtime.Plugin实现了BundleActivator。所有的plugin都必须从Plugin类继承。

原创粉丝点击