4 changed files with 152 additions and 0 deletions
@ -0,0 +1,12 @@
|
||||
package org.leolo.map.osm.extract; |
||||
|
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
@Target({ElementType.TYPE}) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
public @interface SearchProvider { |
||||
String[] searchKey(); |
||||
} |
||||
@ -0,0 +1,112 @@
|
||||
package org.leolo.map.osm.extract; |
||||
|
||||
import eu.infomas.annotation.AnnotationDetector; |
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.leolo.map.osm.extract.model.SearchItem; |
||||
|
||||
import java.io.IOException; |
||||
import java.lang.annotation.Annotation; |
||||
import java.util.*; |
||||
import java.util.function.BiConsumer; |
||||
|
||||
public class SearchProviderManager { |
||||
|
||||
private static SearchProviderManager instance; |
||||
private Logger log = LogManager.getLogger(); |
||||
private Hashtable<String, Class> items = new Hashtable<>(); |
||||
|
||||
public static synchronized SearchProviderManager getInstance(){ |
||||
if(instance==null){ |
||||
instance = new SearchProviderManager(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
private SearchProviderManager(){ |
||||
AnnotationDetector.TypeReporter reporter = new AnnotationDetector.TypeReporter() { |
||||
@Override |
||||
public void reportTypeAnnotation(Class<? extends Annotation> annoClass, String clazz) { |
||||
log.atDebug().log("Class {} has annotation {}", clazz, annoClass.getName()); |
||||
try { |
||||
Class typeClass = Class.forName(clazz); |
||||
if(SearchItem.class.isAssignableFrom(typeClass)){ |
||||
log.atDebug().log("Class {} is an output formatter", clazz); |
||||
SearchProvider of = (SearchProvider) typeClass.getAnnotation(SearchProvider.class); |
||||
for(String format: of.searchKey()){ |
||||
if(items.containsKey(format)){ |
||||
log.atWarn().log("Duplicated search type {}. Shared by (at least) {} and {}", |
||||
format, |
||||
typeClass.getName(), |
||||
items.get(format).getName() |
||||
); |
||||
}else{ |
||||
items.put(format, typeClass); |
||||
log.atInfo().log("Registering class {} for search type {}", typeClass, format); |
||||
} |
||||
} |
||||
}else{ |
||||
log.atError().log("Class {} does not implements OutputFormat but have annotation @OutputFormatter", clazz); |
||||
} |
||||
} catch (ClassNotFoundException e) { |
||||
log.atError().withThrowable(e).log(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public Class<? extends Annotation>[] annotations() { |
||||
return new Class[]{SearchProvider.class}; |
||||
} |
||||
}; |
||||
final AnnotationDetector cf = new AnnotationDetector(reporter); |
||||
try { |
||||
cf.detect(); |
||||
} catch (IOException e) { |
||||
log.atError().withThrowable(e).log(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
public int size() { |
||||
return items.size(); |
||||
} |
||||
|
||||
public boolean isEmpty() { |
||||
return items.isEmpty(); |
||||
} |
||||
|
||||
public Enumeration<String> keys() { |
||||
return items.keys(); |
||||
} |
||||
|
||||
public boolean containsKey(Object key) { |
||||
return items.containsKey(key); |
||||
} |
||||
|
||||
public Class get(Object key) { |
||||
return items.get(key); |
||||
} |
||||
|
||||
public Set<String> keySet() { |
||||
return items.keySet(); |
||||
} |
||||
|
||||
public void forEach(BiConsumer<? super String, ? super Class> action) { |
||||
items.forEach(action); |
||||
} |
||||
|
||||
public int getOutputFormatterCount(){ |
||||
HashSet<Class> set = new HashSet<>(); |
||||
for(Class clazz:items.values()){ |
||||
set.add(clazz); |
||||
} |
||||
return set.size(); |
||||
} |
||||
|
||||
public int getSearchProviderCount(){ |
||||
HashSet<Class> set = new HashSet<>(); |
||||
for(Class clazz:items.values()){ |
||||
set.add(clazz); |
||||
} |
||||
return set.size(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue