The iPOJO API now supports external handlers. To use them, the handler provider has to implements HandlerConfiguration that returns the Element-Attribute structure representing the handler configuration.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@766037 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/tests/api/pom.xml b/ipojo/tests/api/pom.xml
index 4d8faed..2417d4e 100644
--- a/ipojo/tests/api/pom.xml
+++ b/ipojo/tests/api/pom.xml
@@ -72,6 +72,12 @@
 			<version>1.3.0-SNAPSHOT</version>
 		</dependency>
 		
+		<!-- For external handlermanagement -->
+		<dependency>
+			<groupId>org.apache.felix</groupId>
+			<artifactId>org.apache.felix.ipojo.handler.whiteboard</artifactId>
+			<version>1.3.0-SNAPSHOT</version>
+		</dependency>	
 		
 	<!--
 		Pax Exam API:
@@ -108,7 +114,7 @@
 		<version>4.5</version>
 		<type>jar</type>
 		<scope>test</scope>
-	</dependency>	
+	</dependency>
 	</dependencies>
 	
 	<repositories>
diff --git a/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/ExternalHandlerTest.java b/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/ExternalHandlerTest.java
new file mode 100644
index 0000000..59f4e0d
--- /dev/null
+++ b/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/ExternalHandlerTest.java
@@ -0,0 +1,107 @@
+package org.apache.felix.ipojo.tests.api;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.exam.MavenUtils.asInProject;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.api.PrimitiveComponentType;
+import org.apache.felix.ipojo.architecture.HandlerDescription;
+import org.example.service.impl.HostImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Inject;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.BundleContext;
+
+
+
+@RunWith( JUnit4TestRunner.class )
+public class ExternalHandlerTest {
+    
+    @Inject
+    private BundleContext context;
+    
+    private OSGiHelper osgi;
+    
+    private IPOJOHelper ipojo;
+    
+    @Before
+    public void init() {
+        osgi = new OSGiHelper(context);
+        ipojo = new IPOJOHelper(context);
+    }
+    
+    @After
+    public void stop() {
+        ipojo.dispose();
+        osgi.dispose();
+    }
+    
+    @Configuration
+    public static Option[] configure() {    
+        Option[] opt =  options(
+                provision(
+                        mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo").version(asInProject()),
+                        mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo.api").version(asInProject()),
+                        mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo.handler.whiteboard").version(asInProject())
+                    )
+                );
+        return opt;
+    }
+    
+    @Test
+    public void createAHost() throws Exception {
+        PrimitiveComponentType type = createAWhiteboardHost();
+        ComponentInstance ci = type.createInstance();
+        assertThat (ci.getState(), is (ComponentInstance.VALID));
+        HandlerDescription hd = ci.getInstanceDescription().getHandlerDescription(Whiteboard.NAMESPACE + ":" + Whiteboard.NAME);
+        assertThat (hd, is (notNullValue()));
+    }
+    
+    @Test
+    public void createDoubleHost() throws Exception {
+        PrimitiveComponentType type = createASecondWhiteboardHost();
+        ComponentInstance ci = type.createInstance();
+        assertThat (ci.getState(), is (ComponentInstance.VALID));
+        HandlerDescription hd = ci.getInstanceDescription().getHandlerDescription(Whiteboard.NAMESPACE + ":" + Whiteboard.NAME);
+        assertThat (hd, is (notNullValue()));
+    }
+    
+    private PrimitiveComponentType createAWhiteboardHost() {
+        return new PrimitiveComponentType()
+        .setBundleContext(context)
+        .setClassName(HostImpl.class.getName())
+        .addHandler(new Whiteboard()
+            .onArrival("arrival")
+            .onDeparture("departure")
+            .setFilter("(foo=foo)")
+         );
+    }
+    
+    private PrimitiveComponentType createASecondWhiteboardHost() {
+        return new PrimitiveComponentType()
+        .setBundleContext(context)
+        .setClassName(HostImpl.class.getName())
+        .addHandler(new Whiteboard()
+            .onArrival("arrival")
+            .onDeparture("departure")
+            .setFilter("(foo=foo)")
+         )
+         .addHandler(new Whiteboard()
+         .onArrival("arrival")
+         .onDeparture("departure")
+         .setFilter("(foo=bar)")
+         .onModification("modification")
+      );
+    }
+
+}
diff --git a/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/Whiteboard.java b/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/Whiteboard.java
new file mode 100644
index 0000000..17bce48
--- /dev/null
+++ b/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/Whiteboard.java
@@ -0,0 +1,71 @@
+package org.apache.felix.ipojo.tests.api;
+
+import org.apache.felix.ipojo.api.HandlerConfiguration;
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+
+public class Whiteboard implements HandlerConfiguration {
+    
+    public static final String NAME = "wbp";
+    
+    public static final String NAMESPACE = "org.apache.felix.ipojo.whiteboard";
+    
+    private String arrival;
+    
+    private String departure;
+    
+    private String modification;
+    
+    private String filter;
+    
+    public Whiteboard onArrival(String method) {
+        arrival = method;
+        return this;
+    }
+    
+    public Whiteboard onDeparture(String method) {
+        departure = method;
+        return this;
+    }
+    
+    public Whiteboard onModification(String method) {
+        modification = method;
+        return this;
+    }
+    
+    public Whiteboard setFilter(String fil) {
+        filter = fil;
+        return this;
+    }
+
+    public Element getElement() {
+        ensureValidity();
+        // Create the root element.
+        Element element = new Element(NAME, NAMESPACE);
+        // Mandatory attributes
+        element.addAttribute(new Attribute("onArrival", arrival));
+        element.addAttribute(new Attribute("onDeparture", departure));
+        element.addAttribute(new Attribute("filter", filter));
+        
+        // Optional attribute
+        if (modification != null) {
+            element.addAttribute(new Attribute("onModification", modification));
+        }        
+        
+        return element;
+    }
+
+    private void ensureValidity() {
+        if (arrival == null) {
+            throw new IllegalStateException("The whiteboard pattern configuration must have a onArrival method");
+        }
+        if (departure == null) {
+            throw new IllegalStateException("The whiteboard pattern configuration must have a onDeparture method");
+        }
+        if (filter == null) {
+            throw new IllegalStateException("The whiteboard pattern configuration must have a filter");
+        }
+        
+    }
+
+}
diff --git a/ipojo/tests/api/src/test/java/org/example/service/impl/HostImpl.java b/ipojo/tests/api/src/test/java/org/example/service/impl/HostImpl.java
new file mode 100644
index 0000000..1be1a70
--- /dev/null
+++ b/ipojo/tests/api/src/test/java/org/example/service/impl/HostImpl.java
@@ -0,0 +1,19 @@
+package org.example.service.impl;
+
+import org.osgi.framework.ServiceReference;
+
+public class HostImpl {
+    
+    public void arrival(ServiceReference ref) {
+        
+    }
+    
+    public void departure(ServiceReference ref) {
+        
+    }
+    
+    public void modification(ServiceReference ref) {
+        
+    }
+
+}