简体   繁体   English

OSGi包的包结构

[英]OSGi bundle's package structure

I've been thinking some about "good practice" regarding package structure within osgi bundles. 我一直在思考一些关于osgi包中的包结构的“良好实践”。 Currently, on average, we have like 8-12 classes per bundle. 目前,平均而言,我们每捆有8-12个班级。 One of my initiative/proposal has been to have two packages; 我的主动/建议之一是有两个包; com.company_name.osgi.services.api (for api-related classes/interfaces (which is exported externally) and one package com.company_name.osgi.services.impl for implementation (not exported)). com.company_name.osgi.services.api(用于与api相关的类/接口(外部导出)和一个用于实现的包com.company_name.osgi.services.impl(未导出))。 What are the pros cons of this? 这有什么优点? Any other suggestions? 还有其他建议吗?

You might also consider puting the interfaces in com.company_name.subsystem , and the implementation in com.company_name.subsystem.impl , the OSGI specific code, if there is any, could be in com.company_name.subsystem.osgi . 您还可以考虑将接口放在com.company_name.subsystem ,并将实现放在com.company_name.subsystem.impl ,OSGI特定代码(如果有的话)可以在com.company_name.subsystem.osgi Sometime you might have multiple implementation of the same interfaces. 有时你可能有多个相同接口的实现。 In this case you could consider - com.company_name.subsystem.impl1 and com.company_name.subsystem.impl2 , for example: 在这种情况下,您可以考虑 - com.company_name.subsystem.impl1com.company_name.subsystem.impl2 ,例如:

com.company.scm        // the scm api
com.company.scm.git    // its git implementaton
com.company.scm.svn    // its subversion implementation
com.company.scm.osgi   // the place to put an OSGI Activator

In this sense package structure could be OSGi agnostic, if you later on move to a different container, you just put an additional 从这个意义上说,包结构可能是OSGi不可知的,如果你以后转移到另一个容器,你只需要另外一个

com.company.scm.sca     // or whatever component model you might think of

Always having api and impl in your package name could be annoying. 总是在你的包名中加上apiimpl可能很烦人。 If in doubt use impl but not api . 如果有疑问,请使用impl但不使用api

It's not the number of classes that is important but the concepts. 这不是重要的课程数量,而是概念。 In my opinion you should have one conceptual entity in a bundle. 在我看来,你应该在一个包中有一个概念实体。 In some cases this might be just a few classes in other several packages with 100s of classes. 在某些情况下,这可能只是其他几个包含100个类的几个类。

What it important is that you separate the API and the implementation. 重要的是您将API和实现分开。 One bundle contains the API of your concept and the other the implementation. 一个包包含您的概念的API,另一个包含实现。 Like this you can provide different implementations for a well defined API. 像这样,您可以为定义良好的API提供不同的实现。 In some cases this might be even necessary if you want to access the services from a bundle remotely (using eg R-OGSi) 在某些情况下,如果您想远程访问捆绑服务(例如使用R-OGSi),甚至可能需要这样做

The API bundles are then used by code sharing and the services from the implementation bundles by service sharing. 然后,通过服务共享,代码共享和来自实现包的服务使用API​​包。 Best way to explore those possibilities is to look at the ServiceTrackers. 探索这些可能性的最佳方式是查看ServiceTrackers。

In your case you could have the implementation in the same package, but all of its classes "package protected". 在您的情况下,您可以在同一个包中实现,但它的所有类都“包受保护”。 This way, you can export the package and the implementation would not be visible to the outside, even when not using OSGi (but as a regular jar file). 这样,您可以导出包,并且即使不使用OSGi(但作为常规jar文件),外部也不会看到实现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM