1 | /* |
2 | Copyright (C) 2002-2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of version 2 of the GNU General Public License as |
6 | published by the Free Software Foundation. |
7 | |
8 | There are special exceptions to the terms and conditions of the GPL |
9 | as it is applied to this software. View the full text of the |
10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
11 | software distribution. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
22 | |
23 | |
24 | */ |
25 | package com.mysql.jdbc.jdbc2.optional; |
26 | |
27 | import java.util.Hashtable; |
28 | |
29 | import javax.naming.Context; |
30 | import javax.naming.Name; |
31 | import javax.naming.Reference; |
32 | import javax.naming.spi.ObjectFactory; |
33 | |
34 | import com.mysql.jdbc.NonRegisteringDriver; |
35 | |
36 | /** |
37 | * Factory class for MysqlDataSource objects |
38 | * |
39 | * @author Mark Matthews |
40 | */ |
41 | public class MysqlDataSourceFactory implements ObjectFactory { |
42 | /** |
43 | * The class name for a standard MySQL DataSource. |
44 | */ |
45 | protected final static String DATA_SOURCE_CLASS_NAME = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"; |
46 | |
47 | /** |
48 | * The class name for a poolable MySQL DataSource. |
49 | */ |
50 | protected final static String POOL_DATA_SOURCE_CLASS_NAME = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"; |
51 | |
52 | /** |
53 | * DOCUMENT ME! |
54 | * |
55 | * @param refObj |
56 | * DOCUMENT ME! |
57 | * @param nm |
58 | * DOCUMENT ME! |
59 | * @param ctx |
60 | * DOCUMENT ME! |
61 | * @param env |
62 | * DOCUMENT ME! |
63 | * @return DOCUMENT ME! |
64 | * @throws Exception |
65 | * DOCUMENT ME! |
66 | */ |
67 | public Object getObjectInstance(Object refObj, Name nm, Context ctx, |
68 | Hashtable env) throws Exception { |
69 | Reference ref = (Reference) refObj; |
70 | String className = ref.getClassName(); |
71 | |
72 | if ((className != null) |
73 | && (className.equals(DATA_SOURCE_CLASS_NAME) || className |
74 | .equals(POOL_DATA_SOURCE_CLASS_NAME))) { |
75 | MysqlDataSource dataSource = null; |
76 | |
77 | try { |
78 | dataSource = (MysqlDataSource) Class.forName(className) |
79 | .newInstance(); |
80 | } catch (Exception ex) { |
81 | throw new RuntimeException("Unable to create DataSource of " |
82 | + "class '" + className + "', reason: " + ex.toString()); |
83 | } |
84 | |
85 | int portNumber = 3306; |
86 | |
87 | String portNumberAsString = (String) ref.get("port").getContent(); |
88 | |
89 | if (portNumberAsString != null) { |
90 | portNumber = Integer.parseInt(portNumberAsString); |
91 | } |
92 | |
93 | dataSource.setPort(portNumber); |
94 | |
95 | String user = (String) ref.get( |
96 | NonRegisteringDriver.USER_PROPERTY_KEY).getContent(); |
97 | |
98 | if (user != null) { |
99 | dataSource.setUser(user); |
100 | } |
101 | |
102 | String password = (String) ref.get( |
103 | NonRegisteringDriver.PASSWORD_PROPERTY_KEY).getContent(); |
104 | |
105 | if (password != null) { |
106 | dataSource.setPassword(password); |
107 | } |
108 | |
109 | String serverName = (String) ref.get("serverName").getContent(); |
110 | |
111 | if (serverName != null) { |
112 | dataSource.setServerName(serverName); |
113 | } |
114 | |
115 | String databaseName = (String) ref.get("databaseName").getContent(); |
116 | |
117 | if (databaseName != null) { |
118 | dataSource.setDatabaseName(databaseName); |
119 | } |
120 | |
121 | String explicitUrlAsString = (String) ref.get("explicitUrl") |
122 | .getContent(); |
123 | |
124 | if (explicitUrlAsString != null) { |
125 | if (Boolean.valueOf(explicitUrlAsString).booleanValue()) { |
126 | dataSource.setUrl((String) ref.get("url").getContent()); |
127 | } |
128 | } |
129 | |
130 | dataSource.setPropertiesViaRef(ref); |
131 | |
132 | return dataSource; |
133 | } |
134 | |
135 | // We can't create an instance of the reference |
136 | return null; |
137 | } |
138 | } |