1 19 20 package org.apache.cayenne.conf; 21 22 import java.io.BufferedWriter ; 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.apache.cayenne.conn.DataSourceInfo; 34 import org.apache.cayenne.project.CayenneUserDir; 35 import org.apache.commons.collections.ExtendedProperties; 36 37 44 public class ConnectionProperties { 45 46 public static final String EMBEDDED_DATASOURCE = "internal_embedded_datasource"; 47 public static final String EMBEDDED_DATASOURCE_DBADAPTER = "org.apache.cayenne.dba.hsqldb.HSQLDBAdapter"; 48 public static final String EMBEDDED_DATASOURCE_USERNAME = "sa"; 49 public static final String EMBEDDED_DATASOURCE_PASSWORD = ""; 50 public static final String EMBEDDED_DATASOURCE_URL = "jdbc:hsqldb:mem:aname"; 51 public static final String EMBEDDED_DATASOURCE_JDBC_DRIVER = "org.hsqldb.jdbcDriver"; 52 53 public static final String PROPERTIES_FILE = "connection.properties"; 54 public static final String ADAPTER_KEY = "adapter"; 55 static final String ADAPTER20_KEY = "cayenne.adapter"; 56 public static final String USER_NAME_KEY = "jdbc.username"; 57 public static final String PASSWORD_KEY = "jdbc.password"; 58 public static final String URL_KEY = "jdbc.url"; 59 public static final String DRIVER_KEY = "jdbc.driver"; 60 61 protected static ConnectionProperties sharedInstance; 62 protected Map connectionInfos = Collections.synchronizedMap(new HashMap ()); 63 64 static { 65 sharedInstance = loadDefaultProperties(); 66 } 67 68 71 public static ConnectionProperties getInstance() { 72 return sharedInstance; 73 } 74 75 78 protected static ConnectionProperties loadDefaultProperties() { 79 File f = CayenneUserDir.getInstance().resolveFile(PROPERTIES_FILE); 80 81 try { 82 if (f.exists()) { 83 return new ConnectionProperties(new ExtendedProperties(f 84 .getAbsolutePath())); 85 } 86 else { 87 createSamplePropertiesFile(f); 89 } 90 } 91 catch (IOException e) { 92 } 94 95 return new ConnectionProperties(new ExtendedProperties()); 96 } 97 98 protected static void createSamplePropertiesFile(File f) throws IOException { 99 BufferedWriter out = new BufferedWriter (new FileWriter (f)); 100 101 try { 102 out.write("# Cayenne named connections configuration file."); 103 out.newLine(); 104 105 out.write("#"); 106 out.newLine(); 107 out.write("# Sample named connections (named 'example1' and 'example2'): "); 108 out.newLine(); 109 110 out.write("#"); 111 out.newLine(); 112 out.write("# example1." 113 + ADAPTER_KEY 114 + " = org.apache.cayenne.dba.mysql.MySQLAdapter"); 115 out.newLine(); 116 out.write("# example1." + USER_NAME_KEY + " = some_user"); 117 out.newLine(); 118 out.write("# example1." + PASSWORD_KEY + " = some_passwd"); 119 out.newLine(); 120 out.write("# example1." + URL_KEY + " = jdbc:mysql://noise/cayenne"); 121 out.newLine(); 122 out.write("# example1." + DRIVER_KEY + " = org.gjt.mm.mysql.Driver"); 123 out.newLine(); 124 125 out.write("#"); 127 out.newLine(); 128 out.write("# example2." 129 + ADAPTER_KEY 130 + " = org.apache.cayenne.dba.mysql.MySQLAdapter"); 131 out.newLine(); 132 out.write("# example2." + USER_NAME_KEY + " = some_user"); 133 out.newLine(); 134 out.write("# example2." + PASSWORD_KEY + " = some_passwd"); 135 out.newLine(); 136 out.write("# example2." + URL_KEY + " = jdbc:mysql://noise/cayenne"); 137 out.newLine(); 138 out.write("# example2." + DRIVER_KEY + " = org.gjt.mm.mysql.Driver"); 139 out.newLine(); 140 } 141 finally { 142 out.close(); 143 } 144 } 145 146 149 public ConnectionProperties(ExtendedProperties props) { 150 Iterator names = extractNames(props).iterator(); 151 while (names.hasNext()) { 152 String name = (String ) names.next(); 153 DataSourceInfo dsi = buildDataSourceInfo(props.subset(name)); 154 connectionInfos.put(name, dsi); 155 } 156 } 157 158 162 public DataSourceInfo getConnectionInfo(String name) { 163 164 if (EMBEDDED_DATASOURCE.equals(name)) { 165 DataSourceInfo connectionInfo = new DataSourceInfo(); 167 connectionInfo.setAdapterClassName(EMBEDDED_DATASOURCE_DBADAPTER); 168 connectionInfo.setUserName(EMBEDDED_DATASOURCE_USERNAME); 169 connectionInfo.setPassword(EMBEDDED_DATASOURCE_PASSWORD); 170 connectionInfo.setDataSourceUrl(EMBEDDED_DATASOURCE_URL); 171 connectionInfo.setJdbcDriver(EMBEDDED_DATASOURCE_JDBC_DRIVER); 172 return connectionInfo; 173 } 174 175 synchronized (connectionInfos) { 176 return (DataSourceInfo) connectionInfos.get(name); 177 } 178 } 179 180 183 protected DataSourceInfo buildDataSourceInfo(ExtendedProperties props) { 184 DataSourceInfo dsi = new DataSourceInfo(); 185 186 String adapter = props.getString(ADAPTER_KEY); 187 188 if(adapter == null) { 190 adapter = props.getString(ADAPTER20_KEY); 191 } 192 193 dsi.setAdapterClassName(adapter); 194 dsi.setUserName(props.getString(USER_NAME_KEY)); 195 dsi.setPassword(props.getString(PASSWORD_KEY)); 196 dsi.setDataSourceUrl(props.getString(URL_KEY)); 197 dsi.setJdbcDriver(props.getString(DRIVER_KEY)); 198 199 return dsi; 200 } 201 202 205 protected List extractNames(ExtendedProperties props) { 206 Iterator it = props.getKeys(); 207 List list = new ArrayList (); 208 209 while (it.hasNext()) { 210 String key = (String ) it.next(); 211 212 int dotInd = key.indexOf('.'); 213 if (dotInd <= 0 || dotInd >= key.length()) { 214 continue; 215 } 216 217 String name = key.substring(0, dotInd); 218 if (!list.contains(name)) { 219 list.add(name); 220 } 221 } 222 223 return list; 224 } 225 } 226 | Popular Tags |