Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/mysql/jdbc/jdbc2/optional/MysqlDataSourceFactory.java


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