JBoss OSGi用户指南(第一章:Introduction)

JBoss OSGi User Guide(1)–Introduction

1 Introduction

1.1 本章内容

l 什么是OSGi?

l OSGi框架综述

l OSGi 服务纲要

1.2 什么是OSGi?

The OSGi specifications define a standardized, component-oriented, computing environment for networked services that is the foundation of an enhanced service-oriented architecture.

OSGi 规范定义了一个标准的,面向组件的,为网络服务提供计算环境的增强的面向服务的架构体系。

Developing on the OSGi platform means first creating your OSGi bundles, then deploying them in an OSGi Framework.

如果要在OSGi平台上面开发自己的应用,首先需要创建自己的OSGi bundle,然后将这些bundles部署到你的OSGi平台上面。

What does OSGi offer to Java developers?

OSGi为Java开发者带来了什么?

OSGi modules provide classloader semantics to partially expose code that can then be consumed by other modules.

OSGi模块通过提供classloader语法来对外暴露自身部分的代码(服务),通过这种方式让其他模块消费这些代码(服务)。

The implementation details of a module, although scoped public by the Java programming language, remain private to the module.

一个模块的实现细节对于另外一个模块来说是完全私有的,即使模块内的类声明为public。

On top of that you can install multiple versions of the same code and resolve dependencies by version and other criteria.

最重要的是它允许你安装一份代码的不同版本,以及通过版本和其他的条件解决模块依赖。

OSGi also offers advanced lifecycle and services layers, which are explained in more detail further down.

OSGi同样也提供高级的服务如生命周期层和服务层,这部分将会在后面做详细的介绍。

What kind of applications benefit from OSGi?

什么样的应用可以从OSGi框架中获益?

Any application that is designed in a modular fashion where it is necessary to start, stop, update individual modules with minimal impact on other modules. Modules can define their own transitive dependencies without the need to resolve these dependencies at the container level.

如果你想把你的系统设计成流行的模块化的方式,这些模块需要支持随时启动,停止,更新操作并且对你的系统或者其他模块造成的影响最小,OSGi是最最理想的选择。每个模块都是自治的,它们可以自己解决模块依赖传递关系而不需要再容器层解决这些问题。

1.3 OSGi 框架综述

The functionality of the Framework is divided in the following layers:

OSGi框架按照功能被划分成如下几层:

Security Layer (optional)

安全层

Module Layer

模块层

Life Cycle Layer

生命周期层

Service Layer

服务层

Actual Services

实现层

clip_image001

Source: OSGi Alliance

来源:OSGi联盟

OSGi Security Layer

OSGi安全层

The OSGi Security Layer is an optional layer that underlies the OSGi Service Platform. The layer is based on the Java 2 security architecture.

OSGi安全层是可选的并位于服务平台的下面。这层的实现基于Java 2安全架构。

It provides the infrastructure to deploy and manage applications that must run in fine grained controlled environments.

它提供了一个前提,在部署和管理应用之前必须获取对环境的授权才能进行下一个的操作。

The OSGi Service Platform can authenticate code in the following ways:

OSGi服务平台可以通过如下方式对模块进行授权:

l By location

l 基于位置

l By signer

l 基于数字签名

For example, an Operator can grant the ACME company the right to use networking on their devices.

例如, 操作者授权ACME公司获取他的设备的网络使用权限。

The ACME company can then use networking in every bundle they digitally sign and deploy on the Operator’s device.

ACME公司可以在每个已经签名的bundle上面使用网络并且部署到操作者的设备上面。

Also, a specific bundle can be granted permission to only manage the life cycle of bundles that are

signed by the ACME company.

当然,只有被ACME公司签名的特定的bundle才能拥有管理bundle生命周期的权限。

clip_image002

Source: OSGi Alliance

来源:OSGi联盟

The current version of JBoss OSGi does not provide this optional layer. If you would like to see this implemented, let us know on the forums: http://community.jboss.org/en/jbossosgi .

当前版本的JBoss OSGi框架并不提供这部分可选的层。 如果你乐意我们提供这一个实现,请到我们的论坛发帖:http://community.jboss.org/en/jbossosgi

OSGi Module Layer

OSGi 模块层

The OSGi Module Layer provides a generic and standardized solution for Java modularization.

