1 package com.atlassian.plugin.spring.scanner.test.registry;
2
3 import com.atlassian.plugin.spring.scanner.test.dynamic.DynamicContextManager;
4 import com.google.common.base.Joiner;
5 import org.osgi.framework.BundleContext;
6 import org.osgi.framework.Constants;
7 import org.osgi.framework.ServiceReference;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.context.ApplicationContext;
10 import org.springframework.context.ConfigurableApplicationContext;
11 import org.springframework.stereotype.Component;
12
13 import java.util.HashSet;
14 import java.util.Set;
15 import java.util.TreeSet;
16
17
18
19
20 @Component
21 public class BeanLister {
22
23 private static final Joiner INTERFACE_NAME_JOINER = Joiner.on(",");
24
25 private final DynamicContextManager bootstrappingComponent;
26 private final ApplicationContext parentContext;
27 private final BundleContext bundleContext;
28
29 @Autowired
30 public BeanLister(DynamicContextManager bootstrappingComponent,
31 ApplicationContext parentContext,
32 BundleContext bundleContext) {
33 this.bootstrappingComponent = bootstrappingComponent;
34 this.parentContext = parentContext;
35 this.bundleContext = bundleContext;
36 }
37
38 public Set<String> listBeans() {
39 Set<String> beans = new TreeSet<>();
40 final ConfigurableApplicationContext internalContext = bootstrappingComponent.getInternalContext();
41 if (internalContext != null) {
42 beans.addAll(buildBeanDescriptions(internalContext));
43 }
44 beans.addAll(buildBeanDescriptions(parentContext));
45 return beans;
46 }
47
48 private Set<String> buildBeanDescriptions(ApplicationContext context) {
49 final Set<String> beans = new HashSet<>();
50 String[] beanDefinitionNames = context.getBeanDefinitionNames();
51 for (String name : beanDefinitionNames) {
52
53
54
55 beans.add(name + " = " + context.getBean(name).toString().replaceFirst("@.*", ""));
56 }
57 return beans;
58 }
59
60 public Set<String> listServices() {
61 final Set<String> services = new TreeSet<>();
62 for (final ServiceReference serviceReference : bundleContext.getBundle().getRegisteredServices()) {
63 final String[] interfaces = (String[]) serviceReference.getProperty(Constants.OBJECTCLASS);
64 services.add(INTERFACE_NAME_JOINER.join(interfaces));
65 }
66 return services;
67 }
68 }