Fix FELIX-2594 Have a way to create new custom iPojo handler without having to specify a handler usage
Allow to auto-attach primitive handlers to primitive components.
Handlers an be auto-attached if they are contained in the 'org.apache.felix.ipojo.handler.auto.primitive' system property. This property's value must be the list (command separated) of full handler name (namespace:name).
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1006281 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/tests/core/external-handlers/pom.xml b/ipojo/tests/core/external-handlers/pom.xml
index 727f2f4..f182bf2 100644
--- a/ipojo/tests/core/external-handlers/pom.xml
+++ b/ipojo/tests/core/external-handlers/pom.xml
@@ -70,16 +70,16 @@
<instructions>
<Export-Package>
org.apache.felix.ipojo.test.scenarios.eh.service
- </Export-Package>
+ </Export-Package>
<Bundle-SymbolicName>
${project.artifactId}
- </Bundle-SymbolicName>
+ </Bundle-SymbolicName>
<Private-Package>
org.apache.felix.ipojo.test*
- </Private-Package>
+ </Private-Package>
<Test-Suite>
org.apache.felix.ipojo.test.scenarios.eh.ExternalHandlerTestSuite
- </Test-Suite>
+ </Test-Suite>
</instructions>
</configuration>
</plugin>
@@ -105,6 +105,19 @@
<target>1.4</target>
</configuration>
</plugin>
+
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-junit4osgi-plugin</artifactId>
+ <version>1.1.0-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
</project>
diff --git a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
index cd43545..7bdcfb0 100644
--- a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
+++ b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceHandler.java
@@ -1,4 +1,4 @@
-/*
+/*
* 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
@@ -30,21 +30,21 @@
import org.osgi.framework.ServiceRegistration;
public class CheckServiceHandler extends PrimitiveHandler implements CheckService {
-
+
ServiceRegistration sr;
boolean isValid;
int changes = 0;
static final String NAMESPACE = "org.apache.felix.ipojo.test.handler.checkservice";
-
+
Properties props = new Properties();
public void configure(Element metadata, Dictionary configuration) {
Element[] meta = metadata.getElements("check", NAMESPACE);
- if(meta == null) { return; }
- // Get handler props
+ if(meta == null) { return; }
+ // Get handler props
props.put("instance.name", configuration.get("instance.name"));
if(configuration.get("csh.simple") != null) { props.put("Simple", configuration.get("csh.simple")); }
- if(configuration.get("csh.map") != null) {
+ if(configuration.get("csh.map") != null) {
Dictionary m = (Dictionary) configuration.get("csh.map");
if (m.size() > 0) {
props.put("Map1", m.get("a"));
@@ -53,28 +53,28 @@
}
}
props.put("changes", new Integer(changes));
-
+
}
-
+
public void initializeComponentFactory(ComponentTypeDescription cd, Element metadata) {
cd.addProperty(new PropertyDescription("csh.simple", "java.lang.String", null));
cd.addProperty(new PropertyDescription("csh.map", "java.util.Dictionary", null));
}
-
+
public void start() {
if(sr == null) {
sr = getInstanceManager().getContext().registerService(CheckService.class.getName(), this, props);
}
isValid = true;
}
-
+
public void stop() {
isValid = false;
synchronized(this) {
if(sr != null) { sr.unregister(); }
}
}
-
+
public boolean check() {
if(isValid) { isValid = false;}
else { isValid = true; }
@@ -84,7 +84,7 @@
public Properties getProps() {
return props;
}
-
+
public void stateChanged(int state) {
if (sr != null) {
changes++;
@@ -96,7 +96,7 @@
public String getName() {
return NAMESPACE;
}
-
+
public HandlerDescription getDescription() {
return new CheckServiceHandlerDescription(this);
}
diff --git a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java
new file mode 100644
index 0000000..48a3ec9
--- /dev/null
+++ b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/AutoHandlerTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.test.scenarios.eh;
+
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.architecture.HandlerDescription;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.component.CheckServiceHandlerDescription;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+
+public class AutoHandlerTest extends OSGiTestCase {
+
+ private static final String ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE = "org.apache.felix.ipojo.handler.auto.primitive";
+
+ ComponentInstance instance;
+
+ ComponentFactory factory;
+
+ public void setUp() {
+ factory = (ComponentFactory) Utils.getFactoryByName(getContext(), "HANDLER-HandlerTesterWO");
+ System.clearProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE);
+ }
+
+ public void tearDown() {
+ if (instance != null) {
+ instance.dispose();
+ }
+ instance = null;
+
+ System.clearProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE);
+ }
+
+ public void testRequiredHandlerList() {
+ List list = factory.getRequiredHandlers();
+ assertFalse(list.contains("org.apache.felix.ipojo.test.handler.checkservice:check"));
+
+ String v = "org.apache.felix.ipojo.test.handler.checkservice:check";
+ System.setProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE, v);
+
+ factory.stop();
+ factory.restart();
+ factory.start();
+
+ list = factory.getRequiredHandlers();
+ assertTrue(list.contains("org.apache.felix.ipojo.test.handler.checkservice:check"));
+
+ }
+
+ public void testInstanceCreation() throws Exception {
+ String v = "org.apache.felix.ipojo.test.handler.checkservice:check";
+ System.setProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE, v);
+
+ factory.stop();
+ factory.restart();
+ factory.start();
+
+ instance = factory.createComponentInstance(new Properties());
+ assertEquals(ComponentInstance.VALID, instance.getState());
+
+ HandlerDescription hd = instance.getInstanceDescription().getHandlerDescription(v);
+ assertNotNull(hd);
+ assertTrue(hd instanceof CheckServiceHandlerDescription);
+
+ }
+}
diff --git a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
index 9965643..4f66028 100644
--- a/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
+++ b/ipojo/tests/core/external-handlers/src/main/java/org/apache/felix/ipojo/test/scenarios/eh/ExternalHandlerTestSuite.java
@@ -1,4 +1,4 @@
-/*
+/*
* 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
@@ -29,6 +29,7 @@
public static Test suite(BundleContext bc) {
OSGiTestSuite ots = new OSGiTestSuite("External Handler Test Suite", bc);
ots.addTestSuite(HandlerTest.class);
+ ots.addTestSuite(AutoHandlerTest.class);
return ots;
}
diff --git a/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml b/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml
index 343244e..b7da090 100644
--- a/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml
+++ b/ipojo/tests/core/external-handlers/src/main/resources/metadata.xml
@@ -29,5 +29,11 @@
<!-- Empty dictionary -->
</property>
</instance>
-
+
+ <!-- The handler will be added using the auto handler property -->
+ <component
+ classname="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"
+ name="HANDLER-HandlerTesterWO" architecture="true">
+ </component>
+
</ipojo>