OSGi模块层为Java模块化提供了一个通用的,标准的解决方案。

The Framework defines a unit of modularization, called a bundle. A bundle is comprised of Java classes and other resources, which together can provide functions to end users.

框架定义了一个模块化的单位 --- bundle. Bundle就是一个Java 类和其他资源的压缩包,它是可以为最终用户提供功能的基本单位。

Bundles can share Java packages among an exporter bundle and an importer bundle in a well-defined way.

Bundle之间可以通过一种定义良好的,通过导入、导出的方式共享Java包。

Once a Bundle is started, its functionality is provided and services are exposed to other bundles installed in the OSGi Service Platform.

一旦Bundle启动成功后,它的功能就是就会就绪并且相关的服务就会暴露给其他已经安装在OSGi服务平台中Bundle。

A bundle carries descriptive information about itself in the manifest file that is contained in its JAR file. Here are a few important Manifest Headers defined by the OSGi Framework:

Bundle携带有一个描述自身信息的manifest文件,这个文件包含在JAR文件中。下面就描述一些OSGi框架定义的重要的Manifest 头信息:

- class used to start, stop the bundle Bundle-Activator

- Bundle-Activator: bundle启动,停止的入口类

- identifies the bundle Bundle-SymbolicName

- Bundle-SymbolicName:唯一标识bundle

- specifies the version of the bundle Bundle-Version

- Bundle-Version:标识本bundle的版本

- declaration of exported packages Export-Package

- Export-Package:声明了所有需要导出的包列表

- declaration of imported packages Import-Package

- Import-Package:声明了所有需要导入的包列表

The notion of OSGi Version Range describes a range of versions using a mathematical interval notation.

OSGi版本号区间的概念描述了一个版本号区间,类似于数学上面的区间定义。

For example

例如:

Import-Package: com.acme.foo;version="[1.23, 2)", com.acme.bar;version="[4.0, 5.0)"

With the OSGi Class Loading Architecture many bundles can share a single virtual machine (VM).

基于OSGi类加载架构,很多bundle可以共享一个单独的虚拟机。

Within this VM, bundles can hide packages and classes from other bundles, as well as share packages with other bundles.

在这个虚拟机内部,bundle可以对其他bundle隐藏包和class,也可以与其他bundle共享这些。

clip_image003 Source: OSGi Alliance

For example, the following import and export definition resolve correctly because the version range in the import definition matches the version in the export definition:

例如,下面例子中的导入和导出定义可以被正确的解决, 因为A中包导入的区间包含B中的导出版本号。

A: Import-Package: p; version="[1,2)"

B: Export-Package: p; version=1.5.1

clip_image005

Source: OSGi Alliance

Apart from bundle versions, OSGi Attribute Matching is a generic mechanism to allow the importer and exporter to influence the matching process in a declarative way. For example, the following statements will match.

除了按照Bundle版本号, OSGi属性匹配是一个通用的机制,它允许导入者和导出者通过声明的方式来影响匹配过程。 例如,下面的语句可以成功匹配:

A: Import-Package: com.acme.foo;company=ACME

B: Export-Package: com.acme.foo;company=ACME; security=false

An exporter can limit the visibility of the classes in a package with the included and exclude directives on the export definition.

一个导出配置允许限制一个包中具体类的可见性,可以通过include和exclude执行来完成。

Export-Package: com.acme.foo; include:="Qux*,BarImpl"; exclude:=QuxImpl

OSGi Life Cycle Layer

OSGi生命周期层

The Life Cycle Layer provides an API to control the security and life cycle operations of bundles.

A bundle can be in one of the following states:

生命周期层提供了一组API用来控制bundle的安全性和对bundle生命周期的操作。

clip_image006

Source: OSGi Alliance

A bundle is activated by calling its Bundle Activator object, if one exists. The Bundle Activator interface defines methods that the Framework invokes when it starts and stops the bundle.

Bundle通过调用Bundle Activator对象变为Activated状态。当Bundle启动或者停止的时候会调用Bundle Activator接口的实现类中的方法。

A Bundle Context object represents the execution context of a single bundle within the OSGi Service Platform, and acts as a proxy to the underlying Framework.

一个Bundle上下文对象表示一个在OSGi服务层内部的单独的bundle的执行上下文,它可以看做是对底层框架的代理。

