KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
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 /**
40  * PartialProject is a "lightweight" project implementation. It can work with
41  * projects even when some of the resources are missing. It never instantiates
42  * Cayenne stack objects, using other, lightweight, data structures instead.
43  *
44  * @author Andrus Adamchik
45  */

46 public class PartialProject extends Project {
47     protected String JavaDoc projectVersion;
48     protected Map JavaDoc domains;
49     protected ConfigLoaderDelegate loadDelegate;
50     protected Map JavaDoc dataViewLocations;
51
52     /**
53      * Constructor PartialProjectHandler.
54      * @param projectFile
55      */

56     public PartialProject(File JavaDoc projectFile) {
57         super(projectFile);
58     }
59
60     /**
61      * @since 1.1
62      */

63     public void upgrade() throws ProjectException {
64         // upgrades not supported in this type of project
65
throw new ProjectException("'PartialProject' does not support upgrades.");
66     }
67
68     /**
69      * Loads internal project and rewrites its nodes according to the list of
70      * DataNodeConfigInfo objects. Only main project file gets updated, the rest
71      * are assumed to be in place.
72      */

73     public void updateNodes(List JavaDoc list) throws ProjectException {
74         Iterator JavaDoc it = list.iterator();
75         while (it.hasNext()) {
76             DataNodeConfigInfo nodeConfig = (DataNodeConfigInfo) it.next();
77             String JavaDoc domainName = nodeConfig.getDomain();
78             if (domainName == null && domains.size() != 1) {
79                 throw new IllegalArgumentException JavaDoc("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 JavaDoc filesToSave, List JavaDoc wrappedObjects)
107         throws ProjectException {
108         filesToSave.addAll(files);
109     }
110
111     protected void postInitialize(File JavaDoc projectFile) {
112         loadDelegate = new LoadDelegate();
113         domains = new HashMap JavaDoc();
114
115         try {
116             FileInputStream JavaDoc in = new FileInputStream JavaDoc(projectFile);
117             try {
118                 new ConfigLoader(loadDelegate).loadDomains(in);
119             }
120             catch (Exception JavaDoc ex) {
121                 throw new ProjectException("Error creating PartialProject.", ex);
122             }
123             finally {
124                 in.close();
125             }
126         }
127         catch (IOException JavaDoc ioex) {
128             throw new ProjectException("Error creating PartialProject.", ioex);
129         }
130
131         super.postInitialize(projectFile);
132     }
133
134     public List JavaDoc getChildren() {
135         return new ArrayList JavaDoc(domains.values());
136     }
137
138     public void checkForUpgrades() {
139         // do nothing...
140
}
141
142     /**
143      * @see org.apache.cayenne.project.Project#buildFileList()
144      */

145     public List JavaDoc buildFileList() {
146         List JavaDoc list = new ArrayList JavaDoc();
147         list.add(projectFileForObject(this));
148         return list;
149     }
150
151     /**
152      * @see org.apache.cayenne.project.Project#getLoadStatus()
153      */

154     public ConfigStatus getLoadStatus() {
155         return loadDelegate.getStatus();
156     }
157
158     public ProjectFile projectFileForObject(Object JavaDoc 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 JavaDoc domainName) {
169         DomainMetaData domain = (DomainMetaData) domains.get(domainName);
170         if (domain == null) {
171             throw new IllegalArgumentException JavaDoc("Can't find domain: " + domainName);
172         }
173
174         return domain;
175     }
176
177     private NodeMetaData findNode(
178         String JavaDoc domainName,
179         String JavaDoc 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 JavaDoc(
185                 "Can't find node: " + domainName + "." + nodeName);
186         }
187
188         return node;
189     }
190
191     protected class DomainMetaData {
192         protected String JavaDoc name;
193         protected Map JavaDoc nodes = new HashMap JavaDoc();
194         protected Map JavaDoc maps = new HashMap JavaDoc();
195         protected Map JavaDoc mapDependencies = new HashMap JavaDoc();
196         protected Map JavaDoc properties = new HashMap JavaDoc();
197
198         public DomainMetaData(String JavaDoc name) {
199             this.name = name;
200         }
201     }
202
203     protected class NodeMetaData {
204         protected String JavaDoc name;
205         protected String JavaDoc dataSource;
206         protected String JavaDoc adapter;
207         protected String JavaDoc factory;
208         protected List JavaDoc maps = new ArrayList JavaDoc();
209
210         public NodeMetaData(String JavaDoc name) {
211             this.name = name;
212         }
213     }
214
215     protected class MapMetaData {
216         protected String JavaDoc name;
217         protected String JavaDoc location;
218
219         public MapMetaData(String JavaDoc 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 JavaDoc version) {
228             PartialProject.this.projectVersion = version;
229         }
230
231         protected DomainMetaData findDomain(String JavaDoc 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 JavaDoc domainName, String JavaDoc 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 JavaDoc domainName, String JavaDoc 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 JavaDoc th) {
272             status.getOtherFailures().add(th.getMessage());
273             return false;
274         }
275
276         public void shouldLinkDataMap(
277             String JavaDoc domainName,
278             String JavaDoc nodeName,
279             String JavaDoc mapName) {
280             findNode(domainName, nodeName).maps.add(mapName);
281         }
282
283         public void shouldLoadDataDomain(String JavaDoc name) {
284             domains.put(name, new DomainMetaData(name));
285         }
286
287         public void shouldLoadDataDomainProperties(String JavaDoc domainName, Map JavaDoc 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 JavaDoc domainName, Map JavaDoc locations) {
297             if (locations.size() == 0) {
298                 return;
299             }
300
301             DomainMetaData domain = findDomain(domainName);
302
303             // load DataMaps tree
304
Iterator JavaDoc it = locations.entrySet().iterator();
305             while (it.hasNext()) {
306                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
307                 String JavaDoc name = (String JavaDoc) entry.getKey();
308                 MapMetaData map = new MapMetaData(name);
309                 map.location = (String JavaDoc) entry.getValue();
310                 domain.maps.put(name, map);
311             }
312         }
313
314         public void shouldLoadDataNode(
315             String JavaDoc domainName,
316             String JavaDoc nodeName,
317             String JavaDoc dataSource,
318             String JavaDoc adapter,
319             String JavaDoc 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 JavaDoc dataViewName,
330             String JavaDoc dataViewLocation) {
331             Validate.notNull(dataViewName);
332             Validate.notNull(dataViewLocation);
333             if (dataViewLocations == null) {
334                 dataViewLocations = new HashMap JavaDoc();
335             }
336             dataViewLocations.put(dataViewName, dataViewLocation);
337         }
338     }
339
340     class SaveDelegate implements ConfigSaverDelegate {
341         /**
342          * @since 1.1
343          */

344         public String JavaDoc projectVersion() {
345             return projectVersion;
346         }
347
348         public Iterator JavaDoc domainNames() {
349             return domains.keySet().iterator();
350         }
351
352         public Iterator JavaDoc viewNames() {
353             if (dataViewLocations == null) {
354                 dataViewLocations = new HashMap JavaDoc();
355             }
356             return dataViewLocations.keySet().iterator();
357         }
358
359         public String JavaDoc viewLocation(String JavaDoc dataViewName) {
360             if (dataViewLocations == null) {
361                 dataViewLocations = new HashMap JavaDoc();
362             }
363             return (String JavaDoc) dataViewLocations.get(dataViewName);
364         }
365
366         public Iterator JavaDoc propertyNames(String JavaDoc domainName) {
367             return findDomain(domainName).properties.keySet().iterator();
368         }
369
370         public String JavaDoc propertyValue(String JavaDoc domainName, String JavaDoc propertyName) {
371             return (String JavaDoc) findDomain(domainName).properties.get(propertyName);
372         }
373
374         public Iterator JavaDoc linkedMapNames(String JavaDoc domainName, String JavaDoc nodeName) {
375             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName))
376                 .maps
377                 .iterator();
378         }
379
380         public String JavaDoc mapLocation(String JavaDoc domainName, String JavaDoc mapName) {
381             return ((MapMetaData) findDomain(domainName).maps.get(mapName)).location;
382         }
383
384         public Iterator JavaDoc mapNames(String JavaDoc domainName) {
385             return findDomain(domainName).maps.keySet().iterator();
386         }
387
388         public String JavaDoc nodeAdapterName(String JavaDoc domainName, String JavaDoc nodeName) {
389             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).adapter;
390         }
391
392         public String JavaDoc nodeDataSourceName(String JavaDoc domainName, String JavaDoc nodeName) {
393             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).dataSource;
394         }
395
396         public String JavaDoc nodeFactoryName(String JavaDoc domainName, String JavaDoc nodeName) {
397             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).factory;
398         }
399
400         public Iterator JavaDoc nodeNames(String JavaDoc domainName) {
401             return findDomain(domainName).nodes.keySet().iterator();
402         }
403     }
404 }
405
Popular Tags