~maxb/connectorj/5.0

« back to all changes in this revision

Viewing changes to connector-j/src/com/mysql/jdbc/log/LogFactory.java

  • Committer: mmatthews
  • Date: 2007-10-11 20:04:05 UTC
  • Revision ID: svn-v3-trunk0:bce1ec22-edf6-0310-a851-a6aae2aa6c29:branches%2Fbranch_5_0:6637
Changed layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
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$
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 SQLError.createSQLException("Logger class can not be NULL",
59
 
                                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
60
 
                }
61
 
 
62
 
                if (instanceName == null) {
63
 
                        throw SQLError.createSQLException("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 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);
103
 
                }
104
 
        }
105
 
}