简体   繁体   English

OSGi:Import-Package / Export-Package和Require-Capability / Provide Capability之间有什么区别?

[英]OSGi: What's the difference between Import-Package/Export-Package and Require-Capability/Provide Capability?

I am currently working with the OSGi framework but I have a question about some concepts that are not 100% clear to me. 我目前正在使用OSGi框架,但我对一些不是100%明确的概念有疑问。 I have searching for it myself but I could not find a decent answer that clearly explains it. 我自己一直在寻找,但我找不到一个明确解释它的体面答案。

In a bundle his manifest header 2 of the headers that get used are Import-Package and Export-Package . 在捆绑包中,他使用的标题的清单头2是Import-PackageExport-Package The names speak for themselves: a demand for a certain package and a offering of a certain package. 这些名称不言自明:对特定包装的需求和某种包装的提供。 In order to get that package (or give that package), the complete bundle must be installed in the framework where the Import is needed. 为了获得该包(或提供该包),必须在需要Import的框架中安装完整的包。

But then we get to the part of the Requirements-Capabilities model. 但后来我们得到了Requirements-Capabilities模型的一部分。 This can practically do the same as the Import-Package and Export-Package headers. 这实际上可以与Import-PackageExport-Package标头相同。 There are also headers for this Requirements-Capability model: Require-Capability and Provide-Capability . Requirements-Capability模型还有标题: Require-CapabilityProvide-Capability Again these stand for demanding something and for providing something. 这些都代表着要求某些东西和提供某些东西。

I know that Requirements-Capability model was only introduced later on in the development of the OSGi specification(s). 我知道Requirements-Capability模型仅在OSGi规范的开发中稍后介绍。 Can't exactly find at what year and version it was presented. 无法确切地找到它呈现的年份和版本。

But, 但,

  • Why has this been added to the specification? 为什么将这个添加到规范中? I don't see what it has more to offer than what the Import / Export-package already offered: creating dependencies on other packages/bundles? 我没有看到它提供的内容比Import / Export-package已提供的更多:创建对其他包/包的依赖?

  • Could someone give me a better understanding in the difference (pro's and con's) between these 2 sets of concepts? 有人能让我更好地理解这两组概念之间的区别(专业和概念)吗?

When we started with OSGi in 1998 we had some clear requirements but of course, no clear view of what would come out of it. 当我们在1998年开始使用OSGi时,我们有一些明确的要求,但当然,没有明确的看法会产生什么。 So we started to explicitly model the requirements and capabilities we had: packages. 所以我们开始明确地模拟我们的要求和功能:包。 The Import-Package requires a capability and that capability is provided by an Export-Package. Import-Package需要一个功能,并且该功能由Export-Package提供。

In 2003 Eclipse wanted to start using OSGi but they need a facility to require another bundle, they did not like the idea of exporting and importing all their packages. 在2003年,Eclipse想要开始使用OSGi,但他们需要一个设施来要求另一个捆绑,他们不喜欢导出和导入所有包的想法。 Actually, at that time they failed to see the benefit of packages. 实际上,当时他们没有看到包装的好处。 To satisfy them we added Require-Bundle and Fragment-Host (another one of their desires that turned out not to be so good.) 为了满足它们,我们添加了Require-Bundle和Fragment-Host(他们的另一个愿望,结果证明不是那么好。)

After we specified OSGi 4.x with these extensions we starting thinking about a repository, Richard had developed the Oscar Bundle Repository. 在我们使用这些扩展指定OSGi 4.x后,我们开始考虑存储库,Richard开发了Oscar Bundle存储库。 Analyzing the situation with the new headers in OSGi 4.0 it became clear that the implementation of Import-Package looked a lot like Require-Bundle, and even resembled Fragment-Host processing. 使用OSGi 4.0中的新标题分析情况很明显,Import-Package的实现看起来很像Require-Bundle,甚至类似于Fragment-Host处理。

In 2006 Richard S. Hall and I wrote RFC 112 proposing a more generic model that captured the semantics of the existing dependency model but was not specific for each type of requirement. 2006年,Richard S. Hall和我编写了RFC 112,提出了一个更通用的模型,它捕获了现有依赖模型的语义,但并不是针对每种类型的需求。 Ie for the Framework resolver the Import-Package and Require-Bundle only differ in their namespace . 即,对于Framework 解析器 ,Import-Package和Require-Bundle仅在其命名空间方面有所不同。 Thinking of Import-Package as a generic requirement and Export-Package as a generic capability made the repository model extremely simple. 将Import-Package视为通用要求并将Export-Package视为通用功能使存储库模型非常简单。 Even better, it was extendable since we could always add more namespaces. 更好的是,它可以扩展,因为我们总是可以添加更多的命名空间。 This made the resolver completely independent of the actual namespaces used. 这使得解析器完全独立于所使用的实际名称空间。

