View Javadoc

1   package com.atlassian.plugin.spring.pluginns;
2   
3   import static com.atlassian.plugin.spring.pluginns.SpringXmlHostComponentProvider.HOST_COMPONENT_PROVIDER;
4   import org.springframework.beans.factory.config.BeanDefinition;
5   import org.springframework.beans.factory.config.BeanDefinitionHolder;
6   import org.springframework.beans.factory.support.BeanDefinitionRegistry;
7   import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
8   import org.springframework.beans.factory.xml.ParserContext;
9   import org.w3c.dom.Node;
10  
11  import java.util.ArrayList;
12  import java.util.HashMap;
13  import java.util.List;
14  import java.util.Map;
15  
16  /**
17   * Matches the <plugin:interface> element and registers it against the bean for later processing.
18   */
19  public class PluginInterfaceBeanDefinitionDecorator implements BeanDefinitionDecorator
20  {
21      /**
22       * Called when the Spring parser encounters an "interface" element.
23       * @param source The interface element
24       * @param holder The containing bean definition
25       * @param ctx The parser context
26       * @return The containing bean definition
27       */
28      public BeanDefinitionHolder decorate(
29              Node source, BeanDefinitionHolder holder, ParserContext ctx)
30      {
31  
32          String inf = source.getTextContent();
33          if (inf != null)
34          {
35              inf = inf.trim();
36          }
37  
38          BeanDefinitionRegistry registry = ctx.getRegistry();
39          BeanDefinition providerDef = registry.getBeanDefinition(HOST_COMPONENT_PROVIDER);
40          List<String> interfaces = loadBeanInterfaces(providerDef, holder.getBeanName());
41          interfaces.add(inf);
42          return holder;
43      }
44  
45      /**
46       * Loads the interface map into the definition for the component provider
47       *
48       * @param providerDef The definition of the component provider
49       * @param beanName The bean to assign the interface against
50       * @return The list of registered interfaces, will never be null.
51       */
52      private List<String> loadBeanInterfaces(BeanDefinition providerDef, String beanName)
53      {
54          Map<String, List<String>> interfaces = loadInterfaceMap(providerDef);
55          if (!interfaces.containsKey(beanName))
56          {
57              interfaces.put(beanName, new ArrayList<String>());
58          }
59          return interfaces.get(beanName);
60      }
61  
62      /**
63       * Ensures the interfaces map is registered against the host component provider definition
64       * @param providerDef The host component provider defintion
65       * @return The map of bean names to list of interfaces
66       */
67      private Map<String, List<String>> loadInterfaceMap(BeanDefinition providerDef)
68      {
69          Map<String,List<String>> interfaces;
70          if (providerDef.getPropertyValues().contains("interfaces"))
71          {
72              interfaces = (Map<String,List<String>>) providerDef.getPropertyValues().getPropertyValue("interfaces");
73          }
74          else
75          {
76              interfaces = new HashMap<String,List<String>>();
77              providerDef.getPropertyValues().addPropertyValue("interfaces", interfaces);
78          }
79          return interfaces;
80      }
81  }