View Javadoc

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