java - 如何在 JBoss Fuse 蓝图中使用多个属性占位符?

标签 java osgi osgi-bundle jbossfuse blueprint-osgi

我正在尝试使用 JBoss Fuse (6.3) 蓝图etc 目录中的多个属性文件。我的第一个方法如下:

<cm:property-placeholder persistent-id="foo" />
<cm:property-placeholder persistent-id="bar" />

我认为这会起作用,正如 Red Hat's documentation 中所述。 :

Using multiple property placeholders in Blueprint

It is legal to define multiple property placeholders in a Blueprint XML file (that is, defining multiple cm:property-placeholder elements that reference different persistent IDs). One thing that you need to be aware of, however, is that there can be a clash if two properties from different property placeholders have the same name. In this case, the following rules determine which property takes precedence:

  1. Explicitly defined property settings (for example, defined in a etc/PersistentID.cfg file) take precedence over default property settings (defined in a cm:default-properties element).

  2. If there is more than one explicit setting for a given property, the setting from the last property placeholder in the Blueprint file takes precedence.

  3. Default property settings (defined in a cm:default-properties element) have the lowest priority.

但是后来我得到了错误:不允许使用具有相同前缀和后缀的多个占位符:

2018-05-15 17:43:59,065 | ERROR | /s42http1/deploy | BlueprintContainerImpl           | 22 - org.apache.aries.blueprint.core - 1.8.0 | Unable to start blueprint container for bundle myBlueprint/0.0.1.SNAPSHOT
org.osgi.service.blueprint.container.ComponentDefinitionException: Multiple placeholders with the same prefix and suffix are not allowed
        at org.apache.aries.blueprint.ext.PlaceholdersUtils.validatePlaceholder(PlaceholdersUtils.java:49)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.compendium.cm.CmNamespaceHandler.parsePropertyPlaceholder(CmNamespaceHandler.java:318)[20:org.apache.aries.blueprint.cm:1.1.0]
        at org.apache.aries.blueprint.compendium.cm.CmNamespaceHandler.parse(CmNamespaceHandler.java:199)[20:org.apache.aries.blueprint.cm:1.1.0]
        at org.apache.aries.blueprint.parser.Parser.parseCustomElement(Parser.java:1369)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.parser.Parser.loadComponents(Parser.java:427)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.parser.Parser.populate(Parser.java:331)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:350)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:277)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:300)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:269)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:265)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.blueprint.container.BlueprintExtender.modifiedBundle(BlueprintExtender.java:255)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:500)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:433)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$AbstractTracked.track(BundleHookBundleTracker.java:725)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.bundleChanged(BundleHookBundleTracker.java:463)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$BundleEventHook.event(BundleHookBundleTracker.java:422)[22:org.apache.aries.blueprint.core:1.8.0]
        at org.apache.felix.framework.util.SecureAction.invokeBundleEventHook(SecureAction.java:1127)[org.apache.felix.framework-4.4.1.jar:]
        at org.apache.felix.framework.util.EventDispatcher.createWhitelistFromHooks(EventDispatcher.java:696)[org.apache.felix.framework-4.4.1.jar:]
        at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:484)[org.apache.felix.framework-4.4.1.jar:]
        at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4429)[org.apache.felix.framework-4.4.1.jar:]
        at org.apache.felix.framework.Felix.startBundle(Felix.java:2100)[org.apache.felix.framework-4.4.1.jar:]
        at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:976)[org.apache.felix.framework-4.4.1.jar:]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1260)[10:org.apache.felix.fileinstall:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1233)[10:org.apache.felix.fileinstall:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startAllBundles(DirectoryWatcher.java:1221)[10:org.apache.felix.fileinstall:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:515)[10:org.apache.felix.fileinstall:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:365)[10:org.apache.felix.fileinstall:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:316)[10:org.apache.felix.fileinstall:3.6.4]

所以我设置了 placeholder-prefixplaceholder-suffix:

<cm:property-placeholder persistent-id="foo" />
<cm:property-placeholder persistent-id="bar" 
    placeholder-prefix="$(" placeholder-suffix=")"/>

这样,我应该能够使用 $ { } 访问 etc/foo.cfg 并使用 $ ( ) 访问 etc/bar.cfg 属性,对吗?

否:

Caused by: java.io.IOException: Could not open $(bar_property) as a file, class path resource, or URL.
        at org.apache.camel.util.jsse.JsseParameters.resolveResource(JsseParameters.java:182)[199:org.apache.camel.camel-core:2.17.0.redhat-630329]
        at org.apache.camel.util.jsse.KeyStoreParameters.createKeyStore(KeyStoreParameters.java:174)[199:org.apache.camel.camel-core:2.17.0.redhat-630329]
        at org.apache.camel.util.jsse.KeyManagersParameters.createKeyManagers(KeyManagersParameters.java:108)[199:org.apache.camel.camel-core:2.17.0.redhat-630329]
        at org.apache.camel.util.jsse.SSLContextParameters.createSSLContext(SSLContextParameters.java:256)[199:org.apache.camel.camel-core:2.17.0.redhat-630329]
        at org.apache.camel.component.http4.HttpComponent.createConnectionRegistry(HttpComponent.java:285)[296:org.apache.camel.camel-http4:2.17.0.redhat-630329]
        at org.apache.camel.component.http4.HttpComponent.createEndpoint(HttpComponent.java:227)[296:org.apache.camel.camel-http4:2.17.0.redhat-630329]
        at org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:114)[199:org.apache.camel.camel-core:2.17.0.redhat-630329]
        at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:569)[199:org.apache.camel.camel-core:2.17.0.redhat-630329]
        ... 36 more

