View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform;
2   
3   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
4   import com.atlassian.plugin.osgi.hostcomponents.PropertyBuilder;
5   import com.atlassian.plugin.osgi.hostcomponents.ComponentRegistrar;
6   
7   import java.util.List;
8   
9   import org.dom4j.Document;
10  import org.dom4j.Element;
11  
12  /**
13   * Transforms host components into Spring configuration
14   * @since 2.1
15   */
16  public class HostComponentSpringTransformer implements SpringTransformer
17  {
18      public void transform(List<HostComponentRegistration> regs, Document pluginDoc, Document springDoc)
19      {
20          Element root = springDoc.getRootElement();
21          if (regs != null)
22          {
23              for (int x=0; x<regs.size(); x++)
24              {
25                  HostComponentRegistration reg = regs.get(x);
26                  String beanName = reg.getProperties().get(PropertyBuilder.BEAN_NAME);
27  
28                  Element osgiService = root.addElement("osgi:reference");
29                  osgiService.addAttribute("id", determineId(pluginDoc, beanName, x));
30  
31                  // Disabling this for now due to some strange Spring DM bug where it will occasionally generate an invalid
32                  // filter, see http://jira.atlassian.com/browse/CONF-13292
33                  if (beanName != null)
34                      osgiService.addAttribute("filter", "(&(bean-name="+beanName+")("+ ComponentRegistrar.HOST_COMPONENT_FLAG+"=true))");
35  
36                  Element interfaces = osgiService.addElement("osgi:interfaces");
37                  for (String name : reg.getMainInterfaces())
38                  {
39                      Element e = interfaces.addElement("beans:value");
40                      e.setText(name);
41                  }
42              }
43          }
44      }
45  
46      private String determineId(Document pluginDoc, String beanName, int iteration)
47      {
48          String id = beanName;
49          if (id == null)
50              id = "bean"+iteration;
51  
52          id = id.replaceAll("#", "LB");
53  
54          for (Object element : pluginDoc.getRootElement().elements("component-import"))
55          {
56              String key = ((Element)element).attributeValue("key");
57              if (id.equals(key))
58              {
59                  id+=iteration;
60                  break;
61              }
62          }
63          return id;
64      }
65  }