/* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.resource.adapter.jdbc.vendor; import java.sql.Connection; import java.sql.SQLException; import java.lang.reflect.Method; import org.jboss.logging.Logger; import org.jboss.resource.adapter.jdbc.ReauthenticationMechansim; public class MySqlReauthenticationMechansimImpl implements ReauthenticationMechansim { private static final Logger log = Logger.getLogger(MySqlReauthenticationMechansimImpl.class); private Method changeUser; private Object[] params; public MySqlReauthenticationMechansimImpl() { try { Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.jdbc2.optional.ConnectionWrapper"); changeUser = mysqlConnection.getMethod("changeUser", new Class[] {String.class,String.class}); } catch (Exception e) { log.warn("Cannot resolve com.mysq.jdbc.Connection.changeUser method.", e); } } public void reauthenticate(Connection con, String user, String password) throws SQLException { log.trace("Going to Authenticate the Connection here "+con+" with credentianls as username="+user+" password="+password); params = new Object[]{user,password}; try { changeUser.invoke(con, params); } catch (Exception e) { Throwable t = e.getCause(); if (t instanceof SQLException) { throw (SQLException)e; } else { log.error("Unexpected error in changeUser", e); } } log.trace("Re-Authentication succeeded !!!"); } }