View Javadoc

1   package com.atlassian.plugin.util.resource;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
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 Log log = LogFactory.getLog(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                  File file = new File(dir);
36                  if (file.exists())
37                  {
38                      log.info("Found alternative resource directory "+dir);
39                      tmp.add(file);
40                  }
41                  else
42                  {
43                      log.warn("Resource directory "+dir+", which resolves to "+file.getAbsolutePath()+" does not exist");
44                  }
45              }
46          }
47  
48          resourceDirectories = Collections.unmodifiableList(tmp);
49      }
50      /**
51       * Retrieve the URL of the resource from the directories.
52       *
53       * @param path the name of the resource to be loaded
54       * @return The URL to the resource, or null if the resource is not found
55       */
56      public URL getResource(String path)
57      {
58          for (File dir : resourceDirectories)
59          {
60              File file = new File(dir, path);
61              if (file.exists())
62              {
63                  try
64                  {
65                      return file.toURL();
66                  }
67                  catch (MalformedURLException e)
68                  {
69                      log.error("Malformed URL: "+file.toString(), e);
70                  }
71              }
72              else
73              {
74                  log.debug("File "+file+" not found, ignoring");
75              }
76          }
77          return null;
78      }
79  
80      /**
81       * Load a given resource from the directories.
82       *
83       * @param name The name of the resource to be loaded.
84       * @return An InputStream for the resource, or null if the resource is not found.
85       */
86      public InputStream getResourceAsStream(String name)
87      {
88          URL url = getResource(name);
89          if (url != null)
90          {
91              try
92              {
93                  return url.openStream();
94              }
95              catch (IOException e)
96              {
97                  log.error("Unable to open URL "+url, e);
98              }
99          }
100         return null;
101     }
102 
103     public List<File> getResourceDirectories()
104     {
105         return resourceDirectories;
106     }
107 
108 
109 }