Advertisement
Guest User

driver.java

a guest
Nov 4th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. package com.kavalok.monitor.jdbc;
  2.  
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.net.URLDecoder;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.DriverPropertyInfo;
  10. import java.sql.SQLException;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.Properties;
  14. import java.util.StringTokenizer;
  15.  
  16. import com.mysql.jdbc.ConnectionPropertiesTransform;
  17. import com.mysql.jdbc.SQLError;
  18. import com.mysql.jdbc.StringUtils;
  19. import com.mysql.jdbc.Util;
  20.  
  21. public class Driver implements java.sql.Driver {
  22.  
  23. private java.sql.Driver wrapper;
  24.  
  25. static {
  26. try {
  27. DriverManager.registerDriver(new Driver());
  28. } catch (SQLException E) {
  29. throw new RuntimeException("Can't register driver!");
  30. }
  31. }
  32.  
  33. @Override
  34. public boolean acceptsURL(String url) throws SQLException {
  35. // TODO Auto-generated method stub
  36. return parseURL(url, null) != null;
  37. }
  38.  
  39. @Override
  40. public Connection connect(String url, Properties info) throws SQLException {
  41. String wrapperClassName = info.getProperty("wrapper_driver_class");
  42. Connection wrappedConnection = null;
  43. try {
  44. Class<?> wrapperClass = Class.forName(wrapperClassName);
  45. wrapper = (java.sql.Driver) wrapperClass.getConstructor().newInstance();
  46. wrappedConnection = wrapper.connect(url, info);
  47. } catch (ClassNotFoundException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. } catch (IllegalArgumentException e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. } catch (SecurityException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. } catch (InstantiationException e) {
  57. // TODO Auto-generated catch block
  58. e.printStackTrace();
  59. } catch (IllegalAccessException e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. } catch (InvocationTargetException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. } catch (NoSuchMethodException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69. return new com.kavalok.monitor.jdbc.Connection(wrappedConnection);
  70. }
  71.  
  72. @Override
  73. public int getMajorVersion() {
  74. // TODO Auto-generated method stub
  75. return wrapper.getMajorVersion();
  76. }
  77.  
  78. @Override
  79. public int getMinorVersion() {
  80. // TODO Auto-generated method stub
  81. return wrapper.getMinorVersion();
  82. }
  83.  
  84. @Override
  85. public DriverPropertyInfo[] getPropertyInfo(String arg0, Properties arg1) throws SQLException {
  86. // TODO Auto-generated method stub
  87. return wrapper.getPropertyInfo(arg0, arg1);
  88. }
  89.  
  90. @Override
  91. public boolean jdbcCompliant() {
  92. // TODO Auto-generated method stub
  93. return wrapper.jdbcCompliant();
  94. }
  95.  
  96. public Properties parseURL(String url, Properties defaults) throws SQLException {
  97. Properties urlProps = defaults == null ? new Properties() : new Properties(defaults);
  98. if (url == null)
  99. return null;
  100. if (!StringUtils.startsWithIgnoreCase(url, "jdbc:mysql://")
  101. && !StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:mxj://")
  102. && !StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:loadbalance://")
  103. && !StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:replication://"))
  104. return null;
  105. int beginningOfSlashes = url.indexOf("//");
  106. if (StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:mxj://"))
  107. urlProps.setProperty("socketFactory", "com.mysql.management.driverlaunched.ServerLauncherSocketFactory");
  108. int index = url.indexOf("?");
  109. if (index != -1) {
  110. String paramString = url.substring(index + 1, url.length());
  111. url = url.substring(0, index);
  112. StringTokenizer queryParams = new StringTokenizer(paramString, "&");
  113. do {
  114. if (!queryParams.hasMoreTokens())
  115. break;
  116. String parameterValuePair = queryParams.nextToken();
  117. int indexOfEquals = StringUtils.indexOfIgnoreCase(0, parameterValuePair, "=");
  118. String parameter = null;
  119. String value = null;
  120. if (indexOfEquals != -1) {
  121. parameter = parameterValuePair.substring(0, indexOfEquals);
  122. if (indexOfEquals + 1 < parameterValuePair.length())
  123. value = parameterValuePair.substring(indexOfEquals + 1);
  124. }
  125. if (value != null && value.length() > 0 && parameter != null && parameter.length() > 0)
  126. try {
  127. urlProps.put(parameter, URLDecoder.decode(value, "UTF-8"));
  128. } catch (UnsupportedEncodingException badEncoding) {
  129. urlProps.put(parameter, URLDecoder.decode(value));
  130. } catch (NoSuchMethodError nsme) {
  131. urlProps.put(parameter, URLDecoder.decode(value));
  132. }
  133. } while (true);
  134. }
  135. url = url.substring(beginningOfSlashes + 2);
  136. String hostStuff = null;
  137. int slashIndex = url.indexOf("/");
  138. if (slashIndex != -1) {
  139. hostStuff = url.substring(0, slashIndex);
  140. if (slashIndex + 1 < url.length())
  141. urlProps.put("DBNAME", url.substring(slashIndex + 1, url.length()));
  142. } else {
  143. hostStuff = url;
  144. }
  145. if (hostStuff != null && hostStuff.length() > 0)
  146. urlProps.put("HOST", hostStuff);
  147. String propertiesTransformClassName = urlProps.getProperty("propertiesTransform");
  148. if (propertiesTransformClassName != null)
  149. try {
  150. ConnectionPropertiesTransform propTransformer = (ConnectionPropertiesTransform) Class.forName(
  151. propertiesTransformClassName).newInstance();
  152. urlProps = propTransformer.transformProperties(urlProps);
  153. } catch (InstantiationException e) {
  154. throw SQLError.createSQLException("Unable to create properties transform instance '"
  155. + propertiesTransformClassName + "' due to underlying exception: " + e.toString(), "01S00");
  156. } catch (IllegalAccessException e) {
  157. throw SQLError.createSQLException("Unable to create properties transform instance '"
  158. + propertiesTransformClassName + "' due to underlying exception: " + e.toString(), "01S00");
  159. } catch (ClassNotFoundException e) {
  160. throw SQLError.createSQLException("Unable to create properties transform instance '"
  161. + propertiesTransformClassName + "' due to underlying exception: " + e.toString(), "01S00");
  162. }
  163. if (Util.isColdFusion() && urlProps.getProperty("autoConfigureForColdFusion", "true").equalsIgnoreCase("true")) {
  164. String configs = urlProps.getProperty("useConfigs");
  165. StringBuffer newConfigs = new StringBuffer();
  166. if (configs != null) {
  167. newConfigs.append(configs);
  168. newConfigs.append(",");
  169. }
  170. newConfigs.append("coldFusion");
  171. urlProps.setProperty("useConfigs", newConfigs.toString());
  172. }
  173. String configNames = null;
  174. if (defaults != null)
  175. configNames = defaults.getProperty("useConfigs");
  176. if (configNames == null)
  177. configNames = urlProps.getProperty("useConfigs");
  178. if (configNames != null) {
  179. List splitNames = StringUtils.split(configNames, ",", true);
  180. Properties configProps = new Properties();
  181. for (Iterator namesIter = splitNames.iterator(); namesIter.hasNext();) {
  182. String configName = (String) namesIter.next();
  183. try {
  184. java.io.InputStream configAsStream = getClass().getResourceAsStream("configs/" + configName + ".properties");
  185. if (configAsStream == null)
  186. throw SQLError.createSQLException("Can't find configuration template named '" + configName + "'", "01S00");
  187. configProps.load(configAsStream);
  188. } catch (IOException ioEx) {
  189. throw SQLError.createSQLException("Unable to load configuration template '" + configName
  190. + "' due to underlying IOException: " + ioEx, "01S00");
  191. }
  192. }
  193.  
  194. String key;
  195. String property;
  196. for (Iterator propsIter = urlProps.keySet().iterator(); propsIter.hasNext(); configProps.setProperty(key,
  197. property)) {
  198. key = propsIter.next().toString();
  199. property = urlProps.getProperty(key);
  200. }
  201.  
  202. urlProps = configProps;
  203. }
  204. if (defaults != null) {
  205. String key;
  206. String property;
  207. for (Iterator propsIter = defaults.keySet().iterator(); propsIter.hasNext(); urlProps.setProperty(key, property)) {
  208. key = propsIter.next().toString();
  209. property = defaults.getProperty(key);
  210. }
  211.  
  212. }
  213. return urlProps;
  214. }
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement