1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jadira.scanner;
17
18 import java.net.URL;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22
23 import org.jadira.scanner.classpath.filter.NameFilter;
24 import org.jadira.scanner.core.api.Filter;
25 import org.jadira.scanner.core.api.Locator;
26 import org.jadira.scanner.core.exception.ClasspathAccessException;
27 import org.jadira.scanner.core.utils.reflection.ClassLoaderUtils;
28
29 public class ConfigurationBuilder implements Configuration {
30
31 private List<URL> urls = new ArrayList<URL>();
32
33 private List<Locator<URL>> locators = new ArrayList<Locator<URL>>();
34
35 private List<ClassLoader> classLoaders = new ArrayList<ClassLoader>();
36
37 private List<Filter<?>> filters = new ArrayList<Filter<?>>();
38
39 public ConfigurationBuilder() {
40 ClassLoader[] cls = ClassLoaderUtils.getClassLoaders();
41 for (ClassLoader next : cls) {
42 classLoaders.add(next);
43 }
44 }
45
46 public static ConfigurationBuilder build(final Object... params) {
47
48 ConfigurationBuilder builder = new ConfigurationBuilder();
49 List<Object> paramsList = flattenParams(params);
50
51 List<ClassLoader> cLoaders = new ArrayList<ClassLoader>();
52
53 for (Object param : paramsList) {
54 if (param instanceof ClassLoader) {
55 cLoaders.add((ClassLoader) param);
56 } else if (param instanceof URL) {
57 builder.addUrls((URL) param);
58 } else if (param instanceof Locator) {
59 @SuppressWarnings("unchecked") final Locator<URL>[] myParams = new Locator[] {(Locator<URL>)param};
60 builder.addLocators(myParams);
61 } else if (param instanceof Filter) {
62 builder.addFilters((Filter<?>) param);
63 } else if (param instanceof String) {
64 builder.addFilters(new NameFilter((String)param));
65 } else {
66 throw new ClasspathAccessException("Could not handle builder parameter " + param.toString());
67 }
68
69 }
70
71 builder.setClassLoaders(cLoaders);
72 return builder;
73 }
74
75 private static List<Object> flattenParams(final Object... params) {
76 List<Object> paramsList = new ArrayList<Object>(params.length);
77 if (params != null) {
78 for (Object param : params) {
79 if (param != null) {
80 if (param.getClass().isArray()) {
81 for (Object nextEntry : (Object[]) param) {
82 if (nextEntry != null) {
83 paramsList.add(nextEntry);
84 }
85 }
86 }
87 else if (param instanceof Iterable) {
88 for (Object nextEntry : (Iterable<?>) param) {
89 if (nextEntry != null) {
90 paramsList.add(nextEntry); }
91 }
92 }
93 else {
94 paramsList.add(param);
95 }
96 }
97 }
98 }
99 return paramsList;
100 }
101
102 public Scanner build() {
103 return new Scanner(this);
104 }
105
106 @Override
107 public List<URL> getUrls() {
108 return urls;
109 }
110
111 ConfigurationBuilder setUrls(final Collection<URL> urls) {
112 this.urls = new ArrayList<URL>(urls);
113 return this;
114 }
115
116 ConfigurationBuilder setUrls(final URL... urls) {
117 this.urls = new ArrayList<URL>(urls.length);
118 for(URL next : urls) {
119 this.urls.add(next);
120 }
121 return this;
122 }
123
124 ConfigurationBuilder addUrls(final Collection<URL> urls) {
125 this.urls.addAll(urls);
126 return this;
127 }
128
129 ConfigurationBuilder addUrls(final URL... urls) {
130 for(URL next : urls) {
131 this.urls.add(next);
132 }
133 return this;
134 }
135
136 @Override
137 public List<Locator<URL>> getLocators() {
138 return locators;
139 }
140
141 ConfigurationBuilder setLocators(final Collection<Locator<URL>> locators) {
142 this.locators = new ArrayList<Locator<URL>>(locators);
143 return this;
144 }
145
146 @SafeVarargs
147 final ConfigurationBuilder setLocators(final Locator<URL>... locators) {
148 this.locators = new ArrayList<Locator<URL>>(locators.length);
149 for (Locator<URL> next : locators) {
150 this.locators.add(next);
151 }
152 return this;
153 }
154
155 ConfigurationBuilder addLocators(final Collection<Locator<URL>> locators) {
156 this.locators.addAll(locators);
157 return this;
158 }
159
160 @SafeVarargs
161 final ConfigurationBuilder addLocators(final Locator<URL>... locators) {
162 for (Locator<URL> next : locators) {
163 this.locators.add(next);
164 }
165 return this;
166 }
167
168 @Override
169 public List<ClassLoader> getClassLoaders() {
170 return classLoaders;
171 }
172
173 ConfigurationBuilder setClassLoaders(final Collection<ClassLoader> classLoaders) {
174 this.classLoaders = new ArrayList<ClassLoader>(classLoaders);
175 return this;
176 }
177
178 ConfigurationBuilder setClassLoaders(final ClassLoader... classLoaders) {
179 this.classLoaders = new ArrayList<ClassLoader>(classLoaders.length);
180 for (ClassLoader next : classLoaders) {
181 this.classLoaders.add(next);
182 }
183 return this;
184 }
185
186 ConfigurationBuilder addClassLoaders(final Collection<ClassLoader> classLoaders) {
187 this.classLoaders.addAll(classLoaders);
188 return this;
189 }
190
191 ConfigurationBuilder addClassLoaders(final ClassLoader... classLoaders) {
192 for (ClassLoader next : classLoaders) {
193 this.classLoaders.add(next);
194 }
195 return this;
196 }
197
198 @Override
199 public List<Filter<?>> getFilters() {
200 return filters;
201 }
202
203 ConfigurationBuilder setFilters(final List<Filter<?>> filters) {
204 this.filters = new ArrayList<Filter<?>>(filters);
205 return this;
206 }
207
208 ConfigurationBuilder setFilters(final Filter<?>... filters) {
209 this.filters = new ArrayList<Filter<?>>(filters.length);
210 for (Filter<?> next : filters) {
211 this.filters.add(next);
212 }
213 return this;
214 }
215
216 ConfigurationBuilder addFilters(final List<Filter<?>> filters) {
217 this.filters.addAll(filters);
218 return this;
219 }
220
221 ConfigurationBuilder addFilters(final Filter<?>... filters) {
222 for (Filter<?> next : filters) {
223 this.filters.add(next);
224 }
225 return this;
226 }
227 }