1 19 20 package org.apache.cayenne.project; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.cayenne.ConfigurationException; 30 import org.apache.cayenne.access.DataDomain; 31 import org.apache.cayenne.access.DataNode; 32 import org.apache.cayenne.conf.ConfigStatus; 33 import org.apache.cayenne.conf.Configuration; 34 import org.apache.cayenne.conf.DriverDataSourceFactory; 35 import org.apache.cayenne.conf.RuntimeLoadDelegate; 36 import org.apache.cayenne.map.DataMap; 37 38 43 public class ApplicationProject extends Project { 44 45 protected Configuration configuration; 46 47 52 public ApplicationProject(File projectFile) { 53 this(projectFile, null); 54 } 55 56 59 public ApplicationProject(File projectFile, Configuration configuration) { 60 61 if (configuration == null) { 62 63 if (projectFile != null) { 65 66 if (projectFile.isDirectory()) { 67 projectFile = new File (projectFile.getPath() 68 + File.separator 69 + Configuration.DEFAULT_DOMAIN_FILE); 70 } 71 72 try { 73 projectFile = projectFile.getCanonicalFile(); 74 } 75 catch (IOException e) { 76 throw new ProjectException("Bad project file: " + projectFile); 77 } 78 } 79 80 configuration = new ProjectConfiguration(projectFile); 81 configuration.setLoaderDelegate(new ProjectLoader(configuration)); 82 } 83 84 this.configuration = configuration; 85 86 initialize(projectFile); 87 postInitialize(projectFile); 88 } 89 90 93 public void upgrade() throws ProjectException { 94 ApplicationUpgradeHandler.sharedHandler().performUpgrade(this); 95 } 96 97 100 protected void postInitialize(File projectFile) { 101 loadProject(); 102 super.postInitialize(projectFile); 103 } 104 105 108 protected void loadProject() { 109 110 if (configuration.canInitialize()) { 112 113 try { 114 configuration.initialize(); 115 } 116 catch (Exception e) { 117 throw new ProjectException( 118 "Error initializaing project configuration.", 119 e); 120 } 121 configuration.didInitialize(); 122 } 123 124 if (configuration.getProjectVersion() == null) { 126 configuration.setProjectVersion(ApplicationUpgradeHandler 127 .sharedHandler() 128 .supportedVersion()); 129 } 130 } 131 132 135 public Configuration getConfiguration() { 136 return configuration; 137 } 138 139 142 public void setConfiguration(ProjectConfiguration config) { 143 this.configuration = config; 144 } 145 146 public void checkForUpgrades() { 147 this.upgradeStatus = ApplicationUpgradeHandler.sharedHandler().checkForUpgrades( 148 configuration, 149 upgradeMessages); 150 } 151 152 155 public List getChildren() { 156 return new ArrayList (this.getConfiguration().getDomains()); 157 } 158 159 164 public ProjectFile projectFileForObject(Object obj) { 165 if (requiresProjectFile(obj)) { 166 String domainFileName = this.getConfiguration().getDomainConfigurationName(); 167 ApplicationProjectFile file = new ApplicationProjectFile(this, domainFileName); 168 169 file.setSaveDelegate(configuration.getSaverDelegate()); 171 return file; 172 } 173 else if (requiresMapFile(obj)) { 174 return new DataMapFile(this, (DataMap) obj); 175 } 176 else if (requiresNodeFile(obj)) { 177 return new DataNodeFile(this, (DataNode) obj); 178 } 179 180 return null; 181 } 182 183 protected boolean requiresProjectFile(Object obj) { 184 return obj == this; 185 } 186 187 protected boolean requiresMapFile(Object obj) { 188 return obj instanceof DataMap; 189 } 190 191 protected boolean requiresNodeFile(Object obj) { 192 if (obj instanceof DataNode) { 193 DataNode node = (DataNode) obj; 194 195 if (DriverDataSourceFactory.class.getName().equals( 197 node.getDataSourceFactory())) { 198 return true; 199 } 200 } 201 202 return false; 203 } 204 205 public ConfigStatus getLoadStatus() { 206 return (configuration != null) 207 ? configuration.getLoadStatus() 208 : new ConfigStatus(); 209 } 210 211 final class ProjectLoader extends RuntimeLoadDelegate { 212 213 public ProjectLoader(Configuration config) { 214 super(config, config.getLoadStatus()); 215 } 216 217 public void shouldLoadDataDomain(String domainName) { 218 super.shouldLoadDataDomain(domainName); 219 220 try { 221 findDomain(domainName).getEntityResolver().setIndexedByClass(false); 223 } 224 catch (Exception ex) { 225 throw new ConfigurationException("Domain is not loaded: " + domainName); 226 } 227 } 228 229 public void shouldLoadDataDomainProperties(String domainName, Map properties) { 230 231 234 Map propertiesClone = new HashMap (properties); 235 Object dataContextFactory = propertiesClone 236 .remove(DataDomain.DATA_CONTEXT_FACTORY_PROPERTY); 237 238 super.shouldLoadDataDomainProperties(domainName, propertiesClone); 239 240 if (dataContextFactory != null) { 242 try { 243 findDomain(domainName).getProperties().put( 244 DataDomain.DATA_CONTEXT_FACTORY_PROPERTY, 245 dataContextFactory); 246 } 247 catch (Exception ex) { 248 throw new ConfigurationException("Domain is not loaded: " 249 + domainName); 250 } 251 } 252 } 253 } 254 } 255 | Popular Tags |