FELIX-2828: Allows a ResourceDependency annotation to inject the resource on a class field
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1067937 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java
index 274eb87..a2e7720 100644
--- a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java
@@ -648,8 +648,13 @@
Verifier.verifyFilter(filter, 0);
writer.put(EntryParam.filter, filter);
}
+
+ if (m_isField)
+ {
+ writer.put(EntryParam.autoConfig, m_field);
+ }
- writer.putString(annotation, EntryParam.added, m_method);
+ writer.putString(annotation, EntryParam.added, (!m_isField) ? m_method : null);
writer.putString(annotation, EntryParam.changed, null);
writer.putString(annotation, EntryParam.removed, null);
writer.putString(annotation, EntryParam.required, null);
diff --git a/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/DependencyBuilder.java b/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/DependencyBuilder.java
index 993500f..d7b413b 100644
--- a/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/DependencyBuilder.java
+++ b/dependencymanager/runtime/src/main/java/org/apache/felix/dm/runtime/DependencyBuilder.java
@@ -224,13 +224,16 @@
String filter = m_metaData.getString(Params.filter, null);
boolean required = "true".equals(m_metaData.getString(Params.required, "true"));
boolean propagate = "true".equals(m_metaData.getString(Params.propagate, "false"));
+ String autoConfigField = m_metaData.getString(Params.autoConfig, null);
- Dependency dp = createResourceDependency(dm, added, changed, removed, required, filter, propagate, instanceBound);
+ Dependency dp = createResourceDependency(dm, added, changed, removed, required, filter,
+ propagate, autoConfigField, instanceBound);
return dp;
}
private Dependency createResourceDependency(DependencyManager dm, String added,
- String changed, String removed, boolean required, String filter, boolean propagate, boolean instanceBound)
+ String changed, String removed, boolean required, String filter, boolean propagate,
+ String autoConfigField, boolean instanceBound)
{
ResourceDependency rd = dm.createResourceDependency();
rd.setCallbacks(added, changed, removed);
@@ -239,6 +242,10 @@
{
rd.setFilter(filter);
}
+ if (autoConfigField != null)
+ {
+ rd.setAutoConfig(autoConfigField);
+ }
rd.setPropagate(propagate);
rd.setInstanceBound(instanceBound);
return rd;
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumerField.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumerField.java
new file mode 100644
index 0000000..04b27ab
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/resource/ResourceConsumerField.java
@@ -0,0 +1,34 @@
+package org.apache.felix.dm.test.bundle.annotation.resource;
+
+import java.net.URL;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.ResourceDependency;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+/**
+ * A Component which as a resource dependency, using a class field.
+ */
+@Component
+public class ResourceConsumerField
+{
+ @ServiceDependency(required=true,filter = "(test=resourceField)")
+ Sequencer m_sequencer;
+
+ @ResourceDependency(filter = "(&(path=*/test1.txt)(host=localhost))")
+ URL m_resource;
+
+ @Init
+ void init()
+ {
+ if (m_resource != null)
+ {
+ Assert.assertTrue("file://localhost/path/to/test1.txt".equals(m_resource.toString()));
+ m_sequencer.step(1);
+ }
+ }
+}
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java
index 6d30931..3a34ae5 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ResourceAnnotationTest.java
@@ -76,6 +76,19 @@
}
/**
+ * Tests a simple ResourceConsumer using a class field for resource injection
+ */
+ @Test
+ public void testResourceAnnotationAutoConfig(BundleContext context)
+ {
+ DependencyManager m = new DependencyManager(context);
+ Properties props = new Properties() {{ put("test", "resourceField"); }};
+ m.add(m.createComponent().setImplementation(this).setInterface(Sequencer.class.getName(), props));
+ super.stopBundle("ResourceTest", context);
+ m_ensure.waitForStep(1, 10000);
+ }
+
+ /**
* Tests a ResourceAdapter
* @param context
*/