1 package com.atlassian.activeobjects.osgi;
2
3 import org.osgi.framework.Bundle;
4 import org.osgi.framework.BundleContext;
5 import org.osgi.framework.ServiceRegistration;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import java.util.HashMap;
10 import java.util.Hashtable;
11 import java.util.Map;
12
13 import static com.google.common.base.Preconditions.checkNotNull;
14
15
16
17
18 public class OsgiServiceUtilsImpl implements OsgiServiceUtils {
19 private static final String PROPERTY_KEY = "com.atlassian.plugin.key";
20
21 private final Logger logger = LoggerFactory.getLogger(this.getClass());
22
23 public <S, O extends S> ServiceRegistration registerService(Bundle bundle, Class<S> ifce, O obj) {
24 checkNotNull(bundle);
25 checkNotNull(obj);
26 final Map<String, String> properties = getProperties(bundle);
27
28 logger.debug("Registering service {} with interface {} and properties {}", new Object[]{obj, ifce.getName(), properties});
29
30 return getContext(bundle).registerService(ifce.getName(), obj, new Hashtable<String, String>(properties));
31 }
32
33 Map<String, String> getProperties(Bundle bundle) {
34 final Map<String, String> props = new HashMap<String, String>();
35 props.put(PROPERTY_KEY, bundle.getSymbolicName());
36 return props;
37 }
38
39 private BundleContext getContext(Bundle bundle) {
40 return bundle.getBundleContext();
41 }
42
43 }