1 19 package com.mysql.jdbc.jdbc2.optional; 20 21 import java.util.Hashtable ; 22 23 import javax.naming.Context ; 24 import javax.naming.Name ; 25 import javax.naming.Reference ; 26 import javax.naming.spi.ObjectFactory ; 27 28 29 34 public class MysqlDataSourceFactory 35 implements ObjectFactory { 36 37 39 42 protected final String dataSourceClassName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"; 43 44 47 protected final String poolDataSourceName = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"; 48 49 51 61 public Object getObjectInstance(Object refObj, Name nm, Context ctx, 62 Hashtable env) 63 throws Exception { 64 65 Reference ref = (Reference ) refObj; 66 String 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 ex) { 77 throw new RuntimeException ("Unable to create DataSource of " 78 + "class '" + className 79 + "', reason: " + ex.toString()); 80 } 81 82 int portNumber = 3306; 83 String portNumberAsString = (String ) ref.get("port").getContent(); 84 85 if (portNumberAsString != null) { 86 portNumber = Integer.parseInt(portNumberAsString); 87 } 88 89 dataSource.setPort(portNumber); 90 91 String user = (String ) ref.get("user").getContent(); 92 93 if (user != null) { 94 dataSource.setUser(user); 95 } 96 97 String password = (String ) ref.get("password").getContent(); 98 99 if (password != null) { 100 dataSource.setPassword(password); 101 } 102 103 String serverName = (String ) ref.get("serverName").getContent(); 104 105 if (serverName != null) { 106 dataSource.setServerName(serverName); 107 } 108 109 String databaseName = (String ) ref.get("databaseName").getContent(); 110 111 if (databaseName != null) { 112 dataSource.setDatabaseName(databaseName); 113 } 114 115 return dataSource; 116 } else { 118 return null; 119 } 120 } 121 } | Popular Tags |