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