/** * Copyright (C) 2008-2010, Squale Project - http://www.squale.org * * This file is part of Squale. * * Squale 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 3 of the * License, or any later version. * * Squale 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 General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Squale. If not, see <http://www.gnu.org/licenses/>. */ package org.squale.welcom.outils.jdbc.wrapper; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.validator.GenericValidator; import org.squale.welcom.struts.util.WConstants; /** * Classe d'acc�s au pool de connection base de donn�es. Les param�tres sont initialis�s par la m�thode initDataSource * Date de cr�ation : (18/10/2001 16:41:32) * * @author: C�dric Torcq */ public final class ConnectionPool { /** logger */ private static Log logStartup = LogFactory.getLog( "Welcom" ); /** Retoune si la connexion est activ� */ private static boolean initialized = false; /** Datasource retouv� */ private static javax.sql.DataSource ds = null; /** * The application scope attribute under which our JNDI datasource is stored. */ /** JNDI : datasource */ private static String JNDI_DATASOURCE_KEY = ""; /** JNDI : Prefix */ private static String PROVIDER_URL = ""; /** JNDI : User */ private static String USER; /** JNDI : Password */ private static String PASSWD; /** * Commentaire relatif au constructeur ConnectionPool. */ public ConnectionPool() { super(); } /** * Returns a JDBC connection from a connection pool or other resource, to be used and closed promptly. * <p> * * @return JDBC connection from resource layer. * @exception SQLException on SQL or other errors. May wrap other exceptions depending on implementation. Will not * return null. */ public static final Connection getConnection() throws SQLException { try { /* Version JNDI */ /** **************************************************** */ if ( ds == null ) { // create parameter list to access naming system final java.util.Hashtable parms = new java.util.Hashtable(); try { Class.forName( "com.ibm.websphere.naming.WsnInitialContextFactory" ); parms.put( javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory" ); } catch ( final ClassNotFoundException e ) { logStartup.info( "Passage en mode compatibilit� WAS 3.5 " ); } // parms.put(javax.naming.Context.PROVIDER_URL, PROVIDER_URL); // access naming system final javax.naming.Context ctx = new javax.naming.InitialContext( parms ); // get DataSource factory object from naming system ds = (javax.sql.DataSource) ctx.lookup( JNDI_DATASOURCE_KEY ); } // use DataSource factory to get data server connection Connection conn = null; if ( !GenericValidator.isBlankOrNull( USER ) ) { conn = ds.getConnection( USER, PASSWD ); } else { conn = ds.getConnection(); } return conn; } catch ( final Exception ex ) { logStartup.error( "ConnectionPool : Echec connexion : ", ex ); if ( ( ex == null ) || ( ex.getMessage() == null ) ) { logStartup.info( "Verifier la version de votre classes12.zip" ); } return null; } } /** * Ins�rez la description de la m�thode ici. Date de cr�ation : (19/10/2001 08:33:16) * * @return java.lang.String */ public static final String getParameters() { final StringBuffer st = new StringBuffer(); st.append( " url : " ).append( PROVIDER_URL ).append( " - source : " ).append( JNDI_DATASOURCE_KEY ).append( " - user/pwd : " ).append( USER ).append( "/" ).append( PASSWD ); return st.toString(); } /** * Initilisation de la datasource * * @param url : Chaine de connection JDBC (ex: jdbc:oracle:thin:@SERVEUR:POST:INSTANCE) * @param password : Mots de passe bd * @param login : Login BD */ public final static void initDataSource( final String url, final String password, final String login ) { initialized = true; /* * String url = srvConfig.getInitParameter(WConstants.KEY_CONFIG_DATASOURCE_URL); PASSWD = * srvConfig.getInitParameter(WConstants.KEY_CONFIG_DATASOURCE_PASSWD); USER = * srvConfig.getInitParameter(WConstants.KEY_CONFIG_DATASOURCE_USER); */ PASSWD = password; USER = login; try { PROVIDER_URL = url.substring( 0, url.indexOf( "jdbc" ) ); JNDI_DATASOURCE_KEY = url.substring( url.indexOf( "jdbc" ) ); logStartup.info( "Connexion sur : " + PROVIDER_URL + JNDI_DATASOURCE_KEY ); testConnection(); } catch ( final Exception ex ) { logStartup.info( "Pas de connexion BD dans le web.xml sous la clef : " + WConstants.KEY_CONFIG_DATASOURCE_URL ); } } /** * @return Retoun vrai si la connexion JDBC est correcte */ private static boolean testConnection() { try { final Connection conn = getConnection(); logStartup.info( "URL : " + conn.getMetaData().getURL() ); logStartup.info( "User : " + conn.getMetaData().getUserName() ); return true; } catch ( final Exception e ) { logStartup.info( "impossible d'�tablir une connexion � la base de donn�es" ); return false; } } /** * @return Vrai si la connection a la dateSource a �t� effectu� */ public static boolean isInitalized() { return initialized; } }