KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > jdbc2 > optional > MysqlDataSourceFactory


1 /*
2    Copyright (C) 2002 MySQL AB
3    
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8    
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13    
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17       
18  */

19 package com.mysql.jdbc.jdbc2.optional;
20
21 import java.util.Hashtable JavaDoc;
22
23 import javax.naming.Context JavaDoc;
24 import javax.naming.Name JavaDoc;
25 import javax.naming.Reference JavaDoc;
26 import javax.naming.spi.ObjectFactory JavaDoc;
27
28
29 /**
30  * Factory class for MysqlDataSource objects
31  *
32  * @author Mark Matthews
33  */

34 public class MysqlDataSourceFactory
35     implements ObjectFactory JavaDoc {
36
37     //~ Instance/static variables .............................................
38

39     /**
40      * The class name for a standard MySQL DataSource.
41      */

42     protected final String JavaDoc dataSourceClassName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource";
43     
44     /**
45      * The class name for a poolable MySQL DataSource.
46      */

47     protected final String JavaDoc poolDataSourceName = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource";
48
49     //~ Methods ...............................................................
50

51     /**
52      * DOCUMENT ME!
53      *
54      * @param refObj DOCUMENT ME!
55      * @param nm DOCUMENT ME!
56      * @param ctx DOCUMENT ME!
57      * @param env DOCUMENT ME!
58      * @return DOCUMENT ME!
59      * @throws Exception DOCUMENT ME!
60      */

61     public Object JavaDoc getObjectInstance(Object JavaDoc refObj, Name JavaDoc nm, Context JavaDoc ctx,
62                                     Hashtable JavaDoc env)
63                              throws Exception JavaDoc {
64
65         Reference JavaDoc ref = (Reference JavaDoc) refObj;
66         String JavaDoc className = ref.getClassName();
67
68         if (className != null
69             && (className.equals(dataSourceClassName) || className.equals(
70                                                                  poolDataSourceName))) {
71
72             MysqlDataSource dataSource = null;
73
74             try {
75                 dataSource = (MysqlDataSource) Class.forName(className).newInstance();
76             } catch (Exception JavaDoc ex) {
77                 throw new RuntimeException JavaDoc("Unable to create DataSource of "
78                                            + "class '" + className
79                                            + "', reason: " + ex.toString());
80             }
81
82             int portNumber = 3306;
83             String JavaDoc portNumberAsString = (String JavaDoc) ref.get("port").getContent();
84             
85             if (portNumberAsString != null) {
86                 portNumber = Integer.parseInt(portNumberAsString);
87             }
88             
89             dataSource.setPort(portNumber);
90             
91             String JavaDoc user = (String JavaDoc) ref.get("user").getContent();
92             
93             if (user != null) {
94                 dataSource.setUser(user);
95             }
96             
97             String JavaDoc password = (String JavaDoc) ref.get("password").getContent();
98             
99             if (password != null) {
100                 dataSource.setPassword(password);
101             }
102             
103             String JavaDoc serverName = (String JavaDoc) ref.get("serverName").getContent();
104             
105             if (serverName != null) {
106                 dataSource.setServerName(serverName);
107             }
108             
109             String JavaDoc databaseName = (String JavaDoc) ref.get("databaseName").getContent();
110             
111             if (databaseName != null) {
112                 dataSource.setDatabaseName(databaseName);
113             }
114
115             return dataSource;
116         } else { // We can't create an instance of the reference
117

118             return null;
119         }
120     }
121 }
Popular Tags