Add "static-reference" blueprint extension 21/40421/5
authorTom Pantelis <tpanteli@brocade.com>
Wed, 22 Jun 2016 17:11:50 +0000 (13:11 -0400)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 23 Jun 2016 07:19:46 +0000 (03:19 -0400)
Added a blueprint extension, "static-reference", that obtains an OSGi
service and returns the actual instance. This differs from the standard
"reference" element that returns a dynamic proxy whose underlying
service instance can come and go. This is useful especially in cases
where the service exists for the life of the karaf container and you
don't need/want the overhead of the proxy.

Change-Id: I4cbcc7e2b5a85b0a22e50e12f3946d29bfb36c7d
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/AbstractDependentComponentFactoryMetadata.java
opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/OpendaylightNamespaceHandler.java
opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/StaticReferenceMetadata.java [new file with mode: 0644]
opendaylight/blueprint/src/main/resources/opendaylight-blueprint-ext-1.0.0.xsd

index a84b8b68ea5559d10ea35c302c94ca3c91d13b8e..8001c3e4d63461d47e10a5f5619c6e57f6b004fd 100644 (file)
@@ -96,8 +96,12 @@ abstract class AbstractDependentComponentFactoryMetadata implements DependentCom
     }
 
     protected void retrieveService(String name, Class<?> interfaceClass, Consumer<Object> onServiceRetrieved) {
     }
 
     protected void retrieveService(String name, Class<?> interfaceClass, Consumer<Object> onServiceRetrieved) {
+        retrieveService(name, interfaceClass.getName(), onServiceRetrieved);
+    }
+
+    protected void retrieveService(String name, String interfaceName, Consumer<Object> onServiceRetrieved) {
         StaticServiceReferenceRecipe recipe = new StaticServiceReferenceRecipe(getId() + "-" + name,
         StaticServiceReferenceRecipe recipe = new StaticServiceReferenceRecipe(getId() + "-" + name,
-                container, interfaceClass.getName());
+                container, interfaceName);
         setDependendencyDesc(recipe.getOsgiFilter());
         serviceRecipes.add(recipe);
 
         setDependendencyDesc(recipe.getOsgiFilter());
         serviceRecipes.add(recipe);
 
index ab9e216d036d52628fcaa10d7ef257e41616a518..14ca14b69dcb35de9a251ad89c55b693b59a9b16 100644 (file)
@@ -65,6 +65,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
     private static final String ID_ATTR = "id";
     private static final String RPC_SERVICE = "rpc-service";
     private static final String SPECIFIC_SERVICE_REF_LIST = "specific-reference-list";
     private static final String ID_ATTR = "id";
     private static final String RPC_SERVICE = "rpc-service";
     private static final String SPECIFIC_SERVICE_REF_LIST = "specific-reference-list";
+    private static final String STATIC_REFERENCE = "static-reference";
 
     @SuppressWarnings("rawtypes")
     @Override
 
     @SuppressWarnings("rawtypes")
     @Override
@@ -99,6 +100,8 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
             return parseClusteredAppConfig(element, context);
         } else if (nodeNameEquals(element, SPECIFIC_SERVICE_REF_LIST)) {
             return parseSpecificReferenceList(element, context);
             return parseClusteredAppConfig(element, context);
         } else if (nodeNameEquals(element, SPECIFIC_SERVICE_REF_LIST)) {
             return parseSpecificReferenceList(element, context);
+        } else if (nodeNameEquals(element, STATIC_REFERENCE)) {
+            return parseStaticReference(element, context);
         }
 
         throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
         }
 
         throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
@@ -358,6 +361,15 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
         return metadata;
     }
 
         return metadata;
     }
 
+    private Metadata parseStaticReference(Element element, ParserContext context) {
+        ComponentFactoryMetadata metadata = new StaticReferenceMetadata(getId(context, element),
+                element.getAttribute(INTERFACE));
+
+        LOG.debug("parseStaticReference returning {}", metadata);
+
+        return metadata;
+    }
+
     private Element parseXML(String name, String xml) {
         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
         builderFactory.setNamespaceAware(true);
     private Element parseXML(String name, String xml) {
         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
         builderFactory.setNamespaceAware(true);
diff --git a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/StaticReferenceMetadata.java b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/StaticReferenceMetadata.java
new file mode 100644 (file)
index 0000000..693f518
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.blueprint.ext;
+
+import org.osgi.service.blueprint.container.ComponentDefinitionException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Factory metadata corresponding to the "static-reference" element that obtains an OSGi service and
+ * returns the actual instance. This differs from the standard "reference" element that returns a dynamic
+ * proxy whose underlying service instance can come and go.
+ *
+ * @author Thomas Pantelis
+ */
+class StaticReferenceMetadata extends AbstractDependentComponentFactoryMetadata {
+    private static final Logger LOG = LoggerFactory.getLogger(StaticReferenceMetadata.class);
+
+    private final String interfaceName;
+    private volatile Object retrievedService;
+
+    StaticReferenceMetadata(String id, String interfaceName) {
+        super(id);
+        this.interfaceName = interfaceName;
+    }
+
+    @Override
+    protected void startTracking() {
+        retrieveService(interfaceName, interfaceName, service -> {
+            retrievedService = service;
+            setSatisfied();
+        });
+    }
+
+    @Override
+    public Object create() throws ComponentDefinitionException {
+        super.onCreate();
+
+        LOG.debug("{}: create returning service {}", logName(), retrievedService);
+
+        return retrievedService;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder();
+        builder.append("StaticReferenceMetadata [interfaceName=").append(interfaceName).append("]");
+        return builder.toString();
+    }
+}
index 2a2189d615212b0e3a324cc471606e6e41df46cd..b3a7646a37416483e778ba506b4dba58fde23e81 100644 (file)
     <xsd:attribute name="id" type="xsd:ID"/>
   </xsd:complexType>
   <xsd:element name="specific-reference-list" type="TspecificReferenceList"/>
     <xsd:attribute name="id" type="xsd:ID"/>
   </xsd:complexType>
   <xsd:element name="specific-reference-list" type="TspecificReferenceList"/>
+
+  <xsd:complexType name="TstaticReference">
+    <xsd:attribute name="interface" type="bp:Tclass" use="required"/>
+    <xsd:attribute name="id" type="xsd:ID"/>
+  </xsd:complexType>
+  <xsd:element name="static-reference" type="TstaticReference"/>
 </xsd:schema>
\ No newline at end of file
 </xsd:schema>
\ No newline at end of file