public ConnectionService() { connectionService = new ConnectionServiceImpl(); this.dialectService = new DatabaseDialectService( true ); sanitizer = UtilHtmlSanitizer.getInstance(); }
/** * This service method overrides it's parent and removes all the database types other than native. */ @Override public List<IDatabaseType> getDatabaseTypes() { synchronized ( lock ) { List<IDatabaseType> databaseTypes = super.getDatabaseTypes(); for ( IDatabaseType type : databaseTypes ) { Iterator<DatabaseAccessType> iter = type.getSupportedAccessTypes().iterator(); while ( iter.hasNext() ) { DatabaseAccessType accessType = iter.next(); if ( accessType != DatabaseAccessType.NATIVE ) { iter.remove(); } } } return databaseTypes; } } }
/** * Will resolve a database type based on the prefix of the jdbc URL, which we assume originated from one of these * classes * * @param urlPrefix * @return */ private IDatabaseType resolveDatabaseType( String urlPrefix ) { DatabaseDialectService databaseDialectService = new DatabaseDialectService( false ); List<IDatabaseDialect> databaseDialects = databaseDialectService.getDatabaseDialects(); String nativePre = null; for ( IDatabaseDialect databaseDialect : databaseDialects ) { // // NOTE - The GenericDatabaseDialect and the AccessDatabaseDialect // both return null for the value of getNativeJdbcPre - so - this // requires a null-check. // // MB // nativePre = databaseDialect.getNativeJdbcPre(); if ( ( nativePre != null ) && ( nativePre.startsWith( urlPrefix ) ) ) { return databaseDialect.getDatabaseType(); } } return null; }
@Test public void testCreateDatasourceNoClassName() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper( dialectService.getDatabaseTypes() ); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setDatabaseType( databaseTypeHelper.getDatabaseTypeByShortName( "GENERIC" ) ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" ); con.setAttributes( attrs ); try { PooledDatasourceHelper.convert( con, () -> dialectService ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
public static SQLConnection getConnection( String connectionName ) throws DatasourceServiceException { IDatabaseConnection connection = null; try { ConnectionServiceImpl service = new ConnectionServiceImpl(); connection = service.getConnectionByName( connectionName ); DatabaseDialectService dialectService = new DatabaseDialectService(); IDatabaseDialect dialect = dialectService.getDialect( connection ); String driverClass = null; if ( connection.getDatabaseType().getShortName().equals( "GENERIC" ) ) { driverClass = connection.getAttributes().get( GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS ); } else { driverClass = dialect.getNativeDriver(); } return new SQLConnection( driverClass, dialect.getURLWithExtraOptions( connection ), connection.getUsername(), connection.getPassword(), null ); } catch ( ConnectionServiceException e1 ) { return null; } catch ( DatabaseDialectException e ) { return null; } }
/** * Get the dialect of the given IDatabaseType * * @param databaseType IDatabaseType object to get the dialect of * @return IDatabaseDialect containing the dialect of databaseType */ @POST @Path( "/getDialectByType" ) @Consumes( { APPLICATION_JSON } ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public IDatabaseDialect getDialectWS( IDatabaseType databaseType ) { return super.getDialect( databaseType ); }
/** * Get a list of the database dialects * * @return IDatabaseDialectList containing the database dialects */ @GET @Path( "/getDatabaseDialects" ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public IDatabaseDialectList getDatabaseDialectsWS() { IDatabaseDialectList value = new DefaultDatabaseDialectList(); value.setDialects( super.getDatabaseDialects() ); return value; } }
/** * Attempt to load the JDBC Driver class. If it's not available, return false. * * @param classname * validate that this classname exists in the classpath * * @return true if the class exists */ @POST @Path( "/validateJdbcDriverClassExists" ) @Consumes( { APPLICATION_JSON } ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public Boolean validateJdbcDriverClassExistsWS( String classname ) { return super.validateJdbcDriverClassExists( classname ); }
/** * Register a dialect, if the register fails it will return a server error. * * @param databaseDialect IDatabaseDialect object to register * @param validateClassExists * * @return Response determines if the dialect was registered or not. */ @POST @Path( "/registerDatabaseDialectWithValidation/{validateClassExists}" ) @Consumes( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public Response registerDatabaseDialectWS( IDatabaseDialect databaseDialect, @PathParam( "validateClassExists" ) Boolean validateClassExists ) { try { super.registerDatabaseDialect( databaseDialect ); } catch ( Throwable e ) { Response.serverError().entity( e ).build(); } return Response.ok().build(); }
@Test public void testCreatePoolNoDialectService() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper( dialectService.getDatabaseTypes() ); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setDatabaseType( databaseTypeHelper.getDatabaseTypeByShortName( "GENERIC" ) ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "org.postgresql.Driver" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" ); con.setAttributes( attrs ); try { PooledDatasourceHelper.setupPooledDataSource( con ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
DatabaseDialectService dialectService = new DatabaseDialectService(); IDatabaseDialect dialect = dialectService.getDialect( connection );
/** * Get the dialect of a given IDatabaseConnection * * @param connection IDatabaseConnection object to get the dialect of * * @return IDatabaseDialect of the given connection */ @POST @Path( "/getDialectByConnection" ) @Consumes( { APPLICATION_JSON } ) @Produces( { APPLICATION_JSON } ) @Facet ( name = "Unsupported" ) public IDatabaseDialect getDialect( IDatabaseConnection connection ) { return super.getDialect( connection ); }
/** * Create a database connection * * @param driver * String name of the driver to use * @param url * String name of the url used to create the connection. * * @return IDatabaseConnection for the given parameters */ @GET @Path( "/createDatabaseConnection" ) @Produces( { APPLICATION_JSON } ) @Facet( name = "Unsupported" ) public IDatabaseConnection createDatabaseConnection( @QueryParam( "driver" ) String driver, @QueryParam( "url" ) String url ) { for ( IDatabaseDialect dialect : dialectService.getDatabaseDialects() ) { if ( dialect.getNativeDriver() != null && dialect.getNativeDriver().equals( driver ) ) { if ( dialect.getNativeJdbcPre() != null && url.startsWith( dialect.getNativeJdbcPre() ) ) { return dialect.createNativeConnection( url ); } } } // if no native driver was found, create a custom dialect object. IDatabaseConnection conn = genericDialect.createNativeConnection( url ); conn.getAttributes().put( GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, driver ); return conn; }
@Test public void testDatasourceNotFound() throws Exception { final String datasourceName = "not_here"; final String dotKdb = ".kdb"; final String fileName = datasourceName + dotKdb; final String databasesFolderPath = "/etc/pdi/databases"; IUnifiedRepository repo = mock( IUnifiedRepository.class ); // stub out get parent folder doReturn( new RepositoryFile.Builder( "123", "databases" ).folder( true ).build() ).when( repo ).getFile( databasesFolderPath ); doReturn( reservedChars ).when( repo ).getReservedChars(); // stub out get file not found doReturn( null ).when( repo ).getFile( databasesFolderPath + RepositoryFile.SEPARATOR + fileName ); IDatasourceMgmtService datasourceMgmtService = new JcrBackedDatasourceMgmtService( repo, new DatabaseDialectService() ); Assert.assertNull( datasourceMgmtService.getDatasourceByName( datasourceName ) ); }
@Test public void testCreatePoolNoClassName() throws Exception { DatabaseDialectService dialectService = new DatabaseDialectService( false ); final DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper( dialectService.getDatabaseTypes() ); mp = new MicroPlatform( SOLUTION_PATH ); mp.defineInstance( IDatabaseDialectService.class, dialectService ); mp.start(); final DatabaseConnection con = new DatabaseConnection(); con.setId( "Postgres" ); con.setName( "Postgres" ); con.setAccessType( DatabaseAccessType.NATIVE ); con.setDatabaseType( databaseTypeHelper.getDatabaseTypeByShortName( "GENERIC" ) ); con.setUsername( "pentaho_user" ); con.setPassword( "password" ); final HashMap<String, String> attrs = new HashMap<>(); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "" ); attrs.put( DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate" ); con.setAttributes( attrs ); try { PooledDatasourceHelper.setupPooledDataSource( con ); fail( "Expecting the exception to be thrown" ); } catch ( DBDatasourceServiceException ex ) { assertNotNull( ex ); } }
DatabaseDialectService dialectService = new DatabaseDialectService(); IDatabaseDialect dialect = dialectService.getDialect( connection ); String driverClass = null; if ( connection.getDatabaseType().getShortName().equals( "GENERIC" ) ) {
/** * Get a list of the database types * * @return IDatabaseTypesList containing the database types */ @GET @Path( "/getDatabaseTypes" ) @Facet ( name = "Unsupported" ) @Produces( { APPLICATION_JSON } ) public IDatabaseTypesList getDatabaseTypesWS() { DefaultDatabaseTypesList value = new DefaultDatabaseTypesList(); value.setDbTypes( super.getDatabaseTypes() ); return value; }
connection.setPassword( "" ); //$NON-NLS-1$ IDatabaseDialect dialect = dialectService.getDialect( connection ); String driverClass = null;
private void testDeleteDatasourceWithId( boolean throwException ) throws Exception { final String dotKdb = ".kdb"; IUnifiedRepository repo = mock( IUnifiedRepository.class ); // stub out get parent folder doReturn( new RepositoryFile.Builder( "123", "databases" ).folder( true ).build() ).when( repo ).getFileById( EXP_FILE_ID ); doReturn( reservedChars ).when( repo ).getReservedChars(); // stub out get file to delete doReturn( new RepositoryFile.Builder( EXP_FILE_ID, EXP_DBMETA_NAME + dotKdb ).build() ).when( repo ).getFileById( EXP_FILE_ID); IDatasourceMgmtService datasourceMgmtService = new JcrBackedDatasourceMgmtService( repo, new DatabaseDialectService() ); if( throwException ) { deleteDatasourceWithIdThrowException( repo ); } datasourceMgmtService.deleteDatasourceById( EXP_FILE_ID ); verify( repo ).deleteFile( eq( EXP_FILE_ID ), eq( true ), anyString() ); }
private void testDeleteDatasourceWithName( boolean throwException) throws Exception { final String fileId = "456"; final String databasesFolderPath = "/etc/pdi/databases"; final String dotKdb = ".kdb"; IUnifiedRepository repo = mock( IUnifiedRepository.class ); // stub out get parent folder doReturn( new RepositoryFile.Builder( "123", "databases" ).folder( true ).build() ).when( repo ).getFile( databasesFolderPath ); doReturn( reservedChars ).when( repo ).getReservedChars(); // stub out get file to delete doReturn( new RepositoryFile.Builder( fileId, EXP_DBMETA_NAME + dotKdb ).build() ).when( repo ).getFile( databasesFolderPath + RepositoryFile.SEPARATOR + EXP_DBMETA_NAME + dotKdb ); IDatasourceMgmtService datasourceMgmtService = new JcrBackedDatasourceMgmtService( repo, new DatabaseDialectService() ); if( throwException ) { deleteDatasourceWithNameThrowException( repo ); } datasourceMgmtService.deleteDatasourceByName( EXP_DBMETA_NAME ); verify( repo ).deleteFile( eq( fileId ), eq( true ), anyString() ); }