View Javadoc

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