View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.jdbc;
14  
15  import java.sql.Connection;
16  import java.sql.DriverManager;
17  import java.sql.SQLException;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
21  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
22  import com.eviware.soapui.support.GroovyUtils;
23  import com.eviware.soapui.support.SoapUIException;
24  import com.eviware.soapui.support.StringUtils;
25  
26  public class JdbcUtils
27  {
28  
29  	public static final String PASS_TEMPLATE = "PASS_VALUE";
30  
31  	public static Connection initConnection( PropertyExpansionContext context, String driver, String connectionString,
32  			String password ) throws SQLException, SoapUIException
33  	{
34  		if( JdbcUtils.missingConnSettings( driver, connectionString, password ) )
35  		{
36  			throw new SoapUIException( "Some connections settings are missing" );
37  		}
38  		String drvr = PropertyExpander.expandProperties( context, driver ).trim();
39  		String connStr = PropertyExpander.expandProperties( context, connectionString ).trim();
40  		String pass = StringUtils.hasContent( password ) ? PropertyExpander.expandProperties( context, password ).trim()
41  				: "";
42  		String masskedPass = connStr.replace( PASS_TEMPLATE, "#####" );
43  		if( connStr.contains( PASS_TEMPLATE ) )
44  		{
45  			connStr = connStr.replaceFirst( PASS_TEMPLATE, pass );
46  		}
47  		try
48  		{
49  			GroovyUtils.registerJdbcDriver( drvr );
50  			DriverManager.getDriver( connStr );
51  		}
52  		catch( SQLException e )
53  		{
54  			// SoapUI.logError( e );
55  			try
56  			{
57  				Class.forName( drvr ).newInstance();
58  			}
59  			catch( Exception e1 )
60  			{
61  				SoapUI.logError( e );
62  				throw new SoapUIException( "Failed to init connection for drvr [" + drvr + "], connectionString ["
63  						+ masskedPass + "]" );
64  			}
65  		}
66  		return DriverManager.getConnection( connStr );
67  
68  	}
69  
70  	public static boolean hasMasskedPass( String connStr )
71  	{
72  		return !StringUtils.isNullOrEmpty( connStr ) ? connStr.contains( PASS_TEMPLATE ) : false;
73  	}
74  
75  	public static boolean missingConnSettings( String driver, String connectionString, String password )
76  	{
77  		return StringUtils.isNullOrEmpty( driver ) || StringUtils.isNullOrEmpty( connectionString )
78  				|| ( connectionString.contains( PASS_TEMPLATE ) && StringUtils.isNullOrEmpty( password ) );
79  	}
80  
81  }