An object is created by the Bundle Context Framework when a bundle is started. The bundle can use this private BundleContext object for the following purposes:

当Bundle启动的时候Bundle上下文框架会创建一个对象。这个bundle可以使用这个私有的BundleContext对象来完成如下的操作:

- Installing new bundles into the OSGi environment

- 安装新的bundle到OSGi环境中

- Interrogating other bundles installed in the OSGi environment

- 对其他已经安装的bundle进行轮询

- Obtaining a persistent storage area

- 获取一个持久化存储区域

- Retrieving service objects of registered services

- 在已经注册的服务中检索服务对象

- Registering services in the Framework service

- 在框架提供的服务中检索服务对象

- Subscribing or unsubscribing to Framework events

- 注册或者注销系统事件

OSGi Service Layer

OSGi服务层

The OSGi Service Layer defines a dynamic collaborative model that is highly integrated with the Life Cycle Layer.

OSGi服务层定义了一个与服务层深度整合的动态协作模型。

The service model is a publish, find and bind model.

服务层模型是一个发布,查找和绑定的模型。

A service is a normal Java object that is registered under one or more Java interfaces with the service registry.

一个服务是一个普通的Java对象,它通过服务注册的方式注册到一个或者多个服务注册接口下面。

OSGi services are dynamic, they can come and go at any time. OSGi service consumers, when written correctly, can deal with this dynamicity.

OSGi服务是动态的,他们可以在任何时刻获取或者卸载。OSGI服务的消费者,在运行正确的情况下可以处理好这种动态性。

This means that OSGi services provide the capability to create a highly adaptive application which, when written using services, can even be updated at runtime without taking the service consumers down.

这就意味着OSGi服务可以提供高适用性应用的可能,当服务正在被使用的时候,这个服务甚至可以在运行时更新而不需要服务消费者停止使用。

The OSGi Declarative Services and OSGi Blueprint specifications significantly simplify the use of services which means that a consumer gets the benefits of a dynamic services model for very little effort.

OSGi声明式服务和OSGi Blueprint规范对服务的使用进行了极大的简化,这个意味着一个服务的消费者只需很少的工作就可以从一个动态的服务模型中获取到需要的服务。

clip_image008

OSGi Services

1.4 OSGi 服务纲要

The OSGi Service Compendium is described in the OSGi Compendium and Enterprise specifications.

OSGi服务纲要部分主要在OSGi纲要和OSGi企业级规范中描述。

It specifies a number of services that may be available in an OSGi runtime environment.

它制定了一组可能在OSGi运行时环境中可用的服务。

Although the OSGi Core Framework specification is useful in itself already, it only defines the OSGi core infrastructure.

尽管OSGi核心框架规范本身已经很有用,但是它只是定义了OSGi核心基础。

The services defined in the compendium specification define the scope and functionality of some common services that bundle developers might want to use.

这些在纲要规范中定义的服务定义了一些通用服务的功能函数,这些方法对于bundle开发者来说或许会用到。

Here is a quick summary of the popular ones:

下面就是一个最流行的简短总结:

Log Service

日志服务

Chapter 101 in the Compendium and Enterprise specifications.

OSGi规范纲要和企业版规范第101章

The Log Service provides a general purpose message logger for the OSGi Service Platform.

OSGi服务平台提供了一个通用的日志服务。

It consists of two services, one for logging information and another for retrieving current or previously recorded log information.

它包含了两个服务,一个是日志信息,另外一个是用来检索当前及历史日志信息。

The JBoss OSGi Framework provides an implementation of the Log Service which channels logging information through to the currently configured system logger.

JBoss OSGi框架提供了一种日志服务的实现,它可以通过当前配置的系统日志信息来产生频道日志信息。

Http Service

Http服务

Chapter 102 in the Compendium and Enterprise specifications.

The Http Service supports a standard mechanism for registering servlets and resources from inside an OSGi framework.

Http服务是由OSGi框架内部提供的一种注册servlets和资源的标准机制。

This can be used to develop communication and user interface solutions for standard technologies such as HTTP, HTML, XML, etc.

通过这种机制可以开发通信和用户界面的标准解决方案,例如HTTP,HTML,XML等。

Configuration Admin Service

