View Javadoc

1   package com.atlassian.plugin.util.resource;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.net.MalformedURLException;
10  import java.net.URL;
11  import java.util.ArrayList;
12  import java.util.Collections;
13  import java.util.List;
14  
15  /**
16   * Loads resources from directories configured via the system property {@code plugin.resource.directories}, which should
17   * be a comma-delimited list of file paths that contain resources to load.
18   *
19   * @since 2.2.0
20   */
21  public class AlternativeDirectoryResourceLoader implements AlternativeResourceLoader
22  {
23      private final List<File> resourceDirectories;
24      private static final Logger log = LoggerFactory.getLogger(AlternativeDirectoryResourceLoader.class);
25      public static final String PLUGIN_RESOURCE_DIRECTORIES = "plugin.resource.directories";
26  
27      public AlternativeDirectoryResourceLoader()
28      {
29          String dirs = System.getProperty(PLUGIN_RESOURCE_DIRECTORIES);
30          List<File> tmp = new ArrayList<File>();
31          if (dirs != null)
32          {
33              for (String dir : dirs.split(","))
34              {
35                  dir = dir.trim();
36                  File file = new File(dir);
37                  if (file.exists())
38                  {
39                      log.debug("Found alternative resource directory "+dir);
40                      tmp.add(file);
41                  }
42                  else
43                  {
44                      log.warn("Resource directory "+dir+", which resolves to "+file.getAbsolutePath()+" does not exist");
45                  }
46              }
47          }
48  
49          resourceDirectories = Collections.unmodifiableList(tmp);
50      }
51      /**
52       * Retrieve the URL of the resource from the directories.
53       *
54       * @param path the name of the resource to be loaded
55       * @return The URL to the resource, or null if the resource is not found
56       */
57      public URL getResource(String path)
58      {
59          for (File dir : resourceDirectories)
60          {
61              File file = new File(dir, path);
62              if (file.exists())
63              {
64                  try
65                  {
66                      return file.toURL();
67                  }
68                  catch (MalformedURLException e)
69                  {
70                      log.error("Malformed URL: "+file.toString(), e);
71                  }
72              }
73              else
74              {
75                  log.debug("File "+file+" not found, ignoring");
76              }
77          }
78          return null;
79      }
80  
81      /**
82       * Load a given resource from the directories.
83       *
84       * @param name The name of the resource to be loaded.
85       * @return An InputStream for the resource, or null if the resource is not found.
86       */
87      public InputStream getResourceAsStream(String name)
88      {
89          URL url = getResource(name);
90          if (url != null)
91          {
92              try
93              {
94                  return url.openStream();
95              }
96              catch (IOException e)
97              {
98                  log.error("Unable to open URL "+url, e);
99              }
100         }
101         return null;
102     }
103 
104     public List<File> getResourceDirectories()
105     {
106         return resourceDirectories;
107     }
108 
109 
110 }