1 19 20 package org.apache.cayenne.conf; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.PrintStream ; 26 import java.io.PrintWriter ; 27 import java.util.Iterator ; 28 29 import org.apache.cayenne.conn.DataSourceInfo; 30 import org.apache.cayenne.project.Project; 31 import org.apache.cayenne.util.Util; 32 33 38 public class ConfigSaver { 39 40 protected ConfigSaverDelegate delegate; 41 42 45 public ConfigSaver() { 46 super(); 47 } 48 49 52 public ConfigSaver(ConfigSaverDelegate delegate) { 53 this.delegate = delegate; 54 } 55 56 60 public void storeDomains(PrintWriter pw) { 61 pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 62 pw.println("<domains project-version=\"" + delegate.projectVersion() + "\">"); 63 64 Iterator it = delegate.domainNames(); 65 while (it.hasNext()) { 66 storeDomain(pw, (String ) it.next()); 67 } 68 69 Iterator views = delegate.viewNames(); 70 while (views.hasNext()) { 71 storeDataView(pw, (String ) views.next()); 72 } 73 pw.println("</domains>"); 74 } 75 76 protected void storeDataView(PrintWriter pw, String dataViewName) { 77 String 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 pw, String domainName) { 84 pw.println("<domain name=\"" + domainName.trim() + "\">"); 85 86 Iterator properties = delegate.propertyNames(domainName); 88 boolean breakNeeded = properties.hasNext(); 89 while (properties.hasNext()) { 90 String name = (String ) properties.next(); 91 if (name == null) { 92 continue; 93 } 94 95 String 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 Iterator 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 mapName = (String ) maps.next(); 116 String 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 Iterator nodes = delegate.nodeNames(domainName); 125 if (nodes.hasNext() && breakNeeded) { 126 pw.println(); 127 } 128 while (nodes.hasNext()) { 129 String nodeName = (String ) nodes.next(); 130 String datasource = delegate.nodeDataSourceName(domainName, nodeName); 131 String adapter = delegate.nodeAdapterName(domainName, nodeName); 132 String factory = delegate.nodeFactoryName(domainName, nodeName); 133 Iterator 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 mapName = (String ) 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 attribute(String key, String value) { 162 if (value != null) 163 return " " + key + "=\"" + Util.encodeXmlAttribute(value) + "\""; 164 else 165 return ""; 166 } 167 168 172 public void storeDataNode(PrintWriter 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 passwordFile = new File (project.getProjectDirectory() 204 + File.separator 205 + info.getPasswordSource()); 206 PasswordEncoding encoder = info.getPasswordEncoder(); 207 if (encoder != null && passwordFile != null) { 208 try { 209 PrintStream out = new PrintStream (new FileOutputStream ( 210 passwordFile)); 211 out.print(encoder.encodePassword(info.getPassword(), info 212 .getPasswordEncoderSalt())); 213 out.close(); 214 } 215 catch (FileNotFoundException exception) { 216 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 |