配置管理服务

Chapter 104 in the Compendium and Enterprise specifications.

The Configuration Admin service allows an operator to set the configuration information of deployed bundles.

配置管理服务允许操作者对已经部署的Bundle设置配置信息。

clip_image010

Source: OSGi Alliance

The JBoss OSGi Framework provides an implementation of the Configuration Admin Service which obtains its configuration information from the JBoss Application Server configuration data, for instance the standalone.xml file.

JBoss OSGi框架提供了一种配置管理服务的实现,它通过JBoss应用目录下的standalone.xml中的配置数据获取自己的配置信息。

Metatype Service

元类型服务

Chapter 105 in the Compendium and Enterprise specifications.

The Metatype Service specification defines interfaces that allow bundle developers to describe attribute types in a computer readable form using so-called metadata.

元类型服务规范定义了一系列接口, 它允许bundle开发者通过一种计算机可读的方式描述属性类型,也就是所谓的元数据。

This service is mostly used to define the attributes and datatypes used by Configuration Admin Service information.

这个服务通常被配置管理服务信息用来定义属性和数据类型。

User Admin Service

用户管理服务

Chapter 107 in the Compendium and Enterprise specifications.

Bundles can use the User Admin Service to authenticate an initiator and represent this authentication as an authorization object.

Bundle可以使用用户管理服务来对初始化者进行授权及代表这个认证作为一个认证对象。

Bundles that execute actions on behalf of this user can use the Authorization object to verify if that user is authorized.

Bundle执行此用户的动作可以使用这个授权对象来验证用户的权限。

Declarative Services Specification

声明式服务规范

Chapter 112 in the Compendium and Enterprise specifications.

The Declarative Services (DS) specification describes a component model to be used with OSGi services.

声明式服务规范描述了一个被OSGi服务使用的组件模型。

It enables the creation and consumption of OSGi services without directly using any OSGi APIs.

它使得创建和消费OSGi服务而不需要直接使用任何的OSGi API变为可能。

Service consumers are informed of their services through injection.

通过注入的方式来为服务消费者提供他们需要的服务。

The handling of the OSGi service dynamics is done by DS. See also the Blueprint Specification.

动态的OSGi服务处理时由DS层来完成的。同时请参见Blueprint规范。

Event Admin Service

事件管理服务

Chapter 113 in the Compendium and Enterprise specifications.

The Event Admin Service provides an asynchronous inter-bundle communication mechanism.

事件管理服务提供了一种bundle内部异步通信的机制。

It is based on an event publish and subscribe model, popular in many message based systems.

它是基于当前非常流行的以消息传递为基础的系统所设计的事件发布和注册模型。

Blueprint Specification

Blueprint规范

Chapter 121 in the Enterprise specification.

The OSGi Blueprint Specification describes a component framework which simplifies working with OSGi services significantly.

OSGi Blueprint规范描述了一个极大的精简了调用OSGi服务的组件架构。

To a certain extent, Blueprint and DS have goals in common, but the realization is different.

从特定角度来说,Blueprint和DS都致力于通用性,但是他们的实现是不同的。

One of the main differences between Blueprint and DS is in the way service-consumer components react to a change in the availability of required services.

他们之间的主要区别就是服务消费者组件对于一个获取到的可用的服务时的影响。

In the case of DS the service-consumer will disappear when its required dependencies disappear, while in Blueprint the component stays around and waits for a replacement service to appear.

当获取到的服务不可用时,DS的服务消费者也将不可用,而对于Blueprint来说,这个服务仍将可用直到一个新的可替代的服务出现。

Each model has its uses and it can be safely said that both Blueprint as well as DS each have their supporters.

每一个模型都有他们的使用者,可以负责任的说不管是Blupprint还是DS都有他们的支持者。

The Blueprint specification was heavily influenced by the Spring framework.

Sping framework是在受到Blueprint规范的深刻影响下产生的。

Remote Services Specifications

远程服务规范

Chapters 13 and 122 in the Enterprise specification.

OSGi Remote Services add distributed computing to the OSGi service programming model.

OSGi远程服务添加分布式的计算能力到OSGi服务程序模型中。

Where in an ordinary OSGi Framework services are strictly local to the Java VM, with Remote Services the services can be remote.

