View Javadoc
1   package com.atlassian.plugin.spring.scanner.runtime.impl.util;
2   
3   import com.atlassian.plugin.spring.scanner.ProductFilter;
4   import com.atlassian.plugin.spring.scanner.util.CommonConstants;
5   import com.google.common.base.Charsets;
6   import org.osgi.framework.Bundle;
7   import org.osgi.framework.BundleContext;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  import java.io.BufferedReader;
12  import java.io.FileNotFoundException;
13  import java.io.IOException;
14  import java.io.InputStreamReader;
15  import java.net.URL;
16  import java.util.ArrayList;
17  import java.util.List;
18  import java.util.Properties;
19  
20  /**
21   * A utility class to read index files from a classloader or bundle.
22   * Can handle reading a single index file, or concating multiple index files based on the
23   * currently running product.
24   */
25  public class AnnotationIndexReader {
26      private static final Logger log = LoggerFactory.getLogger(AnnotationIndexReader.class);
27  
28      /**
29       * Read a file from a bundle and return the list of lines of the file.
30       *
31       * @param resourceFile the path to the file in the bundle
32       * @param bundle       the bundle to read from
33       * @return the lines in the file
34       */
35      public static List<String> readIndexFile(final String resourceFile, final Bundle bundle) {
36          final URL url = bundle.getEntry(resourceFile);
37          return readIndexFile(url);
38      }
39  
40      /**
41       * Read both the cross-product file and the product-specific file for the currently running product from the bundle and returns
42       * their lines as a single list.
43       *
44       * @param resourceFile the path to the cross-product file in the bundle
45       * @param bundle       the bundle to read from
46       * @param bundleContext the bundle context used to determine the running product
47       * @return the lines in the file
48       */
49      public static List<String> readAllIndexFilesForProduct(final String resourceFile, final Bundle bundle,
50                                                             final BundleContext bundleContext) {
51          final List<String> entries = new ArrayList<String>();
52  
53          final URL url = bundle.getEntry(resourceFile);
54  
55          entries.addAll(readIndexFile(url));
56  
57          final ProductFilter filter = ProductFilterUtil.getFilterForCurrentProduct(bundleContext);
58          if (null != filter) {
59              entries.addAll(readIndexFile(filter.getPerProductFile(resourceFile), bundle));
60          }
61  
62          return entries;
63      }
64  
65      public static List<String> readIndexFile(final URL url) {
66          final List<String> resources = new ArrayList<String>();
67  
68          try {
69              if (null == url) {
70                  log.debug("Could not find annotation index file (null url).");
71                  return resources;
72              }
73  
74              final BufferedReader reader;
75              try {
76                  reader = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8));
77              } catch (final FileNotFoundException e) {
78                  log.debug("Could not find annotation index file " + url);
79                  return resources;
80              }
81  
82              String line = reader.readLine();
83              while (line != null) {
84                  resources.add(line);
85  
86                  line = reader.readLine();
87              }
88  
89              if (log.isDebugEnabled()) {
90                  log.debug("Read annotation index file: " + url);
91                  log.debug("Printing out found annotated beans: ");
92                  log.debug(resources.toString());
93              }
94  
95              reader.close();
96          } catch (final IOException e) {
97              throw new RuntimeException("Cannot read index file [" + url.toString() + "]", e);
98          }
99  
100         return resources;
101     }
102 
103     public static Properties readPropertiesFile(final URL url) {
104         final Properties resources = new Properties();
105         try {
106             if (null == url) {
107                 return resources;
108             }
109 
110             final BufferedReader reader;
111             try {
112                 reader = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8));
113             } catch (final FileNotFoundException e) {
114                 return resources;
115             }
116 
117             resources.load(reader);
118         } catch (final IOException e) {
119             throw new RuntimeException("Cannot read properties file [" + url.toString() + "]", e);
120         }
121         return resources;
122     }
123 
124     public static String[] splitProfiles(final String profiles) {
125         return (profiles != null && !profiles.trim().isEmpty()) ? profiles.split(",") : new String[0];
126     }
127 
128     public static Iterable<String> getIndexFilesForProfiles(final String[] profileNames, final String indexFileName) {
129         final List<String> filesToRead = new ArrayList<String>();
130         if (profileNames.length > 0) {
131             for (String profileName : profileNames) {
132                 profileName = ((profileName == null) ? "" : profileName).trim();
133                 if (!profileName.isEmpty()) {
134                     final String fileToRead = CommonConstants.INDEX_FILES_DIR + "/"
135                             + CommonConstants.PROFILE_PREFIX + profileName + "/"
136                             + indexFileName;
137 
138                     filesToRead.add(fileToRead);
139                 }
140             }
141         } else {
142             final String fileToRead = CommonConstants.INDEX_FILES_DIR + "/" + indexFileName;
143             filesToRead.add(fileToRead);
144         }
145         return filesToRead;
146     }
147 }
148