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
22
23
24
25 public class AnnotationIndexReader {
26 private static final Logger log = LoggerFactory.getLogger(AnnotationIndexReader.class);
27
28
29
30
31
32
33
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
42
43
44
45
46
47
48
49 public static List<String> readAllIndexFilesForProduct(final String resourceFile, final Bundle bundle,
50 final BundleContext bundleContext) {
51
52 final URL url = bundle.getEntry(resourceFile);
53
54 final List<String> entries = new ArrayList<String>(readIndexFile(url));
55
56 final ProductFilter filter = ProductFilterUtil.getFilterForCurrentProduct(bundleContext);
57 if (null != filter) {
58 entries.addAll(readIndexFile(filter.getPerProductFile(resourceFile), bundle));
59 }
60
61 return entries;
62 }
63
64 public static List<String> readIndexFile(final URL url) {
65 final List<String> resources = new ArrayList<String>();
66
67 try {
68 if (null == url) {
69 log.debug("Could not find annotation index file (null url).");
70 return resources;
71 }
72
73 final BufferedReader reader;
74 try {
75 reader = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8));
76 } catch (final FileNotFoundException e) {
77 log.debug("Could not find annotation index file " + url);
78 return resources;
79 }
80
81 String line = reader.readLine();
82 while (line != null) {
83 resources.add(line);
84
85 line = reader.readLine();
86 }
87
88 if (log.isDebugEnabled()) {
89 log.debug("Read annotation index file: " + url);
90 log.debug("Printing out found annotated beans: ");
91 log.debug(resources.toString());
92 }
93
94 reader.close();
95 } catch (final IOException e) {
96 throw new RuntimeException("Cannot read index file [" + url.toString() + "]", e);
97 }
98
99 return resources;
100 }
101
102 public static Properties readPropertiesFile(final URL url) {
103 final Properties resources = new Properties();
104 try {
105 if (null == url) {
106 return resources;
107 }
108
109 final BufferedReader reader;
110 try {
111 reader = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8));
112 } catch (final FileNotFoundException e) {
113 return resources;
114 }
115
116 resources.load(reader);
117 } catch (final IOException e) {
118 throw new RuntimeException("Cannot read properties file [" + url.toString() + "]", e);
119 }
120 return resources;
121 }
122
123 public static String[] splitProfiles(final String profiles) {
124 return (profiles != null && !profiles.trim().isEmpty()) ? profiles.split(",") : new String[0];
125 }
126
127 public static Iterable<String> getIndexFilesForProfiles(final String[] profileNames, final String indexFileName) {
128 final List<String> filesToRead = new ArrayList<String>();
129 if (profileNames.length > 0) {
130 for (String profileName : profileNames) {
131 profileName = ((profileName == null) ? "" : profileName).trim();
132 if (!profileName.isEmpty()) {
133 final String fileToRead = CommonConstants.INDEX_FILES_DIR + "/"
134 + CommonConstants.PROFILE_PREFIX + profileName + "/"
135 + indexFileName;
136
137 filesToRead.add(fileToRead);
138 }
139 }
140 } else {
141 final String fileToRead = CommonConstants.INDEX_FILES_DIR + "/" + indexFileName;
142 filesToRead.add(fileToRead);
143 }
144 return filesToRead;
145 }
146 }
147