After some very heated discussions, the OSGi Core Platform Expert Group decided to accept the basic idea and developed the Requirements and Capabilities specifications. 经过一番激烈的讨论后,OSGi核心平台专家组决定接受基本思想,并制定了要求和能力规范。 Although this was originally a model for the repository, it turned out to be highly useful for the Framework itself. 虽然这最初是存储库的一个模型,但它对框架本身非常有用。 We decided therefore to adapt the existing specifications to this model. 因此,我们决定使现有规格适应该模型。 OSGi 4.3 internally models the Import-Package, Export-Package, Require-Bundle, etc. as requirements and capabilities of a resource (the bundle). OSGi 4.3内部将Import-Package,Export-Package,Require-Bundle等建模为资源 (bundle)的要求和功能。 For backward compatibility, we kept the existing headers but they are internally translated to requirements and capabilities. 为了向后兼容,我们保留了现有标头,但它们在内部转换为要求和功能。

Then finally to the answer to your question. 然后最后回答你的问题。 Over time the OSGi specifications added more and more namespaces . 随着时间的推移,OSGi规范增加了越来越多的命名空间 A namespace is like a type for a Requirement and a Capability. 命名空间就像Requirement和Capability的类型 It defines the semantics of a set of properties of a Capability in that namespace. 它定义了该命名空间中Capability的一组属性的语义。 A Requirement is a filter expression that is asserted on those properties. Requirement是在这些属性上声明的过滤器表达式。 A Resource has a set of Capabilities that are provided to the runtime when all its Requirements are satisfied. 资源具有一组功能,在满足其所有需求时提供给运行时。 It is the task of the Resolver to find a set of resources that are all satisfied with each other's capabilities and capabilities provided by the runtime. 解析器的任务是找到一组资源,这些资源对运行时提供的彼此的功能和功能都满意。

For example, we added the osgi.ee namespace that defines exactly on what VM's the bundle can run. 例如,我们添加了osgi.ee命名空间,该命名空间完全定义了bundle可以运行的VM。 We added the osgi.extender namespace that models a dependency on an external program like the Service Component Runtime (SCR). 我们添加了osgi.extender命名空间,该命名空间模拟了外部程序(如服务组件运行时(SCR))的依赖关系。 Most SCR components do not require any package from the SCR itself, we tried hard to make them as independent as possible. 大多数SCR组件不需要SCR本身的任何包装,我们努力使它们尽可能独立。 However, a SCR component will sit useless unless some bundle in the runtime provides the SCR functionality. 但是,除非运行时中的某些bundle提供SCR功能,否则SCR组件将无用。 Notice that this cannot use Require-Bundle because there are multiple implementations of SCR. 请注意,这不能使用Require-Bundle,因为有多个SCR实现。 I think there are about 20 namespaces. 我认为大约有20个名称空间。 Each namespace is defined in a Namespace class. 每个命名空间都在Namespace类中定义。

This model has given the OSGi a number of advantages: 该模型为OSGi提供了许多优势:

  • Cohesion Although the specification has added many namespaces the resolver implementations never had to change since they worked on the generic model. 内聚尽管规范添加了许多名称空间,但解析器实现从不需要改变,因为它们在泛型模型上工作。
  • Fine-Grained OSGi bundles are unique in how they describe their dependencies in a very fine-grained way. 他们是如何描述一个非常精细的方式及其依赖性细粒度 OSGi包是独一无二的。 All module systems I know tend to use the simple module-to-module dependency that does not allow substitution. 我所知道的所有模块系统都倾向于使用不允许替换的简单的模块到模块依赖。
  • Flexible Since the Framework reifies the dependencies between bundles it is possible in runtime to leverage these dependencies. 灵活由于Framework为bundle之间的依赖关系提供了灵活性,因此在运行时可以利用这些依赖关系。 For example, in OSGi enRoute I linked a bundle to its web page traversing these runtime wirings. 例如,在OSGi enRoute中,我将一个包链接到遍历这些运行时布线的网页。

I personally consider the Requirements and Capability model of OSGi one of its best kept secrets. 我个人认为OSGi的要求和能力模型是其最好的秘密之一。 As far as I can see it could be used in a lot of areas to improve many development projects into the world of software engineering. 据我所知,它可以用于许多领域,以改进许多开发项目进入软件工程领域。

The only disappointing part in this question is that I thought we'd described this pretty well in the Core specification ? 这个问题中唯一令人失望的部分是我认为我们在核心规范中已经很好地描述了这一点? :-) :-)

