Author: gnodet
Date: Mon Jul 23 23:06:34 2012
New Revision: 1364832

URL: http://svn.apache.org/viewvc?rev=1364832&view=rev
Log:
[ARIES-315] Make Blueprint-cm support top-level  cm-properties element

Added:
    
aries/trunk/blueprint/blueprint-cm/src/main/resources/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd
    
aries/trunk/blueprint/blueprint-cm/src/test/java/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.java
    
aries/trunk/blueprint/blueprint-cm/src/test/resources/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.xml
Modified:
    
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmNamespaceHandler.java
    
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmProperties.java
    
aries/trunk/blueprint/blueprint-cm/src/main/resources/OSGI-INF/blueprint/blueprint-cm.xml

Modified: 
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmNamespaceHandler.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmNamespaceHandler.java?rev=1364832&r1=1364831&r2=1364832&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmNamespaceHandler.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmNamespaceHandler.java
 Mon Jul 23 23:06:34 2012
@@ -80,6 +80,7 @@ public class CmNamespaceHandler implemen
     public static final String BLUEPRINT_NAMESPACE = 
"http://www.osgi.org/xmlns/blueprint/v1.0.0";;
     public static final String BLUEPRINT_CM_NAMESPACE_1_0 = 
"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0";;
     public static final String BLUEPRINT_CM_NAMESPACE_1_1 = 
"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0";;
+    public static final String BLUEPRINT_CM_NAMESPACE_1_2 = 
"http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0";;
     public static final String BLUEPRINT_EXT_NAMESPACE_V1_0 = 
"http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0";;
     public static final String BLUEPRINT_EXT_NAMESPACE_V1_1 = 
"http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0";;
     public static final String BLUEPRINT_EXT_NAMESPACE_V1_2 = 
"http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0";;
@@ -147,7 +148,9 @@ public class CmNamespaceHandler implemen
     }
 
     public URL getSchemaLocation(String namespace) {
-        if (BLUEPRINT_CM_NAMESPACE_1_1.equals(namespace)) {
+        if (BLUEPRINT_CM_NAMESPACE_1_2.equals(namespace)) {
+            return getClass().getResource("blueprint-cm-1.2.0.xsd");
+        } else if (BLUEPRINT_CM_NAMESPACE_1_1.equals(namespace)) {
             return getClass().getResource("blueprint-cm-1.1.0.xsd");
         } else if (BLUEPRINT_CM_NAMESPACE_1_0.equals(namespace)) {
             return getClass().getResource("blueprint-cm-1.0.0.xsd");
@@ -173,6 +176,8 @@ public class CmNamespaceHandler implemen
             return parsePropertyPlaceholder(context, element);
         } else if (nodeNameEquals(element, MANAGED_SERVICE_FACTORY_ELEMENT)) {
             return parseManagedServiceFactory(context, element);
+        } else if (nodeNameEquals(element, CM_PROPERTIES_ELEMENT)) {
+            return parseCmProperties(context, element);
         } else {
             throw new ComponentDefinitionException("Unsupported element: " + 
element.getNodeName());
         }
@@ -195,6 +200,35 @@ public class CmNamespaceHandler implemen
         }
     }
 
