1 7 package com.inversoft.config; 8 9 10 import java.io.File ; 11 import java.io.InputStream ; 12 import java.io.Reader ; 13 import java.net.URL ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.apache.log4j.Logger; 19 import org.jdom.Document; 20 import org.jdom.JDOMException; 21 import org.jdom.input.SAXBuilder; 22 import org.xml.sax.InputSource ; 23 24 import com.inversoft.error.ErrorList; 25 26 27 131 public class ConfigMediator { 132 133 136 private static final Logger logger = Logger.getLogger(ConfigMediator.class); 137 138 141 protected SAXBuilder saxBuilder; 142 143 144 147 public ConfigMediator() { 148 saxBuilder = new SAXBuilder(); 149 } 150 151 152 163 public synchronized void mediate(Document[] documents) 164 throws ConfigurationException { 165 rebuild(documents, false); 166 } 167 168 178 public synchronized void mediate(File [] files) throws ConfigurationException { 179 rebuild(files, false); 180 } 181 182 193 public synchronized void mediate(InputSource [] sources) 194 throws ConfigurationException { 195 rebuild(sources, false); 196 } 197 198 209 public synchronized void mediate(InputStream [] streams) 210 throws ConfigurationException { 211 rebuild(streams, false); 212 } 213 214 224 public synchronized void mediate(Reader [] readers) 225 throws ConfigurationException { 226 rebuild(readers, false); 227 } 228 229 239 public synchronized void mediate(URL [] urls) 240 throws ConfigurationException { 241 rebuild(urls, false); 242 } 243 244 255 public synchronized void rebuild(Document[] documents) 256 throws ConfigurationException { 257 rebuild(documents, true); 258 } 259 260 270 public synchronized void rebuild(File [] files) 271 throws ConfigurationException { 272 rebuild(files, true); 273 } 274 275 286 public synchronized void rebuild(InputSource [] sources) 287 throws ConfigurationException { 288 rebuild(sources, true); 289 } 290 291 302 public synchronized void rebuild(InputStream [] streams) 303 throws ConfigurationException { 304 rebuild(streams, true); 305 } 306 307 317 public synchronized void rebuild(Reader [] readers) 318 throws ConfigurationException { 319 rebuild(readers, true); 320 } 321 322 332 public synchronized void rebuild(URL [] urls) throws ConfigurationException { 333 rebuild(urls, true); 334 } 335 336 340 protected synchronized void rebuild(final Object [] sources, 341 final boolean rebuilding) 342 throws ConfigurationException { 343 assert (sources != null && sources.length > 0) : 344 "sources == null || sources.length == 0"; 345 346 Map builders = new HashMap (); 347 Map registries = new HashMap (); 348 Map states = new HashMap (); 349 Document document = null; 350 ErrorList errors = new ErrorList(); 351 352 Map factories = ConfigFactoryRegistry.allFactories(); 354 Iterator iter = factories.keySet().iterator(); 355 Object key; 356 ConfigRegistry registry; 357 ConfigFactory factory; 358 while (iter.hasNext()) { 359 key = iter.next(); 360 factory = (ConfigFactory) factories.get(key); 361 registry = factory.createRegistry(); 362 if (registry != null) { 363 registries.put(key, registry); 364 } 365 } 366 367 for (int i = 0; i < sources.length; i++) { 369 try { 370 if (sources[i] instanceof Document) { 371 document = (Document) sources[i]; 372 } else if (sources[i] instanceof File ) { 373 document = saxBuilder.build((File ) sources[i]); 374 } else if (sources[i] instanceof InputSource ) { 375 document = saxBuilder.build((InputSource ) sources[i]); 376 } else if (sources[i] instanceof InputStream ) { 377 document = saxBuilder.build((InputStream ) sources[i]); 378 } else if (sources[i] instanceof Reader ) { 379 document = saxBuilder.build((Reader ) sources[i]); 380 } else if (sources[i] instanceof URL ) { 381 document = saxBuilder.build((URL ) sources[i]); 382 } 383 } catch (JDOMException jdome) { 384 logger.error(jdome.toString()); 385 continue; 386 } 387 388 rebuild(document, builders, registries, states, rebuilding, errors); 389 } 390 391 validate(builders, registries, states, errors); 393 394 commit(builders, registries, states); 397 398 if (!errors.isEmpty()) { 399 throw new ConfigurationException(errors); 400 } 401 } 402 403 407 protected void rebuild(Document document, Map builders, Map registries, 408 Map states, boolean rebuilding, ErrorList errors) { 409 String rootName = document.getRootElement().getName(); 410 ConfigFactory factory = ConfigFactoryRegistry.lookup(rootName); 411 412 if (factory == null) { 413 logger.warn("Unable to locate factory for configuration: " + rootName); 414 errors.addError("Unable to locate factory for configuration: " + rootName); 415 return; 416 } 417 418 ComponentState state = (ComponentState) states.get(rootName); 419 if (state == null) { 420 state = new ComponentState(); 421 states.put(rootName, state); 422 } 423 424 ConfigBuilder builder = getBuilder(factory, rootName, builders); 425 ConfigRegistry registry = (ConfigRegistry) registries.get(rootName); 426 427 try { 428 if (rebuilding) { 429 builder.rebuild(document, registry); 430 } else { 431 builder.build(document, registry); 432 } 433 } catch (ConfigurationException ce) { 434 errors.addErrorList(ce.getErrors()); 435 state.valid = false; 436 } 437 } 438 439 443 protected ConfigBuilder getBuilder(ConfigFactory factory, String rootName, 444 Map builders) { 445 446 ConfigBuilder builder = (ConfigBuilder) builders.get(rootName); 447 if (builder == null) { 448 builder = factory.createBuilder(); 449 assert (builder != null) : "builder == null"; 450 builders.put(rootName, builder); 451 } 452 453 return builder; 454 } 455 456 461 protected void validate(Map builders, Map registries, Map states, 462 ErrorList errors) { 463 464 Iterator iter = builders.entrySet().iterator(); 465 Map.Entry entry; 466 ConfigBuilder builder; 467 ConfigRegistry registry; 468 String rootName; 469 Map tempRegistries; 470 ComponentState state; 471 while (iter.hasNext()) { 472 entry = (Map.Entry ) iter.next(); 473 builder = (ConfigBuilder) entry.getValue(); 474 rootName = (String ) entry.getKey(); 475 476 state = (ComponentState) states.get(rootName); 477 if (!state.valid) { 478 continue; 479 } 480 481 registry = (ConfigRegistry) registries.get(rootName); 482 tempRegistries = new HashMap (registries); 483 tempRegistries.remove(rootName); 484 485 try { 486 builder.validate(registry, tempRegistries); 487 } catch (ConfigurationException ce) { 488 errors.addErrorList(ce.getErrors()); 489 state.valid = false; 490 } 491 } 492 } 493 494 499 protected void commit(Map builders, Map registries, Map states) { 500 501 Iterator iter = builders.entrySet().iterator(); 502 Map.Entry entry; 503 ConfigBuilder builder; 504 ConfigRegistry registry; 505 String rootName; 506 Map tempRegistries; 507 ComponentState state; 508 while (iter.hasNext()) { 509 entry = (Map.Entry ) iter.next(); 510 builder = (ConfigBuilder) entry.getValue(); 511 rootName = (String ) entry.getKey(); 512 513 state = (ComponentState) states.get(rootName); 514 if (!state.valid) { 515 continue; 516 } 517 518 registry = (ConfigRegistry) registries.get(rootName); 519 tempRegistries = new HashMap (registries); 520 tempRegistries.remove(rootName); 521 522 builder.commit(registry, tempRegistries); 523 } 524 } 525 526 527 534 public class ComponentState { 535 public boolean valid = true; 536 } 537 } | Popular Tags |