1 | /* |
2 | Copyright (C) 2002-2006 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.RefAddr; |
32 | import javax.naming.Reference; |
33 | import javax.naming.spi.ObjectFactory; |
34 | |
35 | import com.mysql.jdbc.NonRegisteringDriver; |
36 | |
37 | /** |
38 | * Factory class for MysqlDataSource objects |
39 | * |
40 | * @author Mark Matthews |
41 | */ |
42 | public class MysqlDataSourceFactory implements ObjectFactory { |
43 | /** |
44 | * The class name for a standard MySQL DataSource. |
45 | */ |
46 | protected final static String DATA_SOURCE_CLASS_NAME = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"; |
47 | |
48 | /** |
49 | * The class name for a poolable MySQL DataSource. |
50 | */ |
51 | protected final static String POOL_DATA_SOURCE_CLASS_NAME = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"; |
52 | |
53 | /** |
54 | * DOCUMENT ME! |
55 | * |
56 | * @param refObj |
57 | * DOCUMENT ME! |
58 | * @param nm |
59 | * DOCUMENT ME! |
60 | * @param ctx |
61 | * DOCUMENT ME! |
62 | * @param env |
63 | * DOCUMENT ME! |
64 | * @return DOCUMENT ME! |
65 | * @throws Exception |
66 | * DOCUMENT ME! |
67 | */ |
68 | public Object getObjectInstance(Object refObj, Name nm, Context ctx, |
69 | Hashtable env) throws Exception { |
70 | Reference ref = (Reference) refObj; |
71 | String className = ref.getClassName(); |
72 | |
73 | if ((className != null) |
74 | && (className.equals(DATA_SOURCE_CLASS_NAME) || className |
75 | .equals(POOL_DATA_SOURCE_CLASS_NAME))) { |
76 | MysqlDataSource dataSource = null; |
77 | |
78 | try { |
79 | dataSource = (MysqlDataSource) Class.forName(className) |
80 | .newInstance(); |
81 | } catch (Exception ex) { |
82 | throw new RuntimeException("Unable to create DataSource of " |
83 | + "class '" + className + "', reason: " + ex.toString()); |
84 | } |
85 | |
86 | int portNumber = 3306; |
87 | |
88 | String portNumberAsString = nullSafeRefAddrStringGet("port", ref); |
89 | |
90 | if (portNumberAsString != null) { |
91 | portNumber = Integer.parseInt(portNumberAsString); |
92 | } |
93 | |
94 | dataSource.setPort(portNumber); |
95 | |
96 | String user = nullSafeRefAddrStringGet(NonRegisteringDriver.USER_PROPERTY_KEY, ref); |
97 | |
98 | if (user != null) { |
99 | dataSource.setUser(user); |
100 | } |
101 | |
102 | String password = nullSafeRefAddrStringGet(NonRegisteringDriver.PASSWORD_PROPERTY_KEY, ref); |
103 | |
104 | if (password != null) { |
105 | dataSource.setPassword(password); |
106 | } |
107 | |
108 | String serverName = nullSafeRefAddrStringGet("serverName", ref); |
109 | |
110 | if (serverName != null) { |
111 | dataSource.setServerName(serverName); |
112 | } |
113 | |
114 | String databaseName = nullSafeRefAddrStringGet("databaseName", ref); |
115 | |
116 | if (databaseName != null) { |
117 | dataSource.setDatabaseName(databaseName); |
118 | } |
119 | |
120 | String explicitUrlAsString = nullSafeRefAddrStringGet("explicitUrl", ref); |
121 | |
122 | if (explicitUrlAsString != null) { |
123 | if (Boolean.valueOf(explicitUrlAsString).booleanValue()) { |
124 | dataSource.setUrl(nullSafeRefAddrStringGet("url", ref)); |
125 | } |
126 | } |
127 | |
128 | dataSource.setPropertiesViaRef(ref); |
129 | |
130 | return dataSource; |
131 | } |
132 | |
133 | // We can't create an instance of the reference |
134 | return null; |
135 | } |
136 | |
137 | private String nullSafeRefAddrStringGet(String referenceName, Reference ref) { |
138 | RefAddr refAddr = ref.get(referenceName); |
139 | |
140 | String asString = refAddr != null ? (String)refAddr.getContent() : null; |
141 | |
142 | return asString; |
143 | } |
144 | } |