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