View Javadoc
1   package com.atlassian.plugin.spring.scanner.runtime.impl;
2   
3   import org.eclipse.gemini.blueprint.service.exporter.OsgiServiceRegistrationListener;
4   import org.eclipse.gemini.blueprint.service.exporter.support.DefaultInterfaceDetector;
5   import org.eclipse.gemini.blueprint.service.exporter.support.ExportContextClassLoaderEnum;
6   import org.eclipse.gemini.blueprint.service.exporter.support.OsgiServiceFactoryBean;
7   import org.osgi.framework.BundleContext;
8   import org.osgi.framework.ServiceRegistration;
9   
10  import java.util.Hashtable;
11  import java.util.Map;
12  
13  /**
14   * Utility class to encapsulate the registration/de-registration of OSGi services exported by public components
15   */
16  public class ExportedSeviceManager {
17      private final Hashtable<Integer, OsgiServiceFactoryBean> exporters;
18  
19      public ExportedSeviceManager() {
20          this.exporters = new Hashtable<Integer, OsgiServiceFactoryBean>();
21      }
22  
23      /**
24       * Exports a component as an OSGi service for use by other bundles
25       *
26       * @param bundleContext the bundle context
27       * @param bean          the bean to register
28       * @param beanName      the name of that bean
29       * @param serviceProps  the service properties
30       * @param interfaces    the interface of that bean
31       * @return the service registration handle
32       * @throws Exception if things go badly
33       */
34      public ServiceRegistration registerService(final BundleContext bundleContext, final Object bean, final String beanName, final Map<String, Object> serviceProps, final Class<?>... interfaces)
35              throws Exception {
36          final OsgiServiceFactoryBean osgiExporter = createExporter(bundleContext, bean, beanName, serviceProps, interfaces);
37  
38          final int hashCode = System.identityHashCode(bean);
39          exporters.put(hashCode, osgiExporter);
40  
41          return osgiExporter.getObject();
42      }
43  
44      /**
45       * Query whether a bean was registered as a service with this exported service manager.
46       *
47       * @param bean the bean to query
48       * @return true if bean was registered, false if not.
49       */
50      public boolean hasService(final Object bean) {
51          final int hashCode = System.identityHashCode(bean);
52          return exporters.containsKey(hashCode);
53      }
54  
55      /**
56       * De-registers an OSGi service
57       *
58       * @param bundleContext the bundle context
59       * @param bean          the bean to un-register
60       */
61      @SuppressWarnings("UnusedParameters")
62      public void unregisterService(final BundleContext bundleContext, final Object bean) {
63          final int hashCode = System.identityHashCode(bean);
64          final OsgiServiceFactoryBean exporter = exporters.get(hashCode);
65          if (null != exporter) {
66              exporter.destroy();
67              exporters.remove(hashCode);
68          }
69      }
70  
71      /**
72       * creates the OsgiServiceFactoryBean used by spring when registering services
73       */
74      private OsgiServiceFactoryBean createExporter(final BundleContext bundleContext, final Object bean, final String beanName, final Map<String, Object> serviceProps, final Class<?>[] interfaces)
75              throws Exception {
76          serviceProps.put("org.eclipse.gemini.blueprint.bean.name", beanName);
77  
78          final OsgiServiceFactoryBean exporter = new OsgiServiceFactoryBean();
79          exporter.setInterfaceDetector(DefaultInterfaceDetector.DISABLED);
80          exporter.setBeanClassLoader(bean.getClass().getClassLoader());
81          exporter.setBeanName(beanName);
82          exporter.setBundleContext(bundleContext);
83          exporter.setExportContextClassLoader(ExportContextClassLoaderEnum.UNMANAGED);
84          exporter.setInterfaces(interfaces);
85          exporter.setServiceProperties(serviceProps);
86          exporter.setTarget(bean);
87          exporter.setListeners(new OsgiServiceRegistrationListener[0]);
88  
89          exporter.afterPropertiesSet();
90  
91          return exporter;
92      }
93  }