25 |
25 |
import java.text.MessageFormat;
|
26 |
26 |
import java.util.ArrayList;
|
27 |
27 |
import java.util.Collection;
|
28 |
|
import java.util.HashMap;
|
|
28 |
import java.util.Comparator;
|
29 |
29 |
import java.util.HashSet;
|
30 |
30 |
import java.util.Iterator;
|
31 |
|
import java.util.LinkedHashSet;
|
32 |
31 |
import java.util.List;
|
33 |
32 |
import java.util.Map;
|
34 |
33 |
import java.util.Set;
|
|
34 |
import java.util.TreeMap;
|
35 |
35 |
|
36 |
36 |
import org.slf4j.Logger;
|
37 |
37 |
import org.slf4j.LoggerFactory;
|
... | ... | |
50 |
50 |
* @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
|
51 |
51 |
*/
|
52 |
52 |
public abstract class AbstractLibrariesInitializer implements
|
53 |
|
LibrariesInitializer {
|
|
53 |
LibrariesInitializer, Library.TYPE {
|
54 |
54 |
|
55 |
55 |
private static final Logger LOG = LoggerFactory
|
56 |
56 |
.getLogger(AbstractLibrariesInitializer.class);
|
... | ... | |
227 |
227 |
} else {
|
228 |
228 |
if (e instanceof RuntimeException) {
|
229 |
229 |
throw (RuntimeException) e;
|
230 |
|
} else
|
231 |
|
if (e instanceof Error) {
|
232 |
|
throw (Error) e;
|
233 |
|
}
|
|
230 |
} else if (e instanceof Error) {
|
|
231 |
throw (Error) e;
|
|
232 |
}
|
234 |
233 |
}
|
235 |
234 |
}
|
236 |
235 |
|
... | ... | |
247 |
246 |
LOG.info(buffer.toString());
|
248 |
247 |
}
|
249 |
248 |
|
250 |
|
private static class OrderedLibs extends LinkedHashSet {
|
|
249 |
private static class OrderedLibs extends ArrayList {
|
251 |
250 |
|
252 |
251 |
private static final long serialVersionUID = -8546268624773345053L;
|
253 |
|
private Map organizedLibs = new HashMap();
|
254 |
252 |
|
255 |
|
OrderedLibs(Set libs) {
|
256 |
|
Iterator iterator = libs.iterator();
|
257 |
|
while (iterator.hasNext()) {
|
258 |
|
Library lib = (Library) iterator.next();
|
259 |
|
addLibrary(lib);
|
260 |
|
}
|
261 |
|
iterator = libs.iterator();
|
262 |
|
while (iterator.hasNext()) {
|
263 |
|
Library lib = (Library) iterator.next();
|
264 |
|
if (lib.getLibrary() != null && lib.getType() != null) {
|
265 |
|
// Always add services
|
266 |
|
if (Library.TYPE.SERVICE.equals(lib.getType())) {
|
267 |
|
this.add(lib);
|
268 |
|
} else {
|
269 |
|
String libName = getLibName(lib);
|
270 |
|
if (this.organizedLibs.containsKey(libName)) {
|
271 |
|
Library storeLib =
|
272 |
|
(Library) this.organizedLibs.get(libName);
|
273 |
|
// Only add a library if it has the greatest
|
274 |
|
// priority
|
275 |
|
if (lib.getPriority() >= storeLib.getPriority()) {
|
276 |
|
this.add(lib);
|
277 |
|
}
|
278 |
|
}
|
279 |
|
}
|
280 |
|
} else {
|
281 |
|
this.add(lib);
|
|
253 |
// By default order alphabetically
|
|
254 |
private Map apis;
|
|
255 |
|
|
256 |
public OrderedLibs(Set libs) {
|
|
257 |
apis = new TreeMap(new Comparator() {
|
|
258 |
|
|
259 |
public int compare(Object o1, Object o2) {
|
|
260 |
Class class1 = (Class) o1;
|
|
261 |
Class class2 = (Class) o2;
|
|
262 |
return class1.getName().compareTo(class2.getName());
|
282 |
263 |
}
|
283 |
|
}
|
|
264 |
});
|
|
265 |
orderLibs(libs);
|
284 |
266 |
}
|
285 |
267 |
|
286 |
|
private String getLibName(Library lib) {
|
287 |
|
return lib.getLibrary().getName() + "-" + lib.getType();
|
|
268 |
private void orderLibs(Set libs) {
|
|
269 |
// First create the groups of libraries, internally ordered:
|
|
270 |
// 1: api, 2: impl, 3: serv1, 4: serv2, ...
|
|
271 |
// (servs ordered by dependency)
|
|
272 |
for (Iterator iterator = libs.iterator(); iterator.hasNext();) {
|
|
273 |
Library library = (Library) iterator.next();
|
|
274 |
Class API = library.getLibrary();
|
|
275 |
if (API == null) {
|
|
276 |
API = library.getClass();
|
|
277 |
}
|
|
278 |
List libraryGroup = (List) apis.get(API);
|
|
279 |
if (libraryGroup == null) {
|
|
280 |
libraryGroup = new ArrayList();
|
|
281 |
apis.put(API, libraryGroup);
|
|
282 |
}
|
|
283 |
addLibraryToGroup(libraryGroup, library);
|
|
284 |
}
|
|
285 |
|
|
286 |
Set alreadyAddedGroups = new HashSet();
|
|
287 |
for (Iterator iterator = apis.keySet().iterator(); iterator
|
|
288 |
.hasNext();) {
|
|
289 |
Class APIRequired = (Class) iterator.next();
|
|
290 |
addFromLibraryGroup(APIRequired, alreadyAddedGroups);
|
|
291 |
}
|
288 |
292 |
}
|
289 |
293 |
|
290 |
|
private void addLibrary(Library lib) {
|
291 |
|
if (lib.getLibrary() != null && lib.getType() != null) {
|
292 |
|
String libName = getLibName(lib);
|
|
294 |
private void addLibraryToGroup(List libraryGroup, Library library) {
|
|
295 |
String type = library.getType();
|
293 |
296 |
|
294 |
|
if (Library.TYPE.SERVICE.equals(lib.getType())) {
|
295 |
|
Set serviceLibs;
|
296 |
|
if (this.organizedLibs.containsKey(libName)) {
|
297 |
|
serviceLibs = (Set) this.organizedLibs.get(libName);
|
298 |
|
} else {
|
299 |
|
serviceLibs = new HashSet();
|
300 |
|
this.organizedLibs.put(libName, serviceLibs);
|
301 |
|
}
|
302 |
|
serviceLibs.add(lib);
|
303 |
|
// Also add the library as itself of type implementation,
|
304 |
|
// just in case another library depends on it. Only for
|
305 |
|
// libraries whose type is SERVICE.
|
306 |
|
this.organizedLibs.put(lib.getClass().getName() + "-"
|
307 |
|
+ Library.TYPE.IMPL, lib);
|
308 |
|
} else
|
309 |
|
if (this.organizedLibs.containsKey(libName)) {
|
|
297 |
// If first, don't look anything else and add it
|
|
298 |
if (libraryGroup.size() == 0) {
|
|
299 |
libraryGroup.add(library);
|
|
300 |
return;
|
|
301 |
}
|
310 |
302 |
|
311 |
|
Library previousLibrary =
|
312 |
|
(Library) this.organizedLibs.get(libName);
|
313 |
|
LOG.warn(
|
314 |
|
"There's already a library initialized with the "
|
315 |
|
+ "name '{}': '{}' with priority '{}'"
|
316 |
|
+ ", and '{}' with priority '{}",
|
317 |
|
new Object[] { libName,
|
318 |
|
previousLibrary.getClass().getName(),
|
319 |
|
String.valueOf(previousLibrary.getPriority()),
|
320 |
|
lib.getClass().getName(),
|
321 |
|
String.valueOf(lib.getPriority()) });
|
322 |
|
|
323 |
|
if (lib.getPriority() < previousLibrary.getPriority()) {
|
324 |
|
LOG.info(
|
325 |
|
"Old library '{}' with priority '{}' was chosen.",
|
326 |
|
previousLibrary.getClass().getName(),
|
327 |
|
String.valueOf(previousLibrary.getPriority()));
|
|
303 |
if (API.equals(type)) {
|
|
304 |
// API library must go first
|
|
305 |
libraryGroup.add(0, library);
|
|
306 |
} else if (IMPL.equals(type)) {
|
|
307 |
// Look for other implementation an replace it if priority
|
|
308 |
// is higher, or discard
|
|
309 |
for (int i = 0; i < libraryGroup.size(); i++) {
|
|
310 |
Library current = (Library) libraryGroup.get(i);
|
|
311 |
if (IMPL.equals(current.getType())) {
|
|
312 |
if (current.getPriority() < library.getPriority()) {
|
|
313 |
libraryGroup.set(i, library);
|
|
314 |
return;
|
328 |
315 |
} else {
|
329 |
|
this.organizedLibs.put(libName, lib);
|
330 |
|
LOG.info(
|
331 |
|
"New library '{}' with priority '{}' was chosen.",
|
332 |
|
lib.getClass().getName(),
|
333 |
|
String.valueOf(lib.getPriority()));
|
|
316 |
return;
|
334 |
317 |
}
|
|
318 |
}
|
|
319 |
}
|
|
320 |
// If it is the first implementation, insert it after the API
|
|
321 |
// and before the services, if any.
|
|
322 |
Library first = (Library) libraryGroup.get(0);
|
|
323 |
if (API.equals(first.getType())) {
|
|
324 |
if (libraryGroup.size() == 1) {
|
|
325 |
libraryGroup.add(library);
|
335 |
326 |
} else {
|
336 |
|
this.organizedLibs.put(libName, lib);
|
|
327 |
libraryGroup.add(1, library);
|
337 |
328 |
}
|
338 |
|
}
|
339 |
|
}
|
|
329 |
return;
|
|
330 |
} else {
|
|
331 |
libraryGroup.add(0, library);
|
|
332 |
return;
|
|
333 |
}
|
|
334 |
} else { // It is a service type
|
340 |
335 |
|
341 |
|
public void add(Library lib) {
|
342 |
|
if (super.contains(lib)) {
|
343 |
|
return;
|
344 |
|
}
|
345 |
|
Set requireds = lib.getRequireds();
|
346 |
|
if (requireds != null) {
|
347 |
|
Iterator iterator = requireds.iterator();
|
348 |
|
while (iterator.hasNext()) {
|
349 |
|
Required req = (Required) iterator.next();
|
350 |
|
if (!req.isAdded()) {
|
351 |
|
req.setAdded(true);
|
352 |
|
addRequiredLibrary(req);
|
|
336 |
// Look for other services and insert by dependency order
|
|
337 |
for (int i = 0; i < libraryGroup.size(); i++) {
|
|
338 |
Library current = (Library) libraryGroup.get(i);
|
|
339 |
if (SERVICE.equals(current.getType())) {
|
|
340 |
if (current.isRequired(library)) {
|
|
341 |
libraryGroup.add(i, library);
|
|
342 |
return;
|
|
343 |
}
|
353 |
344 |
}
|
354 |
345 |
}
|
|
346 |
// Otherwise add the last
|
|
347 |
libraryGroup.add(library);
|
355 |
348 |
}
|
356 |
|
|
357 |
|
if (lib.getLibrary() != null && lib.getType() != null) {
|
358 |
|
organizedLibs.remove(getLibName(lib));
|
359 |
|
}
|
360 |
|
if (!super.contains(lib)) {
|
361 |
|
super.add(lib);
|
362 |
|
}
|
363 |
|
String name = lib.getClass().getName();
|
364 |
|
// Add as required all the related types so the library
|
365 |
|
// has all implementation and services initialized
|
366 |
|
if (Library.TYPE.API.equals(lib.getType())) {
|
367 |
|
addRequiredLibrary(name, Library.TYPE.IMPL);
|
368 |
|
addRequiredLibrary(name, Library.TYPE.SERVICE);
|
369 |
|
}
|
370 |
349 |
}
|
371 |
350 |
|
372 |
|
private void addRequiredLibrary(Required req) {
|
373 |
|
if (req.getLibrary() != null) {
|
374 |
|
String name = req.getLibrary().getName();
|
375 |
|
addRequiredLibrary(name, req.getType());
|
376 |
|
}
|
377 |
|
}
|
|
351 |
private void addFromLibraryGroup(Class APIRequired,
|
|
352 |
Set alreadyAddedGroups) {
|
|
353 |
Collection libraryGroup = (Collection) apis.get(APIRequired);
|
|
354 |
if (libraryGroup != null
|
|
355 |
&& !alreadyAddedGroups.contains(APIRequired)) {
|
|
356 |
alreadyAddedGroups.add(APIRequired);
|
378 |
357 |
|
379 |
|
private void addRequiredLibrary(String libraryName, String libraryType) {
|
380 |
|
String type = getLibraryType(libraryName, libraryType);
|
381 |
|
if (Library.TYPE.SERVICE.equals(type)) {
|
382 |
|
Set libSet = (Set) organizedLibs.get(libraryName + "-" + type);
|
383 |
|
if (libSet != null) {
|
384 |
|
for (Iterator iterator = libSet.iterator(); iterator
|
385 |
|
.hasNext();) {
|
386 |
|
this.add((Library) iterator.next());
|
|
358 |
// Add all group libraries, taking into account dependencies
|
|
359 |
// with other library groups
|
|
360 |
for (Iterator iterator = libraryGroup.iterator(); iterator
|
|
361 |
.hasNext();) {
|
|
362 |
Library library = (Library) iterator.next();
|
|
363 |
Set requireds = library.getRequireds();
|
|
364 |
if (requireds != null) {
|
|
365 |
for (Iterator requiredsIter = requireds.iterator(); requiredsIter
|
|
366 |
.hasNext();) {
|
|
367 |
Required required = (Required) requiredsIter.next();
|
|
368 |
addFromLibraryGroup(required.getLibrary(),
|
|
369 |
alreadyAddedGroups);
|
|
370 |
}
|
387 |
371 |
}
|
|
372 |
add(library);
|
388 |
373 |
}
|
389 |
|
} else {
|
390 |
|
Library lib =
|
391 |
|
(Library) organizedLibs.get(libraryName + "-" + type);
|
392 |
|
if (lib != null) {
|
393 |
|
this.add(lib);
|
394 |
|
}
|
395 |
374 |
}
|
396 |
375 |
}
|
397 |
376 |
|
398 |
|
private String getLibraryType(String libraryName, String libraryType) {
|
399 |
|
if (libraryType != null) {
|
400 |
|
return libraryType;
|
401 |
|
}
|
402 |
|
if (organizedLibs.get(libraryName + "-" + Library.TYPE.SERVICE) != null) {
|
403 |
|
return Library.TYPE.SERVICE;
|
404 |
|
} else
|
405 |
|
if (organizedLibs.get(libraryName + "-" + Library.TYPE.IMPL) != null) {
|
406 |
|
return Library.TYPE.IMPL;
|
407 |
|
} else {
|
408 |
|
return Library.TYPE.API;
|
409 |
|
}
|
410 |
|
|
411 |
|
}
|
412 |
377 |
}
|
|
378 |
|
413 |
379 |
}
|