1 19 20 21 package org.netbeans.modules.i18n; 22 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 import org.openide.util.Lookup; 32 import org.openide.util.LookupEvent; 33 import org.openide.util.LookupListener; 34 35 36 46 public final class FactoryRegistry extends Object { 47 48 private FactoryRegistry() {}; 49 50 51 private static Lookup.Result result; 52 private static final Set cache = Collections.synchronizedSet(new HashSet (5)); 53 private static final Set ncache = Collections.synchronizedSet(new HashSet (50)); 54 55 56 private static Lookup.Result getSupports() { 57 if(result == null) { 58 result = Lookup.getDefault().lookup(new Lookup.Template(I18nSupport.Factory.class)); 59 result.addLookupListener(new LookupListener() { 60 public void resultChanged(LookupEvent e) { 61 cache.clear(); 62 ncache.clear(); 63 } 64 }); 65 } 66 67 return result; 68 } 69 70 72 public static I18nSupport.Factory getFactory(Class dataObjectClass) { 73 74 List candidates = new ArrayList (3); 75 76 for(Iterator it = getSupports().allInstances().iterator(); it.hasNext(); ) { 77 I18nSupport.Factory factory = (I18nSupport.Factory)it.next(); 78 79 Class clazz = factory.getDataObjectClass(); 82 83 if(clazz != null && clazz.isAssignableFrom(dataObjectClass)) { 84 candidates.add(factory); 85 } 86 } 87 88 if(candidates.size() == 0) { 89 return null; 90 } else if(candidates.size() == 1) { 91 return (I18nSupport.Factory)candidates.get(0); 92 } else { 93 I18nSupport.Factory chosen = null; 94 95 for(Iterator it = candidates.iterator(); it.hasNext(); ) { 98 I18nSupport.Factory fct = (I18nSupport.Factory)it.next(); 99 100 if(chosen == null) { 101 chosen = fct; 102 continue; 103 } 104 105 if(chosen.getDataObjectClass().isAssignableFrom(fct.getDataObjectClass()) ) { 106 chosen = fct; 107 } 108 } 109 110 return chosen; 111 } 112 } 113 114 118 public static boolean hasFactory(Class dataObjectClass) { 119 120 if (cache.contains(dataObjectClass)) return true; 121 if (ncache.contains(dataObjectClass)) return false; 122 123 for(Iterator it = getSupports().allInstances().iterator(); it.hasNext(); ) { 124 I18nSupport.Factory factory = (I18nSupport.Factory)it.next(); 125 126 Class clazz = factory.getDataObjectClass(); 129 130 if(clazz != null && clazz.isAssignableFrom(dataObjectClass)) { 131 cache.add(dataObjectClass); 132 return true; 133 } 134 } 135 136 ncache.add(dataObjectClass); 137 return false; 138 } 139 140 } 141 | Popular Tags |