1 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 40 public class LogFactory { 41 42 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 = Class.forName(className); 69 Constructor constructor = loggerClass 70 .getConstructor(new Class [] { String .class }); 71 72 return (Log) constructor.newInstance(new Object [] { instanceName }); 73 } catch (ClassNotFoundException cnfe) { 74 throw new SQLException ("Unable to load class for logger '" 75 + className + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 76 } catch (NoSuchMethodException nsme) { 77 throw new SQLException ( 78 "Logger class does not have a single-arg constructor that takes an instance name", 79 SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 80 } catch (InstantiationException inse) { 81 throw new SQLException ("Unable to instantiate logger class '" 82 + className + "', exception in constructor?", 83 SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 84 } catch (InvocationTargetException ite) { 85 throw new SQLException ("Unable to instantiate logger class '" 86 + className + "', exception in constructor?", 87 SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 88 } catch (IllegalAccessException iae) { 89 throw new SQLException ("Unable to instantiate logger class '" 90 + className + "', constructor not public", 91 SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 92 } catch (ClassCastException cce) { 93 throw new SQLException ("Logger class '" + className 94 + "' does not implement the '" + Log.class.getName() 95 + "' interface", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 96 } 97 } 98 } 99 | Popular Tags |