PentahoXmlaServlet extends DynamicDatasourceXmlaServlet { @Override protected ConnectionFactory createConnectionFactory( final ServletConfig servletConfig ) throws ServletException { final ConnectionFactory delegate = super.createConnectionFactory( servletConfig ); return new ConnectionFactory() { public Map<String, Object> getPreConfiguredDiscoverDatasourcesResponse() { return delegate.getPreConfiguredDiscoverDatasourcesResponse(); } public OlapConnection getConnection( String databaseName, String catalogName, String roleName, Properties props ) throws SQLException { final IPentahoSession session = PentahoSessionHolder.getSession(); final IConnectionUserRoleMapper mondrianUserRoleMapper = PentahoSystem.get( IConnectionUserRoleMapper.class, MDXConnection.MDX_CONNECTION_MAPPER_KEY, null ); String[] effectiveRoles = new String[0]; if ( mondrianUserRoleMapper != null && catalogName != null ) { try { effectiveRoles = mondrianUserRoleMapper .mapConnectionRoles( session, catalogName ); if ( effectiveRoles == null ) { effectiveRoles = new String[0]; } } catch ( PentahoAccessControlException e ) { throw new SQLException( e ); } } boolean addComma = false; roleName = ""; for ( String role : effectiveRoles ) { if ( addComma ) { roleName = roleName.concat( "," ); } roleName = roleName.concat( role ); addComma = true; } if ( catalogName == null ) { return delegate.getConnection( databaseName, catalogName, roleName.equals( "" ) ? null : roleName, props ); } else { IMondrianCatalogService mcs = PentahoSystem.get( IMondrianCatalogService.class ); MondrianCatalog mc = mcs.getCatalog( catalogName, PentahoSessionHolder.getSession() ); if ( mc == null ) { throw new XmlaException( CLIENT_FAULT_FC, HSB_BAD_RESTRICTION_LIST_CODE, HSB_BAD_RESTRICTION_LIST_FAULT_FS, new MondrianException( "No such catalog: " + catalogName ) ); } Connection con = DriverManager.getConnection( mc.getDataSourceInfo() + ";Catalog=" + mc.getDefinition(), catalogLocator ); try { final MondrianServer server = MondrianServer.forConnection( con ); FileRepository fr = new FileRepository( makeContentFinder( makeDataSourcesUrl( servletConfig ) ), catalogLocator ); OlapConnection connection = fr.getConnection( server, databaseName, catalogName, roleName, props ); fr.shutdown(); return connection; } finally { con.close(); } } } }; } PentahoXmlaServlet(); @Override void init( ServletConfig servletConfig );  }
@Test public void createConnectionFactory() throws Exception { ISecurityHelper securityHelper = mock( ISecurityHelper.class ); SecurityHelper.setMockInstance( securityHelper ); when( securityHelper.runAsSystem( any( ( Callable.class ) ) ) ).thenReturn( DATASOURCE_XML ); IMondrianCatalogService catalogService = mock( MondrianCatalogHelper.class ); MondrianCatalog mondrianCatalog = mock( MondrianCatalog.class ); when( mondrianCatalog.getDataSourceInfo() ).thenReturn( "DataSource=foo" ); doReturn( mondrianCatalog ).when( catalogService ).getCatalog( anyString(), anyObject() ); PowerMockito.mockStatic(DriverManager.class); when(DriverManager.getConnection(anyString(), anyObject())).thenReturn( mock( RolapConnection.class )); PentahoSystem.registerObject( catalogService ); PentahoXmlaServlet xmlaServlet = spy( new PentahoXmlaServlet() ); XmlaHandler.ConnectionFactory connectionFactory = xmlaServlet.createConnectionFactory( mock( ServletConfig.class ) ); Properties properties = new Properties(); properties.put("DataSource", "bogus"); try { connectionFactory.getConnection( "SampleData", "SampleData", "baz", properties ); } catch( MondrianException exception ){ } try { connectionFactory.getConnection( "SampleData", "SampleData", "baz", properties ); } catch( MondrianException exception ){ } verify( xmlaServlet, times(1) ).makeCatalogLocator( anyObject() ); }