1 23 package org.hammurapi; 24 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 36 import com.pavelvlasov.config.ConfigurationException; 37 import com.pavelvlasov.logging.Logger; 38 39 43 public class InspectorSet { 44 private Map descriptors=new HashMap (); 45 private Collection inspectors; 46 private Logger logger; 47 private InspectorContextFactory contextFactory; 48 private Collection inspectorSourceInfos=new ArrayList (); 49 50 public InspectorSet(InspectorContextFactory contextFactory, Logger logger) { 51 this.contextFactory=contextFactory; 52 this.logger=logger; 53 } 54 55 public void addDescriptors(Collection descriptors) throws ConfigurationException { 56 Iterator it=descriptors.iterator(); 57 while (it.hasNext()) { 58 addDescriptor((InspectorDescriptor) it.next()); 59 } 60 } 61 62 public void addInspectorSourceInfo(InspectorSourceInfo info) { 63 inspectorSourceInfos.add(info); 64 } 65 66 public Collection getInspectorSourceInfos() { 67 return inspectorSourceInfos; 68 } 69 70 public InspectorDescriptor getDescriptor(String name) { 71 return (InspectorDescriptor) descriptors.get(name); 72 } 73 74 public Collection getDescriptors() { 75 return descriptors.values(); 76 } 77 78 public void addDescriptor(InspectorDescriptor descriptor) throws ConfigurationException { 79 SelfDescribingInspectorProxy sdrp=new SelfDescribingInspectorProxy(descriptor); 80 String inspectorName=descriptor.getName(); 81 if (inspectorName==null) { 82 inspectorName=sdrp.getName(); 83 } 84 85 if (inspectorName==null) { 86 throw new ConfigurationException("Unnamed inspector"); 87 } 88 89 InspectorDescriptorStack rds=(InspectorDescriptorStack) descriptors.get(inspectorName); 90 if (rds==null) { 91 rds=new InspectorDescriptorStack(); 92 descriptors.put(inspectorName, rds); 93 } 94 95 rds.addFirst(sdrp); 96 rds.addFirst(descriptor); 97 } 98 99 public Collection getInspectors() throws ConfigurationException, HammurapiException { 100 if (inspectors==null) { 101 assignOrders(); 102 inspectors=new LinkedList (); 103 Iterator it=descriptors.values().iterator(); 104 while (it.hasNext()) { 105 InspectorDescriptor inspectorDescriptor=(InspectorDescriptor) it.next(); 106 InspectorContext ic=contextFactory.newContext(inspectorDescriptor, logger); 107 if (Boolean.TRUE.equals(inspectorDescriptor.isEnabled())) { 108 Inspector inspector=inspectorDescriptor.getInspector(); 109 if (inspector!=null) { 110 inspector.setContext(ic); 111 inspectors.add(inspector); 112 113 if (inspector instanceof FilteringInspector) { 114 Collection fid=inspectorDescriptor.getFilteredInspectorDesriptors(this, null); 115 if (fid!=null) { 116 Iterator dit=fid.iterator(); 117 while (dit.hasNext()) { 118 ((FilteringInspector) inspector).addTarget(((InspectorDescriptor) dit.next()).getInspector()); 119 } 120 } 121 } 122 } 123 } 124 } 125 } 126 return inspectors; 127 } 128 129 134 public void initInspectors() throws ConfigurationException, HammurapiException { 135 Iterator it=getInspectors().iterator(); 136 while (it.hasNext()) { 137 ((Inspector) it.next()).init(); 138 } 139 } 140 141 private void assignOrders() throws HammurapiException { 142 final Map dependencyMap=new HashMap (); 143 Iterator it=descriptors.values().iterator(); 144 while (it.hasNext()) { 145 InspectorDescriptor inspectorDescriptor=(InspectorDescriptor) it.next(); 146 if (Boolean.TRUE.equals(inspectorDescriptor.isEnabled())) { 147 Collection inspectorsToOrder=new ArrayList (); 148 Collection waivedInspectorNames = inspectorDescriptor.getWaivedInspectorNames(); 149 if (waivedInspectorNames!=null) { 150 inspectorsToOrder.addAll(waivedInspectorNames); 151 } 152 153 Collection filteredInspectors = inspectorDescriptor.getFilteredInspectorDesriptors(this, null); 154 if (filteredInspectors!=null && !filteredInspectors.isEmpty()) { 155 Iterator fit=filteredInspectors.iterator(); 156 while (fit.hasNext()) { 157 inspectorsToOrder.add(((InspectorDescriptor) fit.next()).getName()); 158 } 159 } 160 161 Collection afterInspectors = inspectorDescriptor.getAfterInspectorNames(); 162 if (afterInspectors!=null) { 163 inspectorsToOrder.addAll(afterInspectors); 164 } 165 166 dependencyMap.put(inspectorDescriptor.getName(), inspectorsToOrder); 167 } 168 } 169 170 class GraphEntry implements Comparable { 171 private static final String CICRULAR_REFERENCE_MSG = "Circular <waives> or <filter> reference in inspector '"; 172 String name; 173 private Set dependents=new HashSet (); 174 private boolean ready=false; 175 private int level; 176 177 public String toString() { 178 StringBuffer ret=new StringBuffer (getClass().getName()); 179 ret.append("[").append(name); 180 if (!dependents.isEmpty()) { 181 ret.append(" <- "); 182 Iterator it=dependents.iterator(); 183 while (it.hasNext()) { 184 ret.append(it.next()); 185 if (it.hasNext()) { 186 ret.append(", "); 187 } 188 } 189 } 190 ret.append("]"); 191 return ret.toString(); 192 } 193 194 GraphEntry(String name) throws HammurapiException { 195 this.name=name; 196 Iterator it=((Collection ) dependencyMap.get(name)).iterator(); 197 while (it.hasNext()) { 198 String dependent=(String ) it.next(); 199 if (name.equals(dependent)) { 200 throw new HammurapiException(CICRULAR_REFERENCE_MSG+name+"'"); 201 } 202 dependents.add(dependent); 203 204 Object o=dependencyMap.get(dependent); 205 if (o instanceof Collection ) { 206 o=new GraphEntry(dependent); 207 level=Math.max(level, ((GraphEntry) o).level+1); 208 } 209 210 if (o!=null) { 211 Iterator dit=((GraphEntry) o).getDependents().iterator(); 212 while (dit.hasNext()) { 213 String ddependent=(String ) it.next(); 214 if (name.equals(ddependent)) { 215 throw new HammurapiException(CICRULAR_REFERENCE_MSG+name+"'"); 216 } 217 218 dependents.add(ddependent); 219 } 220 } 221 } 222 ready=true; 223 dependencyMap.put(name, this); 224 } 225 226 Set getDependents() throws HammurapiException { 227 if (ready) { 228 return dependents; 229 } 230 231 throw new HammurapiException(CICRULAR_REFERENCE_MSG+name+"'"); 232 } 233 234 public int compareTo(Object o) { 235 if (o==this) { 236 return 0; 237 } else if (o instanceof GraphEntry) { 238 GraphEntry ge=(GraphEntry) o; 239 240 try { 241 if (getDependents().contains(ge.name)) { 242 return -1; 243 } else if (ge.getDependents().contains(name)){ 244 return 1; 245 } else if (level<ge.level) { 246 return 1; 247 } else if (level>ge.level) { 248 return -1; 249 } else { 250 return name.compareTo(ge.name); 251 } 252 } catch (HammurapiException e) { 253 throw new HammurapiRuntimeException(e); 254 } 255 } else { 256 return -1; 257 } 258 } 259 260 public int hashCode() { 261 return name.hashCode(); 262 } 263 } 264 265 Iterator kit=dependencyMap.keySet().iterator(); 266 while (kit.hasNext()) { 267 String key=(String ) kit.next(); 268 if (dependencyMap.get(key) instanceof Collection ) { 269 new GraphEntry(key); 270 } 271 } 272 273 List inspectors=new ArrayList (dependencyMap.values()); 274 Collections.sort(inspectors); 275 276 int counter=0; 277 Integer start=null; 278 Iterator wit=inspectors.iterator(); 279 while (wit.hasNext()) { 280 String iName=((GraphEntry) wit.next()).name; 281 InspectorDescriptor id=(InspectorDescriptor) descriptors.get(iName); 282 Integer order = id.getOrder(); 283 if (order!=null) { 284 start=new Integer (order.intValue()-counter); 285 break; 286 } 287 counter++; 288 } 289 290 counter = start==null ? 0 : start.intValue(); 291 wit=inspectors.iterator(); 292 while (wit.hasNext()) { 293 String iName=((GraphEntry) wit.next()).name; 294 InspectorDescriptor id=(InspectorDescriptor) descriptors.get(iName); 295 Integer order = id.getOrder(); 296 if (order!=null) { 297 if (order.intValue()<counter) { 298 throw new HammurapiException("Order "+order.intValue()+" conflicts with automatically assigned order "+counter+" for inspector "+iName); 299 } 300 301 counter=order.intValue(); 302 } else { 303 final Integer iOrder=new Integer (counter); 304 descriptors.put(id.getName(), new InspectorDescriptorFilter(id) { 305 public Integer getOrder() { 306 return iOrder; 307 } 308 }); 309 } 310 counter++; 311 } 312 } 313 314 int size() { 315 return descriptors.size(); 316 } 317 318 321 public void destroy() { 322 if (inspectors!=null) { 323 Iterator it=inspectors.iterator(); 324 while (it.hasNext()) { 325 ((Inspector) it.next()).destroy(); 326 } 327 } 328 } 329 } 330 | Popular Tags |