KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > log > LogFactory


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 JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.sql.SQLException JavaDoc;
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,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
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 JavaDoc className, String JavaDoc instanceName)
55             throws SQLException JavaDoc {
56
57         if (className == null) {
58             throw new SQLException JavaDoc("Logger class can not be NULL",
59                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
60         }
61
62         if (instanceName == null) {
63             throw new SQLException JavaDoc("Logger instance name can not be NULL",
64                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
65         }
66
67         try {
68             Class JavaDoc loggerClass = Class.forName(className);
69             Constructor JavaDoc constructor = loggerClass
70                     .getConstructor(new Class JavaDoc[] { String JavaDoc.class });
71
72             return (Log) constructor.newInstance(new Object JavaDoc[] { instanceName });
73         } catch (ClassNotFoundException JavaDoc cnfe) {
74             throw new SQLException JavaDoc("Unable to load class for logger '"
75                     + className + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
76         } catch (NoSuchMethodException JavaDoc nsme) {
77             throw new SQLException JavaDoc(
78                     "Logger class does not have a single-arg constructor that takes an instance name",
79                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
80         } catch (InstantiationException JavaDoc inse) {
81             throw new SQLException JavaDoc("Unable to instantiate logger class '"
82                     + className + "', exception in constructor?",
83                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
84         } catch (InvocationTargetException JavaDoc ite) {
85             throw new SQLException JavaDoc("Unable to instantiate logger class '"
86                     + className + "', exception in constructor?",
87                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
88         } catch (IllegalAccessException JavaDoc iae) {
89             throw new SQLException JavaDoc("Unable to instantiate logger class '"
90                     + className + "', constructor not public",
91                     SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
92         } catch (ClassCastException JavaDoc cce) {
93             throw new SQLException JavaDoc("Logger class '" + className
94                     + "' does not implement the '" + Log.class.getName()
95                     + "' interface", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
96         }
97     }
98 }
99
Popular Tags