View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform.stage;
2   
3   import com.atlassian.plugin.osgi.factory.transform.TransformStage;
4   import com.atlassian.plugin.osgi.factory.transform.TransformContext;
5   import com.atlassian.plugin.osgi.factory.transform.PluginTransformationException;
6   import org.dom4j.Document;
7   import org.dom4j.Element;
8   
9   import java.util.List;
10  import java.util.ArrayList;
11  
12  /**
13   * Transforms component tags in the plugin descriptor into the appropriate spring XML configuration file
14   *
15   * @since 2.2.0
16   */
17  public class ComponentSpringStage implements TransformStage
18  {
19      /** Path of generated Spring XML file */
20      private static final String SPRING_XML = "META-INF/spring/atlassian-plugins-components.xml";
21  
22      public void execute(TransformContext context) throws PluginTransformationException
23      {
24          if (SpringHelper.shouldGenerateFile(context, SPRING_XML))
25          {
26              Document springDoc = SpringHelper.createSpringDocument();
27              Element root = springDoc.getRootElement();
28              List<Element> elements = context.getDescriptorDocument().getRootElement().elements("component");
29              for (Element component : elements)
30              {
31                  Element bean = root.addElement("beans:bean");
32                  bean.addAttribute("id", component.attributeValue("key"));
33                  bean.addAttribute("alias", component.attributeValue("alias"));
34                  bean.addAttribute("class", component.attributeValue("class"));
35                  if ("true".equalsIgnoreCase(component.attributeValue("public")))
36                  {
37                      Element osgiService = root.addElement("osgi:service");
38                      osgiService.addAttribute("id", component.attributeValue("key") + "_osgiService");
39                      osgiService.addAttribute("ref", component.attributeValue("key"));
40  
41                      List<String> interfaceNames = new ArrayList<String>();
42                      List<Element> compInterfaces = component.elements("interface");
43                      for (Element inf : compInterfaces)
44                      {
45                          interfaceNames.add(inf.getTextTrim());
46                      }
47  
48                      Element interfaces = osgiService.addElement("osgi:interfaces");
49                      for (String name : interfaceNames)
50                      {
51                          ensureExported(name, context);
52                          Element e = interfaces.addElement("beans:value");
53                          e.setText(name);
54                      }
55                  }
56              }
57              if (root.elements().size() > 0)
58              {
59                  context.getFileOverrides().put(SPRING_XML, SpringHelper.documentToBytes(springDoc));
60              }
61          }
62      }
63  
64      void ensureExported(String className, TransformContext context)
65      {
66          String pkg = className.substring(0, className.lastIndexOf('.'));
67          if (!context.getExtraExports().contains(pkg))
68          {
69              String fileName = className.replace('.','/') + ".class";
70              
71              if (context.getPluginArtifact().doesResourceExist(fileName))
72              {
73                  context.getExtraExports().add(pkg);
74              }
75          }
76      }
77  
78  }