1 19 20 package org.apache.cayenne.conf; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.apache.cayenne.CayenneRuntimeException; 33 import org.apache.cayenne.ConfigurationException; 34 import org.apache.cayenne.access.DataDomain; 35 import org.apache.cayenne.dataview.DataView; 36 import org.apache.cayenne.event.EventManager; 37 import org.apache.cayenne.util.CayenneMap; 38 import org.apache.cayenne.util.ResourceLocator; 39 import org.apache.commons.collections.Predicate; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 57 public abstract class Configuration { 58 59 private static Log logObj = LogFactory.getLog(Configuration.class); 60 61 public static final String DEFAULT_DOMAIN_FILE = "cayenne.xml"; 62 public static final Class DEFAULT_CONFIGURATION_CLASS = DefaultConfiguration.class; 63 64 protected static Configuration sharedConfiguration = null; 65 66 public static final Predicate ACCEPT_ALL_DATAVIEWS = new Predicate() { 67 68 public boolean evaluate(Object dataViewName) { 69 return true; 70 } 71 }; 72 73 76 protected CayenneMap dataDomains = new CayenneMap(this); 77 protected DataSourceFactory overrideFactory; 78 protected ConfigStatus loadStatus = new ConfigStatus(); 79 protected String domainConfigurationName = DEFAULT_DOMAIN_FILE; 80 protected boolean ignoringLoadFailures; 81 protected ConfigLoaderDelegate loaderDelegate; 82 protected ConfigSaverDelegate saverDelegate; 83 protected ConfigurationShutdownHook configurationShutdownHook = new ConfigurationShutdownHook(); 84 protected Map dataViewLocations = new HashMap (); 85 protected String projectVersion; 86 87 90 protected EventManager eventManager; 91 92 99 public synchronized static Configuration getSharedConfiguration() { 100 if (Configuration.sharedConfiguration == null) { 101 Configuration.initializeSharedConfiguration(); 102 } 103 104 return Configuration.sharedConfiguration; 105 } 106 107 112 public EventManager getEventManager() { 113 return eventManager; 114 } 115 116 121 public void setEventManager(EventManager eventManager) { 122 this.eventManager = eventManager; 123 } 124 125 130 public static void initializeSharedConfiguration() { 131 Configuration.initializeSharedConfiguration(DEFAULT_CONFIGURATION_CLASS); 132 } 133 134 138 public static void initializeSharedConfiguration(Class configurationClass) { 139 Configuration conf = null; 140 141 try { 142 conf = (Configuration) configurationClass.newInstance(); 143 } 144 catch (Exception ex) { 145 logObj.error("Error creating shared Configuration: ", ex); 146 throw new ConfigurationException("Error creating shared Configuration." 147 + ex.getMessage(), ex); 148 } 149 150 Configuration.initializeSharedConfiguration(conf); 151 } 152 153 158 public static void initializeSharedConfiguration(Configuration conf) { 159 if (!conf.canInitialize()) { 161 throw new ConfigurationException("Configuration of class " 162 + conf.getClass().getName() 163 + " refused to be initialized."); 164 } 165 166 try { 167 conf.initialize(); 169 170 conf.didInitialize(); 172 173 Configuration.sharedConfiguration = conf; 175 } 176 catch (Exception ex) { 177 throw new ConfigurationException( 178 "Error during Configuration initialization. " + ex.getMessage(), 179 ex); 180 } 181 } 182 183 189 protected Configuration() { 190 this(DEFAULT_DOMAIN_FILE); 191 } 192 193 197 protected Configuration(String domainConfigurationName) { 198 199 this.setDomainConfigurationName(domainConfigurationName); 201 202 this.eventManager = new EventManager(); 203 } 204 205 209 public abstract boolean canInitialize(); 210 211 216 public abstract void initialize() throws Exception ; 217 218 221 public abstract void didInitialize(); 222 223 226 protected abstract ResourceLocator getResourceLocator(); 227 228 231 protected abstract InputStream getDomainConfiguration(); 234 235 238 protected abstract InputStream getMapConfiguration(String name); 239 240 protected abstract InputStream getViewConfiguration(String location); 241 242 246 public String getDomainConfigurationName() { 247 return this.domainConfigurationName; 248 } 249 250 256 protected void setDomainConfigurationName(String domainConfigurationName) { 257 this.domainConfigurationName = domainConfigurationName; 258 } 259 260 263 public String getProjectVersion() { 264 return projectVersion; 265 } 266 267 270 public void setProjectVersion(String projectVersion) { 271 this.projectVersion = projectVersion; 272 } 273 274 280 public DataSourceFactory getDataSourceFactory() { 281 return this.overrideFactory; 282 } 283 284 public void setDataSourceFactory(DataSourceFactory overrideFactory) { 285 this.overrideFactory = overrideFactory; 286 } 287 288 292 public void addDomain(DataDomain domain) { 293 this.dataDomains.put(domain.getName(), domain); 294 295 if (domain != null) { 297 domain.setEventManager(getEventManager()); 298 } 299 300 logObj.debug("added domain: " + domain.getName()); 301 } 302 303 307 public DataDomain getDomain(String name) { 308 return (DataDomain) this.dataDomains.get(name); 309 } 310 311 318 public DataDomain getDomain() { 319 int size = this.dataDomains.size(); 320 if (size == 0) { 321 return null; 322 } 323 else if (size == 1) { 324 return (DataDomain) this.dataDomains.values().iterator().next(); 325 } 326 else { 327 throw new CayenneRuntimeException( 328 "More than one domain is configured; use 'getDomain(String name)' instead."); 329 } 330 } 331 332 338 public void removeDomain(String name) { 339 DataDomain domain = (DataDomain) dataDomains.remove(name); 340 341 if (domain != null) { 342 domain.setEventManager(null); 343 } 344 345 logObj.debug("removed domain: " + name); 346 } 347 348 351 public Collection getDomains() { 352 return Collections.unmodifiableCollection(dataDomains.values()); 353 } 354 355 360 public boolean isIgnoringLoadFailures() { 361 return this.ignoringLoadFailures; 362 } 363 364 369 protected void setIgnoringLoadFailures(boolean ignoringLoadFailures) { 370 this.ignoringLoadFailures = ignoringLoadFailures; 371 } 372 373 378 public ConfigStatus getLoadStatus() { 379 return this.loadStatus; 380 } 381 382 385 protected void setLoadStatus(ConfigStatus status) { 386 this.loadStatus = status; 387 } 388 389 392 public ConfigLoaderDelegate getLoaderDelegate() { 393 return loaderDelegate; 394 } 395 396 399 public void setLoaderDelegate(ConfigLoaderDelegate loaderDelegate) { 400 this.loaderDelegate = loaderDelegate; 401 } 402 403 406 public ConfigSaverDelegate getSaverDelegate() { 407 return saverDelegate; 408 } 409 410 413 public void setSaverDelegate(ConfigSaverDelegate saverDelegate) { 414 this.saverDelegate = saverDelegate; 415 } 416 417 423 public void setDataViewLocations(Map dataViewLocations) { 424 if (dataViewLocations == null) 425 this.dataViewLocations = new HashMap (); 426 else 427 this.dataViewLocations = dataViewLocations; 428 } 429 430 433 public Map getDataViewLocations() { 434 return dataViewLocations; 435 } 436 437 440 public boolean loadDataView(DataView dataView) throws IOException { 441 return loadDataView(dataView, Configuration.ACCEPT_ALL_DATAVIEWS); 442 } 443 444 447 public boolean loadDataView(DataView dataView, Predicate dataViewNameFilter) 448 throws IOException { 449 450 if (dataView == null) { 451 throw new IllegalArgumentException ("DataView cannot be null."); 452 } 453 454 if (dataViewLocations.size() == 0 || dataViewLocations.size() > 512) { 455 return false; 456 } 457 458 if (dataViewNameFilter == null) 459 dataViewNameFilter = Configuration.ACCEPT_ALL_DATAVIEWS; 460 461 List viewXMLSources = new ArrayList (dataViewLocations.size()); 462 int index = 0; 463 for (Iterator i = dataViewLocations.entrySet().iterator(); i.hasNext(); index++) { 464 Map.Entry entry = (Map.Entry ) i.next(); 465 String name = (String ) entry.getKey(); 466 if (!dataViewNameFilter.evaluate(name)) 467 continue; 468 String location = (String ) entry.getValue(); 469 InputStream in = getViewConfiguration(location); 470 if (in != null) 471 viewXMLSources.add(in); 472 } 473 474 if (viewXMLSources.isEmpty()) 475 return false; 476 477 dataView.load((InputStream []) viewXMLSources 478 .toArray(new InputStream [viewXMLSources.size()])); 479 return true; 480 } 481 482 485 public void shutdown() { 486 Collection domains = getDomains(); 487 for (Iterator i = domains.iterator(); i.hasNext();) { 488 DataDomain domain = (DataDomain) i.next(); 489 domain.shutdown(); 490 } 491 } 492 493 private class ConfigurationShutdownHook extends Thread { 494 495 public void run() { 496 shutdown(); 497 } 498 } 499 500 public void installConfigurationShutdownHook() { 501 uninstallConfigurationShutdownHook(); 502 Runtime.getRuntime().addShutdownHook(configurationShutdownHook); 503 } 504 505 public void uninstallConfigurationShutdownHook() { 506 Runtime.getRuntime().removeShutdownHook(configurationShutdownHook); 507 } 508 } 509 | Popular Tags |