KickJava   Java API By Example, From Geeks To Geeks.

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


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.conf;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.apache.cayenne.conn.DataSourceInfo;
30 import org.apache.cayenne.project.Project;
31 import org.apache.cayenne.util.Util;
32
33 /**
34  * Class that does saving of Cayenne configuration.
35  *
36  * @author Andrus Adamchik
37  */

38 public class ConfigSaver {
39
40     protected ConfigSaverDelegate delegate;
41
42     /**
43      * Constructor for ConfigSaver.
44      */

45     public ConfigSaver() {
46         super();
47     }
48
49     /**
50      * Constructor for ConfigSaver.
51      */

52     public ConfigSaver(ConfigSaverDelegate delegate) {
53         this.delegate = delegate;
54     }
55
56     /**
57      * Saves domains into the specified file. Assumes that the maps have already been
58      * saved.
59      */

60     public void storeDomains(PrintWriter JavaDoc pw) {
61         pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
62         pw.println("<domains project-version=\"" + delegate.projectVersion() + "\">");
63
64         Iterator JavaDoc it = delegate.domainNames();
65         while (it.hasNext()) {
66             storeDomain(pw, (String JavaDoc) it.next());
67         }
68
69         Iterator JavaDoc views = delegate.viewNames();
70         while (views.hasNext()) {
71             storeDataView(pw, (String JavaDoc) views.next());
72         }
73         pw.println("</domains>");
74     }
75
76     protected void storeDataView(PrintWriter JavaDoc pw, String JavaDoc dataViewName) {
77         String JavaDoc location = delegate.viewLocation(dataViewName);
78         pw.print("<view name=\"" + dataViewName.trim());
79         pw.print("\" location=\"" + location.trim());
80         pw.println("\"/>");
81     }
82
83     protected void storeDomain(PrintWriter JavaDoc pw, String JavaDoc domainName) {
84         pw.println("<domain name=\"" + domainName.trim() + "\">");
85
86         // store properties
87
Iterator JavaDoc properties = delegate.propertyNames(domainName);
88         boolean breakNeeded = properties.hasNext();
89         while (properties.hasNext()) {
90             String JavaDoc name = (String JavaDoc) properties.next();
91             if (name == null) {
92                 continue;
93             }
94
95             String JavaDoc value = delegate.propertyValue(domainName, name);
96             if (value == null) {
97                 continue;
98             }
99
100             pw.print("\t<property name=\"" + Util.encodeXmlAttribute(name.trim()));
101             pw.println("\" value=\"" + Util.encodeXmlAttribute(value.trim()) + "\"/>");
102         }
103
104         // store maps
105
Iterator JavaDoc maps = delegate.mapNames(domainName);
106         if (maps.hasNext()) {
107             if (breakNeeded) {
108                 pw.println();
109             }
110
111             breakNeeded = true;
112         }
113
114         while (maps.hasNext()) {
115             String JavaDoc mapName = (String JavaDoc) maps.next();
116             String JavaDoc mapLocation = delegate.mapLocation(domainName, mapName);
117
118             pw.print("\t<map name=\"" + mapName.trim());
119             pw.print("\" location=\"" + mapLocation.trim());
120             pw.println("\"/>");
121         }
122
123         // store nodes
124
Iterator JavaDoc nodes = delegate.nodeNames(domainName);
125         if (nodes.hasNext() && breakNeeded) {
126             pw.println();
127         }
128         while (nodes.hasNext()) {
129             String JavaDoc nodeName = (String JavaDoc) nodes.next();
130             String JavaDoc datasource = delegate.nodeDataSourceName(domainName, nodeName);
131             String JavaDoc adapter = delegate.nodeAdapterName(domainName, nodeName);
132             String JavaDoc factory = delegate.nodeFactoryName(domainName, nodeName);
133             Iterator JavaDoc mapNames = delegate.linkedMapNames(domainName, nodeName);
134
135             pw.println("\t<node name=\"" + nodeName.trim() + "\"");
136
137             if (datasource != null) {
138                 datasource = datasource.trim();
139                 pw.print("\t\t datasource=\"" + datasource + "\"");
140             }
141             pw.println("");
142
143             if (adapter != null) {
144                 pw.println("\t\t adapter=\"" + adapter + "\"");
145             }
146
147             if (factory != null) {
148                 pw.print("\t\t factory=\"" + factory.trim() + "\"");
149             }
150             pw.println(">");
151
152             while (mapNames.hasNext()) {
153                 String JavaDoc mapName = (String JavaDoc) mapNames.next();
154                 pw.println("\t\t\t<map-ref name=\"" + mapName.trim() + "\"/>");
155             }
156             pw.println("\t </node>");
157         }
158         pw.println("</domain>");
159     }
160
161     private String JavaDoc attribute(String JavaDoc key, String JavaDoc value) {
162         if (value != null)
163             return " " + key + "=\"" + Util.encodeXmlAttribute(value) + "\"";
164         else
165             return "";
166     }
167
168     /**
169      * Stores DataSolurceInfo to the specified PrintWriter. <code>info</code> object may
170      * contain full or partial information.
171      */

172     public void storeDataNode(PrintWriter JavaDoc pw, Project project, DataSourceInfo info) {
173         pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
174         pw.println("<driver"
175                 + attribute("project-version", Project.CURRENT_PROJECT_VERSION)
176                 + attribute("class", info.getJdbcDriver())
177                 + ">");
178
179         if (info.getDataSourceUrl() != null)
180             pw.println("\t<url" + attribute("value", info.getDataSourceUrl()) + "/>");
181
182         pw.println("\t<connectionPool"
183                 + attribute("min", String.valueOf(info.getMinConnections()))
184                 + attribute("max", String.valueOf(info.getMaxConnections()))
185                 + "/>");
186
187         pw.print("\t<login");
188         
189         if (info.getUserName() != null) {
190             pw.print(attribute("userName", info.getUserName()));
191         }
192
193         if (info.getPasswordLocation().equals(DataSourceInfo.PASSWORD_LOCATION_MODEL)) {
194             PasswordEncoding encoder = info.getPasswordEncoder();
195             if (encoder != null)
196                 pw.print(attribute("password", encoder.encodePassword(
197                         info.getPassword(),
198                         info.getPasswordEncoderSalt())));
199         }
200         else if (info.getPasswordLocation().equals(
201                 DataSourceInfo.PASSWORD_LOCATION_CLASSPATH)) {
202             if (info.getPasswordSource() != null) {
203                 File JavaDoc passwordFile = new File JavaDoc(project.getProjectDirectory()
204                         + File.separator
205                         + info.getPasswordSource());
206                 PasswordEncoding encoder = info.getPasswordEncoder();
207                 if (encoder != null && passwordFile != null) {
208                     try {
209                         PrintStream JavaDoc out = new PrintStream JavaDoc(new FileOutputStream JavaDoc(
210                                 passwordFile));
211                         out.print(encoder.encodePassword(info.getPassword(), info
212                                 .getPasswordEncoderSalt()));
213                         out.close();
214                     }
215                     catch (FileNotFoundException JavaDoc exception) {
216                         // TODO Auto-generated catch block
217
exception.printStackTrace();
218                     }
219                 }
220             }
221         }
222
223         pw.println(attribute("encoderClass", info.getPasswordEncoderClass())
224                 + attribute("encoderSalt", info.getPasswordEncoderSalt())
225                 + attribute("passwordLocation", info.getPasswordLocation())
226                 + attribute("passwordSource", info.getPasswordSource())
227                 + "/>");
228
229         pw.println("</driver>");
230     }
231 }
232
Popular Tags