The requirements and capabilities model is an extension of the Import/Export package model. 需求和功能模型是Import / Export包模型的扩展。 Actually you can express a package import as a requirement and a package export as a capability. 实际上,您可以将包导入表示为需求,将包导出表示为功能。

Exporting / Importing packages allows for loose coupling. 导出/导入包允许松散耦合。 You export an API and the client imports it. 您导出API并且客户端导入它。 This way the client only needs to know about the API so loose coupling is achieved. 这样客户端只需要知道API就可以实现松耦合。

At a later stage when you assemble the application out of bundles this loose coupling makes it difficult to automate the process. 在稍后阶段,当您将应用程序从捆绑中组装出来时,这种松散的耦合会使该过程自动化变得困难。

If you just provide your client bundle to a resolver then it can only automatically find that you need the bundle that provides the API. 如果您只是将客户端软件包提供给解析程序,那么它只能自动发现您需要提供API的软件包。 If the implementation of the API is in a different bundle then the resolver has no way to know that you need it. 如果API的实现位于不同的包中,则解析器无法知道您是否需要它。

This is where requirements can help. 这是需求可以帮助的地方。 Let's take the HTTP Whiteboard model . 我们来看看HTTP Whiteboard模型吧 A bundle that want to publish a servlet needs to import the servlet api package but is also needs to express that it wants an implementation of the osgi http whiteboard. 想要发布servlet的bundle需要导入servlet api包,但也需要表示它想要osgi http白板的实现。

This can be expressed by the requirement with namespace="osgi.implementation", name="osgi.http", version="1.1.0". 这可以用namespace =“osgi.implementation”,name =“osgi.http”,version =“1.1.0”的要求来表示。 As this is difficult to writer by hand there is annotation support for it. 由于这很难手工编写,因此有一个注释支持。

@HttpWhiteboardServletPattern("/myservlet")
@Component(service = Servlet.class)
public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws IOException {
        resp.getWriter().println("Hello");
    }
}

The annotation @HttpWhiteboardServletPattern indirectly translates to the requirement above. @HttpWhiteboardServletPattern注释间接转换为上述要求。

So when you build a bundle with this class it will import the servlet api package and also have a requirement for an http whiteboard implementation. 因此,当您使用此类构建一个bundle时,它将导入servlet api包,并且还需要一个http whiteboard实现。

Now if you look at an implementation bundle like the felix http service you will see that it provide the capability for the whiteboard impl. 现在,如果您查看像felix http服务这样的实现包,您将看到它提供了白板impl的功能。

So if you have a OSGi repository with your bundle, the servlet API and the felix http service. 因此,如果您有一个包含bundle,OSlet API和felix http服务的OSGi存储库。 Then the resolver can provide you with a complete application if you only give it your bundle. 然后解析器可以为您提供完整的应用程序,如果您只提供捆绑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSGI Import-Package:版本vs bundle-version - 有什么区别? - OSGI Import-Package: version vs bundle-version - what's the difference? Eclipse e4 - 缺少约束:要求能力:osgi.extender - Eclipse e4 - Missing Constraint: Require-Capability: osgi.extender 为什么此OSGi导入包不起作用? - Why does this OSGi Import-Package not work? 如何避免Gradle的osgi插件为私有软件包生成export-pacakge条目,为嵌入式依赖项生​​成import-package条目 - How to avoid that Gradle's osgi plugin generates export-pacakge entry for private packages and import-package entry for embedded dependencies Liferay:无法部署模块。 未解决的需求:需求能力:osgi.ee; 滤波器:=“(osgi.ee =未知)” - Liferay: can't deploy module. Unresolved requirement: Require-Capability: osgi.ee; filter:=“(osgi.ee=UNKNOWN)” 尽管有Import-Package,但org.osgi.framework.BundleActivator的ClassNotFoundException - ClassNotFoundException for org.osgi.framework.BundleActivator despite of Import-Package OSGI捆绑包.bnd文件和冲突的导入包语句 - OSGI bundle .bnd file and conflicting import-package statements 从OSGI命令提示符运行OSGI捆绑包:导入包缺少约束 - Running OSGI bundle from OSGI command prompt :Import-package missing constraint 在Tomcat上为Liferay部署OSGi模块时,“未解决的要求:导入包:javax.ws.rs” - “Unresolved requirement: Import-Package: javax.ws.rs” when deploying OSGi module for Liferay on Tomcat osgi中bootdelegation和DynamicImport-Package有什么区别 - What is the difference between bootdelegation and DynamicImport-Package in osgi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM