View Javadoc

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