Fix FELIX-4715
Support the instance bundle context injection for instances declared in @Configuration
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1642864 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/core-it/ipojo-core-context-injection-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/configuration/MyConfiguration.java b/ipojo/runtime/core-it/ipojo-core-context-injection-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/configuration/MyConfiguration.java
new file mode 100644
index 0000000..eb0e505
--- /dev/null
+++ b/ipojo/runtime/core-it/ipojo-core-context-injection-test/src/main/java/org/apache/felix/ipojo/runtime/core/components/configuration/MyConfiguration.java
@@ -0,0 +1,31 @@
+/*
+* 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.felix.ipojo.runtime.core.components.configuration;
+
+import org.apache.felix.ipojo.configuration.Configuration;
+import org.apache.felix.ipojo.configuration.Instance;
+import org.apache.felix.ipojo.runtime.core.components.annotations.InstanceBundleContextInjectionInField;
+
+import static org.apache.felix.ipojo.configuration.Instance.instance;
+
+@Configuration
+public class MyConfiguration {
+
+ Instance instance1 = instance().of(InstanceBundleContextInjectionInField.class).named("from.configuration");
+}
diff --git a/ipojo/runtime/core-it/ipojo-core-context-injection-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestContextInjectionFromAnnotations.java b/ipojo/runtime/core-it/ipojo-core-context-injection-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestContextInjectionFromAnnotations.java
index 82713e5..37461a8 100644
--- a/ipojo/runtime/core-it/ipojo-core-context-injection-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestContextInjectionFromAnnotations.java
+++ b/ipojo/runtime/core-it/ipojo-core-context-injection-test/src/test/java/org/apache/felix/ipojo/runtime/core/TestContextInjectionFromAnnotations.java
@@ -26,6 +26,7 @@
import java.util.Properties;
import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNotSame;
import static org.junit.Assert.assertEquals;
/**
@@ -116,4 +117,13 @@
assertEquals(bc, context);
}
+ @Test
+ public void testInstanceCreatedFromConfiguration() {
+ CheckService check = ipojoHelper.getServiceObjectByName(CheckService.class, "from.configuration");
+ assertNotNull(check);
+ BundleContext context = (BundleContext) check.map().get("context");
+ assertNotNull(context);
+ assertNotSame(bc, context);
+ }
+
}
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
index 054b086..1244349 100644
--- a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
@@ -1541,6 +1541,7 @@
* @return the set of registered fields.
*/
public Set getRegistredFields() {
+ // IMPORTANT - method used by the manipulator
if (m_fieldRegistration == null) {
return null;
}
@@ -1555,6 +1556,7 @@
* @return the set of registered methods.
*/
public Set getRegistredMethods() {
+ // IMPORTANT - method used by the manipulator
if (m_methodRegistration == null) {
return null;
} else {
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/processor/ConfigurationProcessor.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/processor/ConfigurationProcessor.java
index 729c412..7ec25a6 100644
--- a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/processor/ConfigurationProcessor.java
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/processor/ConfigurationProcessor.java
@@ -202,7 +202,8 @@
fields().ofType(Instance.class).in(configuration).map();
for (Map.Entry<Field, Instance> entry : fields.entrySet()) {
Instance instance = entry.getValue();
- instance.nameIfUnnamed(entry.getKey().getName());
+ instance.nameIfUnnamed(entry.getKey().getName())
+ .with("instance.bundle.context").setto(bundle.getBundleContext());
instances.add(instance);
}
@@ -221,7 +222,9 @@
.getKey().getName() + " of class " + entry.getKey().getDeclaringClass() + " threw an " +
"exception", entry.getValue().error());
} else {
- instance.nameIfUnnamed(entry.getKey().getName());
+ instance
+ .nameIfUnnamed(entry.getKey().getName())
+ .with("instance.bundle.context").setto(bundle.getBundleContext());
instances.add(instance);
}
}