KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > ApplicationProject


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
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 /**
39  * Represents Cayenne application project.
40  *
41  * @author Andrus Adamchik
42  */

43 public class ApplicationProject extends Project {
44
45     protected Configuration configuration;
46
47     /**
48      * Constructor for ApplicationProject.
49      *
50      * @param projectFile
51      */

52     public ApplicationProject(File JavaDoc projectFile) {
53         this(projectFile, null);
54     }
55
56     /**
57      * @since 1.2
58      */

59     public ApplicationProject(File JavaDoc projectFile, Configuration configuration) {
60
61         if (configuration == null) {
62
63             // normalize project file...
64
if (projectFile != null) {
65
66                 if (projectFile.isDirectory()) {
67                     projectFile = new File JavaDoc(projectFile.getPath()
68                             + File.separator
69                             + Configuration.DEFAULT_DOMAIN_FILE);
70                 }
71
72                 try {
73                     projectFile = projectFile.getCanonicalFile();
74                 }
75                 catch (IOException JavaDoc 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     /**
91      * @since 1.1
92      */

93     public void upgrade() throws ProjectException {
94         ApplicationUpgradeHandler.sharedHandler().performUpgrade(this);
95     }
96
97     /**
98      * Initializes internal <code>Configuration</code> object and then calls super.
99      */

100     protected void postInitialize(File JavaDoc projectFile) {
101         loadProject();
102         super.postInitialize(projectFile);
103     }
104
105     /**
106      * @since 1.2
107      */

108     protected void loadProject() {
109
110         // try to initialize configuration
111
if (configuration.canInitialize()) {
112
113             try {
114                 configuration.initialize();
115             }
116             catch (Exception JavaDoc e) {
117                 throw new ProjectException(
118                         "Error initializaing project configuration.",
119                         e);
120             }
121             configuration.didInitialize();
122         }
123
124         // set default version
125
if (configuration.getProjectVersion() == null) {
126             configuration.setProjectVersion(ApplicationUpgradeHandler
127                     .sharedHandler()
128                     .supportedVersion());
129         }
130     }
131
132     /**
133      * Returns Cayenne configuration object associated with this project.
134      */

135     public Configuration getConfiguration() {
136         return configuration;
137     }
138
139     /**
140      * Sets Cayenne configuration object associated with this project.
141      */

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     /**
153      * @see org.apache.cayenne.project.Project#getChildren()
154      */

155     public List JavaDoc getChildren() {
156         return new ArrayList JavaDoc(this.getConfiguration().getDomains());
157     }
158
159     /**
160      * Returns appropriate ProjectFile or null if object does not require a file of its
161      * own. In case of ApplicationProject, the nodes that require separate filed are: the
162      * project itself, each DataMap, each driver DataNode.
163      */

164     public ProjectFile projectFileForObject(Object JavaDoc obj) {
165         if (requiresProjectFile(obj)) {
166             String JavaDoc domainFileName = this.getConfiguration().getDomainConfigurationName();
167             ApplicationProjectFile file = new ApplicationProjectFile(this, domainFileName);
168
169             // inject save delegate...
170
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 JavaDoc obj) {
184         return obj == this;
185     }
186
187     protected boolean requiresMapFile(Object JavaDoc obj) {
188         return obj instanceof DataMap;
189     }
190
191     protected boolean requiresNodeFile(Object JavaDoc obj) {
192         if (obj instanceof DataNode) {
193             DataNode node = (DataNode) obj;
194
195             // only driver datasource factory requires a file
196
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 JavaDoc domainName) {
218             super.shouldLoadDataDomain(domainName);
219
220             try {
221                 // disable class indexing
222
findDomain(domainName).getEntityResolver().setIndexedByClass(false);
223             }
224             catch (Exception JavaDoc ex) {
225                 throw new ConfigurationException("Domain is not loaded: " + domainName);
226             }
227         }
228
229         public void shouldLoadDataDomainProperties(String JavaDoc domainName, Map JavaDoc properties) {
230
231             // remove factory property to avoid instatiation attempts for unknown/invalid
232
// classes
233

234             Map JavaDoc propertiesClone = new HashMap JavaDoc(properties);
235             Object JavaDoc dataContextFactory = propertiesClone
236                     .remove(DataDomain.DATA_CONTEXT_FACTORY_PROPERTY);
237
238             super.shouldLoadDataDomainProperties(domainName, propertiesClone);
239
240             // stick property back in...
241
if (dataContextFactory != null) {
242                 try {
243                     findDomain(domainName).getProperties().put(
244                             DataDomain.DATA_CONTEXT_FACTORY_PROPERTY,
245                             dataContextFactory);
246                 }
247                 catch (Exception JavaDoc ex) {
248                     throw new ConfigurationException("Domain is not loaded: "
249                             + domainName);
250                 }
251             }
252         }
253     }
254 }
255
Popular Tags