View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform;
2   
3   import org.dom4j.Document;
4   import org.dom4j.Element;
5   
6   import java.util.List;
7   import java.util.ArrayList;
8   
9   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
10  
11  /**
12   * Transforms components into spring beans
13   * @since 2.1
14   */
15  public class ComponentSpringTransformer implements SpringTransformer
16  {
17      public void transform(List<HostComponentRegistration> regs, Document pluginDoc, Document springDoc)
18      {
19          Element root = springDoc.getRootElement();
20          List<Element> elements = pluginDoc.getRootElement().elements("component");
21          for (Element component : elements)
22          {
23              Element bean = root.addElement("beans:bean");
24              bean.addAttribute("id", component.attributeValue("key"));
25              bean.addAttribute("alias", component.attributeValue("alias"));
26              bean.addAttribute("class", component.attributeValue("class"));
27              if ("true".equalsIgnoreCase(component.attributeValue("public")))
28              {
29                  Element osgiService = root.addElement("osgi:service");
30                  osgiService.addAttribute("id", component.attributeValue("key")+"_osgiService");
31                  osgiService.addAttribute("ref", component.attributeValue("key"));
32  
33                  List<String> interfaceNames = new ArrayList<String>();
34                  List<Element> compInterfaces = component.elements("interface");
35                  for (Element inf : compInterfaces)
36                      interfaceNames.add(inf.getTextTrim());
37  
38                  Element interfaces = osgiService.addElement("osgi:interfaces");
39                  for (String name : interfaceNames)
40                  {
41                      Element e = interfaces.addElement("beans:value");
42                      e.setText(name);
43                  }
44              }
45          }
46      }
47  }