2
Copyright (C) 2002-2004 MySQL AB
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.
8
There are special exceptions to the terms and conditions of the GPL
9
as it is applied to this software. View the full text of the
10
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11
software distribution.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
package com.mysql.jdbc.log;
27
import java.lang.reflect.Constructor;
28
import java.lang.reflect.InvocationTargetException;
29
import java.sql.SQLException;
31
import com.mysql.jdbc.SQLError;
34
* Creates instances of loggers for the driver to use.
36
* @author Mark Matthews
40
public class LogFactory {
43
* Returns a logger instance of the given class, with the given instance
47
* the class to instantiate
50
* @return a logger instance
51
* @throws SQLException
52
* if unable to create a logger instance
54
public static Log getLogger(String className, String instanceName)
57
if (className == null) {
58
throw SQLError.createSQLException("Logger class can not be NULL",
59
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
62
if (instanceName == null) {
63
throw SQLError.createSQLException("Logger instance name can not be NULL",
64
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
68
Class loggerClass = null;
71
loggerClass = Class.forName(className);
72
} catch (ClassNotFoundException nfe) {
73
loggerClass = Class.forName(Log.class.getPackage().getName() + "." + className);
76
Constructor constructor = loggerClass
77
.getConstructor(new Class[] { String.class });
79
return (Log) constructor.newInstance(new Object[] { instanceName });
80
} catch (ClassNotFoundException cnfe) {
81
throw SQLError.createSQLException("Unable to load class for logger '"
82
+ className + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
83
} catch (NoSuchMethodException nsme) {
84
throw SQLError.createSQLException(
85
"Logger class does not have a single-arg constructor that takes an instance name",
86
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
87
} catch (InstantiationException inse) {
88
throw SQLError.createSQLException("Unable to instantiate logger class '"
89
+ className + "', exception in constructor?",
90
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
91
} catch (InvocationTargetException ite) {
92
throw SQLError.createSQLException("Unable to instantiate logger class '"
93
+ className + "', exception in constructor?",
94
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
95
} catch (IllegalAccessException iae) {
96
throw SQLError.createSQLException("Unable to instantiate logger class '"
97
+ className + "', constructor not public",
98
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
99
} catch (ClassCastException cce) {
100
throw SQLError.createSQLException("Logger class '" + className
101
+ "' does not implement the '" + Log.class.getName()
102
+ "' interface", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);