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 org.dom4j.Document;
6   import org.dom4j.DocumentHelper;
7   import org.dom4j.Element;
8   import org.dom4j.Namespace;
9   import org.dom4j.QName;
10  import org.dom4j.io.OutputFormat;
11  import org.dom4j.io.XMLWriter;
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  import java.io.ByteArrayOutputStream;
16  import java.io.IOException;
17  
18  /**
19   * Helper class for dealing with spring files
20   *
21   * @since 2.2.0
22   */
23  class SpringHelper {
24  
25      private static final Logger log = LoggerFactory.getLogger(SpringHelper.class);
26  
27      /**
28       * Creates a basic spring document with the usual namespaces
29       *
30       * @return An empty spring XML configuration file with namespaces
31       */
32      static Document createSpringDocument() {
33          final Document springDoc = DocumentHelper.createDocument();
34          final Element root = springDoc.addElement("beans");
35  
36          root.addNamespace("beans", "http://www.springframework.org/schema/beans");
37          root.addNamespace("osgi", "http://www.eclipse.org/gemini/blueprint/schema/blueprint");
38          root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
39          root.addAttribute(new QName("schemaLocation", new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")),
40                  "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\n" +
41                          "http://www.eclipse.org/gemini/blueprint/schema/blueprint http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd");
42          root.setName("beans:beans");
43          root.addAttribute("default-autowire", "constructor");
44          root.addAttribute("osgi:default-timeout", "30000");
45          return springDoc;
46      }
47  
48      /**
49       * Converts an XML document into a byte array
50       *
51       * @param doc The document
52       * @return A byte array of the contents
53       */
54      static byte[] documentToBytes(final Document doc) {
55  
56          final ByteArrayOutputStream bout = new ByteArrayOutputStream();
57          final OutputFormat format = OutputFormat.createPrettyPrint();
58  
59          try {
60              final XMLWriter writer = new XMLWriter(bout, format);
61              writer.write(doc);
62          } catch (final IOException e) {
63              throw new PluginTransformationException("Unable to print generated Spring XML", e);
64          }
65  
66          return bout.toByteArray();
67      }
68  
69      /**
70       * Determines if the file should be generated, based on whether it already exists in the context or not
71       *
72       * @param context The transformation context
73       * @param path    The path of the file
74       * @return True if not present, false otherwise
75       */
76      static boolean shouldGenerateFile(final TransformContext context, final String path) {
77          if (context.getPluginJarEntry(path) == null) {
78              log.debug("File " + path + " not present, generating");
79              return true;
80          } else {
81              log.debug("File " + path + " already exists in jar, skipping generation");
82              return false;
83          }
84      }
85  }