1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.bugzilla;
17
18 import java.io.BufferedInputStream;
19 import java.io.InputStream;
20 import java.util.List;
21
22 import org.apache.commons.io.FilenameUtils;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.maven.plugin.MojoExecutionException;
25
26 import de.smartics.maven.issue.config.Component;
27 import de.smartics.maven.issue.config.Configuration;
28 import de.smartics.maven.issue.config.ConfigurationLoader;
29
30
31
32
33 public final class ComponentDescriptionLoader
34 {
35
36
37
38
39
40
41
42
43
44
45
46
47 private static final String SYSTEM_CONFIG_PREFIX =
48 "bugzilla-configuration-system";
49
50
51
52
53
54
55 private final String configurationName;
56
57
58
59
60 private final String locale;
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public ComponentDescriptionLoader(final String configurationName,
75 final String locale)
76 {
77 this.configurationName = configurationName;
78 this.locale = locale;
79 }
80
81
82
83
84
85
86
87
88
89
90
91 List<Component> loadDefaultComponents() throws MojoExecutionException
92 {
93 final InputStream input = openDefaultComponentsStream();
94
95 try
96 {
97 final ConfigurationLoader loader = new ConfigurationLoader();
98 final Configuration configuration = loader.load(input);
99 return configuration.getDefaultComponents();
100 }
101 finally
102 {
103 IOUtils.closeQuietly(input);
104 }
105 }
106
107 private InputStream openDefaultComponentsStream()
108 throws MojoExecutionException
109 {
110 final ClassLoader classLoader =
111 Thread.currentThread().getContextClassLoader();
112 InputStream in = openStream(classLoader);
113
114 if (in == null)
115 {
116 in = openStream(classLoader, SYSTEM_CONFIG_PREFIX);
117 }
118
119 if (in != null)
120 {
121 return new BufferedInputStream(in);
122 }
123
124 throw new MojoExecutionException(
125 "Cannot find configuration for default components. Neither resource '"
126 + configurationName + "' nor the system default resource '"
127 + SYSTEM_CONFIG_PREFIX + ".xml' has been found on the class path.");
128 }
129
130 private InputStream openStream(final ClassLoader classLoader)
131 {
132 final String stem = FilenameUtils.getBaseName(configurationName);
133 return openStream(classLoader, stem);
134 }
135
136 private InputStream openStream(final ClassLoader classLoader,
137 final String stem)
138 {
139 final String localizedName = stem + '-' + locale + ".xml";
140 InputStream in = classLoader.getResourceAsStream(localizedName);
141 if (in == null)
142 {
143 in = classLoader.getResourceAsStream(stem + "-en.xml");
144 }
145 return in;
146 }
147
148
149
150 }