1 56 package org.objectstyle.cayenne.conf; 57 58 import java.io.BufferedWriter ; 59 import java.io.File ; 60 import java.io.FileWriter ; 61 import java.io.IOException ; 62 import java.util.ArrayList ; 63 import java.util.Collections ; 64 import java.util.HashMap ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 import java.util.Map ; 68 69 import org.apache.commons.collections.ExtendedProperties; 70 import org.apache.log4j.Logger; 71 import org.objectstyle.cayenne.conn.DataSourceInfo; 72 import org.objectstyle.cayenne.project.CayenneUserDir; 73 74 82 public class ConnectionProperties { 83 static final Logger logObj = Logger.getLogger(ConnectionProperties.class); 84 85 public static final String EMBEDDED_DATASOURCE = "internal_embedded_datasource"; 86 public static final String EMBEDDED_DATASOURCE_DBADAPTER = "org.objectstyle.cayenne.dba.hsqldb.HSQLDBAdapter"; 87 public static final String EMBEDDED_DATASOURCE_USERNAME = "sa"; 88 public static final String EMBEDDED_DATASOURCE_PASSWORD = ""; 89 public static final String EMBEDDED_DATASOURCE_URL = "jdbc:hsqldb:mem:aname"; 90 public static final String EMBEDDED_DATASOURCE_JDBC_DRIVER = "org.hsqldb.jdbcDriver"; 91 92 public static final String PROPERTIES_FILE = "connection.properties"; 93 public static final String ADAPTER_KEY = "cayenne.adapter"; 94 public static final String USER_NAME_KEY = "jdbc.username"; 95 public static final String PASSWORD_KEY = "jdbc.password"; 96 public static final String URL_KEY = "jdbc.url"; 97 public static final String DRIVER_KEY = "jdbc.driver"; 98 99 protected static ConnectionProperties sharedInstance; 100 protected Map connectionInfos = Collections.synchronizedMap(new HashMap ()); 101 102 static { 103 sharedInstance = loadDefaultProperties(); 104 } 105 106 109 public static ConnectionProperties getInstance() { 110 return sharedInstance; 111 } 112 113 116 protected static ConnectionProperties loadDefaultProperties() { 117 File f = CayenneUserDir.getInstance().resolveFile(PROPERTIES_FILE); 118 119 try { 120 if (f.exists()) { 121 return new ConnectionProperties( 122 new ExtendedProperties(f.getAbsolutePath())); 123 } else { 124 createSamplePropertiesFile(f); 126 } 127 } catch (IOException e) { 128 logObj.warn("Error loading connection properties. Ignoring..", e); 129 } 130 131 return new ConnectionProperties(new ExtendedProperties()); 132 } 133 134 protected static void createSamplePropertiesFile(File f) throws IOException { 135 BufferedWriter out = new BufferedWriter (new FileWriter (f)); 136 137 try { 138 out.write("# Cayenne named connections configuration file."); 139 out.newLine(); 140 141 out.write("#"); 142 out.newLine(); 143 out.write("# Sample named connections (named 'example1' and 'example2'): "); 144 out.newLine(); 145 146 out.write("#"); 147 out.newLine(); 148 out.write( 149 "# example1." 150 + ADAPTER_KEY 151 + " = org.objectstyle.cayenne.dba.mysql.MySQLAdapter"); 152 out.newLine(); 153 out.write("# example1." + USER_NAME_KEY + " = some_user"); 154 out.newLine(); 155 out.write("# example1." + PASSWORD_KEY + " = some_passwd"); 156 out.newLine(); 157 out.write("# example1." + URL_KEY + " = jdbc:mysql://noise/cayenne"); 158 out.newLine(); 159 out.write("# example1." + DRIVER_KEY + " = org.gjt.mm.mysql.Driver"); 160 out.newLine(); 161 162 out.write("#"); 164 out.newLine(); 165 out.write( 166 "# example2." 167 + ADAPTER_KEY 168 + " = org.objectstyle.cayenne.dba.mysql.MySQLAdapter"); 169 out.newLine(); 170 out.write("# example2." + USER_NAME_KEY + " = some_user"); 171 out.newLine(); 172 out.write("# example2." + PASSWORD_KEY + " = some_passwd"); 173 out.newLine(); 174 out.write("# example2." + URL_KEY + " = jdbc:mysql://noise/cayenne"); 175 out.newLine(); 176 out.write("# example2." + DRIVER_KEY + " = org.gjt.mm.mysql.Driver"); 177 out.newLine(); 178 } finally { 179 out.close(); 180 } 181 } 182 183 186 public ConnectionProperties(ExtendedProperties props) { 187 Iterator names = extractNames(props).iterator(); 188 while (names.hasNext()) { 189 String name = (String ) names.next(); 190 DataSourceInfo dsi = buildDataSourceInfo(props.subset(name)); 191 connectionInfos.put(name, dsi); 192 } 193 } 194 195 199 public DataSourceInfo getConnectionInfo(String name) { 200 201 if (EMBEDDED_DATASOURCE.equals(name)) { 202 DataSourceInfo connectionInfo = new DataSourceInfo(); 204 connectionInfo.setAdapterClassName(EMBEDDED_DATASOURCE_DBADAPTER); 205 connectionInfo.setUserName(EMBEDDED_DATASOURCE_USERNAME); 206 connectionInfo.setPassword(EMBEDDED_DATASOURCE_PASSWORD); 207 connectionInfo.setDataSourceUrl(EMBEDDED_DATASOURCE_URL); 208 connectionInfo.setJdbcDriver(EMBEDDED_DATASOURCE_JDBC_DRIVER); 209 return connectionInfo; 210 } 211 212 synchronized (connectionInfos) { 213 return (DataSourceInfo) connectionInfos.get(name); 214 } 215 } 216 217 220 protected DataSourceInfo buildDataSourceInfo(ExtendedProperties props) { 221 DataSourceInfo dsi = new DataSourceInfo(); 222 223 dsi.setAdapterClassName(props.getString(ADAPTER_KEY)); 224 dsi.setUserName(props.getString(USER_NAME_KEY)); 225 dsi.setPassword(props.getString(PASSWORD_KEY)); 226 dsi.setDataSourceUrl(props.getString(URL_KEY)); 227 dsi.setJdbcDriver(props.getString(DRIVER_KEY)); 228 229 return dsi; 230 } 231 232 236 protected List extractNames(ExtendedProperties props) { 237 Iterator it = props.getKeys(); 238 List list = new ArrayList (); 239 240 while (it.hasNext()) { 241 String key = (String ) it.next(); 242 243 int dotInd = key.indexOf('.'); 244 if (dotInd <= 0 || dotInd >= key.length()) { 245 continue; 246 } 247 248 String name = key.substring(0, dotInd); 249 if (!list.contains(name)) { 250 list.add(name); 251 } 252 } 253 254 return list; 255 } 256 } 257 | Popular Tags |