View Javadoc

1   package com.atlassian.plugin.osgi.factory.transform;
2   
3   import com.atlassian.plugin.PluginParseException;
4   import com.atlassian.plugin.osgi.hostcomponents.HostComponentRegistration;
5   import com.atlassian.plugin.parsers.XmlDescriptorParser;
6   import org.codehaus.classworlds.uberjar.protocol.jar.NonLockingJarHandler;
7   import org.dom4j.Document;
8   import org.apache.commons.lang.Validate;
9   
10  import java.io.File;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.net.MalformedURLException;
14  import java.net.URL;
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.jar.JarFile;
20  import java.util.jar.Manifest;
21  
22  /**
23   * The transform context containing any configuration necessary to enact a JAR transformation
24   *
25   * @since 2.2.0
26   */
27  public class TransformContext
28  {
29      private final File pluginFile;
30      private final JarFile pluginJar;
31      private final Manifest manifest;
32      private final List<HostComponentRegistration> regs;
33      private final Map<String, byte[]> fileOverrides;
34      private final Map<String, String> bndInstructions;
35      private final Document descriptorDocument;
36      private final List<String> extraImports;
37  
38      public TransformContext(List<HostComponentRegistration> regs, File pluginFile, String descriptorPath)
39      {
40          Validate.notNull(pluginFile, "The plugin file must be specified");
41          Validate.notNull(descriptorPath, "The plugin descriptor path must be specified");
42  
43          this.regs = regs;
44          this.pluginFile = pluginFile;
45          try
46          {
47              this.pluginJar = new JarFile(pluginFile);
48              this.manifest = pluginJar.getManifest();
49          }
50          catch (IOException e)
51          {
52              throw new IllegalArgumentException("File must be a jar", e);
53          }
54          fileOverrides = new HashMap<String, byte[]>();
55          bndInstructions = new HashMap<String, String>();
56          this.descriptorDocument = retrieveDocFromJar(pluginFile, descriptorPath);
57          this.extraImports = new ArrayList<String>();
58      }
59  
60      private Document retrieveDocFromJar(File pluginFile, String descriptorPath) throws PluginTransformationException
61      {
62          URL atlassianPluginsXmlUrl;
63          try
64          {
65              atlassianPluginsXmlUrl = new URL(new URL("jar:file:" + pluginFile.getAbsolutePath() + "!/"), descriptorPath, NonLockingJarHandler.getInstance());
66          }
67          catch (MalformedURLException e)
68          {
69              throw new PluginTransformationException(e);
70          }
71  
72          Document descriptorDocument;
73          InputStream descriptorStream;
74          try
75          {
76              descriptorStream = atlassianPluginsXmlUrl.openStream();
77              DocumentExposingDescriptorParser parser = new DocumentExposingDescriptorParser(descriptorStream);
78              descriptorDocument = parser.getDocument();
79          }
80          catch (IOException e)
81          {
82              throw new PluginTransformationException("Unable to access descriptor " + descriptorPath, e);
83          }
84          return descriptorDocument;
85      }
86  
87      public File getPluginFile()
88      {
89          return pluginFile;
90      }
91  
92      public JarFile getPluginJar()
93      {
94          return pluginJar;
95      }
96  
97      public List<HostComponentRegistration> getHostComponentRegistrations()
98      {
99          return regs;
100     }
101 
102     public Map<String, byte[]> getFileOverrides()
103     {
104         return fileOverrides;
105     }
106 
107     public Map<String, String> getBndInstructions()
108     {
109         return bndInstructions;
110     }
111 
112     public Document getDescriptorDocument()
113     {
114         return descriptorDocument;
115     }
116 
117     public Manifest getManifest()
118     {
119         return manifest;
120     }
121 
122     public List<String> getExtraImports()
123     {
124         return extraImports;
125     }
126 
127     private static class DocumentExposingDescriptorParser extends XmlDescriptorParser
128     {
129         /**
130          * @throws com.atlassian.plugin.PluginParseException
131          *          if there is a problem reading the descriptor from the XML {@link java.io.InputStream}.
132          */
133         public DocumentExposingDescriptorParser(InputStream source) throws PluginParseException
134         {
135             super(source);
136         }
137 
138         @Override
139         public Document getDocument()
140         {
141             return super.getDocument();
142         }
143     }
144 }