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