View Javadoc

1   package com.atlassian.plugin.repositories;
2   
3   import com.atlassian.plugin.PluginInstaller;
4   import com.atlassian.plugin.PluginArtifact;
5   
6   import org.apache.commons.io.IOUtils;
7   import org.apache.commons.lang.Validate;
8   
9   import java.io.File;
10  import java.io.FileOutputStream;
11  import java.io.IOException;
12  import java.io.OutputStream;
13  
14  /**
15   * Simple implementation of a PluginInstaller which writes plugin artifact
16   * to a specified directory.
17   *
18   * @see PluginInstaller
19   */
20  public class FilePluginInstaller implements PluginInstaller
21  {
22      private File directory;
23  
24      /**
25       * @param directory where plugin JARs will be installed.
26       */
27      public FilePluginInstaller(File directory)
28      {
29          Validate.isTrue(directory != null && directory.exists(), "The plugin installation directory must exist");
30          this.directory = directory;
31      }
32  
33      /**
34       * If there is an existing JAR with the same filename, it is replaced.
35       *
36       * @throws RuntimeException if there was an exception reading or writing files.
37       */
38      public void installPlugin(String key, PluginArtifact pluginArtifact)
39      {
40          Validate.notNull(key, "The plugin key must be specified");
41          Validate.notNull(pluginArtifact, "The plugin artifact must not be null");
42          
43          File newPluginFile = new File(directory, pluginArtifact.getName());
44          if (newPluginFile.exists())
45              newPluginFile.delete();
46  
47          OutputStream os = null;
48          try
49          {
50              os= new FileOutputStream(newPluginFile);
51              IOUtils.copy(pluginArtifact.getInputStream(), os);
52          }
53          catch (IOException e)
54          {
55              throw new RuntimeException("Could not install plugin: " + pluginArtifact, e);
56          }
57          finally
58          {
59              IOUtils.closeQuietly(os);
60          }
61      }
62  }