1 56 package org.objectstyle.cayenne.conf; 57 58 import java.io.InputStream ; 59 import java.util.HashMap ; 60 import java.util.Iterator ; 61 import java.util.Map ; 62 63 import javax.sql.DataSource ; 64 65 import org.apache.log4j.Level; 66 import org.apache.log4j.Logger; 67 import org.objectstyle.cayenne.ConfigurationException; 68 import org.objectstyle.cayenne.access.DataDomain; 69 import org.objectstyle.cayenne.access.DataNode; 70 import org.objectstyle.cayenne.dba.DbAdapter; 71 import org.objectstyle.cayenne.dba.JdbcAdapter; 72 import org.objectstyle.cayenne.map.DataMap; 73 import org.objectstyle.cayenne.map.DataMapException; 74 import org.objectstyle.cayenne.map.MapLoader; 75 import org.xml.sax.InputSource ; 76 77 83 public class RuntimeLoadDelegate implements ConfigLoaderDelegate { 84 private static Logger logObj = Logger.getLogger(RuntimeLoadDelegate.class); 85 86 protected Map domains = new HashMap (); 87 protected Map views = new HashMap (); 88 protected ConfigStatus status; 89 protected Configuration config; 90 protected Level logLevel; 91 protected long startTime; 92 93 public RuntimeLoadDelegate( 94 Configuration config, 95 ConfigStatus status, 96 Level logLevel) { 97 this.config = config; 98 this.logLevel = logLevel; 99 100 if (status == null) { 101 status = new ConfigStatus(); 102 } 103 104 this.status = status; 105 } 106 107 protected DataDomain findDomain(String name) throws FindException { 108 DataDomain domain = (DataDomain) domains.get(name); 109 if (domain == null) { 110 throw new FindException("Can't find DataDomain: " + name); 111 } 112 113 return domain; 114 } 115 116 protected DataMap findMap(String domainName, String mapName) throws FindException { 117 DataDomain domain = findDomain(domainName); 118 DataMap map = domain.getMap(mapName); 119 if (map == null) { 120 throw new FindException("Can't find DataMap: " + mapName); 121 } 122 123 return map; 124 } 125 126 protected DataNode findNode(String domainName, String nodeName) 127 throws FindException { 128 DataDomain domain = findDomain(domainName); 129 DataNode node = domain.getNode(nodeName); 130 if (node == null) { 131 throw new FindException("Can't find DataNode: " + nodeName); 132 } 133 134 return node; 135 } 136 137 public boolean loadError(Throwable th) { 138 logObj.log(logLevel, "Parser Exception.", th); 139 status.getOtherFailures().add(th.getMessage()); 140 return false; 141 } 142 143 146 public void shouldLoadProjectVersion(String version) { 147 config.setProjectVersion(version); 148 } 149 150 153 public void shouldRegisterDataView(String name, String location) { 154 views.put(name, location); 155 } 156 157 public void shouldLoadDataDomainProperties(String domainName, Map properties) { 158 if (properties == null || properties.isEmpty()) { 159 return; 160 } 161 162 DataDomain domain = null; 163 try { 164 domain = findDomain(domainName); 165 } 166 catch (FindException ex) { 167 logObj.log(logLevel, "Error: Domain is not loaded: " + domainName); 168 throw new ConfigurationException("Domain is not loaded: " + domainName); 169 } 170 171 domain.initWithProperties(properties); 172 } 173 174 public void shouldLoadDataDomain(String domainName) { 175 if (domainName == null) { 176 logObj.log(logLevel, "Error: unnamed <domain>."); 177 throw new ConfigurationException("Domain 'name' attribute must be not null."); 178 } 179 180 logObj.log(logLevel, "loaded domain: " + domainName); 181 domains.put(domainName, new DataDomain(domainName)); 182 } 183 184 public void shouldLoadDataMaps(String domainName, Map locations) { 185 if (locations.size() == 0) { 186 return; 187 } 188 189 DataDomain domain = null; 190 try { 191 domain = findDomain(domainName); 192 } 193 catch (FindException ex) { 194 logObj.log(logLevel, "Error: Domain is not loaded: " + domainName); 195 throw new ConfigurationException("Domain is not loaded: " + domainName); 196 } 197 198 Iterator it = locations.keySet().iterator(); 200 while (it.hasNext()) { 201 String name = (String ) it.next(); 202 DataMap map = domain.getMap(name); 203 if (map != null) { 204 continue; 205 } 206 207 loadDataMap(domain, name, locations); 208 } 209 } 210 211 216 protected DataMap loadDataMap(DataDomain domain, String mapName, Map locations) { 217 218 if (mapName == null) { 219 throw new ConfigurationException("Error: <map> without 'name'."); 220 } 221 222 String location = (String ) locations.get(mapName); 223 224 if (location == null) { 225 throw new ConfigurationException( 226 "Error: map '" + mapName + "' without 'location'."); 227 } 228 229 InputStream mapIn = config.getMapConfiguration(location); 231 if (mapIn == null) { 232 logObj.log(logLevel, "Warning: map location not found."); 233 getStatus().addFailedMap(mapName, location, "map location not found"); 234 return null; 235 } 236 237 try { 238 DataMap map = new MapLoader().loadDataMap(new InputSource (mapIn)); 239 240 logObj.log( 241 logLevel, 242 "loaded <map name='" + mapName + "' location='" + location + "'>."); 243 244 map.setName(mapName); 245 map.setLocation(location); 246 247 domain.addMap(map); 248 return map; 249 } 250 catch (DataMapException dmex) { 251 logObj.log(logLevel, "Warning: map loading failed.", dmex); 252 getStatus().addFailedMap( 253 mapName, 254 location, 255 "map loading failed - " + dmex.getMessage()); 256 return null; 257 } 258 } 259 260 public void shouldLoadDataNode( 261 String domainName, 262 String nodeName, 263 String dataSource, 264 String adapter, 265 String factory) { 266 267 logObj.log( 268 logLevel, 269 "loading <node name='" 270 + nodeName 271 + "' datasource='" 272 + dataSource 273 + "' factory='" 274 + factory 275 + "'>."); 276 277 if (nodeName == null) { 278 throw new ConfigurationException("Error: <node> without 'name'."); 279 } 280 281 if (dataSource == null) { 282 logObj.log( 283 logLevel, 284 "Warning: <node> '" + nodeName + "' has no 'datasource'."); 285 } 286 287 if (factory == null) { 288 if (config.getDataSourceFactory() != null) { 289 logObj.log( 290 logLevel, 291 "Warning: <node> '" + nodeName + "' without 'factory'."); 292 } 293 else { 294 throw new ConfigurationException( 295 "Error: <node> '" + nodeName + "' without 'factory'."); 296 } 297 } 298 299 if (adapter == null) { 301 adapter = JdbcAdapter.class.getName(); 302 } 303 304 DbAdapter dbAdapter = null; 305 306 try { 307 ClassLoader cl = config.getClassLoader(); 308 Class dbAdapterClass = (cl != null) ? cl.loadClass(adapter) : Class 309 .forName(adapter); 310 dbAdapter = (DbAdapter) dbAdapterClass.newInstance(); 311 } 312 catch (Exception ex) { 313 logObj.log( 314 logLevel, 315 "instantiating adapter failed, using default adapter.", 316 ex); 317 getStatus().addFailedAdapter( 318 nodeName, 319 adapter, 320 "instantiating adapter failed - " + ex.getMessage()); 321 dbAdapter = new JdbcAdapter(); 322 } 323 324 DataNode node = new DataNode(nodeName); 325 node.setAdapter(dbAdapter); 326 node.setDataSourceFactory(factory); 327 node.setDataSourceLocation(dataSource); 328 329 try { 331 DataSourceFactory confFactory = config.getDataSourceFactory(); 334 DataSourceFactory localFactory = 335 (confFactory != null) 336 ? confFactory 337 : (DataSourceFactory) Class.forName(factory).newInstance(); 338 339 logObj.log(logLevel, "using factory: " + localFactory.getClass().getName()); 340 341 localFactory.initializeWithParentConfiguration(config); 342 DataSource ds = localFactory.getDataSource(dataSource, logLevel); 343 if (ds != null) { 344 logObj.log(logLevel, "loaded datasource."); 345 node.setDataSource(ds); 346 } 347 else { 348 logObj.log(logLevel, "Warning: null datasource."); 349 getStatus().getFailedDataSources().put(nodeName, dataSource); 350 } 351 } 352 catch (Exception ex) { 353 logObj.log(logLevel, "Error: DataSource load failed", ex); 354 getStatus().addFailedDataSource( 355 nodeName, 356 dataSource, 357 "DataSource load failed - " + ex.getMessage()); 358 } 359 360 try { 361 findDomain(domainName).addNode(node); 362 } 363 catch (FindException ex) { 364 logObj.log(logLevel, "Error: can't load node, unknown domain: " + domainName); 365 getStatus().addFailedDataSource( 366 nodeName, 367 nodeName, 368 "can't load node, unknown domain: " + domainName); 369 } 370 } 371 372 public void shouldLinkDataMap(String domainName, String nodeName, String mapName) { 373 374 if (mapName == null) { 375 logObj.log(logLevel, "<map-ref> has no 'name'."); 376 throw new ConfigurationException("<map-ref> has no 'name'."); 377 } 378 379 logObj.log(logLevel, "loaded map-ref: " + mapName + "."); 380 DataMap map = null; 381 DataNode node = null; 382 383 try { 384 map = findMap(domainName, mapName); 385 } 386 catch (FindException ex) { 387 logObj.log(logLevel, "Error: unknown map: " + mapName); 388 getStatus().addFailedMapRefs(mapName, "unknown map: " + mapName); 389 return; 390 } 391 392 try { 393 node = findNode(domainName, nodeName); 394 } 395 catch (FindException ex) { 396 logObj.log(logLevel, "Error: unknown node: " + nodeName); 397 getStatus().addFailedMapRefs(mapName, "unknown node: " + nodeName); 398 return; 399 } 400 401 node.addDataMap(map); 402 } 403 404 408 public Map getDomains() { 409 return domains; 410 } 411 412 416 public ConfigStatus getStatus() { 417 return status; 418 } 419 420 424 public Configuration getConfig() { 425 return config; 426 } 427 428 432 public void setConfig(Configuration config) { 433 this.config = config; 434 } 435 436 440 public Level getLogLevel() { 441 return this.logLevel; 442 } 443 444 448 public void setLogLevel(Level logLevel) { 449 this.logLevel = logLevel; 450 } 451 452 455 public void finishedLoading() { 456 if (status.hasFailures()) { 458 if (!config.isIgnoringLoadFailures()) { 459 StringBuffer msg = new StringBuffer (128); 460 msg.append("Load failures. Main configuration class: "); 461 msg.append(config.getClass().getName()); 462 msg.append(", details: "); 463 msg.append(status.describeFailures()); 464 throw new ConfigurationException(msg.toString()); 465 } 466 } 467 468 Iterator it = getDomains().values().iterator(); 470 while (it.hasNext()) { 471 config.addDomain((DataDomain) it.next()); 472 } 473 474 config.setDataViewLocations(views); 475 476 logObj.log( 477 logLevel, 478 "finished configuration loading in " 479 + (System.currentTimeMillis() - startTime) 480 + " ms."); 481 } 482 483 486 public void startedLoading() { 487 startTime = System.currentTimeMillis(); 488 logObj.log(logLevel, "started configuration loading."); 489 } 490 491 494 class FindException extends Exception { 495 499 public FindException(String msg) { 500 super(msg); 501 } 502 } 503 } 504 | Popular Tags |