blob: d1d65b56815fc04ef43e8fa14f24b773ab6e6a9f [file] [log] [blame]
A. J. David Bosschaert8b3069d2014-02-04 14:13:44 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.bundlerepository.osgict;
20
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.util.Dictionary;
25import java.util.Hashtable;
26
27import org.apache.felix.bundlerepository.RepositoryAdmin;
28import org.osgi.framework.BundleActivator;
29import org.osgi.framework.BundleContext;
30import org.osgi.framework.Filter;
31import org.osgi.framework.ServiceReference;
32import org.osgi.util.tracker.ServiceTracker;
33
34/**
35 * This Activator implements the required glue between an OSGi Repository implementation and the
36 * OSGi CT. It is needed to prime the repository with the data needed by the CT and works as
37 * follows:
38 * <ul>
39 * <li>The CT registers a String service with as property {@code repository-xml}. This service is
40 * literally the repository XML needed by the test, and must be fed to the repository implementation.
41 * <li>When that's done this glue code registers another service with as property
42 * {@code repository-populated} to signal to the CT that the priming is done.
43 * </ul>
44 */
45public class Activator implements BundleActivator
46{
47 private BundleContext bundleContext;
48 private ServiceTracker<String, String> repoXMLTracker;
49 private ServiceTracker<RepositoryAdmin, RepositoryAdmin> repoTracker;
50
51 public void start(BundleContext context) throws Exception
52 {
53 bundleContext = context;
54 Filter f = context.createFilter("(&(objectClass=java.lang.String)(repository-xml=*))");
55 repoXMLTracker = new ServiceTracker<String, String>(context, f, null) {
56 @Override
57 public String addingService(ServiceReference<String> reference)
58 {
59 try
60 {
61 String xml = super.addingService(reference);
62 handleRepositoryXML(reference, xml);
63 return xml;
64 }
65 catch (Exception e)
66 {
67 throw new RuntimeException(e);
68 }
69 }
70 };
71 repoXMLTracker.open();
72 }
73
74 public void stop(BundleContext context) throws Exception
75 {
76 repoXMLTracker.close();
77 if (repoTracker != null)
78 repoTracker.close();
79 }
80
81 private void handleRepositoryXML(ServiceReference<String> reference, String xml) throws Exception
82 {
83 File tempXMLFile = bundleContext.getDataFile("repo-" + reference.getProperty("repository-xml") + ".xml");
84 writeXMLToFile(tempXMLFile, xml);
85
86 repoTracker = new ServiceTracker<RepositoryAdmin, RepositoryAdmin>(bundleContext, RepositoryAdmin.class, null);
87 repoTracker.open();
88 RepositoryAdmin repo = repoTracker.waitForService(30000);
89 repo.addRepository(tempXMLFile.toURI().toURL());
90 tempXMLFile.delete();
91
92 Dictionary<String, Object> props = new Hashtable<String, Object>();
93 props.put("repository-populated", reference.getProperty("repository-xml"));
94 bundleContext.registerService(String.class, "", props);
95 }
96
97 private void writeXMLToFile(File tempXMLFile, String xml) throws IOException
98 {
99 FileOutputStream fos = new FileOutputStream(tempXMLFile);
100 try
101 {
102 fos.write(xml.getBytes());
103 }
104 finally
105 {
106 fos.close();
107 }
108 }
109}