在一个普通的OSGi框架中只能局限于本地Java虚拟机,但是对于远程服务来说是允许远程的。

Services are registered and looked up just like local OSGi services, the Remote Services specifications define standard service properties to indicate that a service is suitable for remoting and to find out whether a service reference is a local one or a remote one.

服务的注册和获取就跟本地OSGi服务一样,远程服务规范定义了一个标准的属性集来指示一个服务适合于远程调用并且确定一个服务是属于远程服务还是本地服务。

JTA Specification

JTA (Java Transaction Architecture)规范

Chapter 123 in the Enterprise specification.

The OSGi-JTA specification describes how JTA can be used from an OSGi environment.

OSGi-JTA规范描述了JTA如何在OSGi环境中使用。

It includes standard JTA-related services that can be obtained from the OSGi registry if an OSGi application needs to make use of JTA.

如果一个OSGi应用需要使用JTA,它提供了一个从OSGi注册容器中获取JTA相关服务的标准。

JMX Specification

JMX (Java Management Extensions)规范

Chapter 124 in the Enterprise specification.

The OSGi-JMX specification defines a number of MBeans that provide management and control over the OSGi Framework.

OSGi JMX规范定义了一些列的MBean来为通过OSGi框架管理和控制提供服务。

JDBC Specification

JDBC (Java DataBase Connectivity)规范

Chapter 125 in the Enterprise specification.

The OSGi-JDBC specification makes using JDBC drivers from within OSGi easy.

OSGi JDBC规范使得使用JDBC非常的方便,就像在OSGi内部一样。

Rather than loading a database driver by class-name (the traditional approach, which causes issues with modularity in general and often requires external access to internal implementation classes), this specification registers the available JDBC drivers under a standard interface in the Service Registry from where they can be obtained by other Bundles without the need to expose internal implementation packages of the drivers.

而不是通过加载类名称的方式一样(传统的方式经常会在模块内部出现问题并且通常需要外部对内部实现类的访问权限),本规范通过在一个标准的服务接口中注册可用的JDBC驱动,这些注册服务可以被其他的bundle获取但不需要暴露内部驱动实现类的细节。

JNDI Specification

JNDI (Java Naming and Directlry Interface)规范

Chapter 126 in the Enterprise specification.

The OSGi-JNDI specification provides access to JNDI through the OSGi Service Registry.

OSGi-JNDI规范提供了JNDI通过OSGi服务注册获取服务的权限。

Additionally, it provides access to the OSGi Service Registry through JNDI.

另外,它为OSGi提供了通过JNDI注册服务的权限。

The special osgi: namespace can be used to look up OSGi services via JNDI.

通过特殊的osgi命名空间可以用来通过JNDI查找OSGi服务。

JPA Specification

JPA (Java Presistence API)规范

Chapter 127 in the Enterprise specification.

The OSGi-JPA specification describes how JPA works from within an OSGi framework.

OSGi-JPA规范描述了JPA如何在OSGi框架内部工作。

Web Applications Specification

Web应用规范

Chapter 128 in the Enterprise specification.

The Web Applications specification describes Web Application Bundles. A WAB is a .WAR file which is effectively turned into a bundle.

Web应用规范描述了Web应用bundle,一个WAB就是一个由.WAR的文件转换为bundle而来的。

The specification describes how Servlets can interact with the OSGi Service Registry and also how to find all the available Web Applications in an OSGi Framework.

规范描述了Servlet与OSGi服务注册组件的交互,以及如何在OSGi框架中找到所有可用的Web应用。

Additionally, the Web Applications spec defines a mechanism to automatically turn an ordinary .WAR file into a Web Application Bundle.

另外,Web应用说明书定义了一个自动的将.WAR文件转换为一个Web应用bundle的机制。

Service Tracker Specification

服务追踪规范

Chapter 701 in the Compendium and Enterprise specifications.

The Service Tracker specification defines a utility class, ServiceTracker.

服务追踪规范定义了一个通用的类:ServiceTracker

The ServiceTracker API makes tracking the registration, modification, and unregistration of services much easier.

ServiceTracker API使得对服务的注册,修改及反注册都变得非常容易。

posted @ 2014-01-23 22:22  Rush_SONG  阅读(1327)  评论(0编辑  收藏  举报