+    private ComponentMetadata parseCmProperties(ParserContext context, Element 
element) {
+        String id = getId(context, element);
+
+        MutableBeanMetadata factoryMetadata = 
context.createMetadata(MutableBeanMetadata.class);
+        generateIdIfNeeded(context, factoryMetadata);
+        factoryMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
+        factoryMetadata.setRuntimeClass(CmProperties.class);
+        factoryMetadata.setInitMethod("init");
+        factoryMetadata.setDestroyMethod("destroy");
+        factoryMetadata.addProperty("blueprintContainer", createRef(context, 
"blueprintContainer"));
+        factoryMetadata.addProperty("configAdmin", 
createConfigurationAdminRef(context));
+        factoryMetadata.addProperty("managedObjectManager", createRef(context, 
MANAGED_OBJECT_MANAGER_NAME));
+        String persistentId = element.getAttribute(PERSISTENT_ID_ATTRIBUTE);
+        factoryMetadata.addProperty("persistentId", createValue(context, 
persistentId));
+        
context.getComponentDefinitionRegistry().registerComponentDefinition(factoryMetadata);
+
+        MutableBeanMetadata propertiesMetadata = 
context.createMetadata(MutableBeanMetadata.class);
+        propertiesMetadata.setId(id);
+        propertiesMetadata.setScope(BeanMetadata.SCOPE_SINGLETON);
+        propertiesMetadata.setRuntimeClass(Properties.class);
+        propertiesMetadata.setFactoryComponent(createRef(context, 
factoryMetadata.getId()));
+        propertiesMetadata.setFactoryComponent(factoryMetadata);
+        propertiesMetadata.setFactoryMethod("getProperties");
+        // Work around ARIES-877
+        
propertiesMetadata.setDependsOn(Arrays.asList(factoryMetadata.getId()));
+
+        return propertiesMetadata;
+    }
+
     private ComponentMetadata parsePropertyPlaceholder(ParserContext context, 
Element element) {
         MutableBeanMetadata metadata = 
context.createMetadata(MutableBeanMetadata.class);
         metadata.setProcessor(true);
@@ -557,7 +591,8 @@ public class CmNamespaceHandler implemen
 
     public static boolean isCmNamespace(String uri) {
         return BLUEPRINT_CM_NAMESPACE_1_0.equals(uri)
-                || BLUEPRINT_CM_NAMESPACE_1_1.equals(uri);
+                || BLUEPRINT_CM_NAMESPACE_1_1.equals(uri)
+                || BLUEPRINT_CM_NAMESPACE_1_2.equals(uri);
     }
 
     public static boolean isExtNamespace(String uri) {

Modified: 
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmProperties.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmProperties.java?rev=1364832&r1=1364831&r2=1364832&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmProperties.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-cm/src/main/java/org/apache/aries/blueprint/compendium/cm/CmProperties.java
 Mon Jul 23 23:06:34 2012
@@ -49,7 +49,7 @@ public class CmProperties implements Man
 
     private final Object lock = new Object();
     private final Set<ServicePropertiesUpdater> services = new 
HashSet<ServicePropertiesUpdater>();
-    private Dictionary<String,Object> properties;
+    private final Properties properties = new Properties();
 
     public ExtendedBlueprintContainer getBlueprintContainer() {
         return blueprintContainer;
@@ -104,7 +104,11 @@ public class CmProperties implements Man
     }
     
     public void init() throws Exception {
-        LOGGER.debug("Initializing CmProperties for service={} / pid={}", 
serviceId, persistentId);
+        if (serviceId != null) {
+            LOGGER.debug("Initializing CmProperties for service={} / pid={}", 
serviceId, persistentId);
+        } else {
+            LOGGER.debug("Initializing CmProperties for pid={}", persistentId);
+        }
         
         Properties props = new Properties();
         props.put(Constants.SERVICE_PID, persistentId);
@@ -116,7 +120,8 @@ public class CmProperties implements Man
             managedObjectManager.register(this, props);
             Configuration config = CmUtils.getConfiguration(configAdmin, 
persistentId);
             if (config != null) {
-                properties = config.getProperties();
+                properties.clear();
+                JavaUtils.copy(properties, config.getProperties());
             }
         }
     }
@@ -125,11 +130,22 @@ public class CmProperties implements Man
         managedObjectManager.unregister(this);
     }
 
