| 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 | 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. |
| 12 | |
| 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. |
| 17 | |
| 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 |
| 21 | |
| 22 | |
| 23 | |
| 24 | */ |
| 25 | package com.mysql.jdbc.log; |
| 26 | |
| 27 | import java.lang.reflect.Constructor; |
| 28 | import java.lang.reflect.InvocationTargetException; |
| 29 | import java.sql.SQLException; |
| 30 | |
| 31 | import com.mysql.jdbc.SQLError; |
| 32 | |
| 33 | /** |
| 34 | * Creates instances of loggers for the driver to use. |
| 35 | * |
| 36 | * @author Mark Matthews |
| 37 | * |
| 38 | * @version $Id: LogFactory.java 4945 2006-02-17 12:40:54 -0600 (Fri, 17 Feb 2006) mmatthews $ |
| 39 | */ |
| 40 | public class LogFactory { |
| 41 | |
| 42 | /** |
| 43 | * Returns a logger instance of the given class, with the given instance |
| 44 | * name. |
| 45 | * |
| 46 | * @param className |
| 47 | * the class to instantiate |
| 48 | * @param instanceName |
| 49 | * the instance name |
| 50 | * @return a logger instance |
| 51 | * @throws SQLException |
| 52 | * if unable to create a logger instance |
| 53 | */ |
| 54 | public static Log getLogger(String className, String instanceName) |
| 55 | throws SQLException { |
| 56 | |
| 57 | if (className == null) { |
| 58 | throw new SQLException("Logger class can not be NULL", |
| 59 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 60 | } |
| 61 | |
| 62 | if (instanceName == null) { |
| 63 | throw new SQLException("Logger instance name can not be NULL", |
| 64 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | Class loggerClass = null; |
| 69 | |
| 70 | try { |
| 71 | loggerClass = Class.forName(className); |
| 72 | } catch (ClassNotFoundException nfe) { |
| 73 | loggerClass = Class.forName(Log.class.getPackage().getName() + "." + className); |
| 74 | } |
| 75 | |
| 76 | Constructor constructor = loggerClass |
| 77 | .getConstructor(new Class[] { String.class }); |
| 78 | |
| 79 | return (Log) constructor.newInstance(new Object[] { instanceName }); |
| 80 | } catch (ClassNotFoundException cnfe) { |
| 81 | throw new SQLException("Unable to load class for logger '" |
| 82 | + className + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 83 | } catch (NoSuchMethodException nsme) { |
| 84 | throw new SQLException( |
| 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 new SQLException("Unable to instantiate logger class '" |
| 89 | + className + "', exception in constructor?", |
| 90 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 91 | } catch (InvocationTargetException ite) { |
| 92 | throw new SQLException("Unable to instantiate logger class '" |
| 93 | + className + "', exception in constructor?", |
| 94 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 95 | } catch (IllegalAccessException iae) { |
| 96 | throw new SQLException("Unable to instantiate logger class '" |
| 97 | + className + "', constructor not public", |
| 98 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 99 | } catch (ClassCastException cce) { |
| 100 | throw new SQLException("Logger class '" + className |
| 101 | + "' does not implement the '" + Log.class.getName() |
| 102 | + "' interface", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); |
| 103 | } |
| 104 | } |
| 105 | } |