/projects/derby-10.9.1.0/db-derby-10.9.1.0-src/java/stubs/jdbc3/java/sql/DriverManager.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 299 lines · 83 code · 27 blank · 189 comment · 9 complexity · 718aedc197925f285019de7c130afc16 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package java.sql;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Properties;
  21. import java.util.Enumeration;
  22. import java.util.Iterator;
  23. import java.io.PrintStream;
  24. import java.io.PrintWriter;
  25. import java.util.Vector;
  26. import java.security.AccessController;
  27. /**
  28. * Provides facilities for managing JDBC Drivers.
  29. * <p>
  30. * The DriverManager class will load JDBC drivers during its initialization,
  31. * from the list of drivers referenced by the System Property "jdbc.drivers".
  32. */
  33. public class DriverManager {
  34. /*
  35. * Facilities for logging. The Print Stream is deprecated but is maintained
  36. * here for compatibility.
  37. */
  38. private static PrintStream thePrintStream;
  39. private static PrintWriter thePrintWriter;
  40. // Login timeout value - by default set to 0 -> "wait forever"
  41. private static int loginTimeout = 0;
  42. /*
  43. * Set to hold Registered Drivers - initial capacity 10 drivers (will expand
  44. * automatically if necessary.
  45. */
  46. private static final List theDrivers = new ArrayList(10);
  47. // Permission for setting log
  48. private static final SQLPermission logPermission = null;
  49. /*
  50. * Load drivers on initialization
  51. */
  52. static {
  53. loadInitialDrivers();
  54. }
  55. /*
  56. * Loads the set of JDBC drivers defined by the Property "jdbc.drivers" if
  57. * it is defined.
  58. */
  59. private static void loadInitialDrivers() {
  60. }
  61. /*
  62. * A private constructor to prevent allocation
  63. */
  64. private DriverManager() {
  65. super();
  66. }
  67. /**
  68. * Removes a driver from the DriverManager's registered driver list. This
  69. * will only succeed where the caller's classloader loaded the driver that
  70. * is to be removed. If the driver was loaded by a different classloader,
  71. * the removal of the driver will fail silently.
  72. * <p>
  73. * If the removal succeeds, the DriverManager will not in future use this
  74. * driver when asked to get a Connection.
  75. *
  76. * @param driver
  77. * @throws SQLException
  78. * if there is an exception accessing the database.
  79. */
  80. public static void deregisterDriver(Driver driver) throws SQLException { }
  81. /**
  82. * Attempts to establish a connection to the given database URL.
  83. *
  84. * @param url
  85. * a URL string representing the database target to connect with
  86. * @return a Connection to the database identified by the URL. null if no
  87. * connection can be made.
  88. * @throws SQLException
  89. * if there is an error while attempting to connect to the
  90. * database identified by the URL
  91. */
  92. public static Connection getConnection(String url) throws SQLException {
  93. return getConnection(url, new Properties());
  94. }
  95. /**
  96. * Attempts to establish a connection to the given database URL.
  97. *
  98. * @param url
  99. * a URL string representing the database target to connect with
  100. * @param info
  101. * a set of Properties to use as arguments to set up the
  102. * connection. Properties are arbitrary string/value pairs.
  103. * Normally, at least the properties "user" and "password" should
  104. * be passed, with appropriate settings for the userid and its
  105. * corresponding password to get access to the database
  106. * concerned.
  107. * @return a Connection to the database identified by the URL. null if no
  108. * connection can be made.
  109. * @throws SQLException
  110. * if there is an error while attempting to connect to the
  111. * database identified by the URL
  112. */
  113. public static Connection getConnection(String url, Properties info)
  114. throws SQLException { return null; }
  115. /**
  116. * Attempts to establish a connection to the given database URL.
  117. *
  118. * @param url
  119. * a URL string representing the database target to connect with
  120. * @param user
  121. * a userid used to login to the database
  122. * @param password
  123. * a password for the userid to login to the database
  124. * @return a Connection to the database identified by the URL. null if no
  125. * connection can be made.
  126. * @throws SQLException
  127. * if there is an error while attempting to connect to the
  128. * database identified by the URL
  129. */
  130. public static Connection getConnection(String url, String user,
  131. String password) throws SQLException { return null; }
  132. /**
  133. * Tries to find a driver that can interpret the supplied URL.
  134. *
  135. * @param url
  136. * the URL of a database
  137. * @return a Driver that can understand the given URL. null if no Driver
  138. * understands the URL
  139. * @throws SQLException
  140. * if there is any kind of Database Access problem
  141. */
  142. public static Driver getDriver(String url) throws SQLException { return null; }
  143. /**
  144. * Returns an Enumeration that contains all of the loaded JDBC drivers that
  145. * the current caller can access.
  146. *
  147. * @return An Enumeration containing all the currently loaded JDBC Drivers
  148. */
  149. public static Enumeration getDrivers() { return null; }
  150. /**
  151. * Returns the login timeout when connecting to a database, in seconds.
  152. *
  153. * @return the login timeout in seconds
  154. */
  155. public static int getLoginTimeout() {
  156. return loginTimeout;
  157. }
  158. /**
  159. * @deprecated Gets the log PrintStream used by the DriverManager and all
  160. * the JDBC Drivers.
  161. * @return the PrintStream used for logging activity
  162. */
  163. public static PrintStream getLogStream() {
  164. return thePrintStream;
  165. }
  166. /**
  167. * Retrieves the log writer.
  168. *
  169. * @return A PrintWriter object used as the log writer. null if no log
  170. * writer is set.
  171. */
  172. public static PrintWriter getLogWriter() {
  173. return thePrintWriter;
  174. }
  175. /**
  176. * Prints a message to the current JDBC log stream. This is either the
  177. * PrintWriter or (deprecated) the PrintStream, if set.
  178. *
  179. * @param message
  180. * the message to print to the JDBC log stream
  181. */
  182. public static void println(String message) {
  183. if (thePrintWriter != null) {
  184. thePrintWriter.println(message);
  185. thePrintWriter.flush();
  186. } else if (thePrintStream != null) {
  187. thePrintStream.println(message);
  188. thePrintStream.flush();
  189. }
  190. /*
  191. * If neither the PrintWriter not the PrintStream are set, then silently
  192. * do nothing the message is not recorded and no exception is generated.
  193. */
  194. return;
  195. }
  196. /**
  197. * Registers a given JDBC driver with the DriverManager.
  198. * <p>
  199. * A newly loaded JDBC driver class should register itself with the
  200. * DriverManager by calling this method.
  201. *
  202. * @param driver
  203. * the Driver to register with the DriverManager
  204. * @throws SQLException
  205. * if a database access error occurs.
  206. */
  207. public static void registerDriver(Driver driver) throws SQLException {
  208. if (driver == null) {
  209. throw new NullPointerException();
  210. }
  211. synchronized (theDrivers) {
  212. theDrivers.add(driver);
  213. }
  214. }
  215. /**
  216. * Set the login timeout when connecting to a database, in seconds.
  217. *
  218. * @param seconds
  219. * seconds until timeout. 0 indicates wait forever.
  220. */
  221. public static void setLoginTimeout(int seconds) {
  222. loginTimeout = seconds;
  223. return;
  224. }
  225. /**
  226. * @deprecated Sets the Print Stream to use for logging data from the
  227. * DriverManager and the JDBC drivers.
  228. * <p>
  229. * Use setLogWriter instead.
  230. * @param out
  231. * the PrintStream to use for logging.
  232. */
  233. public static void setLogStream(PrintStream out) {
  234. checkLogSecurity();
  235. thePrintStream = out;
  236. }
  237. /**
  238. * Sets the PrintWriter that will be used by all loaded drivers, and also
  239. * the DriverManager.
  240. *
  241. * @param out
  242. * the PrintWriter to be used
  243. */
  244. public static void setLogWriter(PrintWriter out) {
  245. checkLogSecurity();
  246. thePrintWriter = out;
  247. }
  248. /*
  249. * Method which checks to see if setting a logging stream is allowed by the
  250. * Security manager
  251. */
  252. private static void checkLogSecurity() {
  253. SecurityManager securityManager = System.getSecurityManager();
  254. if (securityManager != null) {
  255. // Throws a SecurityException if setting the log is not permitted
  256. securityManager.checkPermission(logPermission);
  257. }
  258. }
  259. /**
  260. * Finds if a supplied Object belongs to the given ClassLoader.
  261. *
  262. * @param theObject
  263. * the object to check
  264. * @param theClassLoader
  265. * the ClassLoader
  266. * @return true if the Object does belong to the ClassLoader, false
  267. * otherwise
  268. */
  269. private static boolean isClassFromClassLoader(Object theObject,
  270. ClassLoader theClassLoader)
  271. { return false; }
  272. }