001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package com.mysql.jdbc.log;
026:
027: import java.lang.reflect.Constructor;
028: import java.lang.reflect.InvocationTargetException;
029: import java.sql.SQLException;
030:
031: import com.mysql.jdbc.SQLError;
032:
033: /**
034: * Creates instances of loggers for the driver to use.
035: *
036: * @author Mark Matthews
037: *
038: * @version $Id: LogFactory.java 4946 2006-02-17 18:44:36Z mmatthews $
039: */
040: public class LogFactory {
041:
042: /**
043: * Returns a logger instance of the given class, with the given instance
044: * name.
045: *
046: * @param className
047: * the class to instantiate
048: * @param instanceName
049: * the instance name
050: * @return a logger instance
051: * @throws SQLException
052: * if unable to create a logger instance
053: */
054: public static Log getLogger(String className, String instanceName)
055: throws SQLException {
056:
057: if (className == null) {
058: throw SQLError.createSQLException(
059: "Logger class can not be NULL",
060: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
061: }
062:
063: if (instanceName == null) {
064: throw SQLError.createSQLException(
065: "Logger instance name can not be NULL",
066: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
067: }
068:
069: try {
070: Class loggerClass = null;
071:
072: try {
073: loggerClass = Class.forName(className);
074: } catch (ClassNotFoundException nfe) {
075: loggerClass = Class.forName(Log.class.getPackage()
076: .getName()
077: + "." + className);
078: }
079:
080: Constructor constructor = loggerClass
081: .getConstructor(new Class[] { String.class });
082:
083: return (Log) constructor
084: .newInstance(new Object[] { instanceName });
085: } catch (ClassNotFoundException cnfe) {
086: throw SQLError.createSQLException(
087: "Unable to load class for logger '" + className
088: + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
089: } catch (NoSuchMethodException nsme) {
090: throw SQLError
091: .createSQLException(
092: "Logger class does not have a single-arg constructor that takes an instance name",
093: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
094: } catch (InstantiationException inse) {
095: throw SQLError.createSQLException(
096: "Unable to instantiate logger class '" + className
097: + "', exception in constructor?",
098: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
099: } catch (InvocationTargetException ite) {
100: throw SQLError.createSQLException(
101: "Unable to instantiate logger class '" + className
102: + "', exception in constructor?",
103: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
104: } catch (IllegalAccessException iae) {
105: throw SQLError.createSQLException(
106: "Unable to instantiate logger class '" + className
107: + "', constructor not public",
108: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
109: } catch (ClassCastException cce) {
110: throw SQLError.createSQLException("Logger class '"
111: + className + "' does not implement the '"
112: + Log.class.getName() + "' interface",
113: SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
114: }
115: }
116: }
|