View Javadoc
1   package com.atlassian.refapp.sal;
2   
3   import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
4   import com.atlassian.sal.spi.HostContextAccessor;
5   import org.osgi.framework.BundleContext;
6   import org.osgi.framework.InvalidSyntaxException;
7   import org.osgi.framework.ServiceReference;
8   
9   import javax.inject.Inject;
10  import javax.inject.Named;
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  @ExportAsService
15  @Named("hostContextAccessor")
16  public class RefimplHostContextAccessor implements HostContextAccessor {
17      private final BundleContext bundleContext;
18  
19      @Inject
20      public RefimplHostContextAccessor(final BundleContext bundleContext) {
21          this.bundleContext = bundleContext;
22      }
23  
24      public Object doInTransaction(HostTransactionCallback callback) {
25          return callback.doInTransaction();
26      }
27  
28      public <T> Map<String, T> getComponentsOfType(Class<T> iface) {
29          ServiceReference[] refs = new ServiceReference[0];
30          try {
31              refs = bundleContext.getServiceReferences(iface.getName(), null);
32          } catch (InvalidSyntaxException e) {
33              // will not happen - we pass null as a filter. Woo for checked exceptions!
34              throw new RuntimeException("Invalid filter", e);
35          }
36          Map<String, T> objs = new HashMap<String, T>();
37          if (refs != null) {
38              for (ServiceReference ref : refs) {
39                  objs.put("", (T) bundleContext.getService(ref));
40              }
41          }
42          return objs;
43      }
44  
45  }