+    public Properties getProperties() {
+        return properties;
+    }
+
     public void updated(Dictionary props) {
-        LOGGER.debug("Service properties updated for service={} / pid={}, {}", 
new Object[] {serviceId, persistentId, props});
+        if (serviceId != null) {
+            LOGGER.debug("Service properties updated for service={} / pid={}, 
{}", new Object[] {serviceId, persistentId, props});
+        } else {
+            LOGGER.debug("Service properties updated for pid={}, {}", new 
Object[] {persistentId, props});
+        }
         
         synchronized (lock) {
-            this.properties = props;
+            properties.clear();
+            if (props != null) {
+                JavaUtils.copy(properties, props);
+            }
             if (update) {
                 for (ServicePropertiesUpdater service : services) {
                     service.updateProperties(props);
@@ -139,7 +155,7 @@ public class CmProperties implements Man
     }
 
     public void updateProperties(ServicePropertiesUpdater service, Dictionary 
props) {
-        if (!this.serviceId.equals(service.getId())) {
+        if (this.serviceId == null || !this.serviceId.equals(service.getId())) 
{
             return;
         }
                 

Modified: 
aries/trunk/blueprint/blueprint-cm/src/main/resources/OSGI-INF/blueprint/blueprint-cm.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-cm/src/main/resources/OSGI-INF/blueprint/blueprint-cm.xml?rev=1364832&r1=1364831&r2=1364832&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-cm/src/main/resources/OSGI-INF/blueprint/blueprint-cm.xml
 (original)
+++ 
aries/trunk/blueprint/blueprint-cm/src/main/resources/OSGI-INF/blueprint/blueprint-cm.xml
 Mon Jul 23 23:06:34 2012
@@ -25,6 +25,7 @@
                 <array>
                     
<value>http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0</value>
                     
<value>http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0</value>
+                    
<value>http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0</value>
                 </array>
             </entry>
         </service-properties>

Added: 
aries/trunk/blueprint/blueprint-cm/src/main/resources/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-cm/src/main/resources/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd?rev=1364832&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-cm/src/main/resources/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd
 (added)
+++ 
aries/trunk/blueprint/blueprint-cm/src/main/resources/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd
 Mon Jul 23 23:06:34 2012
@@ -0,0 +1,133 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!--
+    /*
+    * $Revision$
+    *
+    * Copyright (c) OSGi Alliance (2008, 2009). All Rights Reserved.
+    *
+    * Licensed 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.
+    */
+    -->
+<xsd:schema xmlns="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0";
+            xmlns:xsd="http://www.w3.org/2001/XMLSchema";
+            xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0";
+            
targetNamespace="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0";
+            elementFormDefault="qualified"
+            attributeFormDefault="unqualified"
+            version="1.0.0">
+
+    <xsd:import namespace="http://www.osgi.org/xmlns/blueprint/v1.0.0"; />
+
+    <!-- property placeholder -->
+
+    <xsd:element name="property-placeholder" type="TpropertyPlaceholder"/>
+
+    <xsd:complexType name="TpropertyPlaceholder">
+        <xsd:complexContent>
+            <xsd:extension base="bp:Tcomponent">
+                <xsd:sequence>
+                    <!-- nested properties declaration -->
+                    <xsd:element name="default-properties" 
type="TdefaultProperties" minOccurs="0" maxOccurs="1"/>
+                </xsd:sequence>
+
+                <!-- #### What should be the type for a persistent id?  I 
think we need to define one like class and method -->
+                <xsd:attribute name="persistent-id" type="xsd:string" 
use="required"/>
+                <xsd:attribute name="placeholder-prefix" type="xsd:string" 
use="optional" default="${"/>
+                <xsd:attribute name="placeholder-suffix" type="xsd:string" 
use="optional" default="}"/>
+                <xsd:attribute name="defaults-ref" type="bp:Tidref" 
use="optional"/>
+                <xsd:attribute name="update-strategy" 
type="TplaceholderUpdateStrategyType" use="optional" default="none"/>
+            </xsd:extension>
+        </xsd:complexContent>
+    </xsd:complexType>
+
+    <xsd:simpleType name="TplaceholderUpdateStrategyType">
+        <xsd:restriction base="xsd:NMTOKEN">
+            <xsd:enumeration value="none"/>
+            <xsd:enumeration value="reload"/>
+        </xsd:restriction>
+    </xsd:simpleType>
+
+    <!-- #### is this the correct type here?  This is defining placeholder 
properties,
+         so should this be a restricted set of value types or should this be 
expanded to
+         all of the elements you can inject into a bean property? -->
+    <xsd:complexType name="TdefaultProperties">
+        <xsd:sequence minOccurs="0" maxOccurs="unbounded">
+            <xsd:element name="property" type="bp:Tproperty"/>
+        </xsd:sequence>
+    </xsd:complexType>
+
+
+    <!--  managed-properties  -->
+
+    <xsd:element name="managed-properties" type="TmanagedProperties"/>
+
+    <xsd:complexType name="TmanagedProperties">
+        <xsd:attribute name="persistent-id" type="xsd:string" use="required"/>
+        <xsd:attribute name="update-strategy" type="TupdateStrategyType" 
use="optional"/>
+        <xsd:attribute name="update-method" type="xsd:string" use="optional"/>
+    </xsd:complexType>
+
+    <xsd:simpleType name="TupdateStrategyType">
+        <xsd:restriction base="xsd:NMTOKEN">
+            <xsd:enumeration value="none"/>
+            <xsd:enumeration value="component-managed"/>
+            <xsd:enumeration value="container-managed"/>
+        </xsd:restriction>
+    </xsd:simpleType>
+
+    <!--  managed-service-factory -->
+
+    <xsd:element name="managed-service-factory" type="TmanagedServiceFactory"/>
+
+    <xsd:complexType name="TmanagedServiceFactory">
+        <xsd:complexContent>
+            <xsd:extension base="bp:Tcomponent">
+                <xsd:sequence>
+                    <xsd:group ref="bp:GbaseServiceElements"/>
+                    <xsd:element name="managed-component" 
type="TmanagedComponent" minOccurs="1" maxOccurs="1"/>
+                </xsd:sequence>
+                <xsd:attribute name="interface" type="bp:Tclass" 
use="optional" />
+                <xsd:attribute name="ref" type="bp:Tidref" use="optional" />
+                <xsd:attribute name="auto-export" type="bp:TautoExportModes" 
default="disabled" />
+                <xsd:attribute name="ranking" type="xsd:int" default="0"/>
+                <xsd:attribute name="factory-pid" type="xsd:string" 
use="required"/>
+                <xsd:anyAttribute namespace="##other" 
processContents="strict"/>
+            </xsd:extension>
+        </xsd:complexContent>
+    </xsd:complexType>
+
+    <xsd:complexType name="TmanagedComponent">
+        <xsd:group ref="bp:GbeanElements"/>
+        <xsd:attribute name="class" type="bp:Tclass"/>
+        <xsd:attribute name="init-method" type="bp:Tmethod"/>
+        <xsd:attribute name="destroy-method" type="bp:Tmethod"/>
+        <xsd:attribute name="factory-method" type="bp:Tmethod"/>
+        <xsd:attribute name="factory-component" type="bp:Tidref"/>
+        <xsd:anyAttribute namespace="##other" processContents="strict"/>
+    </xsd:complexType>
+
+
+    <!-- cm-properties -->
+
+    <xsd:element name="cm-properties" type="TcmProperties"/>
+
+    <xsd:complexType name="TcmProperties">
+        <xsd:complexContent>
+            <xsd:extension base="bp:Tcomponent">
+                <xsd:attribute name="persistent-id" type="xsd:string" 
use="required"/>
+                <xsd:attribute name="update" type="xsd:boolean" use="optional" 
default="false"/>
+            </xsd:extension>
+        </xsd:complexContent>
+    </xsd:complexType>
+
+</xsd:schema>

Added: 
aries/trunk/blueprint/blueprint-cm/src/test/java/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-cm/src/test/java/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.java?rev=1364832&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-cm/src/test/java/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-cm/src/test/java/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.java
 Mon Jul 23 23:06:34 2012
@@ -0,0 +1,63 @@
+/**
+ * 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.
+ */
+package org.apache.aries.blueprint.compendium.cm;
+
+import java.util.Hashtable;
+
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class CmPropertiesTest extends BaseTest {
+
+    @Override
+    protected String getBlueprintDescriptor() {
+        return "org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.xml";
+    }
+
+    @Test
+    public void test4() throws Exception {
+        BundleContext context = getBundleContext();
+        ServiceReference sr = Helper.getOsgiServiceReference(context, 
FooInterface.class, "(key=foo4)", Helper.DEFAULT_TIMEOUT);
+        assertNotNull(sr);
+
+        FooInterface foo = (FooInterface) context.getService(sr);
+        assertNotNull(foo);
+        assertNotNull(foo.getProps());
+        assertTrue(foo.getProps().isEmpty());
+
+        ConfigurationAdmin ca = getOsgiService(ConfigurationAdmin.class);
+        Configuration cf = 
ca.getConfiguration("blueprint-sample-properties.pid", null);
+        Hashtable<String,String> props = new Hashtable<String,String>();
+        props.put("a", "5");
+        cf.update(props);
+
+        Thread.sleep(500);
+        assertFalse(foo.getProps().isEmpty());
+        assertEquals("5", foo.getProps().getProperty("a"));
+    }
+
+}

Added: 
aries/trunk/blueprint/blueprint-cm/src/test/resources/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-cm/src/test/resources/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.xml?rev=1364832&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-cm/src/test/resources/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.xml
 (added)
+++ 
aries/trunk/blueprint/blueprint-cm/src/test/resources/org/apache/aries/blueprint/compendium/cm/CmPropertiesTest.xml
 Mon Jul 23 23:06:34 2012
@@ -0,0 +1,34 @@
+<?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";
+           
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0";>
+
+    <cm:cm-properties id="myProps" 
persistent-id="blueprint-sample-properties.pid" />
+
+    <service id="myService" auto-export="interfaces">
+        <service-properties>
+            <entry key="key" value="foo4" />
+            <!--<cm:cm-properties persistent-id="org.foo.bar" />-->
+        </service-properties>
+        <bean class="org.apache.aries.blueprint.compendium.cm.Foo">
+            <property name="props" ref="myProps" />
+        </bean>
+    </service>
+
+
+</blueprint>


Reply via email to