aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1461632 - in /aries/trunk/blueprint/blueprint-noosgi/src: main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java test/java/org.apache.aries.blueprint/BlueprintContainerTest.java test/resources/test2.xml
Date Wed, 27 Mar 2013 15:07:20 GMT
Author: gnodet
Date: Wed Mar 27 15:07:20 2013
New Revision: 1461632

URL: http://svn.apache.org/r1461632
Log:
[ARIES-1038] Improve placeholders to support multiple compatible placeholders

Added:
    aries/trunk/blueprint/blueprint-noosgi/src/test/resources/test2.xml
Modified:
    aries/trunk/blueprint/blueprint-noosgi/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java
    aries/trunk/blueprint/blueprint-noosgi/src/test/java/org.apache.aries.blueprint/BlueprintContainerTest.java

Modified: aries/trunk/blueprint/blueprint-noosgi/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-noosgi/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java?rev=1461632&r1=1461631&r2=1461632&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-noosgi/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java
(original)
+++ aries/trunk/blueprint/blueprint-noosgi/src/main/java/org/apache/aries/blueprint/ext/impl/ExtNamespaceHandler.java
Wed Mar 27 15:07:20 2013
@@ -18,8 +18,11 @@
  */
 package org.apache.aries.blueprint.ext.impl;
 
+import org.apache.aries.blueprint.ComponentDefinitionRegistry;
+import org.apache.aries.blueprint.ExtendedBeanMetadata;
 import org.apache.aries.blueprint.ExtendedReferenceListMetadata;
 import org.apache.aries.blueprint.ParserContext;
+import org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder;
 import org.apache.aries.blueprint.ext.PlaceholdersUtils;
 import org.apache.aries.blueprint.ext.PropertyPlaceholder;
 import org.apache.aries.blueprint.ext.evaluator.PropertyEvaluator;
@@ -258,11 +261,92 @@ public class ExtNamespaceHandler impleme
             metadata.addProperty("locations", createList(context, locations));
         }
 
-        PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
+        boolean result = validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
 
-        return metadata;
+        return result ? metadata : null;
     }
 
