如何在JBoss Fuse Blueprint中使用多个property-placeholder?

时间:2018-05-15 16:18:32

标签: 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中所述:

  

在蓝图中使用多个属性占位符

     

在Blueprint XML文件中定义多个属性占位符是合法的(即,定义引用不同持久性ID的多个cm:property-placeholder元素)。但是,您需要注意的一件事是,如果来自不同属性占位符的两个属性具有相同的名称,则可能存在冲突。在这种情况下,以下规则确定哪个属性优先:

     
      
  1. 明确定义的属性设置(例如,在etc/PersistentID.cfg文件中定义)优先于默认属性设置(在cm:default-properties元素中定义)。

  2.   
  3. 如果给定属性有多个显式设置,则Blueprint文件中最后一个属性占位符的设置优先。

  4.   
  5. 默认属性设置(在cm:default-properties元素中定义)的优先级最低。

  6.   

但后来我收到Multiple placeholders with the same prefix and suffix are not allowed

的错误
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 Blueprints中使用多个property-placeholder?我正在使用 JBoss Fuse 6.3.0.329

1 个答案:

答案 0 :(得分:1)

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

  

语法{{prefix}}prefix变量的值替换为蓝图XML文件。 OSGi属性使用以下XML元素设置:

     

<强> cm:property-placeholder
      此元素使您可以访问与指定持久ID关联的属性。定义此元素后,可以使用语法{{PropName}}替换属于指定持久ID的变量。

     

<强> cm:property-placeholder/cm:default-properties
      您可以通过在cm:property元素中定义cm:default-properties元素来指定属性的默认值。但是,如果相应的etc/PersistentID.cfg文件定义了属性值,则将使用这些值。

所以property-placeholder的定义是正确的如下:

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

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