KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > conf > ConfigSaver


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
57 package org.objectstyle.cayenne.conf;
58
59 import java.io.PrintWriter JavaDoc;
60 import java.util.Iterator JavaDoc;
61
62 import org.objectstyle.cayenne.conn.DataSourceInfo;
63 import org.objectstyle.cayenne.project.Project;
64 import org.objectstyle.cayenne.util.Util;
65
66 /**
67  * Class that does saving of Cayenne configuration.
68  *
69  * @author Andrei Adamchik
70  */

71 public class ConfigSaver {
72     protected ConfigSaverDelegate delegate;
73
74     /**
75      * Constructor for ConfigSaver.
76      */

77     public ConfigSaver() {
78         super();
79     }
80
81     /**
82        * Constructor for ConfigSaver.
83        */

84     public ConfigSaver(ConfigSaverDelegate delegate) {
85         this.delegate = delegate;
86     }
87
88     /**
89      * Saves domains into the specified file. Assumes that the maps have already
90      * been saved.
91      */

92     public void storeDomains(PrintWriter JavaDoc pw) {
93         pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
94         pw.println("<domains project-version=\"" + delegate.projectVersion() + "\">");
95
96         Iterator JavaDoc it = delegate.domainNames();
97         while (it.hasNext()) {
98             storeDomain(pw, (String JavaDoc) it.next());
99         }
100
101         Iterator JavaDoc views = delegate.viewNames();
102         while (views.hasNext()) {
103             storeDataView(pw, (String JavaDoc) views.next());
104         }
105         pw.println("</domains>");
106     }
107
108     protected void storeDataView(PrintWriter JavaDoc pw, String JavaDoc dataViewName) {
109         String JavaDoc location = delegate.viewLocation(dataViewName);
110         pw.print("<view name=\"" + dataViewName.trim());
111         pw.print("\" location=\"" + location.trim());
112         pw.println("\"/>");
113     }
114
115     protected void storeDomain(PrintWriter JavaDoc pw, String JavaDoc domainName) {
116         pw.println("<domain name=\"" + domainName.trim() + "\">");
117
118         // store properties
119
Iterator JavaDoc properties = delegate.propertyNames(domainName);
120         boolean breakNeeded = properties.hasNext();
121         while (properties.hasNext()) {
122             String JavaDoc name = (String JavaDoc) properties.next();
123             if (name == null) {
124                 continue;
125             }
126
127             String JavaDoc value = delegate.propertyValue(domainName, name);
128             if (value == null) {
129                 continue;
130             }
131
132             pw.print("\t<property name=\"" + Util.encodeXmlAttribute(name.trim()));
133             pw.println("\" value=\"" + Util.encodeXmlAttribute(value.trim()) + "\"/>");
134         }
135
136         // store maps
137
Iterator JavaDoc maps = delegate.mapNames(domainName);
138         if (maps.hasNext()) {
139             if (breakNeeded) {
140                 pw.println();
141             }
142
143             breakNeeded = true;
144         }
145
146         while (maps.hasNext()) {
147             String JavaDoc mapName = (String JavaDoc) maps.next();
148             String JavaDoc mapLocation = delegate.mapLocation(domainName, mapName);
149
150             pw.print("\t<map name=\"" + mapName.trim());
151             pw.print("\" location=\"" + mapLocation.trim());
152             pw.println("\"/>");
153         }
154
155         // store nodes
156
Iterator JavaDoc nodes = delegate.nodeNames(domainName);
157         if (nodes.hasNext() && breakNeeded) {
158             pw.println();
159         }
160         while (nodes.hasNext()) {
161             String JavaDoc nodeName = (String JavaDoc) nodes.next();
162             String JavaDoc datasource = delegate.nodeDataSourceName(domainName, nodeName);
163             String JavaDoc adapter = delegate.nodeAdapterName(domainName, nodeName);
164             String JavaDoc factory = delegate.nodeFactoryName(domainName, nodeName);
165             Iterator JavaDoc mapNames = delegate.linkedMapNames(domainName, nodeName);
166
167             pw.println("\t<node name=\"" + nodeName.trim() + "\"");
168
169             if (datasource != null) {
170                 datasource = datasource.trim();
171                 pw.print("\t\t datasource=\"" + datasource + "\"");
172             }
173             pw.println("");
174
175             if (adapter != null) {
176                 pw.println("\t\t adapter=\"" + adapter + "\"");
177             }
178
179             if (factory != null) {
180                 pw.print("\t\t factory=\"" + factory.trim() + "\"");
181             }
182             pw.println(">");
183
184             while (mapNames.hasNext()) {
185                 String JavaDoc mapName = (String JavaDoc) mapNames.next();
186                 pw.println("\t\t\t<map-ref name=\"" + mapName.trim() + "\"/>");
187             }
188             pw.println("\t </node>");
189         }
190         pw.println("</domain>");
191     }
192
193     /**
194      * Stores DataSolurceInfo to the specified PrintWriter.
195      * <code>info</code> object may contain full or partial information.
196      */

197     public void storeDataNode(PrintWriter JavaDoc out, DataSourceInfo info) {
198         out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
199         out.print("<driver project-version=\"" + Project.CURRENT_PROJECT_VERSION + "\"");
200         if (info.getJdbcDriver() != null) {
201             out.print(" class=\"" + info.getJdbcDriver() + "\"");
202         }
203         out.println(">");
204
205         if (info.getDataSourceUrl() != null) {
206             String JavaDoc encoded = Util.encodeXmlAttribute(info.getDataSourceUrl());
207             out.println("\t<url value=\"" + encoded + "\"/>");
208         }
209
210         out.println(
211             "\t<connectionPool min=\""
212                 + info.getMinConnections()
213                 + "\" max=\""
214                 + info.getMaxConnections()
215                 + "\" />");
216
217         if (info.getUserName() != null || info.getPassword() != null) {
218             out.print("\t<login");
219             if (info.getUserName() != null) {
220                 String JavaDoc encoded = Util.encodeXmlAttribute(info.getUserName());
221                 out.print(" userName=\"" + encoded + "\"");
222             }
223             if (info.getPassword() != null) {
224                 String JavaDoc encoded = Util.encodeXmlAttribute(info.getPassword());
225                 out.print(" password=\"" + encoded + "\"");
226             }
227             out.println("/>");
228         }
229
230         out.println("</driver>");
231     }
232 }
233
Popular Tags