1 19 20 package org.apache.cayenne.project; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.commons.lang.Validate; 32 import org.apache.cayenne.conf.ConfigLoader; 33 import org.apache.cayenne.conf.ConfigLoaderDelegate; 34 import org.apache.cayenne.conf.ConfigSaverDelegate; 35 import org.apache.cayenne.conf.ConfigStatus; 36 import org.apache.cayenne.conf.DriverDataSourceFactory; 37 import org.apache.cayenne.conf.JNDIDataSourceFactory; 38 39 46 public class PartialProject extends Project { 47 protected String projectVersion; 48 protected Map domains; 49 protected ConfigLoaderDelegate loadDelegate; 50 protected Map dataViewLocations; 51 52 56 public PartialProject(File projectFile) { 57 super(projectFile); 58 } 59 60 63 public void upgrade() throws ProjectException { 64 throw new ProjectException("'PartialProject' does not support upgrades."); 66 } 67 68 73 public void updateNodes(List list) throws ProjectException { 74 Iterator it = list.iterator(); 75 while (it.hasNext()) { 76 DataNodeConfigInfo nodeConfig = (DataNodeConfigInfo) it.next(); 77 String domainName = nodeConfig.getDomain(); 78 if (domainName == null && domains.size() != 1) { 79 throw new IllegalArgumentException ("Node must have domain set explicitly if there is no default domain."); 80 } 81 82 if (domainName == null) { 83 domainName = ((DomainMetaData) domains.values().toArray()[0]).name; 84 } 85 86 NodeMetaData node = findNode(domainName, nodeConfig.getName(), false); 87 if (node == null) { 88 continue; 89 } 90 91 if (nodeConfig.getAdapter() != null) { 92 node.adapter = nodeConfig.getAdapter(); 93 } 94 95 if (nodeConfig.getDataSource() != null) { 96 node.dataSource = nodeConfig.getDataSource(); 97 node.factory = JNDIDataSourceFactory.class.getName(); 98 } 99 else if (nodeConfig.getDriverFile() != null) { 100 node.dataSource = node.name + DataNodeFile.LOCATION_SUFFIX; 101 node.factory = DriverDataSourceFactory.class.getName(); 102 } 103 } 104 } 105 106 protected void prepareSave(List filesToSave, List wrappedObjects) 107 throws ProjectException { 108 filesToSave.addAll(files); 109 } 110 111 protected void postInitialize(File projectFile) { 112 loadDelegate = new LoadDelegate(); 113 domains = new HashMap (); 114 115 try { 116 FileInputStream in = new FileInputStream (projectFile); 117 try { 118 new ConfigLoader(loadDelegate).loadDomains(in); 119 } 120 catch (Exception ex) { 121 throw new ProjectException("Error creating PartialProject.", ex); 122 } 123 finally { 124 in.close(); 125 } 126 } 127 catch (IOException ioex) { 128 throw new ProjectException("Error creating PartialProject.", ioex); 129 } 130 131 super.postInitialize(projectFile); 132 } 133 134 public List getChildren() { 135 return new ArrayList (domains.values()); 136 } 137 138 public void checkForUpgrades() { 139 } 141 142 145 public List buildFileList() { 146 List list = new ArrayList (); 147 list.add(projectFileForObject(this)); 148 return list; 149 } 150 151 154 public ConfigStatus getLoadStatus() { 155 return loadDelegate.getStatus(); 156 } 157 158 public ProjectFile projectFileForObject(Object obj) { 159 if (obj != this) { 160 return null; 161 } 162 163 ApplicationProjectFile projectFile = new ApplicationProjectFile(this); 164 projectFile.setSaveDelegate(new SaveDelegate()); 165 return projectFile; 166 } 167 168 private DomainMetaData findDomain(String domainName) { 169 DomainMetaData domain = (DomainMetaData) domains.get(domainName); 170 if (domain == null) { 171 throw new IllegalArgumentException ("Can't find domain: " + domainName); 172 } 173 174 return domain; 175 } 176 177 private NodeMetaData findNode( 178 String domainName, 179 String nodeName, 180 boolean failIfNotFound) { 181 DomainMetaData domain = findDomain(domainName); 182 NodeMetaData node = (NodeMetaData) domain.nodes.get(nodeName); 183 if (node == null && failIfNotFound) { 184 throw new IllegalArgumentException ( 185 "Can't find node: " + domainName + "." + nodeName); 186 } 187 188 return node; 189 } 190 191 protected class DomainMetaData { 192 protected String name; 193 protected Map nodes = new HashMap (); 194 protected Map maps = new HashMap (); 195 protected Map mapDependencies = new HashMap (); 196 protected Map properties = new HashMap (); 197 198 public DomainMetaData(String name) { 199 this.name = name; 200 } 201 } 202 203 protected class NodeMetaData { 204 protected String name; 205 protected String dataSource; 206 protected String adapter; 207 protected String factory; 208 protected List maps = new ArrayList (); 209 210 public NodeMetaData(String name) { 211 this.name = name; 212 } 213 } 214 215 protected class MapMetaData { 216 protected String name; 217 protected String location; 218 219 public MapMetaData(String name) { 220 this.name = name; 221 } 222 } 223 224 class LoadDelegate implements ConfigLoaderDelegate { 225 protected ConfigStatus status = new ConfigStatus(); 226 227 public void shouldLoadProjectVersion(String version) { 228 PartialProject.this.projectVersion = version; 229 } 230 231 protected DomainMetaData findDomain(String name) { 232 DomainMetaData domain = (DomainMetaData) domains.get(name); 233 if (domain == null) { 234 throw new ProjectException("Can't find domain: " + name); 235 } 236 237 return domain; 238 } 239 240 protected MapMetaData findMap(String domainName, String mapName) { 241 DomainMetaData domain = findDomain(domainName); 242 MapMetaData map = (MapMetaData) domain.maps.get(mapName); 243 if (map == null) { 244 throw new ProjectException("Can't find map: " + mapName); 245 } 246 247 return map; 248 } 249 250 protected NodeMetaData findNode(String domainName, String nodeName) { 251 DomainMetaData domain = findDomain(domainName); 252 NodeMetaData node = (NodeMetaData) domain.nodes.get(nodeName); 253 if (node == null) { 254 throw new ProjectException("Can't find node: " + nodeName); 255 } 256 257 return node; 258 } 259 260 public void startedLoading() { 261 domains.clear(); 262 } 263 264 public void finishedLoading() { 265 } 266 267 public ConfigStatus getStatus() { 268 return status; 269 } 270 271 public boolean loadError(Throwable th) { 272 status.getOtherFailures().add(th.getMessage()); 273 return false; 274 } 275 276 public void shouldLinkDataMap( 277 String domainName, 278 String nodeName, 279 String mapName) { 280 findNode(domainName, nodeName).maps.add(mapName); 281 } 282 283 public void shouldLoadDataDomain(String name) { 284 domains.put(name, new DomainMetaData(name)); 285 } 286 287 public void shouldLoadDataDomainProperties(String domainName, Map properties) { 288 if (properties == null || properties.isEmpty()) { 289 return; 290 } 291 292 DomainMetaData domain = findDomain(domainName); 293 domain.properties.putAll(properties); 294 } 295 296 public void shouldLoadDataMaps(String domainName, Map locations) { 297 if (locations.size() == 0) { 298 return; 299 } 300 301 DomainMetaData domain = findDomain(domainName); 302 303 Iterator it = locations.entrySet().iterator(); 305 while (it.hasNext()) { 306 Map.Entry entry = (Map.Entry ) it.next(); 307 String name = (String ) entry.getKey(); 308 MapMetaData map = new MapMetaData(name); 309 map.location = (String ) entry.getValue(); 310 domain.maps.put(name, map); 311 } 312 } 313 314 public void shouldLoadDataNode( 315 String domainName, 316 String nodeName, 317 String dataSource, 318 String adapter, 319 String factory) { 320 321 NodeMetaData node = new NodeMetaData(nodeName); 322 node.adapter = adapter; 323 node.factory = factory; 324 node.dataSource = dataSource; 325 findDomain(domainName).nodes.put(nodeName, node); 326 } 327 328 public void shouldRegisterDataView( 329 String dataViewName, 330 String dataViewLocation) { 331 Validate.notNull(dataViewName); 332 Validate.notNull(dataViewLocation); 333 if (dataViewLocations == null) { 334 dataViewLocations = new HashMap (); 335 } 336 dataViewLocations.put(dataViewName, dataViewLocation); 337 } 338 } 339 340 class SaveDelegate implements ConfigSaverDelegate { 341 344 public String projectVersion() { 345 return projectVersion; 346 } 347 348 public Iterator domainNames() { 349 return domains.keySet().iterator(); 350 } 351 352 public Iterator viewNames() { 353 if (dataViewLocations == null) { 354 dataViewLocations = new HashMap (); 355 } 356 return dataViewLocations.keySet().iterator(); 357 } 358 359 public String viewLocation(String dataViewName) { 360 if (dataViewLocations == null) { 361 dataViewLocations = new HashMap (); 362 } 363 return (String ) dataViewLocations.get(dataViewName); 364 } 365 366 public Iterator propertyNames(String domainName) { 367 return findDomain(domainName).properties.keySet().iterator(); 368 } 369 370 public String propertyValue(String domainName, String propertyName) { 371 return (String ) findDomain(domainName).properties.get(propertyName); 372 } 373 374 public Iterator linkedMapNames(String domainName, String nodeName) { 375 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)) 376 .maps 377 .iterator(); 378 } 379 380 public String mapLocation(String domainName, String mapName) { 381 return ((MapMetaData) findDomain(domainName).maps.get(mapName)).location; 382 } 383 384 public Iterator mapNames(String domainName) { 385 return findDomain(domainName).maps.keySet().iterator(); 386 } 387 388 public String nodeAdapterName(String domainName, String nodeName) { 389 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).adapter; 390 } 391 392 public String nodeDataSourceName(String domainName, String nodeName) { 393 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).dataSource; 394 } 395 396 public String nodeFactoryName(String domainName, String nodeName) { 397 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).factory; 398 } 399 400 public Iterator nodeNames(String domainName) { 401 return findDomain(domainName).nodes.keySet().iterator(); 402 } 403 } 404 } 405 | Popular Tags |