KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.objectstyle.cayenne.conf;
57
58 import java.io.BufferedWriter JavaDoc;
59 import java.io.File JavaDoc;
60 import java.io.FileWriter JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.Collections JavaDoc;
64 import java.util.HashMap JavaDoc;
65 import java.util.Iterator JavaDoc;
66 import java.util.List JavaDoc;
67 import java.util.Map JavaDoc;
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 /**
75  * ConnectionProperties handles a set of DataSourceInfo objects
76  * using information stored in $HOME/.cayenne/connection.properties.
77  * As of now this is purely a utility class. Its features are not used
78  * in deployment.
79  *
80  * @author Andrei Adamchik
81  */

82 public class ConnectionProperties {
83     static final Logger logObj = Logger.getLogger(ConnectionProperties.class);
84
85     public static final String JavaDoc EMBEDDED_DATASOURCE = "internal_embedded_datasource";
86     public static final String JavaDoc EMBEDDED_DATASOURCE_DBADAPTER = "org.objectstyle.cayenne.dba.hsqldb.HSQLDBAdapter";
87     public static final String JavaDoc EMBEDDED_DATASOURCE_USERNAME = "sa";
88     public static final String JavaDoc EMBEDDED_DATASOURCE_PASSWORD = "";
89     public static final String JavaDoc EMBEDDED_DATASOURCE_URL = "jdbc:hsqldb:mem:aname";
90     public static final String JavaDoc EMBEDDED_DATASOURCE_JDBC_DRIVER = "org.hsqldb.jdbcDriver";
91
92     public static final String JavaDoc PROPERTIES_FILE = "connection.properties";
93     public static final String JavaDoc ADAPTER_KEY = "cayenne.adapter";
94     public static final String JavaDoc USER_NAME_KEY = "jdbc.username";
95     public static final String JavaDoc PASSWORD_KEY = "jdbc.password";
96     public static final String JavaDoc URL_KEY = "jdbc.url";
97     public static final String JavaDoc DRIVER_KEY = "jdbc.driver";
98
99     protected static ConnectionProperties sharedInstance;
100     protected Map JavaDoc connectionInfos = Collections.synchronizedMap(new HashMap JavaDoc());
101
102     static {
103         sharedInstance = loadDefaultProperties();
104     }
105
106     /**
107      * Returns ConnectionProperties singleton.
108      */

109     public static ConnectionProperties getInstance() {
110         return sharedInstance;
111     }
112
113     /**
114      * Loads connection properties from $HOME/.cayenne/connection.properties.
115      */

116     protected static ConnectionProperties loadDefaultProperties() {
117         File JavaDoc 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                 // lets touch this file so that users would get a clue of what it is
125
createSamplePropertiesFile(f);
126             }
127         } catch (IOException JavaDoc 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 JavaDoc f) throws IOException JavaDoc {
135         BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(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             // example 2
163
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     /**
184      * Constructor for ConnectionProperties.
185      */

186     public ConnectionProperties(ExtendedProperties props) {
187         Iterator JavaDoc names = extractNames(props).iterator();
188         while (names.hasNext()) {
189             String JavaDoc name = (String JavaDoc) names.next();
190             DataSourceInfo dsi = buildDataSourceInfo(props.subset(name));
191             connectionInfos.put(name, dsi);
192         }
193     }
194
195     /**
196      * Returns DataSourceInfo object for a symbolic name.
197      * If name does not match an existing object, returns null.
198      */

199     public DataSourceInfo getConnectionInfo(String JavaDoc name) {
200         
201         if (EMBEDDED_DATASOURCE.equals(name)) {
202             // Create embedded data source instead
203
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     /**
218     * Creates a DataSourceInfo object from a set of properties.
219     */

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     /**
233      * Returns a list of connection names configured
234      * in the properties object.
235      */

236     protected List JavaDoc extractNames(ExtendedProperties props) {
237         Iterator JavaDoc it = props.getKeys();
238         List JavaDoc list = new ArrayList JavaDoc();
239
240         while (it.hasNext()) {
241             String JavaDoc key = (String JavaDoc) it.next();
242
243             int dotInd = key.indexOf('.');
244             if (dotInd <= 0 || dotInd >= key.length()) {
245                 continue;
246             }
247
248             String JavaDoc 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