KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.project;
57
58 import java.io.File JavaDoc;
59 import java.io.FileInputStream JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.Map JavaDoc;
66
67 import org.apache.commons.lang.Validate;
68 import org.objectstyle.cayenne.conf.ConfigLoader;
69 import org.objectstyle.cayenne.conf.ConfigLoaderDelegate;
70 import org.objectstyle.cayenne.conf.ConfigSaverDelegate;
71 import org.objectstyle.cayenne.conf.ConfigStatus;
72 import org.objectstyle.cayenne.conf.DriverDataSourceFactory;
73 import org.objectstyle.cayenne.conf.JNDIDataSourceFactory;
74
75 /**
76  * PartialProject is a "lightweight" project implementation. It can work with
77  * projects even when some of the resources are missing. It never instantiates
78  * Cayenne stack objects, using other, lightweight, data structures instead.
79  *
80  * @author Andrei Adamchik
81  */

82 public class PartialProject extends Project {
83     protected String JavaDoc projectVersion;
84     protected Map JavaDoc domains;
85     protected ConfigLoaderDelegate loadDelegate;
86     protected Map JavaDoc dataViewLocations;
87
88     /**
89      * Constructor PartialProjectHandler.
90      * @param projectFile
91      */

92     public PartialProject(File JavaDoc projectFile) {
93         super(projectFile);
94     }
95
96     /**
97      * @since 1.1
98      */

99     public void upgrade() throws ProjectException {
100         // upgrades not supported in this type of project
101
throw new ProjectException("'PartialProject' does not support upgrades.");
102     }
103
104     /**
105      * Loads internal project and rewrites its nodes according to the list of
106      * DataNodeConfigInfo objects. Only main project file gets updated, the rest
107      * are assumed to be in place.
108      */

109     public void updateNodes(List JavaDoc list) throws ProjectException {
110         Iterator JavaDoc it = list.iterator();
111         while (it.hasNext()) {
112             DataNodeConfigInfo nodeConfig = (DataNodeConfigInfo) it.next();
113             String JavaDoc domainName = nodeConfig.getDomain();
114             if (domainName == null && domains.size() != 1) {
115                 throw new IllegalArgumentException JavaDoc("Node must have domain set explicitly if there is no default domain.");
116             }
117
118             if (domainName == null) {
119                 domainName = ((DomainMetaData) domains.values().toArray()[0]).name;
120             }
121
122             NodeMetaData node = findNode(domainName, nodeConfig.getName(), false);
123             if (node == null) {
124                 continue;
125             }
126
127             if (nodeConfig.getAdapter() != null) {
128                 node.adapter = nodeConfig.getAdapter();
129             }
130
131             if (nodeConfig.getDataSource() != null) {
132                 node.dataSource = nodeConfig.getDataSource();
133                 node.factory = JNDIDataSourceFactory.class.getName();
134             }
135             else if (nodeConfig.getDriverFile() != null) {
136                 node.dataSource = node.name + DataNodeFile.LOCATION_SUFFIX;
137                 node.factory = DriverDataSourceFactory.class.getName();
138             }
139         }
140     }
141
142     protected void prepareSave(List JavaDoc filesToSave, List JavaDoc wrappedObjects)
143         throws ProjectException {
144         filesToSave.addAll(files);
145     }
146
147     protected void postInitialize(File JavaDoc projectFile) {
148         loadDelegate = new LoadDelegate();
149         domains = new HashMap JavaDoc();
150
151         try {
152             FileInputStream JavaDoc in = new FileInputStream JavaDoc(projectFile);
153             try {
154                 new ConfigLoader(loadDelegate).loadDomains(in);
155             }
156             catch (Exception JavaDoc ex) {
157                 throw new ProjectException("Error creating PartialProject.", ex);
158             }
159             finally {
160                 in.close();
161             }
162         }
163         catch (IOException JavaDoc ioex) {
164             throw new ProjectException("Error creating PartialProject.", ioex);
165         }
166
167         super.postInitialize(projectFile);
168     }
169
170     public List JavaDoc getChildren() {
171         return new ArrayList JavaDoc(domains.values());
172     }
173
174     public void checkForUpgrades() {
175         // do nothing...
176
}
177
178     /**
179      * @see org.objectstyle.cayenne.project.Project#buildFileList()
180      */

181     public List JavaDoc buildFileList() {
182         List JavaDoc list = new ArrayList JavaDoc();
183         list.add(projectFileForObject(this));
184         return list;
185     }
186
187     /**
188      * @see org.objectstyle.cayenne.project.Project#getLoadStatus()
189      */

190     public ConfigStatus getLoadStatus() {
191         return loadDelegate.getStatus();
192     }
193
194     public ProjectFile projectFileForObject(Object JavaDoc obj) {
195         if (obj != this) {
196             return null;
197         }
198
199         ApplicationProjectFile projectFile = new ApplicationProjectFile(this);
200         projectFile.setSaveDelegate(new SaveDelegate());
201         return projectFile;
202     }
203
204     private DomainMetaData findDomain(String JavaDoc domainName) {
205         DomainMetaData domain = (DomainMetaData) domains.get(domainName);
206         if (domain == null) {
207             throw new IllegalArgumentException JavaDoc("Can't find domain: " + domainName);
208         }
209
210         return domain;
211     }
212
213     private NodeMetaData findNode(
214         String JavaDoc domainName,
215         String JavaDoc nodeName,
216         boolean failIfNotFound) {
217         DomainMetaData domain = findDomain(domainName);
218         NodeMetaData node = (NodeMetaData) domain.nodes.get(nodeName);
219         if (node == null && failIfNotFound) {
220             throw new IllegalArgumentException JavaDoc(
221                 "Can't find node: " + domainName + "." + nodeName);
222         }
223
224         return node;
225     }
226
227     protected class DomainMetaData {
228         protected String JavaDoc name;
229         protected Map JavaDoc nodes = new HashMap JavaDoc();
230         protected Map JavaDoc maps = new HashMap JavaDoc();
231         protected Map JavaDoc mapDependencies = new HashMap JavaDoc();
232         protected Map JavaDoc properties = new HashMap JavaDoc();
233
234         public DomainMetaData(String JavaDoc name) {
235             this.name = name;
236         }
237     }
238
239     protected class NodeMetaData {
240         protected String JavaDoc name;
241         protected String JavaDoc dataSource;
242         protected String JavaDoc adapter;
243         protected String JavaDoc factory;
244         protected List JavaDoc maps = new ArrayList JavaDoc();
245
246         public NodeMetaData(String JavaDoc name) {
247             this.name = name;
248         }
249     }
250
251     protected class MapMetaData {
252         protected String JavaDoc name;
253         protected String JavaDoc location;
254
255         public MapMetaData(String JavaDoc name) {
256             this.name = name;
257         }
258     }
259
260     class LoadDelegate implements ConfigLoaderDelegate {
261         protected ConfigStatus status = new ConfigStatus();
262
263         public void shouldLoadProjectVersion(String JavaDoc version) {
264             PartialProject.this.projectVersion = version;
265         }
266
267         protected DomainMetaData findDomain(String JavaDoc name) {
268             DomainMetaData domain = (DomainMetaData) domains.get(name);
269             if (domain == null) {
270                 throw new ProjectException("Can't find domain: " + name);
271             }
272
273             return domain;
274         }
275
276         protected MapMetaData findMap(String JavaDoc domainName, String JavaDoc mapName) {
277             DomainMetaData domain = findDomain(domainName);
278             MapMetaData map = (MapMetaData) domain.maps.get(mapName);
279             if (map == null) {
280                 throw new ProjectException("Can't find map: " + mapName);
281             }
282
283             return map;
284         }
285
286         protected NodeMetaData findNode(String JavaDoc domainName, String JavaDoc nodeName) {
287             DomainMetaData domain = findDomain(domainName);
288             NodeMetaData node = (NodeMetaData) domain.nodes.get(nodeName);
289             if (node == null) {
290                 throw new ProjectException("Can't find node: " + nodeName);
291             }
292
293             return node;
294         }
295
296         public void startedLoading() {
297             domains.clear();
298         }
299
300         public void finishedLoading() {
301         }
302
303         public ConfigStatus getStatus() {
304             return status;
305         }
306
307         public boolean loadError(Throwable JavaDoc th) {
308             status.getOtherFailures().add(th.getMessage());
309             return false;
310         }
311
312         public void shouldLinkDataMap(
313             String JavaDoc domainName,
314             String JavaDoc nodeName,
315             String JavaDoc mapName) {
316             findNode(domainName, nodeName).maps.add(mapName);
317         }
318
319         public void shouldLoadDataDomain(String JavaDoc name) {
320             domains.put(name, new DomainMetaData(name));
321         }
322
323         public void shouldLoadDataDomainProperties(String JavaDoc domainName, Map JavaDoc properties) {
324             if (properties == null || properties.isEmpty()) {
325                 return;
326             }
327
328             DomainMetaData domain = findDomain(domainName);
329             domain.properties.putAll(properties);
330         }
331
332         public void shouldLoadDataMaps(String JavaDoc domainName, Map JavaDoc locations) {
333             if (locations.size() == 0) {
334                 return;
335             }
336
337             DomainMetaData domain = findDomain(domainName);
338
339             // load DataMaps tree
340
Iterator JavaDoc it = locations.keySet().iterator();
341             while (it.hasNext()) {
342                 String JavaDoc name = (String JavaDoc) it.next();
343                 MapMetaData map = new MapMetaData(name);
344                 map.location = (String JavaDoc) locations.get(name);
345                 domain.maps.put(name, map);
346             }
347         }
348
349         public void shouldLoadDataNode(
350             String JavaDoc domainName,
351             String JavaDoc nodeName,
352             String JavaDoc dataSource,
353             String JavaDoc adapter,
354             String JavaDoc factory) {
355
356             NodeMetaData node = new NodeMetaData(nodeName);
357             node.adapter = adapter;
358             node.factory = factory;
359             node.dataSource = dataSource;
360             findDomain(domainName).nodes.put(nodeName, node);
361         }
362
363         public void shouldRegisterDataView(
364             String JavaDoc dataViewName,
365             String JavaDoc dataViewLocation) {
366             Validate.notNull(dataViewName);
367             Validate.notNull(dataViewLocation);
368             if (dataViewLocations == null) {
369                 dataViewLocations = new HashMap JavaDoc();
370             }
371             dataViewLocations.put(dataViewName, dataViewLocation);
372         }
373     }
374
375     class SaveDelegate implements ConfigSaverDelegate {
376         /**
377          * @since 1.1
378          */

379         public String JavaDoc projectVersion() {
380             return projectVersion;
381         }
382
383         public Iterator JavaDoc domainNames() {
384             return domains.keySet().iterator();
385         }
386
387         public Iterator JavaDoc viewNames() {
388             if (dataViewLocations == null) {
389                 dataViewLocations = new HashMap JavaDoc();
390             }
391             return dataViewLocations.keySet().iterator();
392         }
393
394         public String JavaDoc viewLocation(String JavaDoc dataViewName) {
395             if (dataViewLocations == null) {
396                 dataViewLocations = new HashMap JavaDoc();
397             }
398             return (String JavaDoc) dataViewLocations.get(dataViewName);
399         }
400
401         public Iterator JavaDoc propertyNames(String JavaDoc domainName) {
402             return findDomain(domainName).properties.keySet().iterator();
403         }
404
405         public String JavaDoc propertyValue(String JavaDoc domainName, String JavaDoc propertyName) {
406             return (String JavaDoc) findDomain(domainName).properties.get(propertyName);
407         }
408
409         public Iterator JavaDoc linkedMapNames(String JavaDoc domainName, String JavaDoc nodeName) {
410             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName))
411                 .maps
412                 .iterator();
413         }
414
415         public String JavaDoc mapLocation(String JavaDoc domainName, String JavaDoc mapName) {
416             return ((MapMetaData) findDomain(domainName).maps.get(mapName)).location;
417         }
418
419         public Iterator JavaDoc mapNames(String JavaDoc domainName) {
420             return findDomain(domainName).maps.keySet().iterator();
421         }
422
423         public String JavaDoc nodeAdapterName(String JavaDoc domainName, String JavaDoc nodeName) {
424             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).adapter;
425         }
426
427         public String JavaDoc nodeDataSourceName(String JavaDoc domainName, String JavaDoc nodeName) {
428             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).dataSource;
429         }
430
431         public String JavaDoc nodeFactoryName(String JavaDoc domainName, String JavaDoc nodeName) {
432             return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).factory;
433         }
434
435         public Iterator JavaDoc nodeNames(String JavaDoc domainName) {
436             return findDomain(domainName).nodes.keySet().iterator();
437         }
438     }
439 }
440
Popular Tags