简体   繁体   English

使用XML配置服务..(而不是属性)

[英]Configure a Service .. with XML ( instead of properties )

i have a ( bnd-annotated ) component that implements a simple api and exposes itself as a service 我有一个(带bnd注释的)组件,该组件实现一个简单的api并将其自身公开为服务

package com.mycompany.impl;
import com.mycompany.api.IFoo;

@Component(designateFactory=FooImpl.Configuration.class)
class FooImpl implements IFoo {

  interface Configuration {
    String foo();
    // ..
  }

  Configuration configuration;

  @Activate
  public void activate(Map properties) {
    configuration = Configurable.createConfigurable(Configuration.class, properties);
    // ..
  }

} 

its configuration is loaded from a watched directory by Felix FileInstall and the service is instantiated by the Felix Configuration Service ( at least, i assume thats whats happening - i'm new to OSGi, please bear with me ) This, with the generated MetaType descriptor is working great. 它的配置由Felix FileInstall从监视的目录加载,并且该服务由Felix Configuration Service实例化(至少,我假设发生了什么-我是OSGi的新手,请忍受我)这与生成的MetaType描述符很棒。

However, as it stands, FooImpl requires structured configuration ( lists of lists, maps of lists..etc ) and i was wondering if there is an elegant ( * ) way to configure instances of the component through a similar workflow; 但是,就目前而言,FooImpl需要结构化配置(列表列表,列表映射..etc),我想知道是否存在一种优雅的(*)方法来通过类似的工作流程配置组件实例; that is to say, configuration discovery and instantiation/deployment remains centralised. 也就是说,配置发现和实例化/部署仍然是集中的。

It seems to me that the Configuration Service spec manages maps - will i have to roll my own Configuration Service & FileInstall to be able to present components with xml/json/yaml backed structured configuration? 在我看来,Configuration Service规范管理地图-我是否必须滚动自己的Configuration Service和FileInstall才能使用xml / json / yaml支持的结构化配置来呈现组件?

  • as opposed to, say, defining the location of an xml configuration file in properties ...confiception ? 而不是在属性... confiception中定义xml配置文件的位置? and doing my own parsing. 并自己解析。

Yes and no... 是不是

The OSGi Configuration Admin service deals with abstract Configuration records, which are based on flat maps (actually java.util.Dictionary , but it's essentially the same thing). OSGi Configuration Admin服务处理基于平面映射的抽象Configuration记录(实际上是java.util.Dictionary ,但本质上是同一件事)。 Config Admin does not know anything about the underlying physical storage; 配置管理员知道底层的物理存储任何东西; it always relies on somebody else to call the methods on the ConfigurationAdmin service, ie getConfiguration , createFactoryConfiguration etc. 它总是依靠其他人来调用ConfigurationAdmin服务上的方法,即getConfigurationcreateFactoryConfiguration等。

The "somebody else" that calls Config Admin is usually called a "management agent". 称为Config Admin的“其他人”通常称为“管理代理”。 Felix FileInstall is a very simple example of a management agent that reads files in the Java properties format. Felix FileInstall是管理代理的一个非常简单的示例,它以Java属性格式读取文件。 Actually FileInstall is probably too simple and I don't consider it appropriate for production deployment — but that's a separate discussion. 实际上,FileInstall可能简单了,我认为它不适用于生产部署-但这是一个单独的讨论。

It sounds like you want to write your own management agent that reads XML files and feeds them into Config Admin. 听起来您想编写自己的管理代理来读取XML文件并将其馈入Config Admin。 This is really not a large or difficult task and you should not be afraid to take it on. 这确实不是一个大任务或艰巨的任务,您不应该害怕接下去。 Config Admin was designed under the assumption that applications would have very diverse requirements for configuration data storage, and that most applications would therefore have to write their own simple management agent, which is why it does not define its own storage format or location(s). Config Admin的设计基于以下假设:应用程序对配置数据存储的要求非常不同,因此大多数应用程序必须编写自己的简单管理代理,这就是为什么它不定义自己的存储格式或位置的原因。 。

However, once the configuration data has been read by your management agent, it must be passed into Config Admin as a map/dictionary, which in turn will pass it to the components as a map. 但是,一旦管理代理读取了配置数据,就必须将其作为地图/词典传递到Config Admin中,这又会将其作为地图传递到组件。 Therefore the components themselves do not receive highly structured data eg trees or nested maps. 因此,组件本身不会接收高度结构化的数据,例如树木或嵌套地图。 There is some flexibility though: configuration properties can contain lists of the based type; 但是有一些灵活性:配置属性可以包含基于类型的列表; you can also use enum values etc. 您还可以使用枚举值等。

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

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