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 com.atlassian.plugin.util.PluginUtils;
7   import com.atlassian.plugin.util.validation.ValidationPattern;
8   import org.dom4j.Document;
9   import org.dom4j.Element;
10  
11  import java.util.List;
12  
13  import static com.atlassian.plugin.util.validation.ValidationPattern.createPattern;
14  import static com.atlassian.plugin.util.validation.ValidationPattern.test;
15  
16  /**
17   * Transforms module-type elements into the appropriate Spring XML configuration file
18   *
19   * @since 2.2.0
20   */
21  public class ModuleTypeSpringStage implements TransformStage {
22      /**
23       * The name of the generated Spring XML file for this stage
24       */
25      private static final String SPRING_XML = "META-INF/spring/atlassian-plugins-module-types.xml";
26      static final String HOST_CONTAINER = "springHostContainer";
27      static final String SPRING_HOST_CONTAINER = "com.atlassian.plugin.osgi.bridge.external.SpringHostContainer";
28  
29      public static final String BEAN_SOURCE = "Module Type";
30  
31      public void execute(TransformContext context) throws PluginTransformationException {
32          if (SpringHelper.shouldGenerateFile(context, SPRING_XML)) {
33              Document doc = SpringHelper.createSpringDocument();
34              Element root = doc.getRootElement();
35              List<Element> elements = context.getDescriptorDocument().getRootElement().elements("module-type");
36              if (elements.size() > 0) {
37                  context.getExtraImports().add("com.atlassian.plugin.osgi.external");
38                  context.getExtraImports().add("com.atlassian.plugin.osgi.bridge.external");
39                  context.getExtraImports().add("com.atlassian.plugin");
40  
41                  // create a new bean.
42                  Element hostContainerBean = root.addElement("beans:bean");
43  
44                  // make sure the new bean id is not already in use.
45                  context.trackBean(HOST_CONTAINER, BEAN_SOURCE);
46  
47                  hostContainerBean.addAttribute("id", HOST_CONTAINER);
48                  hostContainerBean.addAttribute("class", SPRING_HOST_CONTAINER);
49  
50                  ValidationPattern validation = createPattern().
51                          rule(
52                                  test("@key").withError("The key is required"),
53                                  test("@class").withError("The class is required"));
54  
55                  for (Element e : elements) {
56                      if (!PluginUtils.doesModuleElementApplyToApplication(e, context.getApplications(), context.getInstallationMode())) {
57                          continue;
58                      }
59                      validation.evaluate(e);
60                      Element bean = root.addElement("beans:bean");
61  
62                      String beanId = getBeanId(e);
63                      // make sure the new bean id is not already in use.
64                      context.trackBean(beanId, BEAN_SOURCE);
65  
66                      bean.addAttribute("id", beanId);
67                      bean.addAttribute("class", "com.atlassian.plugin.osgi.external.SingleModuleDescriptorFactory");
68  
69                      Element arg = bean.addElement("beans:constructor-arg");
70                      arg.addAttribute("index", "0");
71                      arg.addAttribute("ref", HOST_CONTAINER);
72                      Element arg2 = bean.addElement("beans:constructor-arg");
73                      arg2.addAttribute("index", "1");
74                      Element value2 = arg2.addElement("beans:value");
75                      value2.setText(e.attributeValue("key"));
76                      Element arg3 = bean.addElement("beans:constructor-arg");
77                      arg3.addAttribute("index", "2");
78                      Element value3 = arg3.addElement("beans:value");
79                      value3.setText(e.attributeValue("class"));
80  
81                      Element osgiService = root.addElement("osgi:service");
82                      String serviceBeanId = getBeanId(e) + "_osgiService";
83                      // make sure the new bean id is not already in use.
84                      context.trackBean(serviceBeanId, BEAN_SOURCE);
85  
86                      osgiService.addAttribute("id", serviceBeanId);
87                      osgiService.addAttribute("ref", beanId);
88                      osgiService.addAttribute("auto-export", "interfaces");
89                  }
90              }
91  
92              if (root.elements().size() > 0) {
93                  context.setShouldRequireSpring(true);
94                  context.getFileOverrides().put(SPRING_XML, SpringHelper.documentToBytes(doc));
95              }
96          }
97      }
98  
99      private String getBeanId(Element e) {
100         return "moduleType-" + e.attributeValue("key");
101     }
102 }