KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedWriter JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.apache.cayenne.conn.DataSourceInfo;
34 import org.apache.cayenne.project.CayenneUserDir;
35 import org.apache.commons.collections.ExtendedProperties;
36
37 /**
38  * ConnectionProperties handles a set of DataSourceInfo objects using information stored
39  * in $HOME/.cayenne/connection.properties. As of now this is purely a utility class. Its
40  * features are not used in deployment.
41  *
42  * @author Andrus Adamchik
43  */

44 public class ConnectionProperties {
45
46     public static final String JavaDoc EMBEDDED_DATASOURCE = "internal_embedded_datasource";
47     public static final String JavaDoc EMBEDDED_DATASOURCE_DBADAPTER = "org.apache.cayenne.dba.hsqldb.HSQLDBAdapter";
48     public static final String JavaDoc EMBEDDED_DATASOURCE_USERNAME = "sa";
49     public static final String JavaDoc EMBEDDED_DATASOURCE_PASSWORD = "";
50     public static final String JavaDoc EMBEDDED_DATASOURCE_URL = "jdbc:hsqldb:mem:aname";
51     public static final String JavaDoc EMBEDDED_DATASOURCE_JDBC_DRIVER = "org.hsqldb.jdbcDriver";
52
53     public static final String JavaDoc PROPERTIES_FILE = "connection.properties";
54     public static final String JavaDoc ADAPTER_KEY = "adapter";
55     static final String JavaDoc ADAPTER20_KEY = "cayenne.adapter";
56     public static final String JavaDoc USER_NAME_KEY = "jdbc.username";
57     public static final String JavaDoc PASSWORD_KEY = "jdbc.password";
58     public static final String JavaDoc URL_KEY = "jdbc.url";
59     public static final String JavaDoc DRIVER_KEY = "jdbc.driver";
60
61     protected static ConnectionProperties sharedInstance;
62     protected Map JavaDoc connectionInfos = Collections.synchronizedMap(new HashMap JavaDoc());
63
64     static {
65         sharedInstance = loadDefaultProperties();
66     }
67
68     /**
69      * Returns ConnectionProperties singleton.
70      */

71     public static ConnectionProperties getInstance() {
72         return sharedInstance;
73     }
74
75     /**
76      * Loads connection properties from $HOME/.cayenne/connection.properties.
77      */

78     protected static ConnectionProperties loadDefaultProperties() {
79         File JavaDoc 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                 // lets touch this file so that users would get a clue of what it is
88
createSamplePropertiesFile(f);
89             }
90         }
91         catch (IOException JavaDoc e) {
92             // ignoring
93
}
94
95         return new ConnectionProperties(new ExtendedProperties());
96     }
97
98     protected static void createSamplePropertiesFile(File JavaDoc f) throws IOException JavaDoc {
99         BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(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             // example 2
126
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     /**
147      * Constructor for ConnectionProperties.
148      */

149     public ConnectionProperties(ExtendedProperties props) {
150         Iterator JavaDoc names = extractNames(props).iterator();
151         while (names.hasNext()) {
152             String JavaDoc name = (String JavaDoc) names.next();
153             DataSourceInfo dsi = buildDataSourceInfo(props.subset(name));
154             connectionInfos.put(name, dsi);
155         }
156     }
157
158     /**
159      * Returns DataSourceInfo object for a symbolic name. If name does not match an
160      * existing object, returns null.
161      */

162     public DataSourceInfo getConnectionInfo(String JavaDoc name) {
163
164         if (EMBEDDED_DATASOURCE.equals(name)) {
165             // Create embedded data source instead
166
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     /**
181      * Creates a DataSourceInfo object from a set of properties.
182      */

183     protected DataSourceInfo buildDataSourceInfo(ExtendedProperties props) {
184         DataSourceInfo dsi = new DataSourceInfo();
185
186         String JavaDoc adapter = props.getString(ADAPTER_KEY);
187         
188         // try legacy adapter key
189
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     /**
203      * Returns a list of connection names configured in the properties object.
204      */

205     protected List JavaDoc extractNames(ExtendedProperties props) {
206         Iterator JavaDoc it = props.getKeys();
207         List JavaDoc list = new ArrayList JavaDoc();
208
209         while (it.hasNext()) {
210             String JavaDoc key = (String JavaDoc) it.next();
211
212             int dotInd = key.indexOf('.');
213             if (dotInd <= 0 || dotInd >= key.length()) {
214                 continue;
215             }
216
217             String JavaDoc 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