我已经检查 bar_property 是否存在于 etc/bar.cfg 中。

那么如何在 JBoss Fuse 蓝图中使用多个属性占位符?我正在使用 JBoss Fuse 6.3.0.329

最佳答案

当我输入该问题时,我在 RedHat documentation 的引用部分正上方找到了答案。 ( Rubber duck debugging ;-) :

The syntax, {{prefix}}, substitutes the value of the prefix variable into the blueprint XML file. The OSGi properties are set up using the following XML elements:

cm:property-placeholder
This element gives you access to the properties associated with the specified persistent ID. After defining this element, you can use the syntax, {{PropName}}, to substitute variables belonging to the specified persistent ID.

cm:property-placeholder/cm:default-properties
You can optionally specify default values for properties by defining cm:property elements inside the cm:default-properties element. If the corresponding etc/PersistentID.cfg file defines property values, however, these will be used instead.

因此属性占位符的定义是正确的,如下所示:

<cm:property-placeholder persistent-id="foo" />
<cm:property-placeholder persistent-id="bar" 
    placeholder-prefix="$(" placeholder-suffix=")"/>

但在我的示例中,该属性必须与 {{bar_property}} 一起使用,而不是 $(bar_property)

关于java - 如何在 JBoss Fuse 蓝图中使用多个属性占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354932/

相关文章:

java - 为什么 Maven 覆盖范围不能被传递地识别?

java - 如何一次安装并启动 osgi bundle ,其中一个依赖于其他 bundle

java - 如何在 OSGI 中使用嵌入 .so 文件的 jar

java - 使用 java swing 的桌面应用程序的兼容性问题

java - 谷歌云端点生成 Java api HTTP 400 错误

java - 有没有办法在 Java 中安排完整的垃圾收集?

java - 为什么运行 Java 测试时 BundleContext 为 null?

osgi - 如果我必须手动在其中安装 bundle ,features.xml 有什么意义?

java - 无法在 OSGI 中从另一个包导入一个包的包

java - 戈戈 : BundleException: An error occurred trying to read the bundle in OSGi with Equinox