View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform.stage;
2   
3   import com.atlassian.plugin.osgi.factory.transform.PluginTransformationException;
4   import com.atlassian.plugin.osgi.factory.transform.TransformContext;
5   import com.atlassian.plugin.osgi.factory.transform.TransformStage;
6   import org.dom4j.Document;
7   import org.dom4j.Element;
8   
9   import java.util.List;
10  
11  /**
12   * Transforms module-type elements into the appropriate Spring XML configuration file
13   *
14   * @since 2.2.0
15   */
16  public class ModuleTypeSpringStage implements TransformStage
17  {
18      /** The name of the generated Spring XML file for this stage */
19      private static final String SPRING_XML = "META-INF/spring/atlassian-plugins-module-types.xml";
20  
21      public void execute(TransformContext context) throws PluginTransformationException
22      {
23          if (SpringHelper.shouldGenerateFile(context, SPRING_XML))
24          {
25              Document doc = SpringHelper.createSpringDocument();
26              Element root = doc.getRootElement();
27              List<Element> elements = context.getDescriptorDocument().getRootElement().elements("module-type");
28              if (elements.size() > 0)
29              {
30                  context.getExtraImports().add("com.atlassian.plugin.osgi.external");
31                  context.getExtraImports().add("com.atlassian.plugin");
32              }
33              for (Element e : elements)
34              {
35                  Element bean = root.addElement("beans:bean");
36                  bean.addAttribute("id", getBeanId(e));
37                  bean.addAttribute("class", "com.atlassian.plugin.osgi.external.SingleModuleDescriptorFactory");
38  
39                  Element arg = bean.addElement("beans:constructor-arg");
40                  arg.addAttribute("index", "0");
41                  Element value = arg.addElement("beans:value");
42                  value.setText(e.attributeValue("key"));
43                  Element arg2 = bean.addElement("beans:constructor-arg");
44                  arg2.addAttribute("index", "1");
45                  Element value2 = arg2.addElement("beans:value");
46                  value2.setText(e.attributeValue("class"));
47  
48                  Element osgiService = root.addElement("osgi:service");
49                  osgiService.addAttribute("id", getBeanId(e) + "_osgiService");
50                  osgiService.addAttribute("ref", getBeanId(e));
51                  osgiService.addAttribute("auto-export", "interfaces");
52              }
53  
54              if (root.elements().size() > 0)
55              {
56                  context.getFileOverrides().put(SPRING_XML, SpringHelper.documentToBytes(doc));
57              }
58          }
59      }
60  
61      private String getBeanId(Element e)
62      {
63          return "moduleType-" + e.attributeValue("key");
64      }
65  }