+    private boolean validatePlaceholder(MutableBeanMetadata metadata, ComponentDefinitionRegistry
registry) {
+        for (String id : registry.getComponentDefinitionNames()) {
+            ComponentMetadata component = registry.getComponentDefinition(id);
+            if (component instanceof ExtendedBeanMetadata) {
+                ExtendedBeanMetadata bean = (ExtendedBeanMetadata) component;
+                if (bean.getRuntimeClass() != null && AbstractPropertyPlaceholder.class.isAssignableFrom(bean.getRuntimeClass()))
{
+                    if (arePropertiesEquals(bean, metadata, "placeholderPrefix")
+                            && arePropertiesEquals(bean, metadata, "placeholderSuffix"))
{
+                        if (!arePropertiesEquals(bean, metadata, "systemProperties")
+                                || !arePropertiesEquals(bean, metadata, "ignoreMissingLocations"))
{
+                            throw new ComponentDefinitionException("Multiple incompatible
placeholders found");
+                        }
+                        // Merge both placeholders
+                        mergeList(bean, metadata, "locations");
+                        mergeMap(bean, metadata, "defaultProperties");
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
+    private void mergeList(ExtendedBeanMetadata bean1, MutableBeanMetadata bean2, String
name) {
+        Metadata m1 = getProperty(bean1, name);
+        Metadata m2 = getProperty(bean2, name);
+        if (m1 == null && m2 != null) {
+            ((MutableBeanMetadata) bean1).addProperty(name, m2);
+        } else if (m1 != null && m2 != null) {
+            if (!(m1 instanceof MutableCollectionMetadata) || !(m2 instanceof MutableCollectionMetadata))
{
+                throw new ComponentDefinitionException("Unable to merge " + name + " list
properties");
+            }
+            MutableCollectionMetadata c1 = (MutableCollectionMetadata) m1;
+            MutableCollectionMetadata c2 = (MutableCollectionMetadata) m2;
+            for (Metadata v : c2.getValues()) {
+                c1.addValue(v);
+            }
+        }
+    }
+
+    private void mergeMap(ExtendedBeanMetadata bean1, MutableBeanMetadata bean2, String name)
{
+        Metadata m1 = getProperty(bean1, name);
+        Metadata m2 = getProperty(bean2, name);
+        if (m1 == null && m2 != null) {
+            ((MutableBeanMetadata) bean1).addProperty(name, m2);
+        } else if (m1 != null && m2 != null) {
+            if (!(m1 instanceof MutableMapMetadata) || !(m2 instanceof MutableMapMetadata))
{
+                throw new ComponentDefinitionException("Unable to merge " + name + " list
properties");
+            }
+            MutableMapMetadata c1 = (MutableMapMetadata) m1;
+            MutableMapMetadata c2 = (MutableMapMetadata) m2;
+            for (MapEntry e : c2.getEntries()) {
+                c1.addEntry(e);
+            }
+        }
+    }
+
+    private boolean arePropertiesEquals(BeanMetadata bean1, BeanMetadata bean2, String name)
{
+        String v1 = getPlaceholderProperty(bean1, name);
+        String v2 = getPlaceholderProperty(bean2, name);
+        return v1 == null ? v2 == null : v1.equals(v2);
+    }
+
+    private String getPlaceholderProperty(BeanMetadata bean, String name) {
+        Metadata metadata = getProperty(bean, name);
+        if (metadata instanceof ValueMetadata) {
+            return ((ValueMetadata) metadata).getStringValue();
+        }
+        return null;
+    }
+
+    private Metadata getProperty(BeanMetadata bean, String name) {
+        for (BeanProperty property : bean.getProperties()) {
+            if (name.equals(property.getName())) {
+                return property.getValue();
+            }
+        }
+        return null;
+    }
+
+
     private Metadata parseDefaultProperties(ParserContext context, MutableBeanMetadata enclosingComponent,
Element element) {
         MutableMapMetadata props = context.createMetadata(MutableMapMetadata.class);
         NodeList nl = element.getChildNodes();

Modified: aries/trunk/blueprint/blueprint-noosgi/src/test/java/org.apache.aries.blueprint/BlueprintContainerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-noosgi/src/test/java/org.apache.aries.blueprint/BlueprintContainerTest.java?rev=1461632&r1=1461631&r2=1461632&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-noosgi/src/test/java/org.apache.aries.blueprint/BlueprintContainerTest.java
(original)
+++ aries/trunk/blueprint/blueprint-noosgi/src/test/java/org.apache.aries.blueprint/BlueprintContainerTest.java
Wed Mar 27 15:07:20 2013
@@ -43,6 +43,21 @@ public class BlueprintContainerTest {
         container.destroy();
     }
 
+    @Test
+    public void testPlaceholders() throws Exception {
+        URL url1 = getClass().getClassLoader().getResource("test.xml");
+        URL url2 = getClass().getClassLoader().getResource("test2.xml");
+        BlueprintContainerImpl container = new BlueprintContainerImpl(getClass().getClassLoader(),
Arrays.asList(url1, url2));
+
+        Foo foo = (Foo) container.getComponentInstance("foo");
+        System.out.println(foo);
+        assertNotNull(foo);
+        assertEquals(5, foo.getA());
+        assertEquals(1, foo.getB());
+
+        container.destroy();
+    }
+
     public static void main(String[] args) throws Exception {
         URL url = BlueprintContainerTest.class.getClassLoader().getResource("test.xml");
         BlueprintContainerImpl container = new BlueprintContainerImpl(BlueprintContainerTest.class.getClassLoader(),
Arrays.asList(url));

Added: aries/trunk/blueprint/blueprint-noosgi/src/test/resources/test2.xml
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-noosgi/src/test/resources/test2.xml?rev=1461632&view=auto
==============================================================================
--- aries/trunk/blueprint/blueprint-noosgi/src/test/resources/test2.xml (added)
+++ aries/trunk/blueprint/blueprint-noosgi/src/test/resources/test2.xml Wed Mar 27 15:07:20
2013
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements. See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version
+    2.0 (the "License"); you may not use this file except in compliance
+    with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+    applicable law or agreed to in writing, software distributed under the
+    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+    CONDITIONS OF ANY KIND, either express or implied. See the License for
+    the specific language governing permissions and limitations under the
+    License.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0"
+           default-availability="optional">
+
+    <ext:property-placeholder placeholder-prefix="$["
+                              placeholder-suffix="]" ignore-missing-locations="true"
+                              system-properties="override">
+        <ext:default-properties>
+            <ext:property name="c" value="FooBar" />
+        </ext:default-properties>
+    </ext:property-placeholder>
+
+</blueprint>
+



Mime
View raw message