View Javadoc

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