[FELIX-4371] Integration bundle needed to work with the OSGi Repository CT


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1564308 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundlerepository.osgi-ct/pom.xml b/bundlerepository.osgi-ct/pom.xml
new file mode 100644
index 0000000..9ce9613
--- /dev/null
+++ b/bundlerepository.osgi-ct/pom.xml
@@ -0,0 +1,101 @@
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix-parent</artifactId>
+    <version>2.1</version>
+    <relativePath>../../pom/pom.xml</relativePath>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>bundle</packaging>
+  <name>Apache Felix Bundle Repository - OSGi CT integration</name>
+  <description>
+    Bundle repository service OSGi CT integration. To run a Repository implementation in the 
+    OSGi CT, a small integration layer needs to be provided by the implementation that knows
+    how to prime the repository with the provided repository xml file.
+  </description>
+  <artifactId>org.apache.felix.bundlerepository.osgi-ct</artifactId>
+  <version>1.7.0-SNAPSHOT</version>
+  <scm>
+    <connection>scm:svn:http://svn.apache.org/repos/asf/felix/trunk/bundlerepository.osgi-ct</connection>
+    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/felix/trunk/bundlerepository.osgi-ct</developerConnection>
+    <url>http://svn.apache.org/repos/asf/felix/trunk/bundlerepository.osgi-ct</url>
+  </scm>
+  <dependencies>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>5.0.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>5.0.0</version>
+    </dependency>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>org.apache.felix.bundlerepository</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <target>1.5</target>
+          <source>1.5</source>
+        </configuration>
+      </plugin>
+
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <version>2.3.4</version>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Export-Package></Export-Package>
+            <Private-Package>org.apache.felix.bundlerepository.osgict</Private-Package>
+            <Bundle-Activator>org.apache.felix.bundlerepository.osgict.Activator</Bundle-Activator>
+            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+            <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
+          </instructions>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.rat</groupId>
+        <artifactId>apache-rat-plugin</artifactId>
+        <configuration>
+          <excludeSubProjects>false</excludeSubProjects>
+          <useEclipseDefaultExcludes>true</useEclipseDefaultExcludes>
+          <useMavenDefaultExcludes>true</useMavenDefaultExcludes>
+          <excludes>
+            <param>doc/*</param>
+            <param>maven-eclipse.xml</param>
+            <param>.checkstyle</param>
+            <param>.externalToolBuilders/*</param>
+          </excludes>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/bundlerepository.osgi-ct/src/main/java/org/apache/felix/bundlerepository/osgict/Activator.java b/bundlerepository.osgi-ct/src/main/java/org/apache/felix/bundlerepository/osgict/Activator.java
new file mode 100644
index 0000000..d1d65b5
--- /dev/null
+++ b/bundlerepository.osgi-ct/src/main/java/org/apache/felix/bundlerepository/osgict/Activator.java
@@ -0,0 +1,109 @@
+/*
+ * 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.bundlerepository.osgict;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.felix.bundlerepository.RepositoryAdmin;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * This Activator implements the required glue between an OSGi Repository implementation and the
+ * OSGi CT. It is needed to prime the repository with the data needed by the CT and works as
+ * follows:
+ * <ul>
+ * <li>The CT registers a String service with as property {@code repository-xml}. This service is
+ * literally the repository XML needed by the test, and must be fed to the repository implementation.
+ * <li>When that's done this glue code registers another service with as property
+ * {@code repository-populated} to signal to the CT that the priming is done.
+ * </ul>
+ */
+public class Activator implements BundleActivator
+{
+    private BundleContext bundleContext;
+    private ServiceTracker<String, String> repoXMLTracker;
+    private ServiceTracker<RepositoryAdmin, RepositoryAdmin> repoTracker;
+
+    public void start(BundleContext context) throws Exception
+    {
+        bundleContext = context;
+        Filter f = context.createFilter("(&(objectClass=java.lang.String)(repository-xml=*))");
+        repoXMLTracker = new ServiceTracker<String, String>(context, f, null) {
+            @Override
+            public String addingService(ServiceReference<String> reference)
+            {
+                try
+                {
+                    String xml = super.addingService(reference);
+                    handleRepositoryXML(reference, xml);
+                    return xml;
+                }
+                catch (Exception e)
+                {
+                    throw new RuntimeException(e);
+                }
+            }
+        };
+        repoXMLTracker.open();
+    }
+
+    public void stop(BundleContext context) throws Exception
+    {
+        repoXMLTracker.close();
+        if (repoTracker != null)
+            repoTracker.close();
+    }
+
+    private void handleRepositoryXML(ServiceReference<String> reference, String xml) throws Exception
+    {
+        File tempXMLFile = bundleContext.getDataFile("repo-" + reference.getProperty("repository-xml") + ".xml");
+        writeXMLToFile(tempXMLFile, xml);
+
+        repoTracker = new ServiceTracker<RepositoryAdmin, RepositoryAdmin>(bundleContext, RepositoryAdmin.class, null);
+        repoTracker.open();
+        RepositoryAdmin repo = repoTracker.waitForService(30000);
+        repo.addRepository(tempXMLFile.toURI().toURL());
+        tempXMLFile.delete();
+
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put("repository-populated", reference.getProperty("repository-xml"));
+        bundleContext.registerService(String.class, "", props);
+    }
+
+    private void writeXMLToFile(File tempXMLFile, String xml) throws IOException
+    {
+        FileOutputStream fos = new FileOutputStream(tempXMLFile);
+        try
+        {
+            fos.write(xml.getBytes());
+        }
+        finally
+        {
+            fos.close();
+        }
+    }
+}
diff --git a/pom.xml b/pom.xml
index c9b407f..14ca26e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -64,6 +64,7 @@
       <modules>
         <module>utils</module>
         <module>bundlerepository</module>
+        <module>bundlerepository.osgi-ct</module>
      </modules>
    </profile>